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 )
43 {
45 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
46
47 if( object )
48 aProperty->setter( object, aValue );
49
50 return object != nullptr;
51 }
52
53 template<typename T>
54 bool Set( PROPERTY_BASE* aProperty, T aValue )
55 {
57 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
58
59 if( object )
60 aProperty->set<T>( object, aValue );
61
62 return object != nullptr;
63 }
64
65 template<typename T>
66 bool Set( const wxString& aProperty, T aValue )
67 {
69 TYPE_ID thisType = TYPE_HASH( *this );
70 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
71 void* object = nullptr;
72
73 if( prop )
74 {
75 object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
76
77 if( object )
78 prop->set<T>( object, aValue );
79 }
80
81 return object != nullptr;
82 }
83
84 wxAny Get( PROPERTY_BASE* aProperty ) const
85 {
87 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
88 return object ? aProperty->getter( object ) : wxAny();
89 }
90
91 template<typename T>
92 T Get( PROPERTY_BASE* aProperty ) const
93 {
95 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
96
97 if( !object )
98 throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
99
100 return aProperty->get<T>( object );
101 }
102
103 template<typename T>
104 std::optional<T> Get( const wxString& aProperty ) const
105 {
107 TYPE_ID thisType = TYPE_HASH( *this );
108 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
109 std::optional<T> ret;
110
111 if( prop )
112 {
113 const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
114
115 if( object )
116 ret = prop->get<T>( object );
117 }
118
119 return ret;
120 }
121};
122
123#endif /* INSPECTABLE_H */
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
bool Set(const wxString &aProperty, T aValue)
Definition: inspectable.h:66
T Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:92
virtual ~INSPECTABLE()
Definition: inspectable.h:38
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:84
bool Set(PROPERTY_BASE *aProperty, T aValue)
Definition: inspectable.h:54
std::optional< T > Get(const wxString &aProperty) const
Definition: inspectable.h:104
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue)
Definition: inspectable.h:42
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:333
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
T get(const void *aObject) const
Definition: property.h:365
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
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:64
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:46