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 template<typename T>
77 bool Set( const wxString& aProperty, T aValue, bool aNotify = true )
78 {
80 TYPE_ID thisType = TYPE_HASH( *this );
81 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
82 void* object = nullptr;
83
84 if( prop )
85 {
86 object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
87
88 if( object )
89 {
90 prop->set<T>( object, aValue );
91
92 if( aNotify )
93 propMgr.PropertyChanged( this, prop );
94 }
95 }
96
97 return object != nullptr;
98 }
99
100 wxAny Get( PROPERTY_BASE* aProperty ) const
101 {
103 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
104 return object ? aProperty->getter( object ) : wxAny();
105 }
106
107 template<typename T>
108 T Get( PROPERTY_BASE* aProperty ) const
109 {
111 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
112
113 if( !object )
114 throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
115
116 return aProperty->get<T>( object );
117 }
118
119 template<typename T>
120 std::optional<T> Get( const wxString& aProperty ) const
121 {
123 TYPE_ID thisType = TYPE_HASH( *this );
124 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
125 std::optional<T> ret;
126
127 if( prop )
128 {
129 const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
130
131 if( object )
132 ret = prop->get<T>( object );
133 }
134
135 return ret;
136 }
137};
138
139#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:108
bool Set(PROPERTY_BASE *aProperty, T aValue, bool aNotify=true)
Definition: inspectable.h:60
virtual ~INSPECTABLE()
Definition: inspectable.h:39
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:100
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:77
std::optional< T > Get(const wxString &aProperty) const
Definition: inspectable.h:120
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