KiCad PCB EDA Suite
Loading...
Searching...
No Matches
inspectable_impl.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_IMPL_H
24#define INSPECTABLE_IMPL_H
25
26#include <inspectable.h>
28#include <properties/property.h>
29
30template <typename T>
31bool INSPECTABLE::Set( PROPERTY_BASE* aProperty, T aValue, bool aNotify )
32{
34 void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
35
36 if( object )
37 {
38 aProperty->set<T>( object, aValue );
39
40 if( aNotify )
41 propMgr.PropertyChanged( this, aProperty );
42 }
43
44 return object != nullptr;
45}
46
47template <typename T>
48bool INSPECTABLE::Set( const wxString& aProperty, T aValue, bool aNotify )
49{
51 TYPE_ID thisType = TYPE_HASH( *this );
52 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
53 void* object = nullptr;
54
55 if( prop )
56 {
57 object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
58
59 if( object )
60 {
61 prop->set<T>( object, aValue );
62
63 if( aNotify )
64 propMgr.PropertyChanged( this, prop );
65 }
66 }
67
68 return object != nullptr;
69}
70
71template <typename T>
72T INSPECTABLE::Get( PROPERTY_BASE* aProperty ) const
73{
75 const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
76
77 if( !object )
78 throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
79
80 return aProperty->get<T>( object );
81}
82
83template <typename T>
84std::optional<T> INSPECTABLE::Get( const wxString& aProperty ) const
85{
87 TYPE_ID thisType = TYPE_HASH( *this );
88 PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
89 std::optional<T> ret;
90
91 if( prop )
92 {
93 const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
94
95 if( object )
96 ret = prop->get<T>( object );
97 }
98
99 return ret;
100}
101
102#endif /* INSPECTABLE_IMPL_H */
wxAny Get(PROPERTY_BASE *aProperty) const
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
void set(void *aObject, T aValue)
Definition property.h:367
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
T get(const void *aObject) const
Definition property.h:409
Provide class metadata.Helper macro to map type hashes to names.
void PropertyChanged(INSPECTABLE *aObject, PROPERTY_BASE *aProperty)
Callback to alert the notification system that a property has changed.
static PROPERTY_MANAGER & Instance()
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:74
size_t TYPE_ID
Unique type identifier.