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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 <wx/string.h>
28 
29 #include <map>
30 #include <unordered_map>
31 #include <set>
32 #include <vector>
33 #include <memory>
34 #include <eda_units.h>
35 
36 
37 #include <common.h> // Needed for stl hash extensions
38 
39 class PROPERTY_BASE;
40 class TYPE_CAST_BASE;
41 
43 using TYPE_ID = size_t;
44 
45 using PROPERTY_LIST = std::vector<PROPERTY_BASE*>;
46 
47 using PROPERTY_SET = std::set<std::pair<size_t, wxString>>;
48 
64 {
65 public:
67  {
68  static PROPERTY_MANAGER pm;
69  return pm;
70  }
71 
80  void RegisterType( TYPE_ID aType, const wxString& aName );
81 
88  const wxString& ResolveType( TYPE_ID aType ) const;
89 
97  PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
98 
105  const PROPERTY_LIST& GetProperties( TYPE_ID aType ) const;
106 
118  const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
119 
120  void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
121  {
122  return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
123  }
124 
130  void AddProperty( PROPERTY_BASE* aProperty );
131 
142  void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew );
143 
149  void AddTypeCast( TYPE_CAST_BASE* aCast );
150 
157  void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
158 
162  bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
163 
165  {
166  return m_units;
167  }
168 
169  void SetUnits( EDA_UNITS aUnits )
170  {
171  m_units = aUnits;
172  }
173 
178  void Rebuild();
179 
180  struct CLASS_INFO
181  {
182  wxString name;
184  std::vector<PROPERTY_BASE*> properties;
185  };
186 
187  typedef std::vector<CLASS_INFO> CLASSES_INFO;
188 
190 
191  std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
192 
193 private:
195  : m_dirty( false ), m_units( EDA_UNITS::MILLIMETRES )
196  {
197  }
198 
200  struct CLASS_DESC
201  {
203  : m_id( aId )
204  {
205  }
206 
208  const TYPE_ID m_id;
209 
211  std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
212 
214  std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
215 
217  std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
218 
220  std::vector<PROPERTY_BASE*> m_allProperties;
221 
224 
226  void rebuild();
227 
230  void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced ) const;
231  };
232 
234  CLASS_DESC& getClass( TYPE_ID aTypeId );
235 
236  std::unordered_map<TYPE_ID, wxString> m_classNames;
237 
239  std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
240 
242  bool m_dirty;
243 
245 };
246 
247 
249 #define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
250 
251 #endif /* PROPERTY_MGR_H */
void Rebuild()
Rebuild the list of all registered properties.
const PROPERTY_LIST & GetProperties(TYPE_ID aType) const
Return all properties for a specific type.
std::vector< TYPE_ID > GetMatchingClasses(PROPERTY_BASE *aProperty)
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:66
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:223
void SetUnits(EDA_UNITS aUnits)
Definition: property_mgr.h:169
void * TypeCast(void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Definition: property_mgr.h:120
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:184
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:43
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Cast a type to another type.
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:242
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:208
std::map< TYPE_ID, std::unique_ptr< TYPE_CAST_BASE > > m_typeCasts
All properties (both unique to the type and inherited)
Definition: property_mgr.h:217
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:239
void collectPropsRecur(PROPERTY_LIST &aResult, PROPERTY_SET &aReplaced) const
void rebuild()
Traverses the class inheritance hierarchy bottom-to-top, gathering all properties available to a type...
Returns metadata for a specific type.
Definition: property_mgr.h:200
void InheritsAfter(TYPE_ID aDerived, TYPE_ID aBase)
Declare an inheritance relationship between types.
const wxString & ResolveType(TYPE_ID aType) const
Return name of a type.
CLASS_DESC & getClass(TYPE_ID aTypeId)
EDA_UNITS
Definition: eda_units.h:38
CLASS_DESC(TYPE_ID aId)
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:202
void RegisterType(TYPE_ID aType, const wxString &aName)
Associate a name with a type.
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:236
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
EDA_UNITS m_units
Definition: property_mgr.h:244
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:187
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:211
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:45
void AddTypeCast(TYPE_CAST_BASE *aCast)
Register a type converter.
void AddProperty(PROPERTY_BASE *aProperty)
Register a property.
PROPERTY_MANAGER()
Structure holding type meta-data.
Definition: property_mgr.h:194
The common library.
void ReplaceProperty(size_t aBase, const wxString &aName, PROPERTY_BASE *aNew)
Replace an existing property for a specific type.
CLASSES_INFO GetAllClasses()
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:63
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:47
EDA_UNITS GetUnits() const
Definition: property_mgr.h:164
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:214
std::vector< PROPERTY_BASE * > m_allProperties
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:220