00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00021 #ifndef _TypeSafeGenericPointer_hpp
00022 #define _TypeSafeGenericPointer_hpp
00023
00024 #include<ImLib3D/CppTools.hpp>
00025
00026 class TypeSafeGenericPointer
00027 {
00028 void *ptr;
00029 const char *typeinfoName;
00031 map<string,void *> alternateCasts;
00032 public:
00033 string DemangledTypeName(){return TypeNameParser(typeinfoName);}
00034 string TypeName(){return typeinfoName;}
00035 bool IsSet(){return string(typeinfoName)!=string("");}
00036 void Clear(){ptr=NULL;typeinfoName="";alternateCasts.clear();}
00037 template<class T>
00038 T Get()
00039 {
00040 if(!IsSet()){ThrowError("TypeSafeGenericPointer::Get Trying to read value unset pointer");}
00041 string castTypeName=typeid(T).name();
00042 if(castTypeName!=typeinfoName)
00043 {
00044
00045 if(alternateCasts.find(castTypeName)!=alternateCasts.end())
00046 {
00047 return (T)alternateCasts[castTypeName];
00048 }
00049 string alt;
00050 for(map<string,void *>::iterator p=alternateCasts.begin();p!=alternateCasts.end();++p)
00051 {
00052 alt+=TypeNameParser((*p).first)+",";
00053 }
00054 ThrowError("TypeSafeGenericPointer::Get: Attempted to use pointer as:\"%s\". Originally, pointer was stored as:\"%s\": available alternate casts where:\"%s\"",
00055 TypeNameParser(castTypeName).c_str(),
00056 DemangledTypeName().c_str(),
00057 alt.c_str());
00058 }
00059 return (T)ptr;
00060 }
00061 template<class T>
00062 void Set(T _ptr)
00063 {
00064 typeinfoName=typeid(T).name();
00065 ptr=(void*)_ptr;
00066 }
00067
00068 template<class OriginalType,class AlternateType>
00069 void AddAlternateCast()
00070 {
00071 alternateCasts[typeid(AlternateType).name()]=(void*)(AlternateType)(OriginalType)ptr;
00072 }
00073
00074
00075 template<class T>
00076 TypeSafeGenericPointer(T _ptr)
00077 {
00078 Set(_ptr);
00079 }
00080 TypeSafeGenericPointer &operator=(const TypeSafeGenericPointer &other)
00081 {
00082 ptr=other.ptr;
00083 typeinfoName=other.typeinfoName;
00084 alternateCasts=other.alternateCasts;
00085 return *this;
00086 }
00087 TypeSafeGenericPointer(const TypeSafeGenericPointer &other)
00088 {
00089 ptr=other.ptr;
00090 typeinfoName=other.typeinfoName;
00091 alternateCasts=other.alternateCasts;
00092 }
00093 TypeSafeGenericPointer():ptr(NULL),typeinfoName("")
00094 {
00095 }
00096 };
00097
00098
00099 #endif // _TypeSafeGenericPointer_hpp