1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | template <class T> |
6 | class ArrayHolder |
7 | { |
8 | public: |
9 | ArrayHolder(T *ptr) |
10 | : m_ptr(ptr) |
11 | { |
12 | } |
13 | |
14 | ~ArrayHolder() |
15 | { |
16 | Clear(); |
17 | } |
18 | |
19 | ArrayHolder(const ArrayHolder &rhs) |
20 | { |
21 | m_ptr = const_cast<ArrayHolder *>(&rhs)->Detach(); |
22 | } |
23 | |
24 | ArrayHolder &operator=(T *ptr) |
25 | { |
26 | Clear(); |
27 | m_ptr = ptr; |
28 | return *this; |
29 | } |
30 | |
31 | const T &operator[](int i) const |
32 | { |
33 | return m_ptr[i]; |
34 | } |
35 | |
36 | T &operator[](int i) |
37 | { |
38 | return m_ptr[i]; |
39 | } |
40 | |
41 | operator const T *() const |
42 | { |
43 | return m_ptr; |
44 | } |
45 | |
46 | operator T *() |
47 | { |
48 | return m_ptr; |
49 | } |
50 | |
51 | T **operator&() |
52 | { |
53 | return &m_ptr; |
54 | } |
55 | |
56 | T *GetPtr() |
57 | { |
58 | return m_ptr; |
59 | } |
60 | |
61 | T *Detach() |
62 | { |
63 | T *ret = m_ptr; |
64 | m_ptr = NULL; |
65 | return ret; |
66 | } |
67 | |
68 | private: |
69 | void Clear() |
70 | { |
71 | if (m_ptr) |
72 | { |
73 | delete [] m_ptr; |
74 | m_ptr = NULL; |
75 | } |
76 | } |
77 | |
78 | private: |
79 | T *m_ptr; |
80 | }; |
81 | |