1 /*=========================================================================
4 Module: $RCSfile: bbtkRTTI.h,v $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1 $
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)))
40 inline std::string demangle_type_name(const char* name)
43 char* dem = abi::__cxa_demangle(name, 0, 0, &status);
44 std::string demangled(dem);
46 if (!status) return demangled;
52 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
54 //==========================================================================
57 //#include "Windows.h"
61 // include the right library in the linker stage
62 #pragma comment( lib, "imagehlp.lib" )
70 inline std::string demangle_type_name(const char* name)
73 if (UnDecorateSymbolName(name, demangled,
74 sizeof(name), UNDNAME_COMPLETE))
76 return name; //demangled;
84 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
88 #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME
91 #define BBTK_GET_CURRENT_OBJECT_NAME \
92 BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name())
94 #define BBTK_GET_TYPE_NAME(A) \
95 BBTK_DEMANGLE_TYPE_NAME(typeid(A).name())
96 //-----------------------------------------------------------------------------
101 /// Template method which returns the name of a type
103 inline std::string TypeName()
104 { return BBTK_DEMANGLE_TYPE_NAME(typeid(T).name()); }
105 /// Template method which returns the name of the type of a variable
107 inline std::string TypeName(const T& t)
108 { return BBTK_DEMANGLE_TYPE_NAME(typeid(t).name()); }
109 /// Specialisation of TypeName when the type passed is already a type_info :
110 /// The user does not want to know the type of the type_info class but of
111 /// the class whose type_info is passed !
113 inline std::string TypeName<std::type_info>(const std::type_info& t)
114 { return BBTK_DEMANGLE_TYPE_NAME(t.name()); }
116 /// Template method which returns the human readable name of a type
118 inline std::string HumanTypeName()
119 { return TypeName<T>(); }
120 /// Template method which returns the human readable name of the type of a variable
122 inline std::string HumanTypeName(const T& t)
123 { return TypeName(t); }
124 /// Specialisation of TypeName when the type passed is already a type_info :
125 /// The user does not want to know the type of the type_info class but of
126 /// the class whose type_info is passed !
128 inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
129 { return TypeName<std::type_info>(t); }
130 /// Macro to specialise the template function TypeName for certain types
131 /// (typically highly template types, such as STL types)
132 /// in order to return a **really** human readable string
133 #define BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME) \
134 template <> inline std::string HumanTypeName< TYPE >() \
136 template <> inline std::string HumanTypeName< TYPE >(const TYPE&) \
139 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"std::string");
140 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"char");
141 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"short");
142 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"int");
143 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"unsigned char");
144 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"unsigned short");
145 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"unsigned int");
147 // Human readable strings for std::vector
148 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<int8_t>,
149 "std::vector<char>");
150 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<uint8_t>,
151 "std::vector<uchar>");
152 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<int16_t>,
153 "std::vector<short>");
154 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<uint16_t>,
155 "std::vector<ushort>");
156 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<int32_t>,
158 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<uint32_t>,
159 "std::vector<uint>");
160 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<long>,
161 "std::vector<long>");
162 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<float>,
163 "std::vector<float>");
164 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<double>,
165 "std::vector<double>");
166 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::vector<std::string>,
167 "std::vector<std::string>");
170 /// 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))
171 typedef const std::type_info& TypeInfo;
174 BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type,
175 const std::type_info& source_type,
178 BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type,
179 const std::type_info& source_type,
180 const void* source_pointer