kjsembed
pointer.h
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 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 // qDebug("new pointer %s %0x", typeid(ValueType).name(), value); 00048 } 00049 ~Pointer( ) 00050 { 00051 00052 } 00053 void cleanup() 00054 { 00055 // qDebug("delete pointer %s %0x", typeid(ValueType).name(), ptr ); 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 //qDebug("new value %s", typeid(ValueType).name()); 00079 } 00080 ~Value( ) 00081 { 00082 //qDebug("delete value"); 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 // qDebug("pointers %s %s", typeid(ValueType).name(), pointer->type().name() ); 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 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle; 00143
KDE 4.6 API Reference