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-2023 CERN
5 * Copyright (C) 2021-2023 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;
44class COMMIT;
45
47using TYPE_ID = size_t;
48
49using PROPERTY_LIST = std::vector<PROPERTY_BASE*>;
50
51using PROPERTY_SET = std::set<std::pair<size_t, wxString>>;
52
53template<typename ValueType>
54using PROPERTY_MAP = std::map<std::pair<size_t, wxString>, ValueType>;
55
56using PROPERTY_FUNCTOR_MAP = PROPERTY_MAP<std::function<bool( INSPECTABLE* )>>;
57
58using PROPERTY_DISPLAY_ORDER = std::map<PROPERTY_BASE*, int>;
59
60using PROPERTY_LISTENER = std::function<void( INSPECTABLE*, PROPERTY_BASE*, COMMIT* )>;
61
63{
64public:
66
68};
69
85{
86public:
88 {
89 static PROPERTY_MANAGER pm;
90 return pm;
91 }
92
101 void RegisterType( TYPE_ID aType, const wxString& aName );
102
109 const wxString& ResolveType( TYPE_ID aType ) const;
110
118 PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
119
126 const PROPERTY_LIST& GetProperties( TYPE_ID aType ) const;
127
128 const PROPERTY_DISPLAY_ORDER& GetDisplayOrder( TYPE_ID aType ) const;
129
130 const std::vector<wxString>& GetGroupDisplayOrder( TYPE_ID aType ) const;
131
143 const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
144
145 void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
146 {
147 return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
148 }
149
160 PROPERTY_BASE& AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup = wxEmptyString );
161
173 PROPERTY_BASE& ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
174 const wxString& aGroup = wxEmptyString );
175
181 void AddTypeCast( TYPE_CAST_BASE* aCast );
182
189 void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
190
199 void Mask( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName );
200
209 void OverrideAvailability( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName,
210 std::function<bool( INSPECTABLE* )> aFunc );
211
220 void OverrideWriteability( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName,
221 std::function<bool( INSPECTABLE* )> aFunc );
222
229 bool IsAvailableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
230
237 bool IsWriteableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
238
242 bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
243
248 void Rebuild();
249
251 {
252 wxString name;
254 std::vector<PROPERTY_BASE*> properties;
255 };
256
257 typedef std::vector<CLASS_INFO> CLASSES_INFO;
258
260
261 std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
262
268 void PropertyChanged( INSPECTABLE* aObject, PROPERTY_BASE* aProperty );
269
275 void RegisterListener( TYPE_ID aType, PROPERTY_LISTENER aListenerFunc )
276 {
277 m_listeners[aType].emplace_back( aListenerFunc );
278 }
279
280private:
282 m_dirty( false ),
283 m_managedCommit( nullptr )
284 {
285 }
286
288
291 {
293 : m_id( aId )
294 {
295 m_groupDisplayOrder.emplace_back( wxEmptyString );
296 m_groups.insert( wxEmptyString );
297 }
298
301
303 std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
304
306 std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
307
309 std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
310
313
316
319
321 std::vector<PROPERTY_BASE*> m_allProperties;
322
325
327 std::vector<wxString> m_groupDisplayOrder;
328
330 std::vector<PROPERTY_BASE*> m_ownDisplayOrder;
331
333 std::set<wxString> m_groups;
334
337
339 void rebuild();
340
343 void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced,
344 PROPERTY_DISPLAY_ORDER& aDisplayOrder,
345 PROPERTY_SET& aMasked ) const;
346 };
347
349 CLASS_DESC& getClass( TYPE_ID aTypeId );
350
351 std::unordered_map<TYPE_ID, wxString> m_classNames;
352
354 std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
355
358
359 std::map<TYPE_ID, std::vector<PROPERTY_LISTENER>> m_listeners;
360
362};
363
364
366#define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
367
368#endif /* PROPERTY_MGR_H */
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
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:85
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:357
CLASSES_INFO GetAllClasses()
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:257
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 PropertyChanged(INSPECTABLE *aObject, PROPERTY_BASE *aProperty)
Callback to alert the notification system that a property has changed.
COMMIT * m_managedCommit
Definition: property_mgr.h:361
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:87
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
void Rebuild()
Rebuild the list of all registered properties.
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:354
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:145
std::vector< TYPE_ID > GetMatchingClasses(PROPERTY_BASE *aProperty)
std::map< TYPE_ID, std::vector< PROPERTY_LISTENER > > m_listeners
Definition: property_mgr.h:359
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.
void RegisterListener(TYPE_ID aType, PROPERTY_LISTENER aListenerFunc)
Registers a listener for the given type.
Definition: property_mgr.h:275
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:351
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
PROPERTY_MAP< std::function< bool(INSPECTABLE *)> > PROPERTY_FUNCTOR_MAP
Definition: property_mgr.h:56
std::function< void(INSPECTABLE *, PROPERTY_BASE *, COMMIT *)> PROPERTY_LISTENER
Definition: property_mgr.h:60
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:49
std::map< std::pair< size_t, wxString >, ValueType > PROPERTY_MAP
Definition: property_mgr.h:54
std::map< PROPERTY_BASE *, int > PROPERTY_DISPLAY_ORDER
Definition: property_mgr.h:58
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:51
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47
Returns metadata for a specific type.
Definition: property_mgr.h:291
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:303
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:336
PROPERTY_SET m_maskedBaseProperties
Overrides for base class property availabilities.
Definition: property_mgr.h:312
std::vector< wxString > m_groupDisplayOrder
Non-owning list of classes's direct properties in display order.
Definition: property_mgr.h:327
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:306
std::vector< PROPERTY_BASE * > m_ownDisplayOrder
The property groups provided by this class.
Definition: property_mgr.h:330
CLASS_DESC(TYPE_ID aId)
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:292
std::vector< PROPERTY_BASE * > m_allProperties
Compiled display order for all properties.
Definition: property_mgr.h:321
PROPERTY_DISPLAY_ORDER m_displayOrder
List of property groups provided by this class in display order.
Definition: property_mgr.h:324
PROPERTY_FUNCTOR_MAP m_writeabilityOverrides
All properties (both unique to the type and inherited)
Definition: property_mgr.h:318
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:309
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:315
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:300
std::set< wxString > m_groups
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:333
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:254