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 <core/wx_stl_compat.h>
26 
27 #include "property_mgr.h"
28 #include "property.h"
29 
30 #include <boost/optional.hpp>
31 
36 {
37 public:
38  virtual ~INSPECTABLE()
39  {
40  }
41 
42  bool Set( PROPERTY_BASE* aProperty, wxAny& aValue )
43  {
45  TYPE_ID thisType = TYPE_HASH( *this );
46  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
47 
48  if( object )
49  aProperty->setter( object, aValue );
50 
51  return object != nullptr;
52  }
53 
54  template<typename T>
55  bool Set( PROPERTY_BASE* aProperty, T aValue )
56  {
58  TYPE_ID thisType = TYPE_HASH( *this );
59  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
60 
61  if( object )
62  aProperty->set<T>( object, aValue );
63 
64  return object != nullptr;
65  }
66 
67  template<typename T>
68  bool Set( const wxString& aProperty, T aValue )
69  {
71  TYPE_ID thisType = TYPE_HASH( *this );
72  PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
73  void* object = nullptr;
74 
75  if( prop )
76  {
77  object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
78 
79  if( object )
80  prop->set<T>( object, aValue );
81  }
82 
83  return object != nullptr;
84  }
85 
86  wxAny Get( PROPERTY_BASE* aProperty )
87  {
89  TYPE_ID thisType = TYPE_HASH( *this );
90  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
91  return object ? aProperty->getter( object ) : wxAny();
92  }
93 
94  template<typename T>
95  T Get( PROPERTY_BASE* aProperty )
96  {
98  TYPE_ID thisType = TYPE_HASH( *this );
99  void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
100  return object ? aProperty->get<T>( object ) : T();
101  }
102 
103  template<typename T>
104  boost::optional<T> Get( const wxString& aProperty )
105  {
107  TYPE_ID thisType = TYPE_HASH( *this );
108  PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
109  boost::optional<T> ret;
110 
111  if( prop )
112  {
113  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 */
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:65
#define TYPE_HASH(x)
Definition: property.h:59
boost::optional< T > Get(const wxString &aProperty)
Definition: inspectable.h:104
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:42
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Cast a type to another type.
virtual wxAny getter(void *aObject) const =0
virtual ~INSPECTABLE()
Definition: inspectable.h:38
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
bool Set(const wxString &aProperty, T aValue)
Definition: inspectable.h:68
wxAny Get(PROPERTY_BASE *aProperty)
Definition: inspectable.h:86
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue)
Definition: inspectable.h:42
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:35
bool Set(PROPERTY_BASE *aProperty, T aValue)
Definition: inspectable.h:55
virtual void setter(void *aObject, wxAny &aValue)=0
T Get(PROPERTY_BASE *aProperty)
Definition: inspectable.h:95
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:62
T get(void *aObject)
Definition: property.h:268
void set(void *aObject, T aValue)
Definition: property.h:261
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.