Ice 3.7 C++11 API Reference
Loading...
Searching...
No Matches
ScopedArray.h
Go to the documentation of this file.
1//
2// Copyright (c) ZeroC, Inc. All rights reserved.
3//
4
5#ifndef ICE_UTIL_SCOPED_ARRAY_H
6#define ICE_UTIL_SCOPED_ARRAY_H
7
8#include <IceUtil/Config.h>
9
10#ifndef ICE_CPP11_MAPPING
11
12//
13// Only for the C++98 mapping
14//
15
16namespace IceUtil
17{
18
19template<typename T>
20class ScopedArray
21{
22public:
23
24 explicit ScopedArray(T* ptr = 0) :
25 _ptr(ptr)
26 {
27 }
28
29 ScopedArray(const ScopedArray& other)
30 {
31 _ptr = other._ptr;
32 const_cast<ScopedArray&>(other)._ptr = 0;
33 }
34
35 ~ScopedArray()
36 {
37 if(_ptr != 0)
38 {
39 delete[] _ptr;
40 }
41 }
42
43 void reset(T* ptr = 0)
44 {
45 assert(ptr == 0 || ptr != _ptr);
46 if(_ptr != 0)
47 {
48 delete[] _ptr;
49 }
50 _ptr = ptr;
51 }
52
53 ScopedArray& operator=(const ScopedArray& other)
54 {
55 if(_ptr != 0)
56 {
57 delete[] _ptr;
58 }
59 _ptr = other._ptr;
60 const_cast<ScopedArray&>(other)._ptr = 0;
61 return *this;
62 }
63
64 T& operator[](size_t i) const
65 {
66 assert(_ptr != 0);
67 assert(i >= 0);
68 return _ptr[i];
69 }
70
71 T* get() const
72 {
73 return _ptr;
74 }
75
76 void swap(ScopedArray& a)
77 {
78 T* tmp = a._ptr;
79 a._ptr = _ptr;
80 _ptr = tmp;
81 }
82
83 T* release()
84 {
85 T* tmp = _ptr;
86 _ptr = 0;
87 return tmp;
88 }
89
90private:
91
92 T* _ptr;
93};
94
95} // End of namespace IceUtil
96
97#endif
98#endif
Definition Optional.h:1095