CppUnit project page CppUnit home page

typeinfo.h

Go to the documentation of this file.
00001 #ifndef CPPTL_TYPEINFO_H_INCUDED
00002 # define CPPTL_TYPEINFO_H_INCUDED
00003 
00004 # include <string>
00005 
00006 // /////////////////////////////////////////////////////
00007 // /////////////////////////////////////////////////////
00008 // CTTI using std::type_info & typeid
00009 // /////////////////////////////////////////////////////
00010 // /////////////////////////////////////////////////////
00011 # ifndef CPPTL_NO_RTTI
00012 
00013 #  include <typeinfo>
00014 
00015 namespace CppTL {
00016 
00017 class TypeId
00018 {
00019 public:
00020    TypeId( const std::type_info &typeId )
00021       : type_( &typeId )
00022    {
00023    }
00024 
00025    bool operator ==( const TypeId &other ) const
00026    {
00027       return *type_ == *(other.type_);
00028    }
00029 
00030    bool operator !=( const TypeId &other ) const
00031    {
00032       return !( *this == other );
00033    }
00034 
00035    bool operator <( const TypeId &other ) const
00036    {
00037       // Notes: experience has proven this to be bugged:
00038       // in a staticaly linked programm on AIX, 
00039       // two distinct instances of std::type_info for the same type.
00040       // They compared to false. Can be worked-around by comparing name though.
00041       return type_->before( *(other.type_) );
00042    }
00043 private:
00044    const std::type_info *type_;
00045 };
00046 
00047 
00048 // Compile Time Type Information functions
00049 // //////////////////////////////////////////////////////////////////
00050 
00051 template<class AType>
00052 TypeId typeId( Type<AType> )
00053 {
00054    return typeid( AType );
00055 }
00056 
00057 
00058 } // namespace CppTL
00059 
00060 
00061 #define CPPTL_DECLARE_TYPEINFO( AType )
00062 #define CPPTL_DECLARE_TYPE_AND_PTR_INFO( AType )
00063 
00064 // /////////////////////////////////////////////////////
00065 // /////////////////////////////////////////////////////
00066 // CTTI using hande-made mecanism (template overload)
00067 // /////////////////////////////////////////////////////
00068 // /////////////////////////////////////////////////////
00069 # else
00070 
00071 namespace CppTL {
00072 class TypeId
00073 {
00074 public:
00075    TypeId( const char *type )
00076       : type_( type )
00077    {
00078    }
00079 
00080    bool operator <( const TypeId &other ) const
00081    {
00082       return strcmp( type_, other.type_) < 0;
00083    }
00084 
00085    bool operator ==( const TypeId &other ) const
00086    {
00087       return strcmp( type_, other.type_ ) == 0;
00088    }
00089 
00090    bool operator !=( const TypeId &other ) const
00091    {
00092       return !( *this == other );
00093    }
00094 
00095 private:
00096    const char *type_;
00097 };
00098 
00099 #ifdef CPPTL_NO_FUNCTION_TEMPLATE_ORDERING
00100 inline TypeId typeId( ... )
00101 #else
00102 template<class AType>
00103 inline TypeId typeId( Type<AType> )
00104 #endif
00105 {
00106    return __error__typeId_function_not_overloaded;
00107 }
00108 
00109 } // namespace CppTL
00110 
00111 #define CPPTL_DECLARE_TYPEINFO( AType )       \
00112    namespace CppTL {                          \
00113       inline TypeId typeId( Type<AType> )     \
00114       {                                       \
00115          return TypeId( #AType );             \
00116       }                                       \
00117    }
00118 
00119 #define CPPTL_DECLARE_TYPE_AND_PTR_INFO( AType )       \
00120    CPPTL_DECLARE_TYPEINFO( AType );                    \
00121    CPPTL_DECLARE_TYPEINFO( AType * )
00122 
00123 CPPTL_DECLARE_TYPEINFO( void );
00124 CPPTL_DECLARE_TYPEINFO( bool );
00125 CPPTL_DECLARE_TYPEINFO( char );
00126 CPPTL_DECLARE_TYPEINFO( signed char );
00127 CPPTL_DECLARE_TYPEINFO( unsigned char );
00128 CPPTL_DECLARE_TYPEINFO( short );
00129 CPPTL_DECLARE_TYPEINFO( unsigned short );
00130 CPPTL_DECLARE_TYPEINFO( int );
00131 CPPTL_DECLARE_TYPEINFO( unsigned int );
00132 CPPTL_DECLARE_TYPEINFO( long );
00133 CPPTL_DECLARE_TYPEINFO( unsigned long );
00134 CPPTL_DECLARE_TYPEINFO( float );
00135 CPPTL_DECLARE_TYPEINFO( double );
00136 CPPTL_DECLARE_TYPEINFO( long double );
00137 CPPTL_DECLARE_TYPEINFO( const char * );
00138 CPPTL_DECLARE_TYPEINFO( const wchar_t * );
00139 CPPTL_DECLARE_TYPEINFO( std::string );
00140 CPPTL_DECLARE_TYPEINFO( std::wstring );
00141 
00142 # endif
00143 
00144 #endif // CPPTL_TYPEINFO_H_INCUDED

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers