Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TypeSafeGenericPointer.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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             // normal cast failed.. try alternate casts
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

Generated on Fri Jun 17 13:36:09 2005 for ImLib3D by  doxygen 1.4.2