Loading...
Searching...
No Matches
rtti.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_COMMON_RTTI_H
#define BALL_COMMON_RTTI_H
#ifndef BALL_CONFIG_CONFIG_H
# include <BALL/CONFIG/config.h>
#endif
#ifndef BALL_COMMON_H
# include <BALL/common.h>
#endif
#include <string>
#include <typeinfo>
using std::string;
namespace BALL
{
# ifdef __GNUC__
// EGCS produces a nearly unreadable name mangling that requires
// further interpretation
namespace GNUDemangling
{
BALL_EXPORT string demangle(string s);
}
# endif
BALL_EXPORT string streamClassName(const std::type_info& t);
namespace RTTI
{
template <typename T>
const T& getDefault()
{
static T t;
return t;
}
template <typename T>
void* getNew()
{
return static_cast<void*>(new T);
}
template <typename T>
const char* getName()
{
return typeid(T).name();
}
template <typename T>
void* getClassID()
{
static char dummy;
return (void*)&dummy;
}
template <typename T>
const char* getStreamName()
{
// define portable names for the portable
// types (some platforms use Size, some unsigned int,
// SUN CC even unsigned for the Size type)
if ((typeid(T) == typeid(Size))
|| (typeid(T) == typeid(Position))
|| (typeid(T) == typeid(HashIndex))
|| (typeid(T) == typeid(Property))
|| (typeid(T) == typeid(Handle)))
{
return "BALL::Size";
}
if ((typeid(T) == typeid(Index))
|| (typeid(T) == typeid(ErrorCode))
|| (typeid(T) == typeid(Distance)))
{
return "BALL::Index";
}
if (typeid(T) == typeid(::std::string))
{
return "::std::string";
}
if (typeid(T) == typeid(LongSize))
{
return "BALL::LongSize";
}
if (typeid(T) == typeid(bool))
{
return "bool";
}
if (typeid(T) == typeid(float))
{
return "float";
}
if (typeid(T) == typeid(char))
{
return "char";
}
if (typeid(T) == typeid(unsigned char))
{
return "unsigned_char";
}
if (typeid(T) == typeid(double))
{
return "double";
}
static string s("");
static bool is_set = false;
if (!is_set)
{
is_set = true;
s = streamClassName(typeid(T));
}
return s.c_str();
}
template <typename T, typename U>
bool isKindOf(const U *u)
{
return (0 != dynamic_cast<const T*>(u));
}
template <typename T, typename U>
T* castTo(const U& u)
{
return const_cast<T*>(dynamic_cast<const T*>(&u));
}
template <typename T, typename U>
bool isInstanceOf(const U& u)
{
return (typeid(u) == typeid(T));
}
} // namespace RTTI
} // namespace BALL
#endif // BALL_COMMON_RTTI_H
BALL_EXPORT
#define BALL_EXPORT
Definition COMMON/global.h:50
common.h
BALL::streamClassName
BALL_EXPORT string streamClassName(const std::type_info &t)
BALL::RTTI
Definition rtti.h:67
BALL::RTTI::isInstanceOf
bool isInstanceOf(const U &u)
Definition rtti.h:226
BALL::RTTI::getDefault
const T & getDefault()
Definition rtti.h:75
BALL::RTTI::getStreamName
const char * getStreamName()
Definition rtti.h:122
BALL::RTTI::isKindOf
bool isKindOf(const U *u)
Definition rtti.h:192
BALL::RTTI::getNew
void * getNew()
Definition rtti.h:87
BALL::RTTI::castTo
T * castTo(const U &u)
Definition rtti.h:215
BALL::RTTI::getName
const char * getName()
Definition rtti.h:97
BALL::RTTI::getClassID
void * getClassID()
Definition rtti.h:105
BALL
Definition constants.h:13
BALL::HashIndex
BALL_SIZE_TYPE HashIndex
Definition COMMON/global.h:131
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::Property
BALL_SIZE_TYPE Property
Definition COMMON/global.h:165
BALL::LongSize
BALL_ULONG64_TYPE LongSize
Definition COMMON/global.h:192
BALL::ErrorCode
BALL_INDEX_TYPE ErrorCode
Definition COMMON/global.h:173
BALL::Position
BALL_SIZE_TYPE Position
Definition COMMON/global.h:140
BALL::Handle
BALL_SIZE_TYPE Handle
Definition COMMON/global.h:96
BALL::Distance
BALL_INDEX_TYPE Distance
Definition COMMON/global.h:86
BALL::Index
BALL_INDEX_TYPE Index
Definition COMMON/global.h:105