Kiln » TortoiseHg » TortoiseHg
Clone URL:  
auto_buffer.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
// TortoiseSVN - a Windows shell extension for easy version control // Copyright (C) 2009 - TortoiseSVN // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software Foundation, // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // #pragma once /** * A simplified analog to std::auto_ptr<> that encapsulates * an array allocated dynamically via new[]. * * Use this where you could not use a std::auto_ptr<> (works * for single elements only) nor a std::vector<> (no guarantees * w.r.t. to internal organization, i.e. no access to mem buffer). */ template<class T> class auto_buffer { private: T* buffer; /// no copy nor assignment auto_buffer(const auto_buffer&); auto_buffer& operator=(const auto_buffer&); public: explicit auto_buffer (size_t size = 0) throw() : buffer (size == 0 ? NULL : new T[size]) { } ~auto_buffer() { delete[] buffer; } operator T*() const throw() { return buffer; } operator void*() const throw() { return buffer; } operator bool() const throw() { return buffer != NULL; } T* operator->() const throw() { return buffer; } T *get() const throw() { return buffer; } T* release() throw() { T* temp = buffer; buffer = NULL; return temp; } void reset (size_t newSize = 0) { delete[] buffer; buffer = (newSize == 0 ? NULL : new T[newSize]); } };