Loading...
Searching...
No Matches
TypeName.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef DRAIN_TYPE_NAME
32#define DRAIN_TYPE_NAME
33
34#include <iostream>
35#include <typeinfo>
36// #include <bits/types.h> // uint8, uint16 etc
37#include <cstdint>
38
39#include <vector>
40
41
42
43namespace drain {
44
45
46
47
49/*
50
51*/
52template <typename T>
54{
55
56 static
57 const std::string & str();
58
59 inline
60 operator const std::string & () const{
61 return str();
62 }
63
64};
65
67template <typename T>
68const std::string & TypeName<T>::str(){
69 static
70 const std::string name(typeid(T).name());
71 return name;
72}
73
74
75
77/*
78
79*/
80template <typename T>
81inline
82std::ostream & operator<<(std::ostream & ostr, const TypeName<T> &t){
83 ostr << t.str();
84 return ostr;
85}
86
87
88
89
90// Main macro
91#define DRAIN_TYPENAME(tname) template <> inline const std::string & drain::TypeName<tname>::str(){static const std::string n(#tname); return n;}
92// Variant for stripping off the trailing '_t'
93#define DRAIN_TYPENAME_t(tname) template <> inline const std::string & drain::TypeName<tname##_t>::str(){static const std::string n(#tname); return n;}
94
95// Variant for
96#define DRAIN_TYPENAME_T(cname,T) template <class T> struct TypeName<cname<T> > {static const std::string & str(){static const std::string n=std::string(#cname)+'<'+drain::TypeName<T>::str()+'>'; return n;}}
97// #define DRAIN_TYPENAME_T_0(cname,T) template <class T> struct TypeName<cname<T> > {static const std::string & str(){static const std::string n=drain::StringBuilder<>(#cname, '<', drain::TypeName<T>::str(),'>'); return n;}}
98
99
101DRAIN_TYPENAME(char);
102DRAIN_TYPENAME_t(int8);
103DRAIN_TYPENAME_t(uint8);
104DRAIN_TYPENAME_t(int16);
105DRAIN_TYPENAME_t(uint16);
106DRAIN_TYPENAME_t(int32);
107DRAIN_TYPENAME_t(uint32);
108DRAIN_TYPENAME_t(int64);
109DRAIN_TYPENAME_t(uint64);
110
111DRAIN_TYPENAME(void);
112DRAIN_TYPENAME(bool);
113DRAIN_TYPENAME(float);
114DRAIN_TYPENAME(double);
115// NEWOLD
116DRAIN_TYPENAME(char *);
117DRAIN_TYPENAME(const char *); // why const separately...?
118DRAIN_TYPENAME(std::string);
119
120
121DRAIN_TYPENAME_T(std::initializer_list, T);
122DRAIN_TYPENAME_T(std::vector, T);
123
124
125
126
128
138
139public:
140
141 typedef std::string value_t;
142 // typedef const std::string & value_t;
143
148 template <class S, class T>
149 static
151 //return TypeName<S>::get();
152 return TypeName<S>::str();
153 }
154
155
156};
157
158
159
160} // drain::
161
162#endif
Returns the basic type (integer, float, bool, string, void) as a string.
Definition TypeName.h:137
static T callback()
Definition TypeName.h:150
Definition Data.h:1473
Default implementation.
Definition TypeName.h:54
static const std::string & str()
Default implementation: name returned by std::type_info::name()
Definition TypeName.h:68