KiCad PCB EDA Suite
property_mgr.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 (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#ifndef PROPERTY_MGR_H
25#define PROPERTY_MGR_H
26
27#include <core/wx_stl_compat.h> // Needed for stl hash extensions
28
29#include <wx/string.h>
30
31#include <functional>
32#include <list>
33#include <map>
34#include <unordered_map>
35#include <set>
36#include <vector>
37#include <memory>
38#include <eda_units.h>
39
40class PROPERTY_BASE;
41class TYPE_CAST_BASE;
43class INSPECTABLE;
44
46using TYPE_ID = size_t;
47
48using PROPERTY_LIST = std::vector<PROPERTY_BASE*>;
49
50using PROPERTY_SET = std::set<std::pair<size_t, wxString>>;
51
52template<typename ValueType>
53using PROPERTY_MAP = std::map<std::pair<size_t, wxString>, ValueType>;
54
55using PROPERTY_FUNCTOR_MAP = PROPERTY_MAP<std::function<bool( INSPECTABLE* )>>;
56
57using PROPERTY_DISPLAY_ORDER = std::map<PROPERTY_BASE*, int>;
58
74{
75public:
77 {
78 static PROPERTY_MANAGER pm;
79 return pm;
80 }
81
90 void RegisterType( TYPE_ID aType, const wxString& aName );
91
98 const wxString& ResolveType( TYPE_ID aType ) const;
99
107 PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
108
115 const PROPERTY_LIST& GetProperties( TYPE_ID aType ) const;
116
117 const PROPERTY_DISPLAY_ORDER& GetDisplayOrder( TYPE_ID aType ) const;
118
119 const std::vector<wxString>& GetGroupDisplayOrder( TYPE_ID aType ) const;
120
132 const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
133
134 void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
135 {
136 return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
137 }
138
149 void AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup = wxEmptyString );
150
162 void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
163 const wxString& aGroup = wxEmptyString );
164
170 void AddTypeCast( TYPE_CAST_BASE* aCast );
171
178 void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
179
188 void Mask( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName );
189
198 void OverrideAvailability( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName,
199 std::function<bool( INSPECTABLE* )> aFunc );
200
207 bool IsAvailableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
208
212 bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
213
215 {
216 return m_units;
217 }
218
219 void SetUnits( EDA_UNITS aUnits )
220 {
221 m_units = aUnits;
222 }
223
225 void SetTransforms( ORIGIN_TRANSFORMS* aTransforms ) { m_originTransforms = aTransforms; }
226
231 void Rebuild();
232
234 {
235 wxString name;
237 std::vector<PROPERTY_BASE*> properties;
238 };
239
240 typedef std::vector<CLASS_INFO> CLASSES_INFO;
241
243
244 std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
245
246private:
248 m_dirty( false ),
250 m_originTransforms( nullptr )
251 {
252 }
253
256 {
258 : m_id( aId )
259 {
260 m_groupDisplayOrder.emplace_back( wxEmptyString );
261 m_groups.insert( wxEmptyString );
262 }
263
266
268 std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
269
271 std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
272
274 std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
275
278
281
283 std::vector<PROPERTY_BASE*> m_allProperties;
284
287
289 std::vector<wxString> m_groupDisplayOrder;
290
292 std::vector<PROPERTY_BASE*> m_ownDisplayOrder;
293
295 std::set<wxString> m_groups;
296
299
301 void rebuild();
302
305 void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced,
306 PROPERTY_DISPLAY_ORDER& aDisplayOrder,
307 const PROPERTY_SET& aMasked ) const;
308 };
309
311 CLASS_DESC& getClass( TYPE_ID aTypeId );
312
313 std::unordered_map<TYPE_ID, wxString> m_classNames;
314
316 std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
317
320
322
324};
325
326
328#define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
329
330#endif /* PROPERTY_MGR_H */
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
A class to perform either relative or absolute display origin transforms for a single axis of a point...
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:319
CLASSES_INFO GetAllClasses()
EDA_UNITS m_units
Definition: property_mgr.h:321
ORIGIN_TRANSFORMS * m_originTransforms
Definition: property_mgr.h:323
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:240
const wxString & ResolveType(TYPE_ID aType) const
Return name of a type.
const PROPERTY_LIST & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
const PROPERTY_DISPLAY_ORDER & GetDisplayOrder(TYPE_ID aType) const
void Mask(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName)
Sets a base class property as masked in a derived class.
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
void AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
void Rebuild()
Rebuild the list of all registered properties.
EDA_UNITS GetUnits() const
Definition: property_mgr.h:214
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:316
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Cast a type to another type.
CLASS_DESC & getClass(TYPE_ID aTypeId)
void RegisterType(TYPE_ID aType, const wxString &aName)
Associate a name with a type.
bool IsAvailableFor(TYPE_ID aItemClass, PROPERTY_BASE *aProp, INSPECTABLE *aItem)
Checks overriden availability and original availability of a property, returns false if the property ...
void * TypeCast(void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Definition: property_mgr.h:134
std::vector< TYPE_ID > GetMatchingClasses(PROPERTY_BASE *aProperty)
void SetTransforms(ORIGIN_TRANSFORMS *aTransforms)
Definition: property_mgr.h:225
void OverrideAvailability(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName, std::function< bool(INSPECTABLE *)> aFunc)
Sets an override availability functor for a base class property of a given derived class.
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
PROPERTY_MANAGER()
Structure holding type meta-data.
Definition: property_mgr.h:247
void ReplaceProperty(size_t aBase, const wxString &aName, PROPERTY_BASE *aNew, const wxString &aGroup=wxEmptyString)
Replace an existing property for a specific type.
void SetUnits(EDA_UNITS aUnits)
Definition: property_mgr.h:219
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:313
ORIGIN_TRANSFORMS * GetTransforms() const
Definition: property_mgr.h:224
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
EDA_UNITS
Definition: eda_units.h:43
PROPERTY_MAP< std::function< bool(INSPECTABLE *)> > PROPERTY_FUNCTOR_MAP
Definition: property_mgr.h:55
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:48
std::map< std::pair< size_t, wxString >, ValueType > PROPERTY_MAP
Definition: property_mgr.h:53
std::map< PROPERTY_BASE *, int > PROPERTY_DISPLAY_ORDER
Definition: property_mgr.h:57
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:50
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:46
Returns metadata for a specific type.
Definition: property_mgr.h:256
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:268
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:298
PROPERTY_SET m_maskedBaseProperties
Overrides for base class property availabilities.
Definition: property_mgr.h:277
std::vector< wxString > m_groupDisplayOrder
Non-owning list of classes's direct properties in display order.
Definition: property_mgr.h:289
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:271
std::vector< PROPERTY_BASE * > m_ownDisplayOrder
The property groups provided by this class.
Definition: property_mgr.h:292
CLASS_DESC(TYPE_ID aId)
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:257
std::vector< PROPERTY_BASE * > m_allProperties
Compiled display order for all properties.
Definition: property_mgr.h:283
PROPERTY_DISPLAY_ORDER m_displayOrder
List of property groups provided by this class in display order.
Definition: property_mgr.h:286
std::map< TYPE_ID, std::unique_ptr< TYPE_CAST_BASE > > m_typeCasts
Properties from bases that should be masked (hidden) on this subclass.
Definition: property_mgr.h:274
void rebuild()
Traverses the class inheritance hierarchy bottom-to-top, gathering all properties available to a type...
PROPERTY_FUNCTOR_MAP m_availabilityOverrides
All properties (both unique to the type and inherited)
Definition: property_mgr.h:280
void collectPropsRecur(PROPERTY_LIST &aResult, PROPERTY_SET &aReplaced, PROPERTY_DISPLAY_ORDER &aDisplayOrder, const PROPERTY_SET &aMasked) const
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:265
std::set< wxString > m_groups
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:295
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:237