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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
22 #ifndef PROPERTY_MGR_H
23 #define PROPERTY_MGR_H
24 
25 #include <wx/string.h>
26 
27 #include <map>
28 #include <unordered_map>
29 #include <set>
30 #include <vector>
31 #include <memory>
32 #include <eda_units.h>
33 
34 
35 #include <common.h> // Needed for stl hash extensions
36 
37 class PROPERTY_BASE;
38 class TYPE_CAST_BASE;
39 
41 using TYPE_ID = size_t;
42 
43 using PROPERTY_LIST = std::vector<PROPERTY_BASE*>;
44 
45 using PROPERTY_SET = std::set<std::pair<size_t, wxString>>;
46 
62 {
63 public:
65  {
66  static PROPERTY_MANAGER pm;
67  return pm;
68  }
69 
78  void RegisterType( TYPE_ID aType, const wxString& aName );
79 
86  const wxString& ResolveType( TYPE_ID aType ) const;
87 
95  PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
96 
103  const PROPERTY_LIST& GetProperties( TYPE_ID aType ) const;
104 
116  const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
117 
118  void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
119  {
120  return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
121  }
122 
128  void AddProperty( PROPERTY_BASE* aProperty );
129 
140  void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew );
141 
147  void AddTypeCast( TYPE_CAST_BASE* aCast );
148 
155  void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
156 
160  bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
161 
163  {
164  return m_units;
165  }
166 
167  void SetUnits( EDA_UNITS aUnits )
168  {
169  m_units = aUnits;
170  }
171 
176  void Rebuild();
177 
178  struct CLASS_INFO
179  {
180  wxString name;
182  std::vector<PROPERTY_BASE*> properties;
183  };
184 
185  typedef std::vector<CLASS_INFO> CLASSES_INFO;
186 
188 
189  std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
190 
191 private:
193  : m_dirty( false ), m_units( EDA_UNITS::MILLIMETRES )
194  {
195  }
196 
198  struct CLASS_DESC
199  {
201  : m_id( aId )
202  {
203  }
204 
206  const TYPE_ID m_id;
207 
209  std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
210 
212  std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
213 
215  std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
216 
218  std::vector<PROPERTY_BASE*> m_allProperties;
219 
222 
224  void rebuild();
225 
228  void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced ) const;
229  };
230 
232  CLASS_DESC& getClass( TYPE_ID aTypeId );
233 
234  std::unordered_map<TYPE_ID, wxString> m_classNames;
235 
237  std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
238 
240  bool m_dirty;
241 
243 };
244 
245 
247 #define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
248 
249 #endif /* PROPERTY_MGR_H */
void Rebuild()
Rebuilds the list of all registered properties.
const PROPERTY_LIST & GetProperties(TYPE_ID aType) const
Returns all properties for a specific type.
std::vector< TYPE_ID > GetMatchingClasses(PROPERTY_BASE *aProperty)
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:64
PROPERTY_SET m_replaced
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:221
void SetUnits(EDA_UNITS aUnits)
Definition: property_mgr.h:167
void * TypeCast(void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Definition: property_mgr.h:118
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:182
size_t TYPE_ID
Unique type identifier
Definition: property_mgr.h:41
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Casts a type to another type.
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:240
const TYPE_ID m_id
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:206
std::map< TYPE_ID, std::unique_ptr< TYPE_CAST_BASE > > m_typeCasts
Type converters available for this type
Definition: property_mgr.h:215
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Returns a property for a specific type.
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Map of all available types
Definition: property_mgr.h:237
void collectPropsRecur(PROPERTY_LIST &aResult, PROPERTY_SET &aReplaced) const
Traverses the class inheritance hierarchy bottom-to-top, gathering all properties available to a type
void rebuild()
Recreates the list of properties
Structure holding type meta-data
Definition: property_mgr.h:198
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declares an inheritance relationship between types.
const wxString & ResolveType(TYPE_ID aType) const
Returns name of a type.
CLASS_DESC & getClass(TYPE_ID aTypeId)
Returns metadata for a specific type
EDA_UNITS
Definition: eda_units.h:38
void RegisterType(TYPE_ID aType, const wxString &aName)
Associates a name with a type.
std::unordered_map< TYPE_ID, wxString > m_classNames
Definition: property_mgr.h:234
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Returns true if aDerived is inherited from aBase.
EDA_UNITS m_units
Definition: property_mgr.h:242
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:185
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Types after which this type inherits
Definition: property_mgr.h:209
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:43
void AddTypeCast(TYPE_CAST_BASE *aCast)
Registers a type converter.
void AddProperty(PROPERTY_BASE *aProperty)
Registers a property.
The common library.
void ReplaceProperty(size_t aBase, const wxString &aName, PROPERTY_BASE *aNew)
Replaces an existing property for a specific type.
CLASSES_INFO GetAllClasses()
Provides class metadata.
Definition: property_mgr.h:61
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:45
EDA_UNITS GetUnits() const
Definition: property_mgr.h:162
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:212
std::vector< PROPERTY_BASE * > m_allProperties
All properties (both unique to the type and inherited)
Definition: property_mgr.h:218