00001 #ifndef OBTRTTI_H
00002 #define OBTRTTI_H
00003
00004 #include <vector>
00005
00006 #include "OBTAbstractAttribute.h"
00007 #include "OBT_ASSERT.h"
00008
00009 namespace OBT
00010 {
00011 typedef unsigned long TypeID ;
00012
00026 class OBT_API RTTI
00027 {
00028
00029 public:
00030
00031 typedef void ( *RegisterAttributesFunc )() ;
00032
00045 RTTI( TypeID typeID, const char* name, RegisterAttributesFunc registerFunc, unsigned int ancestorsCount, ... ) ;
00046
00050 virtual ~RTTI() ;
00051
00057 bool isDerivedFrom( const RTTI& rtti ) const ;
00058
00064 const RTTI& getAncestorRTTI( unsigned int ancestor ) const ;
00065
00070 unsigned int getAncestorsCount() const;
00071
00076 TypeID getTypeID() const ;
00077
00082 const char* getTypeName() const;
00083
00088 unsigned int getAttributesCount( ) const ;
00089
00103 const AbstractAttribute* const getAttribute( unsigned int attributeNumber ) const ;
00104
00119 template< class TAttribute >
00120 const TAttribute* const getAttribute( unsigned int attributeNumber ) const ;
00121
00131 const AbstractAttribute* const getAttribute( const char* name ) const ;
00132
00144 template< class TAttribute >
00145 const TAttribute* const getAttribute( const char* name ) const ;
00146
00151 template <class TAttribute>
00152 void registerAttribute( const TAttribute& attrib ) const ;
00153
00154 private:
00155
00157 const TypeID _typeID ;
00158
00160 const std::string _typeName ;
00161
00163 std::vector< const RTTI* > _baseClassesRTTI ;
00164
00166 mutable std::vector< const AbstractAttribute* > _attributes ;
00167
00171 RTTI( const RTTI& rtti ) ;
00172
00176 const RTTI& operator=( const RTTI& rtti ) ;
00177 } ;
00178
00179
00180
00181
00182 inline const RTTI&
00183 RTTI::getAncestorRTTI( unsigned int ancestor ) const
00184 {
00185 return *_baseClassesRTTI.at( ancestor ) ;
00186 }
00187
00188
00189
00190
00191 inline unsigned int
00192 RTTI::getAncestorsCount() const
00193 {
00194 return static_cast<unsigned int>( _baseClassesRTTI.size() );
00195 }
00196
00197
00198
00199
00200 inline TypeID
00201 RTTI::getTypeID() const
00202 {
00203 return _typeID ;
00204 }
00205
00206
00207
00208
00209 inline const char*
00210 RTTI::getTypeName() const
00211 {
00212 return _typeName.c_str() ;
00213 }
00214
00215
00216
00217
00218 inline unsigned int
00219 RTTI::getAttributesCount( ) const
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 }
00229
00230
00231
00232
00233 template< class TAttribute >
00234 const TAttribute*
00235 const RTTI::getAttribute( unsigned int attributeNumber ) const
00236 {
00237 const AbstractAttribute* const attribute( getAttribute( attributeNumber ) ) ;
00238 if ( attribute == NULL )
00239 {
00240 return NULL ;
00241 }
00242 else
00243 {
00244 OBT_DBG_ASSERT( ( ( TAttribute::getClassRTTI().getTypeID() == attribute->getRTTI().getTypeID() ) &&
00245 TAttribute::owner_type::getClassRTTI().getTypeID() == attribute->getOwnerRTTI().getTypeID() ) ) ;
00246 return ( static_cast< const TAttribute* const >( attribute ) ) ;
00247 }
00248 }
00249
00250
00251
00252
00253 template< class TAttribute >
00254 const TAttribute*
00255 const RTTI::getAttribute( const char* name ) const
00256 {
00257 const AbstractAttribute* const attribute( getAttribute( name ) ) ;
00258 if ( attribute == NULL )
00259 {
00260 return NULL ;
00261 }
00262 else
00263 {
00264 OBT_DBG_ASSERT( ( ( TAttribute::getClassRTTI().getTypeID() == attribute->getRTTI().getTypeID() ) &&
00265 TAttribute::owner_type::getClassRTTI().getTypeID() == attribute->getOwnerRTTI().getTypeID() ) ) ;
00266 return ( static_cast< const TAttribute* const >( attribute ) ) ;
00267 }
00268 }
00269
00270
00271
00272
00273 template< class TAttribute >
00274 inline void
00275 RTTI::registerAttribute( const TAttribute& attribute ) const
00276 {
00277 _attributes.push_back( new TAttribute( attribute ) ) ;
00278 }
00279 }
00280
00281 #endif //__RTTI_H__