Line data Source code
1 : // file : xsd/cxx/xml/dom/auto-ptr.hxx
2 : // copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
3 : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 :
5 : #ifndef XSD_CXX_XML_DOM_AUTO_PTR_HXX
6 : #define XSD_CXX_XML_DOM_AUTO_PTR_HXX
7 :
8 : #include <xsd/cxx/config.hxx> // XSD_CXX11_*
9 :
10 : #ifdef XSD_CXX11
11 : # include <memory> // std::unique_ptr
12 : # include <utility> // std::move
13 : # include <type_traits> // std::remove_const
14 : #endif
15 :
16 : namespace xsd
17 : {
18 : namespace cxx
19 : {
20 : namespace xml
21 : {
22 : namespace dom
23 : {
24 : #ifdef XSD_CXX11
25 : template <typename T>
26 : struct deleter
27 : {
28 : void
29 0 : operator() (T* p) const
30 : {
31 0 : if (p != 0)
32 0 : const_cast<typename std::remove_const<T>::type*> (p)->release ();
33 0 : }
34 : };
35 :
36 : #ifdef XSD_CXX11_TEMPLATE_ALIAS
37 : template <typename T>
38 : using unique_ptr = std::unique_ptr<T, deleter<T>>;
39 : #else
40 : template <typename T>
41 : class unique_ptr: public std::unique_ptr<T, deleter<T>>
42 : {
43 : public:
44 : typedef std::unique_ptr<T, deleter<T>> base;
45 :
46 : typedef typename base::pointer pointer;
47 : typedef T element_type;
48 : typedef deleter<T> deleter_type;
49 :
50 : unique_ptr (): base () {}
51 : explicit unique_ptr (pointer p): base (p) {}
52 : unique_ptr (pointer p, const deleter_type& d): base (p, d) {}
53 : unique_ptr (pointer p, deleter_type&& d): base (p, std::move (d)) {}
54 : unique_ptr (unique_ptr&& p): base (std::move (p)) {}
55 : template <class T1>
56 : unique_ptr (unique_ptr<T1>&& p): base (std::move (p)) {}
57 : template <class T1>
58 : unique_ptr (std::auto_ptr<T1>&& p): base (std::move (p)) {}
59 :
60 : unique_ptr& operator= (unique_ptr&& p)
61 : {
62 : static_cast<base&> (*this) = std::move (p);
63 : return *this;
64 : }
65 :
66 : template <class T1>
67 : unique_ptr& operator= (unique_ptr<T1>&& p)
68 : {
69 : static_cast<base&> (*this) = std::move (p);
70 : return *this;
71 : }
72 :
73 : #ifdef XSD_CXX11_NULLPTR
74 : unique_ptr (std::nullptr_t p): base (p) {}
75 :
76 : unique_ptr& operator= (std::nullptr_t p)
77 : {
78 : static_cast<base&> (*this) = p;
79 : return *this;
80 : }
81 : #endif
82 : };
83 : #endif // XSD_CXX11_TEMPLATE_ALIAS
84 :
85 : #define XSD_DOM_AUTO_PTR xsd::cxx::xml::dom::unique_ptr
86 :
87 : #else
88 : // Simple auto_ptr version for C++98 that calls release() instead
89 : // of delete.
90 : //
91 : template <typename T>
92 : struct remove_c
93 : {
94 : typedef T r;
95 : };
96 :
97 : template <typename T>
98 : struct remove_c<const T>
99 : {
100 : typedef T r;
101 : };
102 :
103 : template <typename T>
104 : struct auto_ptr_ref
105 : {
106 : T* x_;
107 :
108 : explicit
109 : auto_ptr_ref (T* x)
110 : : x_ (x)
111 : {
112 : }
113 : };
114 :
115 : template <typename T>
116 : struct auto_ptr
117 : {
118 : ~auto_ptr ()
119 : {
120 : reset ();
121 : }
122 :
123 : explicit
124 : auto_ptr (T* x = 0)
125 : : x_ (x)
126 : {
127 : }
128 :
129 : auto_ptr (auto_ptr& y)
130 : : x_ (y.release ())
131 : {
132 : }
133 :
134 : template <typename T2>
135 : auto_ptr (auto_ptr<T2>& y)
136 : : x_ (y.release ())
137 : {
138 : }
139 :
140 : auto_ptr (auto_ptr_ref<T> r)
141 : : x_ (r.x_)
142 : {
143 : }
144 :
145 : auto_ptr&
146 : operator= (auto_ptr& y)
147 : {
148 : if (x_ != y.x_)
149 : reset (y.release ());
150 :
151 : return *this;
152 : }
153 :
154 : template <typename T2>
155 : auto_ptr&
156 : operator= (auto_ptr<T2>& y)
157 : {
158 : if (x_ != y.x_)
159 : reset (y.release ());
160 :
161 : return *this;
162 : }
163 :
164 : auto_ptr&
165 : operator= (auto_ptr_ref<T> r)
166 : {
167 : if (r.x_ != x_)
168 : reset (r.x_);
169 :
170 : return *this;
171 : }
172 :
173 : template <typename T2>
174 : operator auto_ptr_ref<T2> ()
175 : {
176 : return auto_ptr_ref<T2> (release ());
177 : }
178 :
179 : template <typename T2>
180 : operator auto_ptr<T2> ()
181 : {
182 : return auto_ptr<T2> (release ());
183 : }
184 :
185 : public:
186 : T&
187 : operator* () const
188 : {
189 : return *x_;
190 : }
191 :
192 : T*
193 : operator-> () const
194 : {
195 : return x_;
196 : }
197 :
198 : T*
199 : get () const
200 : {
201 : return x_;
202 : }
203 :
204 : T*
205 : release ()
206 : {
207 : T* x (x_);
208 : x_ = 0;
209 : return x;
210 : }
211 :
212 : void
213 : reset (T* x = 0)
214 : {
215 : if (x_)
216 : const_cast<typename remove_c<T>::r*> (x_)->release ();
217 :
218 : x_ = x;
219 : }
220 :
221 : private:
222 : T* x_;
223 : };
224 :
225 : #define XSD_DOM_AUTO_PTR xsd::cxx::xml::dom::auto_ptr
226 :
227 : #endif // XSD_CXX11
228 : }
229 : }
230 : }
231 : }
232 :
233 : #endif // XSD_CXX_XML_DOM_AUTO_PTR_HXX
|