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 The 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_LISTENER = std::function<void( INSPECTABLE*, PROPERTY_BASE*, COMMIT* )>;
50
52{
53public:
55
57};
58
74{
75public:
77 {
78 static PROPERTY_MANAGER pm;
79 return pm;
80 }
81
90 void RegisterType( TYPE_ID aType, const wxString& aName );
91
99 PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
100
107 const std::vector<PROPERTY_BASE*>& GetProperties( TYPE_ID aType ) const;
108
109 const std::map<PROPERTY_BASE*, int>& GetDisplayOrder( TYPE_ID aType ) const;
110
111 const std::vector<wxString>& GetGroupDisplayOrder( TYPE_ID aType ) const;
112
124 const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
125
126 void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
127 {
128 return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
129 }
130
141 PROPERTY_BASE& AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup = wxEmptyString );
142
154 PROPERTY_BASE& ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew,
155 const wxString& aGroup = wxEmptyString );
156
162 void AddTypeCast( TYPE_CAST_BASE* aCast );
163
170 void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
171
180 void Mask( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName );
181
190 void OverrideAvailability( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName,
191 std::function<bool( INSPECTABLE* )> aFunc );
192
201 void OverrideWriteability( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName,
202 std::function<bool( INSPECTABLE* )> aFunc );
203
210 bool IsAvailableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
211
218 bool IsWriteableFor( TYPE_ID aItemClass, PROPERTY_BASE* aProp, INSPECTABLE* aItem );
219
223 bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
224
229 void Rebuild();
230
232 {
233 wxString name;
235 std::vector<PROPERTY_BASE*> properties;
236 };
237
238 typedef std::vector<CLASS_INFO> CLASSES_INFO;
239
241
247 void PropertyChanged( INSPECTABLE* aObject, PROPERTY_BASE* aProperty );
248
254 void RegisterListener( TYPE_ID aType, PROPERTY_LISTENER aListenerFunc )
255 {
256 m_listeners[aType].emplace_back( aListenerFunc );
257 }
258
260 {
261 m_listeners[aType].clear();
262 }
263
264private:
266 m_dirty( false ),
267 m_managedCommit( nullptr )
268 {
269 }
270
272
275 {
277 : m_id( aId )
278 {
279 m_groupDisplayOrder.emplace_back( wxEmptyString );
280 m_groups.insert( wxEmptyString );
281 }
282
285
287 std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
288
290 std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
291
293 std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
294
296 std::set<std::pair<size_t, wxString>> m_maskedBaseProperties;
297
299 std::map<std::pair<size_t, wxString>, std::function<bool( INSPECTABLE* )>> m_availabilityOverrides;
300
302 std::map<std::pair<size_t, wxString>, std::function<bool( INSPECTABLE* )>> m_writeabilityOverrides;
303
305 std::vector<PROPERTY_BASE*> m_allProperties;
306
308 std::map<PROPERTY_BASE*, int> m_displayOrder;
309
311 std::vector<wxString> m_groupDisplayOrder;
312
314 std::vector<PROPERTY_BASE*> m_ownDisplayOrder;
315
317 std::set<wxString> m_groups;
318
320 std::set<std::pair<size_t, wxString>> m_replaced;
321
323 void rebuild();
324
327 void collectPropsRecur( std::vector<PROPERTY_BASE*>& aResult,
328 std::set<std::pair<size_t, wxString>>& aReplaced,
329 std::map<PROPERTY_BASE*, int>& aDisplayOrder,
330 std::set<std::pair<size_t, wxString>>& aMasked ) const;
331 };
332
334 CLASS_DESC& getClass( TYPE_ID aTypeId );
335
336 std::unordered_map<TYPE_ID, wxString> m_classNames;
337
339 std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
340
343
344 std::map<TYPE_ID, std::vector<PROPERTY_LISTENER>> m_listeners;
345
347};
348
349
351#define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
352
353#endif /* PROPERTY_MGR_H */
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:73
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:37
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
const std::vector< PROPERTY_BASE * > & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:342
CLASSES_INFO GetAllClasses()
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:238
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
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:346
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 UnregisterListeners(TYPE_ID aType)
Definition: property_mgr.h:259
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:339
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:126
const std::map< PROPERTY_BASE *, int > & GetDisplayOrder(TYPE_ID aType) const
std::map< TYPE_ID, std::vector< PROPERTY_LISTENER > > m_listeners
Definition: property_mgr.h:344
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:254
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:336
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
std::function< void(INSPECTABLE *, PROPERTY_BASE *, COMMIT *)> PROPERTY_LISTENER
Definition: property_mgr.h:49
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:47
Returns metadata for a specific type.
Definition: property_mgr.h:275
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:287
std::set< std::pair< size_t, wxString > > m_replaced
Recreates the list of properties.
Definition: property_mgr.h:320
std::vector< wxString > m_groupDisplayOrder
Non-owning list of classes's direct properties in display order.
Definition: property_mgr.h:311
std::map< std::pair< size_t, wxString >, std::function< bool(INSPECTABLE *)> > m_availabilityOverrides
Overrides for base class property writeable status.
Definition: property_mgr.h:299
std::map< std::pair< size_t, wxString >, std::function< bool(INSPECTABLE *)> > m_writeabilityOverrides
All properties (both unique to the type and inherited)
Definition: property_mgr.h:302
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:290
std::vector< PROPERTY_BASE * > m_ownDisplayOrder
The property groups provided by this class.
Definition: property_mgr.h:314
std::set< std::pair< size_t, wxString > > m_maskedBaseProperties
Overrides for base class property availabilities.
Definition: property_mgr.h:296
std::map< PROPERTY_BASE *, int > m_displayOrder
List of property groups provided by this class in display order.
Definition: property_mgr.h:308
CLASS_DESC(TYPE_ID aId)
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:276
std::vector< PROPERTY_BASE * > m_allProperties
Compiled display order for all properties.
Definition: property_mgr.h:305
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:293
void collectPropsRecur(std::vector< PROPERTY_BASE * > &aResult, std::set< std::pair< size_t, wxString > > &aReplaced, std::map< PROPERTY_BASE *, int > &aDisplayOrder, std::set< std::pair< size_t, wxString > > &aMasked) const
void rebuild()
Traverses the class inheritance hierarchy bottom-to-top, gathering all properties available to a type...
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:284
std::set< wxString > m_groups
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:317
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:235