WvStreams
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 
00036 #if defined(__GNUC__) && __GNUC__ > 3
00037 # pragma GCC system_header
00038 #endif
00039 
00045 #include <xplc/IObject.h>
00046 
00047 #ifndef UNSTABLE
00048 #error "xplc_ptr is experimental!"
00049 #endif
00050 
00051 
00055 template<class T>
00056 class xplc_ptr {
00057 private:
00058   T* ptr;
00059 
00060   class ProtectedPtr: public T {
00061   private:
00062     virtual unsigned int addRef() = 0;
00063     virtual unsigned int release() = 0;
00064 #ifndef __XPLC_DELETE_H__
00065     void operator delete(void*);
00066 #endif
00067   };
00068 
00069   xplc_ptr& operator=(const xplc_ptr&);
00070 
00071 public:
00072   xplc_ptr():
00073     ptr(0) {
00074   }
00080   explicit xplc_ptr(T* aObj):
00081     ptr(aObj) {
00082   }
00087   template<class P>
00088   explicit xplc_ptr(const xplc_ptr<P>& aObj):
00089     ptr(aObj) {
00090     if(ptr)
00091       ptr->addRef();
00092   }
00093   ~xplc_ptr() {
00094     if(ptr)
00095       ptr->release();
00096   }
00104   ProtectedPtr* operator->() const {
00105     return static_cast<ProtectedPtr*>(ptr);
00106   }
00112   operator ProtectedPtr*() const {
00113     return static_cast<ProtectedPtr*>(ptr);
00114   }
00120   xplc_ptr& operator=(T* _ptr) {
00121     if(_ptr)
00122       _ptr->addRef();
00123 
00124     if(ptr)
00125       ptr->release();
00126 
00127     ptr = _ptr;
00128 
00129     return *this;
00130   }
00131 };
00132 
00133 
00138 template<class T>
00139 T* do_addRef(T* obj) {
00140   if (obj)
00141     obj->addRef();
00142 
00143   return obj;
00144 }
00145 
00146 
00147 #endif /* __XPLC_PTR_H__ */