Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SmartPtrBase.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/SmartPtrBase.h                   **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter, Robert Haschke                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/Macros.h>
00034 #include <cstdlib>
00035 
00036 namespace icl{
00037   namespace utils{
00038     
00040     class DelOpBase { };
00041     
00043     struct PointerDelOp : public DelOpBase{ template<class T> static void delete_func(T *t){ delete t; } };
00044   
00046     struct ArrayDelOp : public DelOpBase{ template<class T>  static void delete_func(T *t){ delete[] t; } };
00047   
00049     struct FreeDelOp : public DelOpBase{ static void delete_func(void *v){ free(v); } };
00050     
00052 
00075     template<class T, class delOp = ArrayDelOp > 
00076     class SmartPtrBase{
00077       protected:
00078       T   *e; 
00079       int *c; 
00080       bool d; 
00082 
00083       void inc() {
00084         if(c) (*c)++; 
00085       }
00086   
00088       void dec() { 
00089         if(c && *c) { 
00090           if ( --(*c) == 0) { 
00091             //               if(d) delete[] e; 
00092             if(d) delOp::delete_func(e);
00093             delete c;
00094             c = 0;
00095           }
00096         }
00097       }
00098   
00100       void set(T *e,int *c, bool d) {
00101         this->e=e; 
00102         this->c=c; 
00103         this->d=d;
00104       }
00105         
00107       template<class DerivedT>
00108       inline SmartPtrBase<T,delOp> &assign(const SmartPtrBase<DerivedT,delOp>& r){
00109         if(r.e == e) return *this;
00110         dec();
00111         set(r.e,r.c,r.d);
00112         inc();
00113         return *this;
00114       }
00115            
00116       public:
00117         
00119       template<class A, class B> friend class SmartPtrBase;
00120     
00122       SmartPtrBase(): e(0),c(0),d(0){
00123       }    
00124         
00126       template<class DerivedT>
00127       SmartPtrBase(DerivedT *ptData, bool bOwn=true): e(ptData), c(new int(1)),d(bOwn){
00128       }
00129         
00131 
00132       template<class DerivedT>
00133       SmartPtrBase(const SmartPtrBase<DerivedT,delOp> &r): e(&dynamic_cast<T&>(*r.e)), c(r.c), d(r.d){ 
00134         //      SmartPtrBase(const SmartPtrBase<DerivedT,delOp> &r): e(r.e), c(r.c), d(r.d){ 
00135         inc(); 
00136       }
00137   
00139 
00140       SmartPtrBase(const SmartPtrBase<T,delOp> &r): e(r.e), c(r.c), d(r.d){ 
00141         inc(); 
00142       }
00143         
00145 
00153       template<class DerivedT>
00154       SmartPtrBase<T,delOp> &operator=(const SmartPtrBase<DerivedT,delOp>& r){
00155         return assign(r);
00156   
00157       }
00158   
00160 
00163       SmartPtrBase<T,delOp> &operator=(const SmartPtrBase<T,delOp>& r){
00164         return assign(r);
00165       }
00166   
00168 
00169       template<class DerivedT>
00170       SmartPtrBase<T,delOp> &operator=(DerivedT *p){
00171         return this->operator=(SmartPtrBase<T,delOp>(p));
00172       }
00173   
00175       SmartPtrBase<T,delOp> &operator=(T *p){
00176         return this->operator=(SmartPtrBase<T,delOp>(p));
00177       }
00178   
00180       ~SmartPtrBase() { dec(); }
00181           
00183 
00186       T &operator* () { ICLASSERT(e); return *e; }
00187   
00189 
00192       const T &operator* () const { ICLASSERT(e); return *e; }
00193         
00194   
00196 
00199       T* get () { return e; }
00200   
00202 
00205       const T* get () const { return e; }
00206   
00207   
00209 
00212       T *operator-> () { ICLASSERT(e); return e; }
00213   
00215 
00218       const T *operator-> () const { ICLASSERT(e); return e; }
00219   
00220   
00222       operator bool() const { return (e != 0); }
00223     
00225       int use_count() const { return c ? *c : 0; }
00226         
00228 
00234       void setNull() { 
00235         dec();
00236         set(0,0,0);
00237       }
00238     };
00239   
00240   } // namespace utils
00241 }
00242 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines