#include <OBTRTTI.h>
Public Types | |
typedef void(* | RegisterAttributesFunc )() |
Public Member Functions | |
RTTI (TypeID typeID, const char *name, RegisterAttributesFunc registerFunc, unsigned int ancestorsCount,...) | |
virtual | ~RTTI () |
bool | isDerivedFrom (const RTTI &rtti) const |
const RTTI & | getAncestorRTTI (unsigned int ancestor) const |
unsigned int | getAncestorsCount () const |
TypeID | getTypeID () const |
const char * | getTypeName () const |
unsigned int | getAttributesCount () const |
const AbstractAttribute *const | getAttribute (unsigned int attributeNumber) const |
template<class TAttribute> | |
const TAttribute *const | getAttribute (unsigned int attributeNumber) const |
const AbstractAttribute *const | getAttribute (const char *name) const |
template<class TAttribute> | |
const TAttribute *const | getAttribute (const char *name) const |
template<class TAttribute> | |
void | registerAttribute (const TAttribute &attrib) const |
Private Member Functions | |
RTTI (const RTTI &rtti) | |
const RTTI & | operator= (const RTTI &rtti) |
Private Attributes | |
const TypeID | _typeID |
class ID | |
const std::string | _typeName |
class name | |
std::vector< const RTTI * > | _baseClassesRTTI |
base classes RTTI | |
std::vector< const AbstractAttribute * > | _attributes |
pointers to the inspectable attributes |
Classes which support RTTI declare an instance of this class as a static member. RTTI enables to identify the type ID of a class, its base classes, as well as its inspectable attributes. Each such class must declare the following methods :
Definition at line 26 of file OBTRTTI.h.
typedef void( * OBT::RTTI::RegisterAttributesFunc)() |
RTTI::RTTI | ( | TypeID | typeID, | |
const char * | name, | |||
RegisterAttributesFunc | registerFunc, | |||
unsigned int | ancestorsCount, | |||
... | ||||
) |
constructor
typeID | A unique class ID | |
name | class name, usually of the form typeID( MyClassName ).name() | |
registerFunc | pointer to a static member method of the owner class. This method is called on RTTI construction to register the inspectable attributes. | |
ancestorsCount | number of base classes. | |
... | variable arguments list providing the pointers to the RTTIs of the base classes. |
OBT_Exception | [DEBUG only] if :
|
Definition at line 11 of file OBTRTTI.cpp.
References _baseClassesRTTI, and OBT_DBG_ASSERT.
00012 : 00013 _typeID( typeID ), 00014 _typeName( name ) 00015 { 00016 OBT_DBG_ASSERT( typeID != 0 ) ; 00017 OBT_DBG_ASSERT( name != 0 ) ; 00018 00019 // register the inspectable attributes. 00020 if ( registerFunc != NULL ) 00021 { 00022 registerFunc() ; 00023 } 00024 00025 // set the ancestors for this RTTI. 00026 if ( ancestorsCount != 0 ) 00027 { 00028 va_list marker; 00029 // initialize variable arguments. 00030 va_start( marker, ancestorsCount ); 00031 00032 for ( unsigned int ancestor = 0; ancestor < ancestorsCount; ++ancestor ) 00033 { 00034 RTTI* tmp( va_arg( marker, RTTI* ) ) ; 00035 _baseClassesRTTI.push_back( tmp ) ; 00036 } 00037 va_end( marker ) ; 00038 } 00039 }
RTTI::~RTTI | ( | ) | [virtual] |
destructor
Definition at line 44 of file OBTRTTI.cpp.
References _attributes.
00045 { 00046 std::vector< const AbstractAttribute* >::iterator ite( _attributes.begin() ) ; 00047 for ( ; ite != _attributes.end() ; ++ite ) 00048 { 00049 delete *ite ; 00050 } 00051 }
OBT::RTTI::RTTI | ( | const RTTI & | rtti | ) | [private] |
copy constructor
bool RTTI::isDerivedFrom | ( | const RTTI & | rtti | ) | const |
check the type of the ancestor RTTI.
typeID | ancestor type ID to test. |
Definition at line 57 of file OBTRTTI.cpp.
References _baseClassesRTTI, _typeID, and getTypeID().
00058 { 00059 if( _typeID == rtti.getTypeID() ) 00060 { 00061 return true ; 00062 } 00063 00064 // perform a depth-first search for an ancestor with typeID. 00065 std::vector< const RTTI* >::const_iterator ite( _baseClassesRTTI.begin() ) ; 00066 for ( ; ite != _baseClassesRTTI.end() ; ++ite ) 00067 { 00068 if ( ( *ite )->isDerivedFrom( rtti ) ) 00069 { 00070 return true ; 00071 } 00072 } 00073 return false; 00074 }
const RTTI & OBT::RTTI::getAncestorRTTI | ( | unsigned int | ancestor | ) | const [inline] |
get the RTTI of a base class.
std::out_of_range | if ancestor is out of bounds |
Definition at line 183 of file OBTRTTI.h.
References _baseClassesRTTI.
00184 { 00185 return *_baseClassesRTTI.at( ancestor ) ; 00186 }
unsigned int OBT::RTTI::getAncestorsCount | ( | ) | const [inline] |
get the number of ancestors
Definition at line 192 of file OBTRTTI.h.
References _baseClassesRTTI.
00193 { 00194 return static_cast<unsigned int>( _baseClassesRTTI.size() ); 00195 }
TypeID OBT::RTTI::getTypeID | ( | ) | const [inline] |
get the type ID
Definition at line 201 of file OBTRTTI.h.
References _typeID.
Referenced by isDerivedFrom().
00202 { 00203 return _typeID ; 00204 }
const char * OBT::RTTI::getTypeName | ( | ) | const [inline] |
unsigned int OBT::RTTI::getAttributesCount | ( | ) | const [inline] |
get the number of inspectable attributes.
Definition at line 219 of file OBTRTTI.h.
References _attributes, and _baseClassesRTTI.
00220 { 00221 unsigned int attributesCount( static_cast<unsigned int>( _attributes.size() ) ) ; 00222 std::vector< const RTTI* >::const_iterator ite( _baseClassesRTTI.begin() ) ; 00223 for ( ; ite != _baseClassesRTTI.end() ; ++ite ) 00224 { 00225 attributesCount += ( *ite )->getAttributesCount( ) ; 00226 } 00227 return attributesCount ; 00228 }
const TAttribute *const OBT::RTTI::getAttribute | ( | unsigned int | attributeNumber | ) | const [inline] |
get an inspectable attribute by its index.
By convention, a class inherits from its base class attributes, and attribute indices can be seen as sorted in increasing order from base classes to derived classes. Note that it is an error to specify an index superior to the number of attributes of a given class. If you do not know this number, call getAttributesCount to get it.
attributeNumber | index of the inspectable attribute. |
Definition at line 80 of file OBTRTTI.cpp.
References _attributes, and _baseClassesRTTI.
00081 { 00082 unsigned int attributesCount( 0 ); 00083 std::vector< const RTTI* >::const_iterator ite( _baseClassesRTTI.begin() ) ; 00084 // get the ancestor actually owning the attribute (if it exists) 00085 for ( ; ite != _baseClassesRTTI.end() ; ++ite ) 00086 { 00087 attributesCount = ( *ite )->getAttributesCount() ; 00088 if ( attributeNumber < attributesCount ) 00089 { 00090 // found the right ancestor : get the attribute from it. 00091 return ( *ite )->getAttribute( attributeNumber ) ; 00092 } 00093 else 00094 { 00095 // proceed to the next ancestor 00096 attributeNumber -= attributesCount ; 00097 } 00098 } 00099 // the attribute actually belongs to this class, return it. 00100 return _attributes.at( attributeNumber ) ; 00101 }
const TAttribute* const OBT::RTTI::getAttribute | ( | unsigned int | attributeNumber | ) | const [inline] |
get an inspectable attribute by its index.
If you already know the actual type of the inspectable attribute, you can pass it as a template parameter to this method, which will make the cast for you. Note that passing the wrong type invalidates an assertion, which causes execution abortion in debug mode.
attributeNumber | index of the inspectable attribute. |
OBT_Exception | [DEBUG only] if :
|
const TAttribute *const OBT::RTTI::getAttribute | ( | const char * | name | ) | const [inline] |
get an inspectable attribute by its name.
This method performs a breadth-first search for the attribute with the specified name, starting from the current RTTI, up through its ancestors.
name | name of the inspectable attribute. |
Definition at line 107 of file OBTRTTI.cpp.
References _attributes, and _baseClassesRTTI.
00108 { 00109 std::string str( name ) ; 00110 std::vector< const AbstractAttribute* >::const_iterator ite( _attributes.begin() ) ; 00111 // look for the queried attribute in this instance. 00112 for ( ; ite != _attributes.end() ; ++ite ) 00113 { 00114 if ( ( *ite )->getName() == str ) 00115 { 00116 return *ite ; 00117 } 00118 } 00119 00120 // attribute was not found : search through the ancestors tree. 00121 std::vector< const RTTI* >::const_iterator ancestorIte( _baseClassesRTTI.begin() ) ; 00122 for ( ; ancestorIte != _baseClassesRTTI.end() ; ++ancestorIte ) 00123 { 00124 const AbstractAttribute* const attribute( ( *ancestorIte )->getAttribute( name ) ) ; 00125 if ( attribute != NULL ) 00126 { 00127 return attribute ; 00128 } 00129 } 00130 return NULL ; 00131 }
const TAttribute* const OBT::RTTI::getAttribute | ( | const char * | name | ) | const [inline] |
get an inspectable attribute by its name. If you already know the actual type of the inspectable attribute, you can pass it as a template parameter to this method, which will make the cast for you. Note that passing the wrong type invalidates an assertion, which causes execution abortion in debug mode.
name | name of the inspectable attribute. |
OBT_Exception | [DEBUG only] if TAttribute is neither AbstractAttribute nor the actual attribute type. |
void OBT::RTTI::registerAttribute | ( | const TAttribute & | attrib | ) | const [inline] |
Add an inspectable attribute to this RTTI.
attrib | the attribute to add. |
Definition at line 275 of file OBTRTTI.h.
References _attributes.
00276 { 00277 _attributes.push_back( new TAttribute( attribute ) ) ; 00278 }
const TypeID OBT::RTTI::_typeID [private] |
const std::string OBT::RTTI::_typeName [private] |
std::vector< const RTTI* > OBT::RTTI::_baseClassesRTTI [private] |
base classes RTTI
Definition at line 163 of file OBTRTTI.h.
Referenced by getAncestorRTTI(), getAncestorsCount(), getAttribute(), getAttributesCount(), isDerivedFrom(), and RTTI().
std::vector< const AbstractAttribute* > OBT::RTTI::_attributes [mutable, private] |
pointers to the inspectable attributes
Definition at line 166 of file OBTRTTI.h.
Referenced by getAttribute(), getAttributesCount(), registerAttribute(), and ~RTTI().