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 <core/wx_stl_compat.h> // Needed for stl hash extensions
28 
29 #include <wx/string.h>
30 
31 #include <map>
32 #include <unordered_map>
33 #include <set>
34 #include <vector>
35 #include <memory>
36 #include <eda_units.h>
37 
38 class PROPERTY_BASE;
39 class TYPE_CAST_BASE;
40 
42 using TYPE_ID = size_t;
43 
44 using PROPERTY_LIST = std::vector<PROPERTY_BASE*>;
45 
46 using PROPERTY_SET = std::set<std::pair<size_t, wxString>>;
47 
63 {
64 public:
66  {
67  static PROPERTY_MANAGER pm;
68  return pm;
69  }
70 
79  void RegisterType( TYPE_ID aType, const wxString& aName );
80 
87  const wxString& ResolveType( TYPE_ID aType ) const;
88 
96  PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
97 
104  const PROPERTY_LIST& GetProperties( TYPE_ID aType ) const;
105 
117  const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
118 
119  void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
120  {
121  return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
122  }
123 
129  void AddProperty( PROPERTY_BASE* aProperty );
130 
141  void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew );
142 
148  void AddTypeCast( TYPE_CAST_BASE* aCast );
149 
156  void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
157 
161  bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
162 
164  {
165  return m_units;
166  }
167 
168  void SetUnits( EDA_UNITS aUnits )
169  {
170  m_units = aUnits;
171  }
172 
177  void Rebuild();
178 
179  struct CLASS_INFO
180  {
181  wxString name;
183  std::vector<PROPERTY_BASE*> properties;
184  };
185 
186  typedef std::vector<CLASS_INFO> CLASSES_INFO;
187 
189 
190  std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
191 
192 private:
194  : m_dirty( false ), m_units( EDA_UNITS::MILLIMETRES )
195  {
196  }
197 
199  struct CLASS_DESC
200  {
202  : m_id( aId )
203  {
204  }
205 
207  const TYPE_ID m_id;
208 
210  std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
211 
213  std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
214 
216  std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
217 
219  std::vector<PROPERTY_BASE*> m_allProperties;
220 
223 
225  void rebuild();
226 
229  void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced ) const;
230  };
231 
233  CLASS_DESC& getClass( TYPE_ID aTypeId );
234 
235  std::unordered_map<TYPE_ID, wxString> m_classNames;
236 
238  std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
239 
241  bool m_dirty;
242 
244 };
245 
246 
248 #define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
249 
250 #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:65
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:222
void SetUnits(EDA_UNITS aUnits)
Definition: property_mgr.h:168
void * TypeCast(void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Definition: property_mgr.h:119
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:183
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:42
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:241
const TYPE_ID m_id
Types after which this type inherits.
Definition: property_mgr.h:207
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:216
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:238
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:199
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:201
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:235
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:243
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:186
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:210
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:44
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:193
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:62
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:46
EDA_UNITS GetUnits() const
Definition: property_mgr.h:163
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Type converters available for this type.
Definition: property_mgr.h:213
std::vector< PROPERTY_BASE * > m_allProperties
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:219