KiCad PCB EDA Suite
Loading...
Searching...
No Matches
property_mgr.cpp
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 * @author Tomasz Wlostowski <[email protected]>
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23#include <properties/property.h>
24
25#include <algorithm>
26#include <utility>
27#include <wx/wx.h>
28
29static wxString EMPTY_STRING( wxEmptyString );
30
31
32void PROPERTY_MANAGER::RegisterType( TYPE_ID aType, const wxString& aName )
33{
34 wxASSERT( m_classNames.count( aType ) == 0 );
35 m_classNames.emplace( aType, aName );
36}
37
38
39const wxString& PROPERTY_MANAGER::ResolveType( TYPE_ID aType ) const
40{
41 auto it = m_classNames.find( aType );
42 return it != m_classNames.end() ? it->second : EMPTY_STRING;
43}
44
45
46PROPERTY_BASE* PROPERTY_MANAGER::GetProperty( TYPE_ID aType, const wxString& aProperty ) const
47{
48 if( m_dirty )
49 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
50
51 auto it = m_classes.find( aType );
52
53 if( it == m_classes.end() )
54 return nullptr;
55
56 const CLASS_DESC& classDesc = it->second;
57
58 for( PROPERTY_BASE* property : classDesc.m_allProperties )
59 {
60 if( !aProperty.CmpNoCase( property->Name() ) )
61 return property;
62 }
63
64 return nullptr;
65}
66
67
69{
70 if( m_dirty )
71 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
72
73 static const PROPERTY_LIST empty;
74 auto it = m_classes.find( aType );
75
76 if( it == m_classes.end() )
77 return empty;
78
79 return it->second.m_allProperties;
80}
81
82
84{
85 if( m_dirty )
86 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
87
88 static const PROPERTY_DISPLAY_ORDER empty;
89 auto it = m_classes.find( aType );
90
91 if( it == m_classes.end() )
92 return empty;
93
94 return it->second.m_displayOrder;
95}
96
97
98const std::vector<wxString>& PROPERTY_MANAGER::GetGroupDisplayOrder( TYPE_ID aType ) const
99{
100 if( m_dirty )
101 const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
102
103 static const std::vector<wxString> empty;
104 auto it = m_classes.find( aType );
105
106 if( it == m_classes.end() )
107 return empty;
108
109 return it->second.m_groupDisplayOrder;
110}
111
112
113const void* PROPERTY_MANAGER::TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
114{
115 if( aBase == aTarget )
116 return aSource;
117
118 auto classDesc = m_classes.find( aBase );
119
120 if( classDesc == m_classes.end() )
121 return aSource;
122
123 auto& converters = classDesc->second.m_typeCasts;
124 auto converter = converters.find( aTarget );
125
126 if( converter == converters.end() ) // explicit type cast not found
127 return IsOfType( aBase, aTarget ) ? aSource : nullptr;
128
129 return (*converter->second)( aSource );
130}
131
132
133PROPERTY_BASE& PROPERTY_MANAGER::AddProperty( PROPERTY_BASE* aProperty, const wxString& aGroup )
134{
135 const wxString& name = aProperty->Name();
136 TYPE_ID hash = aProperty->OwnerHash();
137 CLASS_DESC& classDesc = getClass( hash );
138 classDesc.m_ownProperties.emplace( name, aProperty );
139 classDesc.m_ownDisplayOrder.emplace_back( aProperty );
140
141 aProperty->SetGroup( aGroup );
142
143 if( !classDesc.m_groups.count( aGroup ) )
144 {
145 classDesc.m_groupDisplayOrder.emplace_back( aGroup );
146 classDesc.m_groups.insert( aGroup );
147 }
148
149 m_dirty = true;
150 return *aProperty;
151}
152
153
154PROPERTY_BASE& PROPERTY_MANAGER::ReplaceProperty( size_t aBase, const wxString& aName,
155 PROPERTY_BASE* aNew, const wxString& aGroup )
156{
157 CLASS_DESC& classDesc = getClass( aNew->OwnerHash() );
158 classDesc.m_replaced.insert( std::make_pair( aBase, aName ) );
159 return AddProperty( aNew, aGroup );
160}
161
162
164{
165 TYPE_ID derivedHash = aCast->DerivedHash();
166 CLASS_DESC& classDesc = getClass( aCast->BaseHash() );
167 auto& typeCasts = classDesc.m_typeCasts;
168 wxASSERT_MSG( typeCasts.count( derivedHash ) == 0, "Such converter already exists" );
169 typeCasts.emplace( derivedHash, aCast );
170}
171
172
174{
175 wxASSERT_MSG( aDerived != aBase, "Class cannot inherit from itself" );
176
177 CLASS_DESC& derived = getClass( aDerived );
178 CLASS_DESC& base = getClass( aBase );
179 derived.m_bases.push_back( base );
180 m_dirty = true;
181
182 wxASSERT_MSG( derived.m_bases.size() == 1 || derived.m_typeCasts.count( aBase ) == 1,
183 "You need to add a TYPE_CAST for classes inheriting from multiple bases" );
184}
185
186
187void PROPERTY_MANAGER::Mask( TYPE_ID aDerived, TYPE_ID aBase, const wxString& aName )
188{
189 wxASSERT_MSG( aDerived != aBase, "Class cannot mask from itself" );
190
191 CLASS_DESC& derived = getClass( aDerived );
192 derived.m_maskedBaseProperties.insert( std::make_pair( aBase, aName ) );
193 m_dirty = true;
194}
195
196
198 const wxString& aName,
199 std::function<bool( INSPECTABLE* )> aFunc )
200{
201 wxASSERT_MSG( aDerived != aBase, "Class cannot override from itself" );
202
203 CLASS_DESC& derived = getClass( aDerived );
204 derived.m_availabilityOverrides[std::make_pair( aBase, aName )] = aFunc;
205 m_dirty = true;
206}
207
208
210 const wxString& aName,
211 std::function<bool( INSPECTABLE* )> aFunc )
212{
213 wxASSERT_MSG( aDerived != aBase, "Class cannot override from itself" );
214
215 CLASS_DESC& derived = getClass( aDerived );
216 derived.m_writeabilityOverrides[std::make_pair( aBase, aName )] = aFunc;
217 m_dirty = true;
218}
219
220
222 INSPECTABLE* aItem )
223{
224 if( !aProp->Available( aItem ) )
225 return false;
226
227 CLASS_DESC& derived = getClass( aItemClass );
228
229 auto it = derived.m_availabilityOverrides.find( std::make_pair( aProp->BaseHash(),
230 aProp->Name() ) );
231
232 if( it != derived.m_availabilityOverrides.end() )
233 return it->second( aItem );
234
235 return true;
236}
237
238
240 INSPECTABLE* aItem )
241{
242 if( !aProp->Writeable( aItem ) )
243 return false;
244
245 CLASS_DESC& derived = getClass( aItemClass );
246
247 auto it = derived.m_writeabilityOverrides.find( std::make_pair( aProp->BaseHash(),
248 aProp->Name() ) );
249
250 if( it != derived.m_writeabilityOverrides.end() )
251 return it->second( aItem );
252
253 return true;
254}
255
256
257bool PROPERTY_MANAGER::IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const
258{
259 if( aDerived == aBase )
260 return true;
261
262 auto derived = m_classes.find( aDerived );
263 wxCHECK( derived != m_classes.end(), false ); // missing class description
264
265 // traverse the hierarchy seeking for the base class
266 for( auto& base : derived->second.m_bases )
267 {
268 if( IsOfType( base.get().m_id, aBase ) )
269 return true;
270 }
271
272 return false;
273}
274
275
277{
278 for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
279 classEntry.second.rebuild();
280
281 m_dirty = false;
282}
283
284
286{
287 auto it = m_classes.find( aTypeId );
288
289 if( it == m_classes.end() )
290 tie( it, std::ignore ) = m_classes.emplace( aTypeId, CLASS_DESC( aTypeId ) );
291
292 return it->second;
293}
294
295
297{
298 PROPERTY_SET replaced;
299 PROPERTY_SET masked;
300 m_allProperties.clear();
302 // We need to keep properties sorted to be able to use std::set_* functions
303 sort( m_allProperties.begin(), m_allProperties.end() );
304
305 std::vector<wxString> displayOrder;
306 std::set<wxString> groups;
307
308 auto collectGroups =
309 [&]( std::set<wxString>& aSet, std::vector<wxString>& aResult )
310 {
311 auto collectGroupsRecursive =
312 []( auto& aSelf, std::set<wxString>& aSetR, std::vector<wxString>& aResultR,
313 const CLASS_DESC& aClassR ) -> void
314 {
315 for( const wxString& group : aClassR.m_groupDisplayOrder )
316 {
317 if( !aSetR.count( group ) )
318 {
319 aSetR.insert( group );
320 aResultR.emplace_back( group );
321 }
322 }
323
324 for( const CLASS_DESC& base : aClassR.m_bases )
325 aSelf( aSelf, aSetR, aResultR, base );
326 };
327
328 collectGroupsRecursive( collectGroupsRecursive, aSet, aResult, *this );
329 };
330
331 // TODO(JE): This currently relies on rebuild() happening after all properties are added
332 // separate out own groups vs. all groups to fix
333 collectGroups( groups, displayOrder );
334 m_groupDisplayOrder = displayOrder;
335}
336
337
339 PROPERTY_SET& aReplaced,
340 PROPERTY_DISPLAY_ORDER& aDisplayOrder,
341 PROPERTY_SET& aMasked ) const
342{
343 for( const std::pair<size_t, wxString>& replacedEntry : m_replaced )
344 aReplaced.emplace( replacedEntry );
345
346 for( const std::pair<size_t, wxString>& maskedEntry : m_maskedBaseProperties )
347 aMasked.emplace( maskedEntry );
348
349 /*
350 * We want to insert our own properties in forward order, but earlier than anything already in
351 * the list (which will have been added by a subclass of us)
352 */
353 int displayOrderStart = 0;
354
355 if( !aDisplayOrder.empty() )
356 {
357 int firstSoFar = std::min_element( aDisplayOrder.begin(), aDisplayOrder.end(),
358 []( const std::pair<PROPERTY_BASE*, int>& aFirst,
359 const std::pair<PROPERTY_BASE*, int>& aSecond )
360 {
361 return aFirst.second < aSecond.second;
362 } )->second;
363
364 displayOrderStart = firstSoFar - m_ownProperties.size();
365 }
366
367 int idx = 0;
368
369 for( PROPERTY_BASE* property : m_ownDisplayOrder )
370 {
371 PROPERTY_SET::key_type propertyKey = std::make_pair( property->OwnerHash(),
372 property->Name() );
373 // Do not store replaced properties
374 if( aReplaced.count( propertyKey ) )
375 continue;
376
377 // Do not store masked properties
378 if( aMasked.count( propertyKey ) )
379 continue;
380
381 aDisplayOrder[property] = displayOrderStart + idx++;
382 aResult.push_back( property );
383 }
384
385 // Iterate backwards so that replaced properties appear before base properties
386 for( auto it = m_bases.rbegin(); it != m_bases.rend(); ++it )
387 it->get().collectPropsRecur( aResult, aReplaced, aDisplayOrder, aMasked );
388}
389
390
391std::vector<TYPE_ID> PROPERTY_MANAGER::GetMatchingClasses( PROPERTY_BASE* aProperty )
392{
393 std::vector<TYPE_ID> ids;
394
395/*
396 for( auto& cls : m_classes )
397 {
398 CLASS_INFO info;
399
400 for( auto prop : cls.second.m_allProperties )
401 info.properties.push_back(prop);
402
403
404 }
405 */
406
407 return ids;
408}
409
410
412{
413 CLASSES_INFO rv;
414
415 for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
416 {
418
419 info.type = classEntry.first;
420 info.name = m_classNames[classEntry.first];
421
422 for( PROPERTY_BASE* prop : classEntry.second.m_allProperties )
423 info.properties.push_back( prop );
424
425 rv.push_back( info );
426 }
427
428 return rv;
429}
const char * name
Definition: DXF_plotter.cpp:57
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
PROPERTY_BASE & SetGroup(const wxString &aGroup)
Definition: property.h:313
virtual size_t BaseHash() const =0
Return type-id of the Base class.
virtual bool Writeable(INSPECTABLE *aObject) const
Definition: property.h:255
bool Available(INSPECTABLE *aObject) const
Return true if aObject offers this PROPERTY.
Definition: property.h:241
const wxString & Name() const
Definition: property.h:209
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.
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:326
CLASSES_INFO GetAllClasses()
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:246
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.
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:323
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 ...
std::vector< TYPE_ID > GetMatchingClasses(PROPERTY_BASE *aProperty)
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 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:320
const std::vector< wxString > & GetGroupDisplayOrder(TYPE_ID aType) const
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
virtual size_t BaseHash() const =0
virtual size_t DerivedHash() const =0
static bool empty(const wxTextEntryBase *aCtrl)
static wxString EMPTY_STRING(wxEmptyString)
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:48
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:260
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:272
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:305
PROPERTY_SET m_maskedBaseProperties
Overrides for base class property availabilities.
Definition: property_mgr.h:281
std::vector< wxString > m_groupDisplayOrder
Non-owning list of classes's direct properties in display order.
Definition: property_mgr.h:296
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:275
std::vector< PROPERTY_BASE * > m_ownDisplayOrder
The property groups provided by this class.
Definition: property_mgr.h:299
std::vector< PROPERTY_BASE * > m_allProperties
Compiled display order for all properties.
Definition: property_mgr.h:290
PROPERTY_DISPLAY_ORDER m_displayOrder
List of property groups provided by this class in display order.
Definition: property_mgr.h:293
PROPERTY_FUNCTOR_MAP m_writeabilityOverrides
All properties (both unique to the type and inherited)
Definition: property_mgr.h:287
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:278
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:284
std::set< wxString > m_groups
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:302