GFC Logo GFC Title Logo
Reference Manual
Main Page  |  Namespace List  |  Alphabetical List  |  Class List  |  File List

pointer.hh

Go to the documentation of this file.
00001 /* GFC-Core: GTK+ Foundation Classes (Core Library) 00002 * Copyright (C) 2002-2004 The GFC Development Team. 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 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00023 00024 #ifndef GFC_POINTER_HH 00025 #define GFC_POINTER_HH 00026 00027 namespace GFC { 00028 00035 00036 template <typename T> 00037 class Pointer 00038 { 00039 T *t; 00040 00041 void set(T *object) 00042 { 00043 if (object) 00044 { 00045 if (!object->owns_reference()) 00046 object->ref(); 00047 object->set_owns_reference(false); 00048 } 00049 if (t) 00050 t->unref(); 00051 t = object; 00052 } 00053 00054 public: 00057 00058 Pointer(T *object = 0) : t(0) 00059 { 00060 set(object); 00061 } 00066 00067 Pointer(Pointer& src) : t(0) 00068 { 00069 set(src.get()); 00070 } 00075 00076 template <typename T1> 00077 Pointer(const Pointer<T1>& src) : t(0) 00078 { 00079 set(src.get()); 00080 } 00086 00087 ~Pointer() 00088 { 00089 set(0); 00090 } 00093 00094 Pointer& operator=(T *object) 00095 { 00096 set(object); 00097 return *this; 00098 } 00104 00105 Pointer& operator=(const Pointer& src) 00106 { 00107 set(src.get()); 00108 return *this; 00109 } 00115 00116 template <typename T1> 00117 Pointer& operator=(const Pointer<T1>& src) 00118 { 00119 set(src.get()); 00120 return *this; 00121 } 00128 00132 00133 T& operator*() const 00134 { 00135 return *get(); 00136 } 00139 00140 T* operator->() const 00141 { 00142 return get(); 00143 } 00146 00147 operator T*() const 00148 { 00149 return get(); 00150 } 00157 00158 T* get() const 00159 { 00160 return t; 00161 } 00164 00165 bool null() const 00166 { 00167 return t == 0; 00168 } 00170 00171 template<typename T1> 00172 bool operator==(const Pointer<T1>& other) 00173 { 00174 return t == other.t; 00175 } 00179 00180 template<typename T1> 00181 bool operator!=(const Pointer<T1>& other) 00182 { 00183 return t != other.t; 00184 } 00188 00192 00193 T* release() 00194 { 00195 T *tmp = t; 00196 if (tmp) 00197 tmp->ref(); 00198 set(0); 00199 return tmp; 00200 } 00207 00208 void reset(T *object = 0) 00209 { 00210 set(object); 00211 } 00217 00219 }; 00220 00223 00224 template <typename To, typename From> 00225 inline Pointer<To> 00226 cast_const(const Pointer<From>& from) 00227 { 00228 return Pointer<To>(from ? const_cast<To*>(from.get()) : 0); 00229 } 00236 00237 template <typename To, typename From> 00238 inline Pointer<To> 00239 cast_dynamic(const Pointer<From>& from) 00240 { 00241 return Pointer<To>(dynamic_cast<To*>(from.get())); 00242 } 00249 00250 template <typename To, typename From> 00251 inline Pointer<To> 00252 cast_static(const Pointer<From>& from) 00253 { 00254 return Pointer<To>(from ? static_cast<To*>(from.get()) : 0); 00255 } 00262 00264 00265 } // namespace GFC 00266 00267 #endif // GFC_POINTER_HH

Generated on Tue Aug 24 00:04:56 2004 for GFC-Core by doxygen 1.3.8