55 Type() : currentType(&
typeid(
void)) {
59 Type(
const Type & t) : currentType(t.currentType) {
64 Type(
const T & t) : currentType(&
typeid(
void)){
73 currentType = &
typeid(T);
84 call<typeSetter>(*
this, t);
90 Type & operator=(
const T & t){
96 operator const std::type_info & ()
const {
return (*currentType); }
99 operator char ()
const {
return getTypeChar(*currentType); }
102 operator std::string ()
const {
return std::string(1, getTypeChar(*currentType)); }
140 return typeid(
unsigned char);
142 return typeid(short);
144 return typeid(
unsigned short);
148 return typeid(
unsigned int);
152 return typeid(
unsigned long);
154 return typeid(float);
156 return typeid(double);
161 return typeid(std::string);
168 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
" undefined type: '" << t <<
"'=" << (int)t << std::endl;
170 throw std::runtime_error(
"undefined type");
178 char getTypeChar(
const std::type_info & t){
179 if (t ==
typeid(
char))
181 else if (t ==
typeid(
unsigned char))
183 else if (t ==
typeid(
short))
185 else if (t ==
typeid(
unsigned short))
187 else if (t ==
typeid(
int))
189 else if (t ==
typeid(
unsigned int))
191 else if (t ==
typeid(
long))
193 else if (t ==
typeid(
unsigned long))
195 else if (t ==
typeid(
float))
197 else if (t ==
typeid(
double))
199 else if (t ==
typeid(
bool))
201 else if (t ==
typeid(
void))
203 else if (t ==
typeid(std::string))
205 else if (t ==
typeid(
const char *))
207 else if (t ==
typeid(
char *))
221 template <
class F,
class T>
223 T
call(
const std::type_info & t){
227 if (t ==
typeid(
char)){
228 return F::template callback<char,T>();
230 else if (t ==
typeid(
unsigned char)) {
231 return F::template callback<unsigned char,T>();
233 else if (t ==
typeid(
int)) {
234 return F::template callback<int,T>();
236 else if (t ==
typeid(
unsigned int)) {
237 return F::template callback<unsigned int,T>();
239 else if (t ==
typeid(
short int)) {
240 return F::template callback<short int,T>();
242 else if (t ==
typeid(
unsigned short int)) {
243 return F::template callback<unsigned short int,T>();
245 else if (t ==
typeid(
long int)) {
246 return F::template callback<long int,T>();
248 else if (t ==
typeid(
unsigned long int)) {
249 return F::template callback<unsigned long int,T>();
251 else if (t ==
typeid(
float)) {
252 return F::template callback<float,T>();
254 else if (t ==
typeid(
double)) {
255 return F::template callback<double,T>();
257 else if (t ==
typeid(
bool)) {
258 return F::template callback<bool,T>();
260 else if (t ==
typeid(
void)) {
261 return F::template callback<void,T>();
263 else if (t ==
typeid(std::string)) {
264 return F::template callback<std::string,T>();
273 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
"unimplemented type: ..." << t.name() << std::endl;
275 throw std::runtime_error(
"unimplemented type: ...");
280 return F::template callback<char,T>();
291 template <
class F,
class D,
class S>
304 template <
class F,
class S>
306 typename F::value_t
call(
const S & typeCode){
307 return call<F,typename F::value_t>(
getTypeInfo(typeCode));
338 template <
class F,
class T>
340 void call(T & target,
const std::type_info &t){
342 if (t ==
typeid(
char))
343 F::template callback<char>(target);
344 else if (t ==
typeid(
unsigned char))
345 F::template callback<unsigned char>(target);
346 else if (t ==
typeid(
int))
347 F::template callback<int>(target);
348 else if (t ==
typeid(
unsigned int))
349 F::template callback<unsigned int>(target);
350 else if (t ==
typeid(
short))
351 F::template callback<short>(target);
352 else if (t ==
typeid(
unsigned short))
353 F::template callback<unsigned short>(target);
354 else if (t ==
typeid(
long))
355 F::template callback<long>(target);
356 else if (t ==
typeid(
unsigned long))
357 F::template callback<unsigned long>(target);
358 else if (t ==
typeid(
float))
359 F::template callback<float>(target);
360 else if (t ==
typeid(
double))
361 F::template callback<double>(target);
362 else if (t ==
typeid(
bool))
363 F::template callback<bool>(target);
364 else if (t ==
typeid(std::string))
365 F::template callback<std::string>(target);
366 else if (t ==
typeid(
void)) {
367 F::template callback<void>(target);
375 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
"unimplemented type: ..." << t.name() << std::endl;
377 throw std::runtime_error(
"unimplemented type: ...");
396 template <
class F,
class T,
class T2>
398 void call(T & target,
const T2 &t){
411 if ((typeCode != NULL) && (*typeCode !=
'\0'))
413 else if (typeCode == NULL)
416 throw std::runtime_error(std::string(
"Type::operator=, not unit-length std::string: ") + typeCode);
426 if (typeCode.length() == 1)
428 else if (typeCode.empty())
431 throw std::runtime_error(std::string(
"Type::operator=, not unit-length std::string: ") + typeCode);
465 const std::type_info & type = t.
getType();
466 ostr << Type::getTypeChar(type) <<
'@';
471 std::ostream & toStreamFull(std::ostream & ostr,
const Type &t){
486 typedef bool value_t;
492 template <
class S,
class T>
495 return std::is_enum<S>::value;
522 const std::type_info *currentType;
529 std::ostream & operator<<(std::ostream & ostr,
const Type &t){
540 template <
typename T>
545 static const std::string & str(){
563 template <
typename T>
567 #define DRAIN_TYPENAME(tname) template <> const std::string TypeName<tname>::name
569 #define DRAIN_TYPENAME_DEF(tname) template <> const std::string TypeName<tname>::name(#tname)
572 #define DRAIN_TYPENAME_T(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;}}
579 DRAIN_TYPENAME(
void);
580 DRAIN_TYPENAME(
bool);
581 DRAIN_TYPENAME(
char);
582 DRAIN_TYPENAME(
unsigned char);
583 DRAIN_TYPENAME(
short);
584 DRAIN_TYPENAME(
unsigned short);
586 DRAIN_TYPENAME(
unsigned int);
587 DRAIN_TYPENAME(
long);
588 DRAIN_TYPENAME(
unsigned long);
589 DRAIN_TYPENAME(
float);
590 DRAIN_TYPENAME(
double);
591 DRAIN_TYPENAME(
char *);
592 DRAIN_TYPENAME(
const char *);
593 DRAIN_TYPENAME(std::string);
595 template <
typename T>
598 static const std::string & str(){
599 static std::string
name;
601 name =
"std::initializer_list<";
625 template <
typename T>
628 static const std::string & str(){
629 static std::string
name;
631 name =
"std::vector<";
663 typedef std::string value_t;
670 template <
class S,
class T>
Utilities related to std::type_info.
Definition: Type.h:51
static std::ostream & toStream(std::ostream &ostr, const Type &t)
Given a string, check if it could be stored as int or double instead of std::string .
Definition: Type.h:464
static const std::type_info & getTypeInfo(const char *typeCode)
Returns the type corresponding to a single-character string.
Definition: Type.h:410
const std::type_info & getType() const
Deprecated! Use cast (above) instead?
Definition: Type.h:107
static void call(T &target, const std::type_info &t)
Static function call without parameters on a single target of type T.
Definition: Type.h:340
static const std::type_info & getTypeInfo(char t)
Returns the base type associated with a character key.
Definition: Type.h:134
void setType(const T &t)
Set current type to t.
Definition: Type.h:83
static T call(const std::type_info &t)
Calls a static function that has no parameters.
Definition: Type.h:223
static F::value_t call(const S &typeCode)
A shorthand for functors with a fixed return type, hence D = F::value_t.
Definition: Type.h:306
static D call(const S &typeCode)
New, preferred implementation: a single if-then structure once and for all.
Definition: Type.h:293
static const std::type_info & getTypeInfo(const std::string &typeCode)
Returns the type corresponding to a single-character string.
Definition: Type.h:425
static void call(T &target, const T2 &t)
Static function call with a single parameter.
Definition: Type.h:398
static const std::type_info & getTypeInfo(const Type &t)
Convenience. Purpose is to have a full set of getTypeInfo commands for templated constructs.
Definition: Type.h:437
Returns the basic type (integer, float, bool, string, void) as a string.
Definition: Type.h:659
static T callback()
Definition: Type.h:672
Definition: DataSelector.cpp:1277
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition: Type.h:558
static T callback()
Definition: Type.h:494
static void callback(Type &target)
Definition: Type.h:515