KiCad PCB EDA Suite
Loading...
Searching...
No Matches
inspectable.h
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 (C) 2020 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#ifndef INSPECTABLE_H
24#define INSPECTABLE_H
25
26#include <core/wx_stl_compat.h>
27
29#include <properties/property.h>
30
31#include <optional>
32
37{
38public:
39 virtual ~INSPECTABLE()
40 {
41 }
42
43 bool Set( PROPERTY_BASE* aProperty, wxAny& aValue, bool aNotify = true )
44 {
46 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
47
48 if( object )
49 {
50 aProperty->setter( object, aValue );
51
52 if( aNotify )
53 propMgr.PropertyChanged( this, aProperty );
54 }
55
56 return object != nullptr;
57 }
58
59 template<typename T>
60 bool Set( PROPERTY_BASE* aProperty, T aValue, bool aNotify = true )
61 {
63 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
64
65 if( object )
66 {
67 aProperty->set<T>( object, aValue );
68
69 if( aNotify )
70 propMgr.PropertyChanged( this, aProperty );
71 }
72
73 return object != nullptr;
74 }
75
76 bool Set( PROPERTY_BASE* aProperty, wxVariant aValue, bool aNotify = true )
77 {
79 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
80
81 if( object )
82 {
83 wxPGChoices choices = aProperty->GetChoices( this );
84
85 if( choices.GetCount() )
86 {
87 // If the property type is int, use the choice value directly
88 // Otherwise, convert to the choice label string
89 if( aProperty->TypeHash() == TYPE_HASH( int ) )
90 aProperty->set<int>( object, aValue.GetInteger() );
91 else
92 aProperty->set<wxString>( object, choices.GetLabel( aValue.GetInteger() ) );
93 }
94 else
95 aProperty->set<wxVariant>( object, aValue );
96
97 if( aNotify )
98 propMgr.PropertyChanged( this, aProperty );
99 }
100
101 return object != nullptr;
102 }
103
104 template<typename T>
105 bool Set( const wxString& aProperty, T aValue, bool aNotify = true )
106 {
108 TYPE_ID thisType = TYPE_HASH( *this );
109 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
110 void* object = nullptr;
111
112 if( prop )
113 {
114 object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
115
116 if( object )
117 {
118 prop->set<T>( object, aValue );
119
120 if( aNotify )
121 propMgr.PropertyChanged( this, prop );
122 }
123 }
124
125 return object != nullptr;
126 }
127
128 wxAny Get( PROPERTY_BASE* aProperty ) const
129 {
131 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
132 return object ? aProperty->getter( object ) : wxAny();
133 }
134
135 template<typename T>
136 T Get( PROPERTY_BASE* aProperty ) const
137 {
139 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
140
141 if( !object )
142 throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
143
144 return aProperty->get<T>( object );
145 }
146
147 template<typename T>
148 std::optional<T> Get( const wxString& aProperty ) const
149 {
151 TYPE_ID thisType = TYPE_HASH( *this );
152 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
153 std::optional<T> ret;
154
155 if( prop )
156 {
157 const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
158
159 if( object )
160 ret = prop->get<T>( object );
161 }
162
163 return ret;
164 }
165};
166
167#endif /* INSPECTABLE_H */
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:37
T Get(PROPERTY_BASE *aProperty) const
bool Set(PROPERTY_BASE *aProperty, T aValue, bool aNotify=true)
Definition inspectable.h:60
virtual ~INSPECTABLE()
Definition inspectable.h:39
bool Set(PROPERTY_BASE *aProperty, wxVariant aValue, bool aNotify=true)
Definition inspectable.h:76
wxAny Get(PROPERTY_BASE *aProperty) const
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
Definition inspectable.h:43
bool Set(const wxString &aProperty, T aValue, bool aNotify=true)
std::optional< T > Get(const wxString &aProperty) const
virtual size_t TypeHash() const =0
Return type-id of the property type.
virtual wxAny getter(const void *aObject) const =0
wxPGChoices GetChoices(INSPECTABLE *aObject) const
Definition property.h:268
virtual void setter(void *aObject, wxAny &aValue)=0
void set(void *aObject, T aValue)
Definition property.h:367
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
T get(const void *aObject) const
Definition property.h:409
Provide class metadata.Helper macro to map type hashes to names.
void PropertyChanged(INSPECTABLE *aObject, PROPERTY_BASE *aProperty)
Callback to alert the notification system that a property has changed.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Cast a type to another type.
#define TYPE_HASH(x)
Definition property.h:74
size_t TYPE_ID
Unique type identifier.