Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members  

ptr.h

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * XPLC - Cross-Platform Lightweight Components
00004  * Copyright (C) 2002-2004, Pierre Phaneuf
00005  * Copyright (C) 2002-2004, Net Integration Technologies, Inc.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00020  * USA
00021  *
00022  * As a special exception, you may use this file as part of a free
00023  * software library without restriction.  Specifically, if other files
00024  * instantiate templates or use macros or inline functions from this
00025  * file, or you compile this file and link it with other files to
00026  * produce an executable, this file does not by itself cause the
00027  * resulting executable to be covered by the GNU Lesser General Public
00028  * License.  This exception does not however invalidate any other
00029  * reasons why the executable file might be covered by the GNU Lesser
00030  * General Public License.
00031  */
00032 
00033 #ifndef __XPLC_PTR_H__
00034 #define __XPLC_PTR_H__
00035 
00041 #include <xplc/IObject.h>
00042 
00043 #ifndef UNSTABLE
00044 #error "xplc_ptr is experimental!"
00045 #endif
00046 
00047 
00051 template<class T>
00052 class xplc_ptr {
00053 private:
00054   T* ptr;
00055 
00056   class ProtectedPtr: public T {
00057   private:
00058     virtual unsigned int addRef() = 0;
00059     virtual unsigned int release() = 0;
00060 #ifndef __XPLC_DELETE_H__
00061     void operator delete(void*);
00062 #endif
00063   };
00064 
00065   xplc_ptr& operator=(const xplc_ptr&);
00066 
00067 public:
00068   xplc_ptr():
00069     ptr(0) {
00070   }
00076   explicit xplc_ptr(T* aObj):
00077     ptr(aObj) {
00078   }
00083   template<class P>
00084   explicit xplc_ptr(const xplc_ptr<P>& aObj):
00085     ptr(aObj) {
00086     if(ptr)
00087       ptr->addRef();
00088   }
00089   ~xplc_ptr() {
00090     if(ptr)
00091       ptr->release();
00092   }
00100   ProtectedPtr* operator->() const {
00101     return static_cast<ProtectedPtr*>(ptr);
00102   }
00108   operator ProtectedPtr*() const {
00109     return static_cast<ProtectedPtr*>(ptr);
00110   }
00116   xplc_ptr& operator=(T* _ptr) {
00117     if(_ptr)
00118       _ptr->addRef();
00119 
00120     if(ptr)
00121       ptr->release();
00122 
00123     ptr = _ptr;
00124 
00125     return *this;
00126   }
00127 };
00128 
00129 
00134 template<class T>
00135 T* do_addRef(T* obj) {
00136   if (obj)
00137     obj->addRef();
00138 
00139   return obj;
00140 }
00141 
00142 
00143 #endif /* __XPLC_PTR_H__ */

Generated on Sun Sep 26 04:01:25 2004 for XPLC by doxygen1.2.18