MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memory_array.hpp
1 /*
2  Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 
20 /* mySTL memory_arry implements auto_array
21  *
22  */
23 
24 #ifndef mySTL_MEMORY_ARRAY_HPP
25 #define mySTL_MEMORY_ARRAY_HPP
26 
27 
28 #ifdef _MSC_VER
29  // disable operator-> warning for builtins
30  #pragma warning(disable:4284)
31 #endif
32 
33 
34 namespace mySTL {
35 
36 
37 template<typename T>
39  T* ptr_;
40  explicit auto_array_ref(T* p) : ptr_(p) {}
41 };
42 
43 
44 template<typename T>
45 class auto_array {
46  T* ptr_;
47 
48  void Destroy()
49  {
50  #ifdef YASSL_LIB
51  yaSSL::ysArrayDelete(ptr_);
52  #else
53  TaoCrypt::tcArrayDelete(ptr_);
54  #endif
55  }
56 public:
57  explicit auto_array(T* p = 0) : ptr_(p) {}
58 
59  ~auto_array()
60  {
61  Destroy();
62  }
63 
64 
65  auto_array(auto_array& other) : ptr_(other.release()) {}
66 
67  auto_array& operator=(auto_array& that)
68  {
69  if (this != &that) {
70  Destroy();
71  ptr_ = that.release();
72  }
73  return *this;
74  }
75 
76 
77  T* operator->() const
78  {
79  return ptr_;
80  }
81 
82  T& operator*() const
83  {
84  return *ptr_;
85  }
86 
87  T* get() const
88  {
89  return ptr_;
90  }
91 
92  T* release()
93  {
94  T* tmp = ptr_;
95  ptr_ = 0;
96  return tmp;
97  }
98 
99  void reset(T* p = 0)
100  {
101  if (ptr_ != p) {
102  Destroy();
103  ptr_ = p;
104  }
105  }
106 
107  // auto_array_ref conversions
108  auto_array(auto_array_ref<T> ref) : ptr_(ref.ptr_) {}
109 
110  auto_array& operator=(auto_array_ref<T> ref)
111  {
112  if (this->ptr_ != ref.ptr_) {
113  Destroy();
114  ptr_ = ref.ptr_;
115  }
116  return *this;
117  }
118 
119  template<typename T2>
120  operator auto_array<T2>()
121  {
122  return auto_array<T2>(this->release());
123  }
124 
125  template<typename T2>
126  operator auto_array_ref<T2>()
127  {
128  return auto_array_ref<T2>(this->release());
129  }
130 };
131 
132 
133 } // namespace mySTL
134 
135 #endif // mySTL_MEMORY_ARRAY_HPP