Reference Manual

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

# pointer.h

Go to the documentation of this file.

    00001 /*  Inti: Integrated Foundation Classes
    00002  *  Copyright (C) 2002 The Inti Development Team.
    00003  *      Copyright (C) 2000 Red Hat, Inc.
    00004  *
    00005  *  This program is free software; you can redistribute it and/or modify
    00006  *  it under the terms of the GNU General Public License as published by
    00007  *  the Free Software Foundation; either version 2 of the License, or
    00008  *  (at your option) any later version.
    00009  *
    00010  *  This program is distributed in the hope that it will be useful,
    00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013  *  GNU Library General Public License for more details.
    00014  *
    00015  *  You should have received a copy of the GNU Library General Public License
    00016  *  along with this program; if not, write to the Free Software
    00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00018  */
    00019
    00024
    00025 #ifndef INTI_POINTER_H
    00026 #define INTI_POINTER_H
    00027
    00028 namespace Inti {
    00029
    00036
    00037 template <typename T>
    00038 class Pointer
    00039 {
    00040         T *t;
    00041
    00042         void set(T *object)
    00043         {
    00044                 if (object)
    00045                 {
    00046                         if (!object->is_referenced())
    00047                                 object->ref();
    00048                         object->set_referenced(false);
    00049                 }
    00050                 if (t)
    00051                         t->unref();
    00052                 t = object;
    00053         }
    00054
    00055 public:
    00058
    00059         Pointer(T *object = 0) : t(0)
    00060         {
    00061                 set(object);
    00062         }
    00067
    00068         Pointer(Pointer& src) : t(0)
    00069         {
    00070                 set(src.get());
    00071         }
    00076
    00077         template <typename T1>
    00078         Pointer(const Pointer<T1>& src) : t(0)
    00079         {
    00080                 set(src.get());
    00081         }
    00087
    00088         ~Pointer()
    00089         {
    00090                 set(0);
    00091         }
    00094
    00095         Pointer& operator=(T *object)
    00096         {
    00097                 set(object);
    00098                 return *this;
    00099         }
    00105
    00106         Pointer& operator=(const Pointer& src)
    00107         {
    00108                 set(src.get());
    00109                 return *this;
    00110         }
    00116
    00117         template <typename T1>
    00118         Pointer& operator=(const Pointer<T1>& src)
    00119         {
    00120                 set(src.get());
    00121                 return *this;
    00122         }
    00129
    00133
    00134         T& operator*() const
    00135         {
    00136                 return *get();
    00137         }
    00140
    00141         T* operator->() const
    00142         {
    00143                 return get();
    00144         }
    00147
    00148         operator T*() const
    00149         {
    00150                 return get();
    00151         }
    00158
    00159         T* get() const
    00160         {
    00161                 return t;
    00162         }
    00165
    00166         bool null() const
    00167         {
    00168                 return t == 0;
    00169         }
    00171
    00175
    00176         T* release()
    00177         {
    00178                 T *tmp = t;
    00179                 if (tmp)
    00180                         tmp->ref();
    00181                 set(0);
    00182                 return tmp;
    00183         }
    00190
    00191         void reset(T *object = 0)
    00192         {
    00193                 set(object);
    00194         }
    00200
    00202 };
    00203
    00206
    00207 template<typename T1, typename T2>
    00208 inline bool operator == (const Pointer<T1>& t1, const Pointer<T2>& t2)
    00209 {
    00210         return t1.t == t2.t;
    00211 }
    00214
    00215 template<typename T1, typename T2>
    00216 inline bool operator != (const Pointer<T1>& t1, const Pointer<T2>& t2)
    00217 {
    00218         return !(t1 == t2);
    00219 }
    00222
    00226
    00227 template <typename To, typename From>
    00228 inline Pointer<To>
    00229 cast_const(const Pointer<From>& from)
    00230 {
    00231         return Pointer<To>(from ? const_cast<To*>(from.get()) : 0);
    00232 }
    00239
    00240 template <typename To, typename From>
    00241 inline Pointer<To>
    00242 cast_dynamic(const Pointer<From>& from)
    00243 {
    00244         return Pointer<To>(dynamic_cast<To*>(from.get()));
    00245 }
    00252
    00253 template <typename To, typename From>
    00254 inline Pointer<To>
    00255 cast_static(const Pointer<From>& from)
    00256 {
    00257         return Pointer<To>(from ? static_cast<To*>(from.get()) : 0);
    00258 }
    00265
    00267
    00268 } // namespace Inti
    00269
    00270 #endif // INTI_POINTER_H

Main Page - Footer