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 <common.h>
33 
34 class PROPERTY_BASE;
35 class TYPE_CAST_BASE;
36 
38 using TYPE_ID = size_t;
39 
40 using PROPERTY_LIST = std::vector<PROPERTY_BASE*>;
41 
42 using PROPERTY_SET = std::set<std::pair<size_t, wxString>>;
43 
59 {
60 public:
62  {
63  static PROPERTY_MANAGER pm;
64  return pm;
65  }
66 
75  void RegisterType( TYPE_ID aType, const wxString& aName );
76 
83  const wxString& ResolveType( TYPE_ID aType ) const;
84 
92  PROPERTY_BASE* GetProperty( TYPE_ID aType, const wxString& aProperty ) const;
93 
100  const PROPERTY_LIST& GetProperties( TYPE_ID aType ) const;
101 
113  const void* TypeCast( const void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const;
114 
115  void* TypeCast( void* aSource, TYPE_ID aBase, TYPE_ID aTarget ) const
116  {
117  return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
118  }
119 
125  void AddProperty( PROPERTY_BASE* aProperty );
126 
137  void ReplaceProperty( size_t aBase, const wxString& aName, PROPERTY_BASE* aNew );
138 
144  void AddTypeCast( TYPE_CAST_BASE* aCast );
145 
152  void InheritsAfter( TYPE_ID aDerived, TYPE_ID aBase );
153 
157  bool IsOfType( TYPE_ID aDerived, TYPE_ID aBase ) const;
158 
160  {
161  return m_units;
162  }
163 
164  void SetUnits( EDA_UNITS aUnits )
165  {
166  m_units = aUnits;
167  }
168 
173  void Rebuild();
174 
175  struct CLASS_INFO
176  {
177  wxString name;
179  std::vector<PROPERTY_BASE*> properties;
180  };
181 
182  typedef std::vector<CLASS_INFO> CLASSES_INFO;
183 
185 
186  std::vector<TYPE_ID> GetMatchingClasses( PROPERTY_BASE* aProperty );
187 
188 private:
190  : m_dirty( false ), m_units( EDA_UNITS::MILLIMETRES )
191  {
192  }
193 
195  struct CLASS_DESC
196  {
198  : m_id( aId )
199  {
200  }
201 
203  const TYPE_ID m_id;
204 
206  std::vector<std::reference_wrapper<CLASS_DESC>> m_bases;
207 
209  std::map<wxString, std::unique_ptr<PROPERTY_BASE>> m_ownProperties;
210 
212  std::map<TYPE_ID, std::unique_ptr<TYPE_CAST_BASE>> m_typeCasts;
213 
215  std::vector<PROPERTY_BASE*> m_allProperties;
216 
219 
221  void rebuild();
222 
225  void collectPropsRecur( PROPERTY_LIST& aResult, PROPERTY_SET& aReplaced ) const;
226  };
227 
229  CLASS_DESC& getClass( TYPE_ID aTypeId );
230 
231  std::unordered_map<TYPE_ID, wxString> m_classNames;
232 
234  std::unordered_map<TYPE_ID, CLASS_DESC> m_classes;
235 
237  bool m_dirty;
238 
240 };
241 
242 
244 #define REGISTER_TYPE(x) PROPERTY_MANAGER::Instance().RegisterType(TYPE_HASH(x), TYPE_NAME(x))
245 
246 #endif /* PROPERTY_MGR_H */
void Rebuild()
Rebuilds the list of all registered properties.
EDA_UNITS
Definition: common.h:198
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:61
PROPERTY_SET m_replaced
Replaced properties (TYPE_ID / name)
Definition: property_mgr.h:218
void SetUnits(EDA_UNITS aUnits)
Definition: property_mgr.h:164
void * TypeCast(void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Definition: property_mgr.h:115
std::vector< PROPERTY_BASE * > properties
Definition: property_mgr.h:179
size_t TYPE_ID
Unique type identifier
Definition: property_mgr.h:38
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:237
const TYPE_ID m_id
Unique type identifier (obtained using TYPE_HASH)
Definition: property_mgr.h:203
std::map< TYPE_ID, std::unique_ptr< TYPE_CAST_BASE > > m_typeCasts
Type converters available for this type
Definition: property_mgr.h:212
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:234
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:195
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
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:231
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:239
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:182
std::vector< std::reference_wrapper< CLASS_DESC > > m_bases
Types after which this type inherits
Definition: property_mgr.h:206
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:40
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:58
std::set< std::pair< size_t, wxString > > PROPERTY_SET
Definition: property_mgr.h:42
EDA_UNITS GetUnits() const
Definition: property_mgr.h:159
std::map< wxString, std::unique_ptr< PROPERTY_BASE > > m_ownProperties
Properties unique to this type (i.e. not inherited)
Definition: property_mgr.h:209
std::vector< PROPERTY_BASE * > m_allProperties
All properties (both unique to the type and inherited)
Definition: property_mgr.h:215