kjsembed
pointer.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef POINTER_H
00025 #define POINTER_H
00026
00027 #include <algorithm>
00028 #include <typeinfo>
00029 #include <QtCore/QVariant>
00030
00031 struct PointerBase
00032 {
00033 public:
00034 virtual ~PointerBase() {;}
00035 virtual void cleanup() = 0;
00036 virtual const std::type_info &type() const = 0;
00037 virtual void *voidStar() = 0;
00038 };
00039 Q_DECLARE_METATYPE(PointerBase*)
00040
00041 template<typename ValueType>
00042 struct Pointer : public PointerBase
00043 {
00044 public:
00045 Pointer( ValueType *value) : ptr(value)
00046 {
00047
00048 }
00049 ~Pointer( )
00050 {
00051
00052 }
00053 void cleanup()
00054 {
00055
00056 delete ptr;
00057 ptr=0L;
00058 }
00059 const std::type_info &type() const
00060 {
00061 return typeid(ValueType);
00062 }
00063
00064 void *voidStar()
00065 {
00066 return (void*)ptr;
00067 }
00068
00069 ValueType *ptr;
00070 };
00071
00072 template<typename ValueType>
00073 struct Value : public PointerBase
00074 {
00075 public:
00076 Value( ValueType val) : value(val)
00077 {
00078
00079 }
00080 ~Value( )
00081 {
00082
00083 }
00084
00085 void cleanup()
00086 {
00087
00088 }
00089
00090 const std::type_info &type() const
00091 {
00092 return typeid(ValueType);
00093 }
00094
00095 void *voidStar()
00096 {
00097 return (void*)&value;
00098 }
00099
00100 ValueType value;
00101 };
00102
00103 struct NullPtr : public PointerBase
00104 {
00105 NullPtr( )
00106 {
00107 ;
00108 }
00109 ~NullPtr( )
00110 {
00111 ;
00112 }
00113 void cleanup()
00114 {
00115 ;
00116 }
00117
00118 const std::type_info &type() const
00119 {
00120 return typeid(NullPtr);
00121 }
00122
00123 void *voidStar()
00124 {
00125 return (void*)0;
00126 }
00127
00128 };
00129
00130 template<typename ValueType>
00131 ValueType *pointer_cast( PointerBase *pointer )
00132 {
00133
00134 if( typeid(ValueType) != pointer->type() )
00135 return 0L;
00136 Pointer<ValueType> *upcast = static_cast< Pointer<ValueType> *>(pointer);
00137 return upcast->ptr;
00138 }
00139
00140 #endif
00141
00142
00143