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
|
// Copyright 2005 Ben Hutchings <ben@decadent.org.uk>.
// See the file "COPYING" for licence details.
#ifndef INC_AUTO_ARRAY_HPP
#define INC_AUTO_ARRAY_HPP
#include <cstddef>
// Like auto_ptr, but for arrays
template<typename element_type>
class auto_array_ref;
template<typename element_type>
class auto_array
{
typedef auto_array_ref<element_type> ref_type;
public:
auto_array()
: ptr_(0)
{}
explicit auto_array(element_type * ptr)
: ptr_(ptr)
{}
auto_array(ref_type other)
: ptr_(other.release())
{}
auto_array & operator=(auto_array & other)
{
reset(other.release());
}
~auto_array()
{
reset();
}
element_type * get() const
{
return ptr_;
}
element_type * release()
{
element_type * ptr(ptr_);
ptr_ = 0;
return ptr;
}
void reset(element_type * ptr = 0)
{
delete[] ptr_;
ptr_ = ptr;
}
element_type & operator[](std::ptrdiff_t index)
{
return ptr_[index];
}
operator ref_type()
{
return ref_type(*this);
}
private:
element_type * ptr_;
};
template<typename element_type>
class auto_array_ref
{
typedef auto_array<element_type> target_type;
public:
explicit auto_array_ref(target_type & target)
: target_(target)
{}
element_type * release()
{
return target_.release();
}
private:
target_type & target_;
};
#endif // !INC_AUTO_ARRAY_HPP
|