KiCad PCB EDA Suite
PROPERTY_MANAGER Class Reference

Provide class metadata.Helper macro to map type hashes to names. More...

#include <property_mgr.h>

Classes

struct  CLASS_DESC
 Returns metadata for a specific type. More...
 
struct  CLASS_INFO
 

Public Types

typedef std::vector< CLASS_INFOCLASSES_INFO
 

Public Member Functions

void RegisterType (TYPE_ID aType, const wxString &aName)
 Associate a name with a type. More...
 
const wxString & ResolveType (TYPE_ID aType) const
 Return name of a type. More...
 
PROPERTY_BASEGetProperty (TYPE_ID aType, const wxString &aProperty) const
 Return a property for a specific type. More...
 
const PROPERTY_LISTGetProperties (TYPE_ID aType) const
 Return all properties for a specific type. More...
 
const void * TypeCast (const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
 Cast a type to another type. More...
 
void * TypeCast (void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
 
void AddProperty (PROPERTY_BASE *aProperty)
 Register a property. More...
 
void ReplaceProperty (size_t aBase, const wxString &aName, PROPERTY_BASE *aNew)
 Replace an existing property for a specific type. More...
 
void AddTypeCast (TYPE_CAST_BASE *aCast)
 Register a type converter. More...
 
void InheritsAfter (TYPE_ID aDerived, TYPE_ID aBase)
 Declare an inheritance relationship between types. More...
 
bool IsOfType (TYPE_ID aDerived, TYPE_ID aBase) const
 Return true if aDerived is inherited from aBase. More...
 
EDA_UNITS GetUnits () const
 
void SetUnits (EDA_UNITS aUnits)
 
void Rebuild ()
 Rebuild the list of all registered properties. More...
 
CLASSES_INFO GetAllClasses ()
 
std::vector< TYPE_IDGetMatchingClasses (PROPERTY_BASE *aProperty)
 

Static Public Member Functions

static PROPERTY_MANAGERInstance ()
 

Private Member Functions

 PROPERTY_MANAGER ()
 Structure holding type meta-data. More...
 
CLASS_DESCgetClass (TYPE_ID aTypeId)
 

Private Attributes

std::unordered_map< TYPE_ID, wxString > m_classNames
 Map of all available types. More...
 
std::unordered_map< TYPE_ID, CLASS_DESCm_classes
 
bool m_dirty
 Flag indicating that the list of properties needs to be rebuild (RebuildProperties()) More...
 
EDA_UNITS m_units
 

Detailed Description

Provide class metadata.Helper macro to map type hashes to names.

Each class handled by PROPERTY_MANAGER needs to be described using AddProperty(), AddTypeCast() and InheritsAfter() methods.

Enum types use a dedicated property type (PROPERTY_ENUM), define its possible values with ENUM_MAP class, then describe the type using macros:

  • DECLARE_ENUM_TO_WXANY (in header files)
  • IMPLEMENT_ENUM_TO_WXANY (in source files)
  • ENUM_TO_WXANY (most often used; combines DECLARE and IMPLEMENT macros, if there is no need to share the description using header files)

Once all classes are described, the property list must be build using Rebuild() method.

Definition at line 63 of file property_mgr.h.

Member Typedef Documentation

◆ CLASSES_INFO

Definition at line 187 of file property_mgr.h.

Constructor & Destructor Documentation

◆ PROPERTY_MANAGER()

PROPERTY_MANAGER::PROPERTY_MANAGER ( )
inlineprivate

Structure holding type meta-data.

Definition at line 194 of file property_mgr.h.

Member Function Documentation

◆ AddProperty()

void PROPERTY_MANAGER::AddProperty ( PROPERTY_BASE aProperty)

Register a property.

Parameters
aPropertyis the property to register.

Definition at line 102 of file property_mgr.cpp.

103 {
104  const wxString& name = aProperty->Name();
105  TYPE_ID hash = aProperty->OwnerHash();
106  CLASS_DESC& classDesc = getClass( hash );
107  classDesc.m_ownProperties.emplace( name, aProperty );
108  m_dirty = true;
109 }
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:43
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:242
const wxString & Name() const
Definition: property.h:187
CLASS_DESC & getClass(TYPE_ID aTypeId)
const char * name
Definition: DXF_plotter.cpp:59
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.

References getClass(), m_dirty, PROPERTY_MANAGER::CLASS_DESC::m_ownProperties, name, PROPERTY_BASE::Name(), and PROPERTY_BASE::OwnerHash().

Referenced by BOARD_CONNECTED_ITEM_DESC::BOARD_CONNECTED_ITEM_DESC(), BOARD_ITEM_DESC::BOARD_ITEM_DESC(), CLASS_A_DESC::CLASS_A_DESC(), CLASS_B_DESC::CLASS_B_DESC(), CLASS_C_DESC::CLASS_C_DESC(), CLASS_D_DESC::CLASS_D_DESC(), DRAWSEGMENT_DESC::DRAWSEGMENT_DESC(), EDA_ITEM_DESC::EDA_ITEM_DESC(), EDA_TEXT_DESC::EDA_TEXT_DESC(), FOOTPRINT_DESC::FOOTPRINT_DESC(), FP_SHAPE_DESC::FP_SHAPE_DESC(), FP_TEXT_DESC::FP_TEXT_DESC(), PAD_DESC::PAD_DESC(), PCB_TARGET_DESC::PCB_TARGET_DESC(), ReplaceProperty(), TRACK_VIA_DESC::TRACK_VIA_DESC(), and ZONE_DESC::ZONE_DESC().

◆ AddTypeCast()

void PROPERTY_MANAGER::AddTypeCast ( TYPE_CAST_BASE aCast)

Register a type converter.

Required prior TypeCast() usage.

Parameters
aCastis the type converter to register.

Definition at line 121 of file property_mgr.cpp.

122 {
123  TYPE_ID derivedHash = aCast->DerivedHash();
124  CLASS_DESC& classDesc = getClass( aCast->BaseHash() );
125  auto& typeCasts = classDesc.m_typeCasts;
126  wxASSERT_MSG( typeCasts.count( derivedHash ) == 0, "Such converter already exists" );
127  typeCasts.emplace( derivedHash, aCast );
128 }
virtual size_t BaseHash() const =0
size_t TYPE_ID
Unique type identifier.
Definition: property_mgr.h:43
virtual size_t DerivedHash() const =0
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
CLASS_DESC & getClass(TYPE_ID aTypeId)

References TYPE_CAST_BASE::BaseHash(), TYPE_CAST_BASE::DerivedHash(), getClass(), and PROPERTY_MANAGER::CLASS_DESC::m_typeCasts.

Referenced by CLASS_D_DESC::CLASS_D_DESC(), FOOTPRINT_DESC::FOOTPRINT_DESC(), FP_TEXT_DESC::FP_TEXT_DESC(), and TEXTE_PCB_DESC::TEXTE_PCB_DESC().

◆ GetAllClasses()

PROPERTY_MANAGER::CLASSES_INFO PROPERTY_MANAGER::GetAllClasses ( )

Definition at line 234 of file property_mgr.cpp.

235 {
236  CLASSES_INFO rv;
237 
238  for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
239  {
240  CLASS_INFO info;
241 
242  info.type = classEntry.first;
243  info.name = m_classNames[classEntry.first];
244 
245  for( PROPERTY_BASE* prop : classEntry.second.m_allProperties )
246  info.properties.push_back( prop );
247 
248  rv.push_back( info );
249  }
250 
251  return rv;
252 }
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:236
std::vector< CLASS_INFO > CLASSES_INFO
Definition: property_mgr.h:187

References m_classes, m_classNames, PROPERTY_MANAGER::CLASS_INFO::name, PROPERTY_MANAGER::CLASS_INFO::properties, and PROPERTY_MANAGER::CLASS_INFO::type.

Referenced by PCB_EXPR_UCODE::CreateVarRef(), and PANEL_SETUP_RULES::onScintillaCharAdded().

◆ getClass()

PROPERTY_MANAGER::CLASS_DESC & PROPERTY_MANAGER::getClass ( TYPE_ID  aTypeId)
private

Definition at line 173 of file property_mgr.cpp.

174 {
175  auto it = m_classes.find( aTypeId );
176 
177  if( it == m_classes.end() )
178  tie( it, std::ignore ) = m_classes.emplace( aTypeId, CLASS_DESC( aTypeId ) );
179 
180  return it->second;
181 }
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239

References m_classes.

Referenced by AddProperty(), AddTypeCast(), InheritsAfter(), and ReplaceProperty().

◆ GetMatchingClasses()

std::vector< TYPE_ID > PROPERTY_MANAGER::GetMatchingClasses ( PROPERTY_BASE aProperty)

Definition at line 214 of file property_mgr.cpp.

215 {
216  std::vector<TYPE_ID> ids;
217 
218 /*
219  for( auto& cls : m_classes )
220  {
221  CLASS_INFO info;
222 
223  for( auto prop : cls.second.m_allProperties )
224  info.properties.push_back(prop);
225 
226 
227  }
228  */
229 
230  return ids;
231 }

◆ GetProperties()

const PROPERTY_LIST & PROPERTY_MANAGER::GetProperties ( TYPE_ID  aType) const

Return all properties for a specific type.

Parameters
aTypeis the type identifier (obtained using TYPE_HASH()).
Returns
Vector storing all properties of the requested type.

Definition at line 67 of file property_mgr.cpp.

68 {
69  if( m_dirty )
70  const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
71 
72  static const PROPERTY_LIST empty;
73  auto it = m_classes.find( aType );
74 
75  if( it == m_classes.end() )
76  return empty;
77 
78  return it->second.m_allProperties;
79 }
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:242
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239
std::vector< PROPERTY_BASE * > PROPERTY_LIST
Definition: property_mgr.h:45
static bool empty(const wxTextEntryBase *aCtrl)

References empty(), m_classes, and m_dirty.

Referenced by PANEL_SETUP_RULES::onScintillaCharAdded().

◆ GetProperty()

PROPERTY_BASE * PROPERTY_MANAGER::GetProperty ( TYPE_ID  aType,
const wxString &  aProperty 
) const

Return a property for a specific type.

Parameters
aTypeis the type identifier (obtained using TYPE_HASH()).
aPropertyis the property name used during class registration.
Returns
Requested property or null pointer if requested property does not exist.

Definition at line 45 of file property_mgr.cpp.

46 {
47  if( m_dirty )
48  const_cast<PROPERTY_MANAGER*>( this )->Rebuild();
49 
50  auto it = m_classes.find( aType );
51 
52  if( it == m_classes.end() )
53  return nullptr;
54 
55  const CLASS_DESC& classDesc = it->second;
56 
57  for( PROPERTY_BASE* property : classDesc.m_allProperties )
58  {
59  if( !aProperty.CmpNoCase( property->Name() ) )
60  return property;
61  }
62 
63  return nullptr;
64 }
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:242
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239
const wxString & Name() const
Definition: property.h:187

References PROPERTY_MANAGER::CLASS_DESC::m_allProperties, m_classes, m_dirty, and PROPERTY_BASE::Name().

Referenced by PCB_EXPR_UCODE::CreateVarRef(), INSPECTABLE::Get(), and INSPECTABLE::Set().

◆ GetUnits()

EDA_UNITS PROPERTY_MANAGER::GetUnits ( ) const
inline

Definition at line 164 of file property_mgr.h.

165  {
166  return m_units;
167  }
EDA_UNITS m_units
Definition: property_mgr.h:244

References m_units.

◆ InheritsAfter()

void PROPERTY_MANAGER::InheritsAfter ( TYPE_ID  aDerived,
TYPE_ID  aBase 
)

Declare an inheritance relationship between types.

Parameters
aBaseis the base type identifier (obtained using TYPE_HASH()).
aDerivedis the derived type identifier (obtained using TYPE_HASH()).

Definition at line 131 of file property_mgr.cpp.

132 {
133  wxASSERT_MSG( aDerived != aBase, "Class cannot inherit from itself" );
134 
135  CLASS_DESC& derived = getClass( aDerived );
136  CLASS_DESC& base = getClass( aBase );
137  derived.m_bases.push_back( base );
138  m_dirty = true;
139 
140  wxASSERT_MSG( derived.m_bases.size() == 1 || derived.m_typeCasts.count( aBase ) == 1,
141  "You need to add a TYPE_CAST for classes inheriting from multiple bases" );
142 }
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:242
CLASS_DESC & getClass(TYPE_ID aTypeId)

References getClass(), PROPERTY_MANAGER::CLASS_DESC::m_bases, m_dirty, and PROPERTY_MANAGER::CLASS_DESC::m_typeCasts.

Referenced by BOARD_CONNECTED_ITEM_DESC::BOARD_CONNECTED_ITEM_DESC(), BOARD_ITEM_DESC::BOARD_ITEM_DESC(), CLASS_B_DESC::CLASS_B_DESC(), CLASS_D_DESC::CLASS_D_DESC(), DIMENSION_DESC::DIMENSION_DESC(), DRAWSEGMENT_DESC::DRAWSEGMENT_DESC(), FOOTPRINT_DESC::FOOTPRINT_DESC(), FP_SHAPE_DESC::FP_SHAPE_DESC(), FP_TEXT_DESC::FP_TEXT_DESC(), PAD_DESC::PAD_DESC(), PCB_TARGET_DESC::PCB_TARGET_DESC(), TEXTE_PCB_DESC::TEXTE_PCB_DESC(), TRACK_VIA_DESC::TRACK_VIA_DESC(), and ZONE_DESC::ZONE_DESC().

◆ Instance()

◆ IsOfType()

bool PROPERTY_MANAGER::IsOfType ( TYPE_ID  aDerived,
TYPE_ID  aBase 
) const

Return true if aDerived is inherited from aBase.

Definition at line 145 of file property_mgr.cpp.

146 {
147  if( aDerived == aBase )
148  return true;
149 
150  auto derived = m_classes.find( aDerived );
151  wxCHECK( derived != m_classes.end(), false ); // missing class description
152 
153  // traverse the hierarchy seeking for the base class
154  for( auto& base : derived->second.m_bases )
155  {
156  if( IsOfType( base.get().m_id, aBase ) )
157  return true;
158  }
159 
160  return false;
161 }
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.

References m_classes.

Referenced by PCB_EXPR_UCODE::CreateVarRef(), and TypeCast().

◆ Rebuild()

void PROPERTY_MANAGER::Rebuild ( )

Rebuild the list of all registered properties.

Needs to be called once before GetProperty()/GetProperties() are used.

Definition at line 164 of file property_mgr.cpp.

165 {
166  for( std::pair<const TYPE_ID, CLASS_DESC>& classEntry : m_classes )
167  classEntry.second.rebuild();
168 
169  m_dirty = false;
170 }
bool m_dirty
Flag indicating that the list of properties needs to be rebuild (RebuildProperties())
Definition: property_mgr.h:242
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239

References m_classes, and m_dirty.

Referenced by BOOST_AUTO_TEST_CASE(), and main().

◆ RegisterType()

void PROPERTY_MANAGER::RegisterType ( TYPE_ID  aType,
const wxString &  aName 
)

Associate a name with a type.

Build a map to provide faster type look-up.

Parameters
aTypeis the type identifier (obtained using TYPE_HASH()).
aNameis the type name.

Definition at line 31 of file property_mgr.cpp.

32 {
33  wxASSERT( m_classNames.count( aType ) == 0 );
34  m_classNames.emplace( aType, aName );
35 }
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:236

References m_classNames.

◆ ReplaceProperty()

void PROPERTY_MANAGER::ReplaceProperty ( size_t  aBase,
const wxString &  aName,
PROPERTY_BASE aNew 
)

Replace an existing property for a specific type.

It is used to modify a property that has been inherited from a base class. This method is used instead of AddProperty().

Parameters
aBaseis the base class type the delivers the original property.
aNameis the name of the replaced property.
aNewis the property replacing the inherited one.

Definition at line 112 of file property_mgr.cpp.

113 {
114  wxASSERT( aBase == aNew->BaseHash() );
115  CLASS_DESC& classDesc = getClass( aNew->OwnerHash() );
116  classDesc.m_replaced.insert( std::make_pair( aBase, aName ) );
117  AddProperty( aNew );
118 }
PROPERTY_SET m_replaced
Recreates the list of properties.
Definition: property_mgr.h:223
CLASS_DESC & getClass(TYPE_ID aTypeId)
void AddProperty(PROPERTY_BASE *aProperty)
Register a property.
virtual size_t BaseHash() const =0
Return type-id of the Base class.
virtual size_t OwnerHash() const =0
Return type-id of the Owner class.

References AddProperty(), PROPERTY_BASE::BaseHash(), getClass(), PROPERTY_MANAGER::CLASS_DESC::m_replaced, and PROPERTY_BASE::OwnerHash().

Referenced by BOARD_CONNECTED_ITEM_DESC::BOARD_CONNECTED_ITEM_DESC(), CLASS_D_DESC::CLASS_D_DESC(), CLASS_E_DESC::CLASS_E_DESC(), FOOTPRINT_DESC::FOOTPRINT_DESC(), and TRACK_VIA_DESC::TRACK_VIA_DESC().

◆ ResolveType()

const wxString & PROPERTY_MANAGER::ResolveType ( TYPE_ID  aType) const

Return name of a type.

Parameters
aTypeis the type identifier (obtained using TYPE_HASH()).
Returns
Name of the type or empty string, if not available.

Definition at line 38 of file property_mgr.cpp.

39 {
40  auto it = m_classNames.find( aType );
41  return it != m_classNames.end() ? it->second : EMPTY_STRING;
42 }
static wxString EMPTY_STRING(wxEmptyString)
std::unordered_map< TYPE_ID, wxString > m_classNames
Map of all available types.
Definition: property_mgr.h:236

References EMPTY_STRING(), and m_classNames.

◆ SetUnits()

void PROPERTY_MANAGER::SetUnits ( EDA_UNITS  aUnits)
inline

Definition at line 169 of file property_mgr.h.

170  {
171  m_units = aUnits;
172  }
EDA_UNITS m_units
Definition: property_mgr.h:244

References m_units.

◆ TypeCast() [1/2]

const void * PROPERTY_MANAGER::TypeCast ( const void *  aSource,
TYPE_ID  aBase,
TYPE_ID  aTarget 
) const

Cast a type to another type.

Used for correct type-casting of types with multi-inheritance. Requires registration of an appropriate converter (AddTypeCast).

Parameters
aSourceis a pointer to the casted object.
aBaseis aSource type identifier (obtained using TYPE_HASH()).
aTargetis the desired type identifier (obtained using TYPE_HASH()).
Returns
Properly casted pointer of aTarget type. *
See also
AddTypeCast

Definition at line 82 of file property_mgr.cpp.

83 {
84  if( aBase == aTarget )
85  return aSource;
86 
87  auto classDesc = m_classes.find( aBase );
88 
89  if( classDesc == m_classes.end() )
90  return aSource;
91 
92  auto& converters = classDesc->second.m_typeCasts;
93  auto converter = converters.find( aTarget );
94 
95  if( converter == converters.end() ) // explicit type cast not found
96  return IsOfType( aBase, aTarget ) ? aSource : nullptr;
97 
98  return (*converter->second)( aSource );
99 }
std::unordered_map< TYPE_ID, CLASS_DESC > m_classes
Definition: property_mgr.h:239
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.

References IsOfType(), and m_classes.

Referenced by INSPECTABLE::Get(), INSPECTABLE::Set(), and TypeCast().

◆ TypeCast() [2/2]

void* PROPERTY_MANAGER::TypeCast ( void *  aSource,
TYPE_ID  aBase,
TYPE_ID  aTarget 
) const
inline

Definition at line 120 of file property_mgr.h.

121  {
122  return const_cast<void*>( TypeCast( (const void*) aSource, aBase, aTarget ) );
123  }
const void * TypeCast(const void *aSource, TYPE_ID aBase, TYPE_ID aTarget) const
Cast a type to another type.

References TypeCast().

Member Data Documentation

◆ m_classes

std::unordered_map<TYPE_ID, CLASS_DESC> PROPERTY_MANAGER::m_classes
private

◆ m_classNames

std::unordered_map<TYPE_ID, wxString> PROPERTY_MANAGER::m_classNames
private

Map of all available types.

Definition at line 236 of file property_mgr.h.

Referenced by GetAllClasses(), RegisterType(), and ResolveType().

◆ m_dirty

bool PROPERTY_MANAGER::m_dirty
private

Flag indicating that the list of properties needs to be rebuild (RebuildProperties())

Definition at line 242 of file property_mgr.h.

Referenced by AddProperty(), GetProperties(), GetProperty(), InheritsAfter(), and Rebuild().

◆ m_units

EDA_UNITS PROPERTY_MANAGER::m_units
private

Definition at line 244 of file property_mgr.h.

Referenced by GetUnits(), and SetUnits().


The documentation for this class was generated from the following files: