MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memory.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 implements auto_ptr
21  *
22  */
23 
24 #ifndef mySTL_MEMORY_HPP
25 #define mySTL_MEMORY_HPP
26 
27 #include "memory_array.hpp" // for auto_array
28 
29 #ifdef _MSC_VER
30  // disable operator-> warning for builtins
31  #pragma warning(disable:4284)
32 #endif
33 
34 
35 namespace mySTL {
36 
37 
38 template<typename T>
39 struct auto_ptr_ref {
40  T* ptr_;
41  explicit auto_ptr_ref(T* p) : ptr_(p) {}
42 };
43 
44 
45 template<typename T>
46 class auto_ptr {
47  T* ptr_;
48 
49  void Destroy()
50  {
51  #ifdef YASSL_LIB
52  yaSSL::ysDelete(ptr_);
53  #else
54  TaoCrypt::tcDelete(ptr_);
55  #endif
56  }
57 public:
58  explicit auto_ptr(T* p = 0) : ptr_(p) {}
59 
60  ~auto_ptr()
61  {
62  Destroy();
63  }
64 
65 
66  auto_ptr(auto_ptr& other) : ptr_(other.release()) {}
67 
68  auto_ptr& operator=(auto_ptr& that)
69  {
70  if (this != &that) {
71  Destroy();
72  ptr_ = that.release();
73  }
74  return *this;
75  }
76 
77 
78  T* operator->() const
79  {
80  return ptr_;
81  }
82 
83  T& operator*() const
84  {
85  return *ptr_;
86  }
87 
88  T* get() const
89  {
90  return ptr_;
91  }
92 
93  T* release()
94  {
95  T* tmp = ptr_;
96  ptr_ = 0;
97  return tmp;
98  }
99 
100  void reset(T* p = 0)
101  {
102  if (ptr_ != p) {
103  Destroy();
104  ptr_ = p;
105  }
106  }
107 
108  // auto_ptr_ref conversions
109  auto_ptr(auto_ptr_ref<T> ref) : ptr_(ref.ptr_) {}
110 
111  auto_ptr& operator=(auto_ptr_ref<T> ref)
112  {
113  if (this->ptr_ != ref.ptr_) {
114  Destroy();
115  ptr_ = ref.ptr_;
116  }
117  return *this;
118  }
119 
120  template<typename T2>
121  operator auto_ptr<T2>()
122  {
123  return auto_ptr<T2>(this->release());
124  }
125 
126  template<typename T2>
127  operator auto_ptr_ref<T2>()
128  {
129  return auto_ptr_ref<T2>(this->release());
130  }
131 };
132 
133 
134 } // namespace mySTL
135 
136 #endif // mySTL_MEMORY_HPP