-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscopedptr.h
153 lines (123 loc) · 3.73 KB
/
scopedptr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef HDF5_SCOPEDPTR_INC
#define HDF5_SCOPEDPTR_INC
#include <hdf5.h>
#include <algorithm>
#include <vector>
typedef void (*dealloc_t)(void*);
/**
* A class, that automatically deletes/frees a pointer when destroyed.
*/
template <class T, dealloc_t dealloc> class scoped_ptr
{
private:
// No copy
scoped_ptr(const scoped_ptr&);
scoped_ptr& operator=(const scoped_ptr&);
T* ptr;
public:
/** Default CTOR: Creates NULL pointer. */
scoped_ptr() throw (): ptr(NULL) {}
/** Create scoped ptr from pointer. */
explicit scoped_ptr(T* ptr_in) throw (): ptr(ptr_in) {}
// DTOR
~scoped_ptr() throw ()
{
T* tmp = ptr;
if (tmp) {
ptr = NULL;
dealloc(ptr);
}
}
/** Returns true, if this is a valid pointer */
bool valid() const throw () { return !!ptr; }
/** Swaps pointers. */
void swap(scoped_ptr<T, dealloc>& other) throw () { std::swap(ptr, other.ptr); }
/** Resets this pointer to a new value. */
void reset(T* ptr_in = NULL) throw ()
{
scoped_ptr<T, dealloc> tmp(ptr_in);
swap(tmp);
}
/** Returns handle id */
T* get() const throw () { return ptr; }
/** Releases handle id. This is a NULL afterwards. */
T* release() throw ()
{
T* tmp = ptr;
ptr = NULL;
return tmp;
}
// Dereference ptr
T* operator->()const { return ptr; }
T& operator*()const { return *ptr; }
};
template <class T, dealloc_t D> inline void swap(scoped_ptr<T, D>& a, scoped_ptr<T, D>& b)
{
a.swap(b);
}
/**
* A class, that automatically deletes/frees a pointers when destroyed.
*/
template <class T, dealloc_t dealloc> class scoped_ptr_array
{
private:
// No default
scoped_ptr_array();
// No copy
scoped_ptr_array(const scoped_ptr_array&);
scoped_ptr_array& operator=(const scoped_ptr_array&);
std::vector<T*> data;
public:
/** Create vector with num NULL entries. */
explicit scoped_ptr_array(std::size_t num) throw (): data(num, static_cast<T*>(NULL)) {}
// DTOR
~scoped_ptr_array() throw ()
{
std::vector<T*>::iterator iter = data.begin();
std::vector<T*>::iterator end = data.end();
while (iter != end) {
T* tmp = *iter;
*iter = NULL;
dealloc(tmp);
++iter;
}
}
/** Return size of array. */
std::size_t size() const { return data.size(); }
/** Return whether array is empty. */
bool empty() const { return data.empty(); }
// Iteration
typedef typename std::vector<T*>::const_iterator const_iterator;
const_iterator begin() const { return data.begin(); }
const_iterator end() const { return data.end(); }
/** Access to underlying array, use with care */
T** unsafe_data() { return &data[0]; }
/** Swaps vectors. */
void swap(scoped_ptr_array<T, dealloc>& other) throw () { data.swap(other); }
/** Returns pointer at index */
T* get(std::size_t index) const throw () { return index < data.size() ? data[index] : NULL; }
/** Sets pointer at index, clearing previous value */
void set(std::size_t index, T* ptr) const throw ()
{
if (index < data.size()) {
T* tmp = data[index];
data[index] = ptr;
deleter(tmp);
} else
deleter(ptr);
}
/** Releases pointer at index. Is is NULL afterwards. */
T* release(std::size_t index) throw ()
{
if (index >= data.size())
return NULL;
T* tmp = data[index];
data[index] = NULL;
return tmp;
}
};
template <class T, dealloc_t D> inline void swap(scoped_ptr_array<T, D>& a, scoped_ptr_array<T, D>& b)
{
a.swap(b);
}
#endif // HDF5_SCOPEDPTR_INC