54 Type() : currentType(&
typeid(
void)) {
58 Type(
const Type & t) : currentType(t.currentType) {
63 Type(
const T & t) : currentType(&
typeid(
void)){
72 currentType = &
typeid(T);
83 call<typeSetter>(*
this, t);
89 Type & operator=(
const T & t){
95 operator const std::type_info & ()
const {
return (*currentType); }
98 operator char ()
const {
return getTypeChar(*currentType); }
101 operator std::string ()
const {
return std::string(1, getTypeChar(*currentType)); }
139 return typeid(
unsigned char);
141 return typeid(short);
143 return typeid(
unsigned short);
147 return typeid(
unsigned int);
151 return typeid(
unsigned long);
153 return typeid(float);
155 return typeid(double);
160 return typeid(std::string);
167 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
" undefined type: '" << t <<
"'=" << (int)t << std::endl;
169 throw std::runtime_error(
"undefined type");
177 char getTypeChar(
const std::type_info & t){
178 if (t ==
typeid(
char))
180 else if (t ==
typeid(
unsigned char))
182 else if (t ==
typeid(
short))
184 else if (t ==
typeid(
unsigned short))
186 else if (t ==
typeid(
int))
188 else if (t ==
typeid(
unsigned int))
190 else if (t ==
typeid(
long))
192 else if (t ==
typeid(
unsigned long))
194 else if (t ==
typeid(
float))
196 else if (t ==
typeid(
double))
198 else if (t ==
typeid(
bool))
200 else if (t ==
typeid(
void))
202 else if (t ==
typeid(std::string))
204 else if (t ==
typeid(
const char *))
206 else if (t ==
typeid(
char *))
220 template <
class F,
class T>
222 T
call(
const std::type_info & t){
226 if (t ==
typeid(
char)){
227 return F::template callback<char,T>();
229 else if (t ==
typeid(
unsigned char)) {
230 return F::template callback<unsigned char,T>();
232 else if (t ==
typeid(
int)) {
233 return F::template callback<int,T>();
235 else if (t ==
typeid(
unsigned int)) {
236 return F::template callback<unsigned int,T>();
238 else if (t ==
typeid(
short int)) {
239 return F::template callback<short int,T>();
241 else if (t ==
typeid(
unsigned short int)) {
242 return F::template callback<unsigned short int,T>();
244 else if (t ==
typeid(
long int)) {
245 return F::template callback<long int,T>();
247 else if (t ==
typeid(
unsigned long int)) {
248 return F::template callback<unsigned long int,T>();
250 else if (t ==
typeid(
float)) {
251 return F::template callback<float,T>();
253 else if (t ==
typeid(
double)) {
254 return F::template callback<double,T>();
256 else if (t ==
typeid(
bool)) {
257 return F::template callback<bool,T>();
259 else if (t ==
typeid(
void)) {
260 return F::template callback<void,T>();
262 else if (t ==
typeid(std::string)) {
263 return F::template callback<std::string,T>();
272 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
"unimplemented type: ..." << t.name() << std::endl;
274 throw std::runtime_error(
"unimplemented type: ...");
279 return F::template callback<char,T>();
290 template <
class F,
class D,
class S>
303 template <
class F,
class S>
305 typename F::value_t
call(
const S & typeCode){
306 return call<F,typename F::value_t>(
getTypeInfo(typeCode));
337 template <
class F,
class T>
339 void call(T & target,
const std::type_info &t){
341 if (t ==
typeid(
char))
342 F::template callback<char>(target);
343 else if (t ==
typeid(
unsigned char))
344 F::template callback<unsigned char>(target);
345 else if (t ==
typeid(
int))
346 F::template callback<int>(target);
347 else if (t ==
typeid(
unsigned int))
348 F::template callback<unsigned int>(target);
349 else if (t ==
typeid(
short))
350 F::template callback<short>(target);
351 else if (t ==
typeid(
unsigned short))
352 F::template callback<unsigned short>(target);
353 else if (t ==
typeid(
long))
354 F::template callback<long>(target);
355 else if (t ==
typeid(
unsigned long))
356 F::template callback<unsigned long>(target);
357 else if (t ==
typeid(
float))
358 F::template callback<float>(target);
359 else if (t ==
typeid(
double))
360 F::template callback<double>(target);
361 else if (t ==
typeid(
bool))
362 F::template callback<bool>(target);
363 else if (t ==
typeid(std::string))
364 F::template callback<std::string>(target);
365 else if (t ==
typeid(
void)) {
366 F::template callback<void>(target);
374 std::cerr << __FILE__ <<
' ' << __FUNCTION__ <<
"unimplemented type: ..." << t.name() << std::endl;
376 throw std::runtime_error(
"unimplemented type: ...");
395 template <
class F,
class T,
class T2>
397 void call(T & target,
const T2 &t){
410 if ((typeCode != NULL) && (*typeCode !=
'\0'))
412 else if (typeCode == NULL)
415 throw std::runtime_error(std::string(
"Type::operator=, not unit-length std::string: ") + typeCode);
425 if (typeCode.length() == 1)
427 else if (typeCode.empty())
430 throw std::runtime_error(std::string(
"Type::operator=, not unit-length std::string: ") + typeCode);
459 const std::type_info & guessArrayType(const C & container);
464 const std::type_info & type = t.
getType();
465 ostr << Type::getTypeChar(type) <<
'@';
470 std::ostream & toStreamFull(std::ostream & ostr,
const Type &t){
485 typedef bool value_t;
491 template <
class S,
class T>
494 return std::is_enum<S>::value;
521 const std::type_info *currentType;
528std::ostream & operator<<(std::ostream & ostr,
const Type &t){
543 static const std::string & str(){
558#define DRAIN_TYPENAME(tname) template <> const std::string TypeName<tname>::name
560#define DRAIN_TYPENAME_DEF(tname) template <> const std::string TypeName<tname>::name(#tname)
563#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;}}
586#define DRAIN_TYPENAME_T0(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;}}
588DRAIN_TYPENAME_T0(std::initializer_list, T);
589DRAIN_TYPENAME_T0(std::vector, T);
643 typedef std::string value_t;
650 template <
class S,
class T>
Utilities related to std::type_info.
Definition Type.h:50
static const std::type_info & getTypeInfo(const char *typeCode)
Returns the type corresponding to a single-character string.
Definition Type.h:409
static const std::type_info & getTypeInfo(const std::string &typeCode)
Returns the type corresponding to a single-character string.
Definition Type.h:424
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:463
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:436
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:339
const std::type_info & getType() const
Deprecated! Use cast (above) instead?
Definition Type.h:106
void setType(const T &t)
Set current type to t.
Definition Type.h:82
static T call(const std::type_info &t)
Calls a static function that has no parameters.
Definition Type.h:222
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:305
static D call(const S &typeCode)
New, preferred implementation: a single if-then structure once and for all.
Definition Type.h:292
static void call(T &target, const T2 &t)
Static function call with a single parameter.
Definition Type.h:397
static const std::type_info & getTypeInfo(char t)
Returns the base type associated with a character key.
Definition Type.h:133
Returns the basic type (integer, float, bool, string, void) as a string.
Definition Type.h:639
static T callback()
Definition Type.h:652
Definition DataSelector.cpp:1277
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.
Default implementation.
Definition Type.h:541
static const std::string name
Default implementation: name returned by std::type_info::name()
Definition Type.h:549
static T callback()
Definition Type.h:493
static void callback(Type &target)
Definition Type.h:514