summaryrefslogtreecommitdiff
path: root/mysql/extra/yassl/taocrypt/mySTL/memory.hpp
blob: 7e709be3083d0a451477007e90a5b77e9323d8be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
   Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING. If not, write to the
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   MA  02110-1301  USA.
*/


/* mySTL memory implements auto_ptr
 *
 */

#ifndef mySTL_MEMORY_HPP
#define mySTL_MEMORY_HPP

#include "memory_array.hpp"   // for auto_array

#ifdef _MSC_VER
    // disable operator-> warning for builtins
    #pragma warning(disable:4284)
#endif


namespace mySTL {


template<typename T>
struct auto_ptr_ref {
    T* ptr_;
    explicit auto_ptr_ref(T* p) : ptr_(p) {}
};


template<typename T>
class auto_ptr {
    T*       ptr_;

    void Destroy()
    {
        #ifdef YASSL_LIB
            yaSSL::ysDelete(ptr_);
        #else
            TaoCrypt::tcDelete(ptr_);
        #endif
    }
public:
    explicit auto_ptr(T* p = 0) : ptr_(p) {}

    ~auto_ptr() 
    {
        Destroy();
    }


    auto_ptr(auto_ptr& other) : ptr_(other.release()) {}

    auto_ptr& operator=(auto_ptr& that)
    {
        if (this != &that) {
            Destroy();
            ptr_ = that.release();
        }
        return *this;
    }


    T* operator->() const
    {
        return ptr_;
    }

    T& operator*() const
    {
        return *ptr_;
    }

    T* get() const 
    { 
        return ptr_; 
    }

    T* release()
    {
        T* tmp = ptr_;
        ptr_ = 0;
        return tmp;
    }

    void reset(T* p = 0)
    {
        if (ptr_ != p) {
            Destroy();
            ptr_ = p;
        }
    }

    // auto_ptr_ref conversions
    auto_ptr(auto_ptr_ref<T> ref) : ptr_(ref.ptr_) {}

    auto_ptr& operator=(auto_ptr_ref<T> ref)
    {
        if (this->ptr_ != ref.ptr_) {
            Destroy();
            ptr_ = ref.ptr_;
        }
        return *this;
    }

    template<typename T2>
    operator auto_ptr<T2>()
    {
        return auto_ptr<T2>(this->release());
    }

    template<typename T2>
    operator auto_ptr_ref<T2>()
    {
        return auto_ptr_ref<T2>(this->release());
    }
};


} // namespace mySTL

#endif // mySTL_MEMORY_HPP