Debian Codegraph @

src/common/scoped_ptr.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2005 Dizzy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #ifndef PVPGN_SCOPED_PTR_H
20 #define PVPGN_SCOPED_PTR_H
21
22 #include <memory>
23
24 namespace pvpgn
25 {
26
27 template<typename T>
28 class scoped_ptr
29 {
30 public:
31 /** initilize the object aquiring ownership of the given parameter (0 for no onwership) */
32 explicit scoped_ptr(T* ptr_ = 0)
33 :ptr(ptr_) {}
34
35 /** initilize from a blind pointer supporting implicit conversion */
36 template<typename V>
37 explicit scoped_ptr(V* ptr_)
38 :ptr(ptr_) {}
39
40 /** initilize the object transfering ownership from an std::unique_ptr<> */
41 template<typename V>
42 explicit scoped_ptr(std::unique_ptr<V>& aptr)
43 :ptr(aptr.release()) {}
44
45 /** release memory if aquired ownershipt */
46 ~scoped_ptr() throw() {
47 cleanup();
48 }
49
50 /** get the wrapped pointer */
51 T* get() const { return ptr; }
52
53 /** release ownership of the memory */
54 T* release() {
55 T* tmp = ptr;
56 ptr = 0;
57 return tmp;
58 }
59
60 /** reinitilize object, release owned resource first if any */
61 void reset(T* ptr_ = 0) {
62 cleanup();
63 ptr = ptr_;
64 }
65
66 /** reinitilize object, supports implicit conversions */
67 template<typename V>
68 void reset(V* ptr_) {
69 cleanup();
70 ptr = ptr_;
71 }
72
73 /** const-dereference of the wrapped pointer */
74 const T& operator*() const {
75 return *ptr;
76 }
77
78 /** dereference of the wrapped pointer */
79 T& operator*() {
80 return *ptr;
81 }
82
83 /** const-member access to the wrapped pointer */
84 const T* operator->() const {
85 return ptr;
86 }
87
88 /** member access to the wrapped pointer */
89 T* operator->() {
90 return ptr;
91 }
92
93 private:
94 T* ptr;
95
96 /* do not allow to copy a scoped_ptr (use std::unique_ptr<> if you need that) */
97 scoped_ptr(const scoped_ptr&);
98 scoped_ptr& operator=(const scoped_ptr&);
99
100 void cleanup() {
101 delete ptr;
102 }
103 };
104
105 }
106
107 #endif