Qore Programming Language  0.8.3
include/qore/ReferenceHolder.h (4311)
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003   ReferenceHolder.h
00004 
00005   Smart pointer like class that dereferences
00006   obtained pointer to a QoreReferenceCounter in its destructor.
00007 
00008   Qore Programming Language
00009 
00010   Copyright (C) 2006 - 2011 Qore Technologies
00011 
00012   This library is free software; you can redistribute it and/or
00013   modify it under the terms of the GNU Lesser General Public
00014   License as published by the Free Software Foundation; either
00015   version 2.1 of the License, or (at your option) any later version.
00016 
00017   This library is distributed in the hope that it will be useful,
00018   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020   Lesser General Public License for more details.
00021 
00022   You should have received a copy of the GNU Lesser General Public
00023   License along with this library; if not, write to the Free Software
00024   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025 */
00026 
00027 #ifndef QORE_REFERENCE_HOLDER_H_
00028 #define QORE_REFERENCE_HOLDER_H_
00029 
00030 #include <stdlib.h>
00031 
00033 
00042 template<typename T>
00043 class ReferenceHolder {
00044    private:
00045       DLLLOCAL ReferenceHolder(const ReferenceHolder&); // not implemented
00046       DLLLOCAL ReferenceHolder& operator=(const ReferenceHolder&); // not implemented
00047       DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
00048       
00049       T* p;
00050       ExceptionSink* xsink;
00051 
00052    public:
00054       DLLLOCAL ReferenceHolder(ExceptionSink* xsink_) : p(0), xsink(xsink_) {}
00055 
00057       DLLLOCAL ReferenceHolder(T* p_, ExceptionSink* xsink_) : p(p_), xsink(xsink_) {}
00058 
00060       DLLLOCAL ~ReferenceHolder() { if (p) p->deref(xsink);}
00061 
00063       DLLLOCAL T* operator->() { return p; }
00064 
00066       DLLLOCAL T* operator*() { return p; }
00067 
00069       DLLLOCAL void operator=(T *nv)
00070       {
00071          if (p)
00072             p->deref(xsink);
00073          p = nv;
00074       }
00075 
00077       DLLLOCAL T *release()
00078       {
00079          T *rv = p;
00080          p = 0;
00081          return rv;
00082       }
00083 
00085       DLLLOCAL operator bool() const { return p != 0; }
00086       
00088       DLLLOCAL T **getPtrPtr() { return &p; }
00089 };
00090 
00092 
00098 template<typename T>
00099 class SimpleRefHolder
00100 {
00101    private:
00102       DLLLOCAL SimpleRefHolder(const SimpleRefHolder&); // not implemented
00103       DLLLOCAL SimpleRefHolder& operator=(const SimpleRefHolder&); // not implemented
00104       DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
00105       
00106       T* p;
00107 
00108    public:
00109       DLLLOCAL SimpleRefHolder() : p(0) {}
00110       DLLLOCAL SimpleRefHolder(T* p_) : p(p_) {}
00111       DLLLOCAL ~SimpleRefHolder() { if (p) p->deref(); }
00112       
00113       DLLLOCAL T* operator->() { return p; }
00114       DLLLOCAL T* operator*() { return p; }
00115       DLLLOCAL void operator=(T *nv)
00116       {
00117          if (p)
00118             p->deref();
00119          p = nv;
00120       }
00121       DLLLOCAL T *release()
00122       {
00123          T *rv = p;
00124          p = 0;
00125          return rv;
00126       }
00127       DLLLOCAL operator bool() const { return p != 0; }
00128 };
00129 
00130 #endif
00131 
00132 // EOF
00133 
00134 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines