1 /*=========================================================================
4 Module: $RCSfile: bbtkRTTI.h,v $
6 Date: $Date: 2008/10/06 07:55:50 $
7 Version: $Revision: 1.5 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See doc/license.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 *\brief RTTI tools (system dependent).
23 #ifndef __BBTKRTTI_H_INCLUDED__
24 #define __BBTKRTTI_H_INCLUDED__
26 #include "bbtkSystem.h"
29 //-----------------------------------------------------------------------------
30 // RRTI type_info.name() demangling
31 // For type_info.name() demangling (gcc >= 3.1, see http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html)
32 // Test for GCC > 3.1.0 //
34 (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 || \
35 (__GNUC_MINOR__ == 1 && \
36 __GNUC_PATCHLEVEL__ > 0)))
41 inline std::string demangle_type_name(const char* name)
44 char* dem = abi::__cxa_demangle(name, 0, 0, &status);
45 std::string demangled(dem);
47 if (!status) return demangled;
53 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
55 //==========================================================================
58 //#include "Windows.h"
62 // include the right library in the linker stage
63 #pragma comment( lib, "imagehlp.lib" )
71 inline std::string demangle_type_name(const char* name)
74 if (UnDecorateSymbolName(name, demangled,
75 sizeof(name), UNDNAME_COMPLETE))
77 return name; //demangled;
85 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
89 #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME
92 #define BBTK_GET_CURRENT_OBJECT_NAME \
93 BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name())
95 #define BBTK_GET_TYPE_NAME(A) \
96 BBTK_DEMANGLE_TYPE_NAME(typeid(A).name())
97 //-----------------------------------------------------------------------------
102 /// Template method which returns the name of a type
104 inline std::string TypeName()
105 { return BBTK_DEMANGLE_TYPE_NAME(typeid(T).name()); }
106 /// Template method which returns the name of the type of a variable
108 inline std::string TypeName(const T& t)
109 { return BBTK_DEMANGLE_TYPE_NAME(typeid(t).name()); }
110 /// Specialisation of TypeName when the type passed is already a type_info :
111 /// The user does not want to know the type of the type_info class but of
112 /// the class whose type_info is passed !
114 inline std::string TypeName<std::type_info>(const std::type_info& t)
115 { return BBTK_DEMANGLE_TYPE_NAME(t.name()); }
117 /// Template method which returns the human readable name of a type
119 inline std::string HumanTypeName()
120 { return TypeName<T>(); }
121 /// Template method which returns the human readable name of the type of a variable
123 inline std::string HumanTypeName(const T& t)
124 { return TypeName(t); }
125 /// Specialisation of TypeName when the type passed is already a type_info :
126 /// The user does not want to know the type of the type_info class but of
127 /// the class whose type_info is passed !
129 inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
130 { return TypeName<std::type_info>(t); }
131 /// Macro to specialise the template function TypeName for certain types
132 /// (typically highly template types, such as STL types)
133 /// in order to return a **really** human readable string
134 #define BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME) \
135 template <> inline std::string HumanTypeName< TYPE >() \
137 template <> inline std::string HumanTypeName< TYPE >(const TYPE&) \
140 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String");
141 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char");
142 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short");
143 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int");
144 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar");
145 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort");
146 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt");
147 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float");
148 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double");
149 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool");
150 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long");
152 // Human readable strings for std::vector
153 #define BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE) \
154 template <> inline std::string HumanTypeName< std::vector<TYPE> >() \
155 { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;} \
156 template <> inline std::string HumanTypeName< std::vector<TYPE> > \
157 (const std::vector<TYPE>&) \
158 { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;}
160 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t);
161 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t);
162 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t);
163 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t);
164 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t);
165 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t);
166 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long);
167 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float);
168 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double);
169 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string);
171 /// The bbtk::TypeInfo type is a const ref on std::type_info (which can only be manipulated as such (because typeid returns const std::type_info& and type_info has all constructors private))
172 typedef const std::type_info& TypeInfo;
175 BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type,
176 const std::type_info& source_type,
179 BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type,
180 const std::type_info& source_type,
181 const void* source_pointer