types.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * types.hpp -- provides a simple type reflection mechanism. *
3  * *
4  * Author: Hosein Attarzadeh (shan2@kth.se) based on: *
5  * http://stackoverflow.com/questions/1055452/c-get-name-of-type-in-template *
6  * *
7  * Purpose: Provide facilities to store the type names, used in *
8  * introspection. *
9  * Usage: This file is included automatically *
10  * *
11  * License: *
12  *******************************************************************/
13 
14 #ifndef TYPES_HPP
15 #define TYPES_HPP
16 
25 //~ namespace ForSyDe
26 //~ {
27 
28 // The general case uses RTTI (if the type is not registered explicitly)
29 #pragma once
30 template<typename T> const char* get_type_name() {return typeid(T).name();}
31 
32 // Specialization for each type
33 #define DEFINE_TYPE(X) \
34  template<>const char* get_type_name<X>(){return #X;}
35 // Another version where we explicitly provide the type name (for complex types)
36 #define DEFINE_TYPE_NAME(X,N) \
37  template<>const char* get_type_name<X>(){return N;}
38 
39 // Specialization for base types
40 
41 DEFINE_TYPE(char);
42 DEFINE_TYPE(short int);
43 DEFINE_TYPE(unsigned short int);
44 DEFINE_TYPE(int);
45 DEFINE_TYPE(unsigned int);
46 DEFINE_TYPE(long int);
47 DEFINE_TYPE(unsigned long int);
48 DEFINE_TYPE(long long int);
49 DEFINE_TYPE(unsigned long long int);
50 DEFINE_TYPE(bool);
51 DEFINE_TYPE(float);
52 DEFINE_TYPE(double);
53 DEFINE_TYPE(long double);
54 DEFINE_TYPE(wchar_t);
55 
56 
57 //~ }
58 
59 #endif