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 * @author Tomasz Wlostowski <[email protected]>
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef INSPECTABLE_H
23#define INSPECTABLE_H
24
25#include <core/wx_stl_compat.h>
26
28#include <properties/property.h>
29
30#include <optional>
31
36{
37public:
38 virtual ~INSPECTABLE()
39 {
40 }
41
42 bool Set( PROPERTY_BASE* aProperty, wxAny& aValue, bool aNotify = true )
43 {
45 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
46
47 if( object )
48 {
49 aProperty->setter( object, aValue );
50
51 if( aNotify )
52 propMgr.PropertyChanged( this, aProperty );
53 }
54
55 return object != nullptr;
56 }
57
58 template<typename T>
59 bool Set( PROPERTY_BASE* aProperty, T aValue, bool aNotify = true )
60 {
62 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
63
64 if( object )
65 {
66 aProperty->set<T>( object, aValue );
67
68 if( aNotify )
69 propMgr.PropertyChanged( this, aProperty );
70 }
71
72 return object != nullptr;
73 }
74
75 template<typename T>
76 bool Set( const wxString& aProperty, T aValue, bool aNotify = true )
77 {
79 TYPE_ID thisType = TYPE_HASH( *this );
80 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
81 void* object = nullptr;
82
83 if( prop )
84 {
85 object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
86
87 if( object )
88 {
89 prop->set<T>( object, aValue );
90
91 if( aNotify )
92 propMgr.PropertyChanged( this, prop );
93 }
94 }
95
96 return object != nullptr;
97 }
98
99 wxAny Get( PROPERTY_BASE* aProperty ) const
100 {
102 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
103 return object ? aProperty->getter( object ) : wxAny();
104 }
105
106 template<typename T>
107 T Get( PROPERTY_BASE* aProperty ) const
108 {
110 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
111
112 if( !object )
113 throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
114
115 return aProperty->get<T>( object );
116 }
117
118 template<typename T>
119 std::optional<T> Get( const wxString& aProperty ) const
120 {
122 TYPE_ID thisType = TYPE_HASH( *this );
123 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
124 std::optional<T> ret;
125
126 if( prop )
127 {
128 const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
129
130 if( object )
131 ret = prop->get<T>( object );
132 }
133
134 return ret;
135 }
136};
137
138#endif /* INSPECTABLE_H */
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
T Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:107
bool Set(PROPERTY_BASE *aProperty, T aValue, bool aNotify=true)
Definition: inspectable.h:59
virtual ~INSPECTABLE()
Definition: inspectable.h:38
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:99
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
Definition: inspectable.h:42
bool Set(const wxString &aProperty, T aValue, bool aNotify=true)
Definition: inspectable.h:76
std::optional< T > Get(const wxString &aProperty) const
Definition: inspectable.h:119
virtual wxAny getter(const void *aObject) const =0
virtual void setter(void *aObject, wxAny &aValue)=0
void set(void *aObject, T aValue)
Definition: property.h:348
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
T get(const void *aObject) const
Definition: property.h:390
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
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:87
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:71
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47