KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_any_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <properties/property.h>
27#include <geometry/eda_angle.h>
28#include <gal/color4d.h>
29#include <math/vector2d.h>
30#include <math/box2.h>
31#include <kiid.h>
32#include <layer_ids.h>
33
34#include <wx/string.h>
35
36#include <optional>
37
38
39bool KiWxAnyEquals( const wxAny& aA, const wxAny& aB, const PROPERTY_BASE* aProperty )
40{
41 // Enum-backed properties: compare via the integer choice value (or string
42 // label) rather than the enum type itself, since wxAny equality on enums
43 // requires knowing the exact enum type at compile time.
44 if( aProperty && aProperty->HasChoices() )
45 {
46 int aInt = 0, bInt = 0;
47
48 if( aA.GetAs<int>( &aInt ) && aB.GetAs<int>( &bInt ) )
49 return aInt == bInt;
50
51 wxString aStr, bStr;
52
53 if( aA.GetAs<wxString>( &aStr ) && aB.GetAs<wxString>( &bStr ) )
54 return aStr == bStr;
55
56 // Couldn't extract — fall through to scalar checks below.
57 }
58
59
60 // Same type is the precondition for value equality. wxAny doesn't expose a
61 // direct type_index, so we check via CheckType<>() for the types we support.
62 // For an unsupported type we conservatively report "not equal" so that a
63 // property delta is generated (the differ will then drop the property if
64 // conversion to DIFF_VALUE also fails).
65
66 auto cmp = [&]( auto sentinel ) -> std::optional<bool>
67 {
68 using T = decltype( sentinel );
69
70 if( aA.CheckType<T>() && aB.CheckType<T>() )
71 return aA.As<T>() == aB.As<T>();
72
73 if( aA.CheckType<T>() != aB.CheckType<T>() )
74 return false;
75
76 return std::nullopt;
77 };
78
79 using llong = long long;
80
81 if( auto r = cmp( bool{} ) ) return *r;
82 if( auto r = cmp( int{} ) ) return *r;
83 if( auto r = cmp( long{} ) ) return *r;
84 if( auto r = cmp( llong{} ) ) return *r;
85 if( auto r = cmp( unsigned{} ) ) return *r;
86 if( auto r = cmp( float{} ) ) return *r;
87 if( auto r = cmp( double{} ) ) return *r;
88 if( auto r = cmp( wxString{} ) ) return *r;
89 if( auto r = cmp( std::string{} ) ) return *r;
90
91 if( aA.CheckType<std::optional<int>>() && aB.CheckType<std::optional<int>>() )
92 return aA.As<std::optional<int>>() == aB.As<std::optional<int>>();
93
94 if( aA.CheckType<std::optional<double>>() && aB.CheckType<std::optional<double>>() )
95 return aA.As<std::optional<double>>() == aB.As<std::optional<double>>();
96
97 if( aA.CheckType<EDA_ANGLE>() && aB.CheckType<EDA_ANGLE>() )
98 return aA.As<EDA_ANGLE>() == aB.As<EDA_ANGLE>();
99
100 if( aA.CheckType<VECTOR2I>() && aB.CheckType<VECTOR2I>() )
101 return aA.As<VECTOR2I>() == aB.As<VECTOR2I>();
102
103 if( aA.CheckType<BOX2I>() && aB.CheckType<BOX2I>() )
104 return aA.As<BOX2I>() == aB.As<BOX2I>();
105
106 if( aA.CheckType<KIGFX::COLOR4D>() && aB.CheckType<KIGFX::COLOR4D>() )
107 return aA.As<KIGFX::COLOR4D>() == aB.As<KIGFX::COLOR4D>();
108
109 if( aA.CheckType<KIID>() && aB.CheckType<KIID>() )
110 return aA.As<KIID>() == aB.As<KIID>();
111
112 if( aA.CheckType<PCB_LAYER_ID>() && aB.CheckType<PCB_LAYER_ID>() )
113 return aA.As<PCB_LAYER_ID>() == aB.As<PCB_LAYER_ID>();
114
115 return false; // unsupported type pair — conservatively report differing
116}
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Definition kiid.h:44
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition property.h:246
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
bool KiWxAnyEquals(const wxAny &aA, const wxAny &aB, const PROPERTY_BASE *aProperty)
Compare two wxAny values for equality across the KiCad property type set.