]> Creatis software - crea.git/blob - src/creaRTTI.h
#3180 crea Feature New Normal Future - Set wx-config for wxWidgets 2.8
[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 #include <cxxabi.h>
57 #include <stdlib.h>
58 namespace crea
59 {
60   inline std::string demangle_type_name(const char* name) 
61   {
62     int  status;
63     char* dem = abi::__cxa_demangle(name, 0, 0, &status);
64     std::string demangled(dem);
65     free(dem);
66     if (!status) return demangled;
67     return name;
68   }
69
70 }
71
72 #define CREA_DEMANGLE_TYPE_NAME(NAME) crea::demangle_type_name(NAME)
73
74 //==========================================================================
75 #elif defined(_WIN32)
76 // WIN32
77 //#include "Windows.h"
78 //#include "stdafx.h"
79 #include <windows.h>
80 #include <imagehlp.h>
81 // include the right library in the linker stage
82 #pragma comment( lib, "imagehlp.lib" )
83
84 namespace crea
85 {
86
87   
88   
89   
90   inline std::string demangle_type_name(const char* name) 
91   {
92     char demangled[513]; 
93     if (UnDecorateSymbolName(name, demangled, 
94                              sizeof(name), UNDNAME_COMPLETE))
95       {
96                   return name; //demangled;
97       }
98     else 
99       {
100         return name;
101       }
102   }
103 }
104 #define CREA_DEMANGLE_TYPE_NAME(NAME) crea::demangle_type_name(NAME)
105
106 #else 
107 // OTHER
108 #define CREA_DEMANGLE_TYPE_NAME(NAME) NAME
109 #endif 
110
111 #define CREA_GET_CURRENT_OBJECT_NAME \
112   CREA_DEMANGLE_TYPE_NAME(typeid(*this).name())
113
114 #define CREA_GET_TYPE_NAME(A)           \
115   CREA_DEMANGLE_TYPE_NAME(typeid(A).name())
116 //-----------------------------------------------------------------------------
117
118
119 namespace crea 
120 {
121   /// Template method which returns the name of a type
122   template <class T>
123   inline std::string TypeName() 
124   { return CREA_DEMANGLE_TYPE_NAME(typeid(T).name()); }
125   /// Template method which returns the name of the type of a variable
126   template <class T>
127   inline std::string TypeName(const T& t) 
128   { return CREA_DEMANGLE_TYPE_NAME(typeid(t).name()); }
129   /// Specialisation of TypeName when the type passed is already a type_info :
130   /// The user does not want to know the type of the type_info class but of 
131   /// the class whose type_info is passed !
132   template <> 
133   inline std::string TypeName<std::type_info>(const std::type_info& t)
134   { return CREA_DEMANGLE_TYPE_NAME(t.name()); }
135
136   /// Template method which returns the human readable name of a type
137   template <class T>
138   inline std::string HumanTypeName() 
139   { return TypeName<T>(); }
140   /// Template method which returns the human readable name of the type of a variable
141   template <class T>
142   inline std::string HumanTypeName(const T& t) 
143   { return TypeName(t); }
144   /// Specialisation of TypeName when the type passed is already a type_info :
145   /// The user does not want to know the type of the type_info class but of 
146   /// the class whose type_info is passed !
147   template <> 
148   inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
149   { return TypeName<std::type_info>(t); }
150   /// Macro to specialise the template function TypeName for certain types 
151   /// (typically highly template types, such as STL types) 
152   /// in order to return a **really** human readable string
153 #define CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME)                 \
154   template <> inline std::string HumanTypeName< TYPE >()                \
155   { return NAME; }                                                      \
156     template <> inline std::string HumanTypeName< TYPE >(const TYPE&)   \
157     { return NAME; }    
158   
159   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String");
160   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char");
161   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short");
162   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int");
163   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar");
164   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort");
165   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt");
166   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float");
167   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double");
168   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool");
169   CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long");
170
171   // Human readable strings for std::vector
172 #define CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE)               \
173   template <> inline std::string HumanTypeName< std::vector<TYPE> >()   \
174   { return "Vector"+HumanTypeName<TYPE>(); }                            \
175     template <> inline std::string HumanTypeName< std::vector<TYPE> >   \
176     (const std::vector<TYPE>&) { return "Vector"+HumanTypeName<TYPE>(); }       
177
178   /*
179   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t);
180   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t);
181   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t);
182   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t);
183   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t);
184   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t);
185   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long);
186   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float);
187   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double);
188   CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string);
189 */
190 /// 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)) 
191   typedef const std::type_info& TypeInfo;
192
193
194   CREA_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
195                                   const std::type_info& source_type,
196                                   void*  source_pointer
197                                   );
198  CREA_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
199                                   const std::type_info& source_type,
200                                   const void*  source_pointer
201                                   );
202
203
204 }
205
206 #endif
207