KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 PROPERTY_BASE& AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup = wxEmptyString );
150
162 PROPERTY_BASE& 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
209 void OverrideWriteability( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName,
210 std::function<bool( INSPECTABLE* )> aFunc );
211
218 bool IsAvailableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
219
226 bool IsWriteableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
227
231 bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
232
234 {
235 return m_units;
236 }
237
238 void SetUnits( EDA_UNITS aUnits )
239 {
240 m_units = aUnits;
241 }
242
244 void SetTransforms( ORIGIN_TRANSFORMS* aTransforms ) { m_originTransforms = aTransforms; }
245
246 const EDA_IU_SCALE* GetIuScale() const { return m_iuScale; }
247 void SetIuScale( const EDA_IU_SCALE* aScale ) { m_iuScale = aScale; }
248
253 void Rebuild();
254
256 {
257 wxString name;
259 std::vector<PROPERTY_BASE*> properties;
260 };
261
262 typedef std::vector<CLASS_INFO> CLASSES_INFO;
263
265
266 std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
267
268private:
270 m_dirty( false ),
272 m_originTransforms( nullptr ),
274 {
275 }
276
279 {
281 : m_id( aId )
282 {
283 m_groupDisplayOrder.emplace_back( wxEmptyString );
284 m_groups.insert( wxEmptyString );
285 }
286
289
291 std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
292
294 std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
295
297 std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
298
301
304
307
309 std::vector<PROPERTY_BASE*> m_allProperties;
310
313
315 std::vector<wxString> m_groupDisplayOrder;
316
318 std::vector<PROPERTY_BASE*> m_ownDisplayOrder;
319
321 std::set<wxString> m_groups;
322
325
327 void rebuild();
328
331 void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced,
332 PROPERTY_DISPLAY_ORDER& aDisplayOrder,
333 PROPERTY_SET& aMasked ) const;
334 };
335
337 CLASS_DESC& getClass( TYPE_ID aTypeId );
338
339 std::unordered_map<TYPE_ID, wxString> m_classNames;
340
342 std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
343
346
348
350
352};
353
354
356#define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
357
358#endif /* PROPERTY_MGR_H */
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
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:345
CLASSES_INFO GetAllClasses()
EDA_UNITS m_units
Definition: property_mgr.h:347
ORIGIN_TRANSFORMS * m_originTransforms
Definition: property_mgr.h:349
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:262
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
bool IsWriteableFor(TYPE_ID aItemClass, PROPERTY_BASE *aProp, INSPECTABLE *aItem)
Checks overriden availability and original availability of a property, returns false if the property ...
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
const EDA_IU_SCALE * m_iuScale
Definition: property_mgr.h:351
PROPERTY_BASE & 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:233
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:342
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)
const EDA_IU_SCALE * GetIuScale() const
Definition: property_mgr.h:246
void SetTransforms(ORIGIN_TRANSFORMS *aTransforms)
Definition: property_mgr.h:244
void SetIuScale(const EDA_IU_SCALE *aScale)
Definition: property_mgr.h:247
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_BASE & ReplaceProperty(size_t aBase, const wxString &aName, PROPERTY_BASE *aNew, const wxString &aGroup=wxEmptyString)
Replace an existing property for a specific type.
PROPERTY_MANAGER()
Structure holding type meta-data.
Definition: property_mgr.h:269
void SetUnits(EDA_UNITS aUnits)
Definition: property_mgr.h:238
void OverrideWriteability(TYPE_ID aDerived, TYPE_ID aBase, const wxString &aName, std::function< bool(INSPECTABLE *)> aFunc)
Sets an override writeability functor for a base class property of a given derived class.
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:339
ORIGIN_TRANSFORMS * GetTransforms() const
Definition: property_mgr.h:243
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:44
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:279
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:291
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:324
PROPERTY_SET m_maskedBaseProperties
Overrides for base class property availabilities.
Definition: property_mgr.h:300
std::vector< wxString > m_groupDisplayOrder
Non-owning list of classes's direct properties in display order.
Definition: property_mgr.h:315
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:294
std::vector< PROPERTY_BASE * > m_ownDisplayOrder
The property groups provided by this class.
Definition: property_mgr.h:318
CLASS_DESC(TYPE_ID aId)
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:280
std::vector< PROPERTY_BASE * > m_allProperties
Compiled display order for all properties.
Definition: property_mgr.h:309
PROPERTY_DISPLAY_ORDER m_displayOrder
List of property groups provided by this class in display order.
Definition: property_mgr.h:312
PROPERTY_FUNCTOR_MAP m_writeabilityOverrides
All properties (both unique to the type and inherited)
Definition: property_mgr.h:306
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:297
void rebuild()
Traverses the class inheritance hierarchy bottom-to-top, gathering all properties available to a type...
void collectPropsRecur(PROPERTY_LIST &aResult, PROPERTY_SET &aReplaced, PROPERTY_DISPLAY_ORDER &aDisplayOrder, PROPERTY_SET &aMasked) const
PROPERTY_FUNCTOR_MAP m_availabilityOverrides
Overrides for base class property writeable status.
Definition: property_mgr.h:303
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:288
std::set< wxString > m_groups
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:321
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:259