xoreos  0.0.5
scopedptr.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_SCOPEDPTR_H
52 #define COMMON_SCOPEDPTR_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 #include "src/common/util.h"
60 
61 namespace Common {
62 
68 template<typename T, class Deallocator>
69 class ScopedPtrBase : boost::noncopyable {
70 public:
71  typedef T ValueType;
72  typedef T *PointerType;
73  typedef T &ReferenceType;
74 
75  explicit ScopedPtrBase(PointerType o = 0) : _pointer(o) {}
76 
80  XOREOS_EXPLICIT_OPERATOR_CONV operator bool() const { return _pointer != 0; }
81 
83  Deallocator::destroy(_pointer);
84  }
85 
87  void reset(PointerType o = 0) {
88  Deallocator::destroy(_pointer);
89  _pointer = o;
90  }
91 
96  PointerType get() const { return _pointer; }
97 
104  PointerType r = _pointer;
105  _pointer = 0;
106  return r;
107  }
108 
111  SWAP(_pointer, right._pointer);
112  }
113 
114 private:
116 };
117 
119 template<typename T, class Deallocator = DeallocatorDefault>
120 class ScopedPtr : public ScopedPtrBase<T, Deallocator> {
121 public:
123  ScopedPtrBase<T, Deallocator>(o) {
124  }
125 
128  }
129 
132  }
133 };
134 
136 template<typename T, class Deallocator = DeallocatorArray>
137 class ScopedArray : public ScopedPtrBase<T, Deallocator> {
138 public:
140  ScopedPtrBase<T, Deallocator>(o) {
141  }
142 
145  }
146 };
147 
148 } // End of namespace Common
149 
150 #endif // COMMON_SCOPEDPTR_H
Definition: 2dafile.h:39
ScopedPtrBase< T, Deallocator >::ReferenceType operator[](size_t i) const
Definition: scopedptr.h:143
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
ScopedArray(typename ScopedPtrBase< T, Deallocator >::PointerType o=0)
Definition: scopedptr.h:139
void swap(ScopedPtrBase< T, Deallocator > &right)
Swap the managed pointers of two ScopedPtrs of the same type.
Definition: scopedptr.h:110
Base template class for a scoped pointer.
Definition: scopedptr.h:69
#define XOREOS_EXPLICIT_OPERATOR_CONV
Definition: system.h:402
Utility templates and functions.
ScopedPtrBase< T, Deallocator >::ReferenceType operator*() const
Definition: scopedptr.h:126
Low-level type definitions to handle fixed width types portably.
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
Simple deallocator concept.
PointerType _pointer
Definition: scopedptr.h:115
ScopedPtrBase(PointerType o=0)
Definition: scopedptr.h:75
Low-level detection of architecture/system properties.
A scoped array pointer, allowing array-y access and array deletion.
Definition: scopedptr.h:137
ScopedPtr(typename ScopedPtrBase< T, Deallocator >::PointerType o=0)
Definition: scopedptr.h:122
ScopedPtrBase< T, Deallocator >::PointerType operator->() const
Definition: scopedptr.h:130
void SWAP(T &a, T &b)
Template method which swaps the values of its two parameters.
Definition: util.h:78