PageRenderTime 22ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/win32/shellext/auto_buffer.h

https://bitbucket.org/tortoisehg/hgtk/
C++ Header | 91 lines | 49 code | 19 blank | 23 comment | 3 complexity | fdb4a8d4a8fced6ffcd04ea6b68ed64b MD5 | raw file
 1// TortoiseSVN - a Windows shell extension for easy version control
 2
 3// Copyright (C) 2009 - TortoiseSVN
 4
 5// This program is free software; you can redistribute it and/or
 6// modify it under the terms of the GNU General Public License
 7// as published by the Free Software Foundation; either version 2
 8// of the License, or (at your option) any later version.
 9
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software Foundation,
17// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18//
19#pragma once
20
21/**
22 * A simplified analog to std::auto_ptr<> that encapsulates
23 * an array allocated dynamically via new[].
24 *
25 * Use this where you could not use a std::auto_ptr<> (works
26 * for single elements only) nor a std::vector<> (no guarantees
27 * w.r.t. to internal organization, i.e. no access to mem buffer).
28 */
29
30template<class T>
31class auto_buffer
32{
33private:
34
35    T* buffer;
36
37    /// no copy nor assignment
38
39    auto_buffer(const auto_buffer&);
40    auto_buffer& operator=(const auto_buffer&);
41
42public:
43
44    explicit auto_buffer (size_t size = 0) throw()
45        : buffer (size == 0 ? NULL : new T[size])
46    {
47    }
48
49    ~auto_buffer()
50    {
51        delete[] buffer;
52    }
53
54    operator T*() const throw()
55    {
56        return buffer;
57    }
58
59    operator void*() const throw()
60    {
61        return buffer;
62    }
63
64    operator bool() const throw()
65    {
66        return buffer != NULL;
67    }
68
69    T* operator->() const throw()
70    {
71        return buffer;
72    }
73
74    T *get() const throw()
75    {
76        return buffer;
77    }
78
79    T* release() throw()
80    {
81        T* temp = buffer;
82        buffer = NULL;
83        return temp;
84    }
85
86    void reset (size_t newSize = 0)
87    {
88        delete[] buffer;
89        buffer = (newSize == 0 ? NULL : new T[newSize]);
90    }
91};