xoreos  0.0.5
disposableptr.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on Cabal (<https://github.com/project-cabal/cabal/>) code,
26  * which in turn is based on ScummVM (<http://scummvm.org>) code,
27  * which is released under the terms of version 2 or later of the
28  * GNU General Public License.
29  *
30  * The original copyright note in ScummVM reads as follows:
31  *
32  * ScummVM is the legal property of its developers, whose names
33  * are too numerous to list here. Please refer to the COPYRIGHT
34  * file distributed with this source distribution.
35  *
36  * This program is free software; you can redistribute it and/or
37  * modify it under the terms of the GNU General Public License
38  * as published by the Free Software Foundation; either version 2
39  * of the License, or (at your option) any later version.
40  *
41  * This program is distributed in the hope that it will be useful,
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44  * GNU General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program; if not, write to the Free Software
48  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
49  */
50 
51 #ifndef COMMON_DISPOSABLEPTR_H
52 #define COMMON_DISPOSABLEPTR_H
53 
54 #include <boost/noncopyable.hpp>
55 
56 #include "src/common/system.h"
57 #include "src/common/types.h"
58 #include "src/common/deallocator.h"
59 
60 namespace Common {
61 
67 template<typename T, class Deallocator>
68 class DisposablePtrBase : boost::noncopyable {
69 public:
70  typedef T ValueType;
71  typedef T *PointerType;
72  typedef T &ReferenceType;
73 
74  explicit DisposablePtrBase(PointerType o, bool d) : _pointer(o), _dispose(d) {}
75 
79  XOREOS_EXPLICIT_OPERATOR_CONV operator bool() const { return _pointer != 0; }
80 
82  if (_dispose)
83  Deallocator::destroy(_pointer);
84  }
85 
87  void reset(PointerType o = 0) {
88  if (_dispose)
89  Deallocator::destroy(_pointer);
90 
91  _pointer = o;
92  }
93 
98  PointerType get() const { return _pointer; }
99 
101  void setDisposable(bool d) { _dispose = d; }
102 
104  void dispose() {
105  Deallocator::destroy(_pointer);
106  _pointer = 0;
107  }
108 
109 private:
111 
112  bool _dispose;
113 };
114 
116 template<typename T, class Deallocator = DeallocatorDefault>
117 class DisposablePtr : public DisposablePtrBase<T, Deallocator> {
118 public:
120  DisposablePtrBase<T, Deallocator>(o, d) {
121  }
122 
125  }
126 
129  }
130 };
131 
133 template<typename T, class Deallocator = DeallocatorArray>
134 class DisposableArray : public DisposablePtrBase<T, Deallocator> {
135 public:
137  DisposablePtrBase<T, Deallocator>(o, d) {
138  }
139 
142  }
143 };
144 
145 } // End of namespace Common
146 
147 #endif // COMMON_DISPOSABLEPTR_H
A disposable array pointer, allowing array-y access and array deletion.
Definition: 2dafile.h:39
DisposablePtrBase< T, Deallocator >::PointerType operator->() const
DisposableArray(typename DisposablePtrBase< T, Deallocator >::PointerType o, bool d)
#define XOREOS_EXPLICIT_OPERATOR_CONV
Definition: system.h:402
Base template class for a disposable pointer.
Definition: disposableptr.h:68
Low-level type definitions to handle fixed width types portably.
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: disposableptr.h:87
A disposable plain pointer, allowing pointer-y access and normal deletion.
DisposablePtrBase< T, Deallocator >::ReferenceType operator*() const
Simple deallocator concept.
void setDisposable(bool d)
Change the disposable flag.
DisposablePtrBase< T, Deallocator >::ReferenceType operator[](size_t i) const
Low-level detection of architecture/system properties.
PointerType get() const
Returns the plain pointer value.
Definition: disposableptr.h:98
DisposablePtrBase(PointerType o, bool d)
Definition: disposableptr.h:74
DisposablePtr(typename DisposablePtrBase< T, Deallocator >::PointerType o, bool d)
void dispose()
Unconditionally dispose of the pointer, destroying the old object.