]> Creatis software - crea.git/blob - src/creaRTTI.h
#3374 crea Bug New Normal - vtk8itk5wx3-mingw64
[crea.git] / src / creaRTTI.h
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ 
26 */ 
27
28 /*=========================================================================
29                                                                                 
30   Program:   crea
31   Module:    $RCSfile: creaRTTI.h,v $
32   Language:  C++
33   Date:      $Date: 2012/11/15 10:43:26 $
34   Version:   $Revision: 1.4 $
35     
36 =========================================================================*/
37 /**
38  *\file
39  *\brief RTTI tools (system dependent).
40  */
41
42 #ifndef __CREARTTI_H_INCLUDED__
43 #define __CREARTTI_H_INCLUDED__
44
45 #include "creaSystem.h"
46 #include <vector>
47
48 //-----------------------------------------------------------------------------
49 // RRTI type_info.name() demangling
50 // For type_info.name() demangling (gcc >= 3.1, see http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html)
51 // Test for GCC > 3.1.0 //
52 #if __GNUC__ > 3 ||                                     \
53   (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 ||              \
54                      (__GNUC_MINOR__ == 1 &&            \
55                       __GNUC_PATCHLEVEL__ > 0)))
56                       
57 #include <cxxabi.h>
58 #include <stdlib.h>
59
60 namespace crea
61 {
62   inline std::string demangle_type_name(const char* name) 
63   {
64     int  status;
65     char* dem = abi::__cxa_demangle(name, 0, 0, &status);
66     std::string demangled(dem);
67     free(dem);
68     if (!status) return demangled;
69     return name;
70   }
71
72 }
73
74 #define CREA_DEMANGLE_TYPE_NAME(NAME) crea::demangle_type_name(NAME)
75
76 //==========================================================================
77 #elif defined(_WIN32)
78 // WIN32
79 //#include "Windows.h"
80 //#include "stdafx.h"
81 #include <windows.h>
82 #include <imagehlp.h>
83 // include the right library in the linker stage
84 #pragma comment( lib, "imagehlp.lib" )
85
86 namespace crea
87 {
88
89   
90   
91   
92   inline std::string demangle_type_name(const char* name) 
93   {
94     char demangled[513]; 
95     if (UnDecorateSymbolName(name, demangled, 
96                              sizeof(name), UNDNAME_COMPLETE))
97       {
98                   return name; //demangled;
99       }
100     else 
101       {
102         return name;
103       }
104   }
105 }
106 #define CREA_DEMANGLE_TYPE_NAME(NAME) crea::demangle_type_name(NAME)
107
108 #else 
109 // OTHER
110 #define CREA_DEMANGLE_TYPE_NAME(NAME) NAME
111 #endif 
112
113 #define CREA_GET_CURRENT_OBJECT_NAME \
114   CREA_DEMANGLE_TYPE_NAME(typeid(*this).name())
115
116 #define CREA_GET_TYPE_NAME(A)           \
117   CREA_DEMANGLE_TYPE_NAME(typeid(A).name())
118 //-----------------------------------------------------------------------------
119
120
121 namespace crea 
122 {
123   /// Template method which returns the name of a type
124   template <class T>
125   inline std::string TypeName() 
126   { return CREA_DEMANGLE_TYPE_NAME(typeid(T).name()); }
127   /// Template method which returns the name of the type of a variable
128   template <class T>
129   inline std::string TypeName(const T& t) 
130   { return CREA_DEMANGLE_TYPE_NAME(typeid(t).name()); }
131   /// Specialisation of TypeName when the type passed is already a type_info :
132   /// The user does not want to know the type of the type_info class but of 
133   /// the class whose type_info is passed !
134   template <> 
135   inline std::string TypeName<std::type_info>(const std::type_info& t)
136   { return CREA_DEMANGLE_TYPE_NAME(t.name()); }
137
138   /// Template method which returns the human readable name of a type
139   template <class T>
140   inline std::string HumanTypeName() 
141   { return TypeName<T>(); }
142   /// Template method which returns the human readable name of the type of a variable
143   template <class T>
144   inline std::string HumanTypeName(const T& t) 
145   { return TypeName(t); }
146   /// Specialisation of TypeName when the type passed is already a type_info :
147   /// The user does not want to know the type of the type_info class but of 
148   /// the class whose type_info is passed !
149   template <> 
150   inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
151   { return TypeName<std::type_info>(t); }
152   /// Macro to specialise the template function TypeName for certain types 
153   /// (typically highly template types, such as STL types) 
154   /// in order to return a **really** human readable string
155 #define CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME)                 \
156   template <> inline std::string HumanTypeName< TYPE >()                \
157   { return NAME; }                                                      \
158     template <> inline std::string HumanTypeName< TYPE >(const TYPE&)   \
159     { return NAME; }    
160   
161   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String");
162   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char");
163   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short");
164   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int");
165   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar");
166   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort");
167   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt");
168   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float");
169   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double");
170   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool");
171   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long");
172
173   // Human readable strings for std::vector
174 #define CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE)               \
175   template <> inline std::string HumanTypeName< std::vector<TYPE> >()   \
176   { return "Vector"+HumanTypeName<TYPE>(); }                            \
177     template <> inline std::string HumanTypeName< std::vector<TYPE> >   \
178     (const std::vector<TYPE>&) { return "Vector"+HumanTypeName<TYPE>(); }       
179
180   /*
181   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t);
182   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t);
183   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t);
184   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t);
185   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t);
186   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t);
187   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long);
188   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float);
189   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double);
190   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string);
191 */
192 /// The crea::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)) 
193   typedef const std::type_info& TypeInfo;
194
195
196   CREA_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
197                                   const std::type_info& source_type,
198                                   void*  source_pointer
199                                   );
200  CREA_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
201                                   const std::type_info& source_type,
202                                   const void*  source_pointer
203                                   );
204
205
206 }
207
208 #endif
209