KiCad PCB EDA Suite
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 <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 "property_mgr.h"
26 #include "property.h"
27 
28 #include <boost/optional.hpp>
29 
34 {
35 public:
36  virtual ~INSPECTABLE()
37  {
38  }
39 
40  bool Set( PROPERTY_BASE* aProperty, wxAny& aValue )
41  {
43  TYPE_ID thisType = TYPE_HASH( *this );
44  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
45 
46  if( object )
47  aProperty->setter( object, aValue );
48 
49  return object != nullptr;
50  }
51 
52  template<typename T>
53  bool Set( PROPERTY_BASE* aProperty, T aValue )
54  {
56  TYPE_ID thisType = TYPE_HASH( *this );
57  void* object = propMgr.TypeCast( this, thisType, 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 )
85  {
87  TYPE_ID thisType = TYPE_HASH( *this );
88  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
89  return object ? aProperty->getter( object ) : wxAny();
90  }
91 
92  template<typename T>
93  T Get( PROPERTY_BASE* aProperty )
94  {
96  TYPE_ID thisType = TYPE_HASH( *this );
97  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
98  return object ? aProperty->get<T>( object ) : T();
99  }
100 
101  template<typename T>
102  boost::optional<T> Get( const wxString& aProperty )
103  {
105  TYPE_ID thisType = TYPE_HASH( *this );
106  PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
107  boost::optional<T> ret;
108 
109  if( prop )
110  {
111  void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
112 
113  if( object )
114  ret = prop->get<T>( object );
115  }
116 
117  return ret;
118  }
119 };
120 
121 #endif /* INSPECTABLE_H */
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:61
#define TYPE_HASH(x)
Macro to generate unique identifier for a type
Definition: property.h:53
boost::optional< T > Get(const wxString &aProperty)
Definition: inspectable.h:102
size_t TYPE_ID
Unique type identifier
Definition: property_mgr.h:38
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Casts a type to another type.
T
enum T contains all this lexer's tokens.
virtual wxAny getter(void *aObject) const =0
virtual ~INSPECTABLE()
Definition: inspectable.h:36
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Returns a property for a specific type.
bool Set(const wxString &aProperty, T aValue)
Definition: inspectable.h:66
wxAny Get(PROPERTY_BASE *aProperty)
Definition: inspectable.h:84
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue)
Definition: inspectable.h:40
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:33
bool Set(PROPERTY_BASE *aProperty, T aValue)
Definition: inspectable.h:53
virtual void setter(void *aObject, wxAny &aValue)=0
T Get(PROPERTY_BASE *aProperty)
Definition: inspectable.h:93
Provides class metadata.
Definition: property_mgr.h:58
T get(void *aObject)
Definition: property.h:273
void set(void *aObject, T aValue)
Definition: property.h:266
virtual size_t OwnerHash() const =0
Returns type-id of the Owner class.