blob: b642d0649aee90ed229d46aa15ff7a6b9213e244 (
plain)
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
|
#ifndef UTIL_SCOPED__
#define UTIL_SCOPED__
/* Other scoped objects in the style of scoped_ptr. */
#include "util/exception.hh"
#include <cstddef>
namespace util {
class MallocException : public ErrnoException {
public:
explicit MallocException(std::size_t requested) throw();
~MallocException() throw();
};
void *MallocOrThrow(std::size_t requested);
void *CallocOrThrow(std::size_t requested);
class scoped_malloc {
public:
scoped_malloc() : p_(NULL) {}
scoped_malloc(void *p) : p_(p) {}
~scoped_malloc();
void reset(void *p = NULL) {
scoped_malloc other(p_);
p_ = p;
}
void call_realloc(std::size_t to);
void *get() { return p_; }
const void *get() const { return p_; }
private:
void *p_;
scoped_malloc(const scoped_malloc &);
scoped_malloc &operator=(const scoped_malloc &);
};
// Hat tip to boost.
template <class T> class scoped_array {
public:
explicit scoped_array(T *content = NULL) : c_(content) {}
~scoped_array() { delete [] c_; }
T *get() { return c_; }
const T* get() const { return c_; }
T &operator*() { return *c_; }
const T&operator*() const { return *c_; }
T &operator[](std::size_t idx) { return c_[idx]; }
const T &operator[](std::size_t idx) const { return c_[idx]; }
void reset(T *to = NULL) {
scoped_array<T> other(c_);
c_ = to;
}
private:
T *c_;
scoped_array(const scoped_array &);
void operator=(const scoped_array &);
};
template <class T> class scoped_ptr {
public:
explicit scoped_ptr(T *content = NULL) : c_(content) {}
~scoped_ptr() { delete c_; }
T *get() { return c_; }
const T* get() const { return c_; }
T &operator*() { return *c_; }
const T&operator*() const { return *c_; }
T *operator->() { return c_; }
const T*operator->() const { return c_; }
T &operator[](std::size_t idx) { return c_[idx]; }
const T &operator[](std::size_t idx) const { return c_[idx]; }
void reset(T *to = NULL) {
scoped_ptr<T> other(c_);
c_ = to;
}
private:
T *c_;
scoped_ptr(const scoped_ptr &);
void operator=(const scoped_ptr &);
};
} // namespace util
#endif // UTIL_SCOPED__
|