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 aProperty->set<wxString>( object, choices.GetLabel( aValue.GetInteger() ) );
87 else
88 aProperty->set<wxVariant>( object, aValue );
89
90 if( aNotify )
91 propMgr.PropertyChanged( this, aProperty );
92 }
93
94 return object != nullptr;
95 }
96
97 template<typename T>
98 bool Set( const wxString& aProperty, T aValue, bool aNotify = true )
99 {
101 TYPE_ID thisType = TYPE_HASH( *this );
102 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
103 void* object = nullptr;
104
105 if( prop )
106 {
107 object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
108
109 if( object )
110 {
111 prop->set<T>( object, aValue );
112
113 if( aNotify )
114 propMgr.PropertyChanged( this, prop );
115 }
116 }
117
118 return object != nullptr;
119 }
120
121 wxAny Get( PROPERTY_BASE* aProperty ) const
122 {
124 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
125 return object ? aProperty->getter( object ) : wxAny();
126 }
127
128 template<typename T>
129 T Get( PROPERTY_BASE* aProperty ) const
130 {
132 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
133
134 if( !object )
135 throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
136
137 return aProperty->get<T>( object );
138 }
139
140 template<typename T>
141 std::optional<T> Get( const wxString& aProperty ) const
142 {
144 TYPE_ID thisType = TYPE_HASH( *this );
145 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
146 std::optional<T> ret;
147
148 if( prop )
149 {
150 const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
151
152 if( object )
153 ret = prop->get<T>( object );
154 }
155
156 return ret;
157 }
158};
159
160#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
Definition: inspectable.h:129
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
Definition: inspectable.h:121
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
Definition: inspectable.h:43
bool Set(const wxString &aProperty, T aValue, bool aNotify=true)
Definition: inspectable.h:98
std::optional< T > Get(const wxString &aProperty) const
Definition: inspectable.h:141
virtual wxAny getter(const void *aObject) const =0
wxPGChoices GetChoices(INSPECTABLE *aObject) const
Definition: property.h:264
virtual void setter(void *aObject, wxAny &aValue)=0
void set(void *aObject, T aValue)
Definition: property.h:363
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
T get(const void *aObject) const
Definition: property.h:405
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
void PropertyChanged(INSPECTABLE *aObject, PROPERTY_BASE *aProperty)
Callback to alert the notification system that a property has changed.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
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:72
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47