Qore Programming Language  0.8.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ReferenceHolder.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  ReferenceHolder.h
4 
5  Smart pointer like class that dereferences
6  obtained pointer to a QoreReferenceCounter in its destructor.
7 
8  Qore Programming Language
9 
10  Copyright (C) 2006 - 2013 Qore Technologies
11 
12  This library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU Lesser General Public
14  License as published by the Free Software Foundation; either
15  version 2.1 of the License, or (at your option) any later version.
16 
17  This library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  Lesser General Public License for more details.
21 
22  You should have received a copy of the GNU Lesser General Public
23  License along with this library; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26 
27 #ifndef QORE_REFERENCE_HOLDER_H_
28 #define QORE_REFERENCE_HOLDER_H_
29 
30 #include <stdlib.h>
31 
33 
42 template<typename T = class AbstractQoreNode>
44 private:
45  DLLLOCAL ReferenceHolder(const ReferenceHolder&); // not implemented
46  DLLLOCAL ReferenceHolder& operator=(const ReferenceHolder&); // not implemented
47  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
48 
49  T* p;
50  ExceptionSink* xsink;
51 
52 public:
54  DLLLOCAL ReferenceHolder(ExceptionSink* xsink_) : p(0), xsink(xsink_) {}
55 
57  DLLLOCAL ReferenceHolder(T* p_, ExceptionSink* xsink_) : p(p_), xsink(xsink_) {}
58 
60  DLLLOCAL ~ReferenceHolder() { if (p) p->deref(xsink);}
61 
63  DLLLOCAL T* operator->() { return p; }
64 
66  DLLLOCAL T* operator*() { return p; }
67 
69  DLLLOCAL void operator=(T *nv) {
70  if (p)
71  p->deref(xsink);
72  p = nv;
73  }
74 
76  DLLLOCAL T* release() {
77  T *rv = p;
78  p = 0;
79  return rv;
80  }
81 
83  DLLLOCAL operator bool() const { return p != 0; }
84 
86  DLLLOCAL T** getPtrPtr() { return &p; }
87 
89  DLLLOCAL T*& getRef() { return p; }
90 };
91 
93 
99 template<typename T>
101 private:
102  DLLLOCAL SimpleRefHolder(const SimpleRefHolder&); // not implemented
103  DLLLOCAL SimpleRefHolder& operator=(const SimpleRefHolder&); // not implemented
104  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
105 
106  T* p;
107 
108 public:
109  DLLLOCAL SimpleRefHolder() : p(0) {}
110  DLLLOCAL SimpleRefHolder(T* p_) : p(p_) {}
111  DLLLOCAL ~SimpleRefHolder() { if (p) p->deref(); }
112 
113  DLLLOCAL T* operator->() { return p; }
114  DLLLOCAL T* operator*() { return p; }
115  DLLLOCAL void operator=(T *nv) {
116  if (p)
117  p->deref();
118  p = nv;
119  }
120  DLLLOCAL T *release() {
121  T *rv = p;
122  p = 0;
123  return rv;
124  }
125  DLLLOCAL operator bool() const { return p != 0; }
126 };
127 
128 #endif
129 // EOF