Branch data Line data Source code
1 : : // file : xsd/cxx/tree/containers.txx
2 : : // copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
3 : : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 : :
5 : : #include <ostream>
6 : :
7 : : #ifdef XSD_CXX11
8 : : # include <utility> // std::move
9 : : #endif
10 : :
11 : : #include <xsd/cxx/tree/bits/literals.hxx>
12 : :
13 : : namespace xsd
14 : : {
15 : : namespace cxx
16 : : {
17 : : namespace tree
18 : : {
19 : : // one
20 : : //
21 : : template<typename T>
22 : : one<T, false>::
23 : : ~one ()
24 : : {
25 [ + - ][ + - ]: 1 : delete x_;
[ # # # #
# # # # #
# # # ]
26 : 0 : }
27 : :
28 : : template<typename T>
29 : : one<T, false>::
30 : : one (container* c)
31 : 1 : : x_ (0), container_ (c)
32 : : {
33 : : }
34 : :
35 : : template<typename T>
36 : : one<T, false>::
37 : : one (const T& x, container* c)
38 : 0 : : x_ (0), container_ (c)
39 : : {
40 [ # # ][ # # ]: 0 : set (x);
41 : : }
42 : :
43 : : template<typename T>
44 : 0 : one<T, false>::
45 : : one (XSD_AUTO_PTR<T> x, container* c)
46 : 0 : : x_ (0), container_ (c)
47 : : {
48 : : #ifdef XSD_CXX11
49 [ # # ][ # # ]: 0 : set (std::move (x));
50 : : #else
51 : : set (x);
52 : : #endif
53 : 0 : }
54 : :
55 : : template<typename T>
56 : 0 : one<T, false>::
57 : : one (const one<T, false>& x, flags f, container* c)
58 : 0 : : x_ (0), container_ (c)
59 : : {
60 [ # # ][ # # ]: 0 : if (x.present ())
61 : : set (x.get (), f);
62 : 0 : }
63 : :
64 : : template<typename T>
65 : 0 : one<T, false>& one<T, false>::
66 : : operator= (const one<T, false>& x)
67 : : {
68 [ # # ][ # # ]: 0 : if (this == &x)
69 : : return *this;
70 : :
71 [ # # ][ # # ]: 0 : if (x.present ())
72 : 0 : set (x.get ());
73 : : else
74 : : {
75 [ # # ][ # # ]: 0 : delete x_;
76 : 0 : x_ = 0;
77 : : }
78 : :
79 : : return *this;
80 : : }
81 : :
82 : : template<typename T>
83 : : void one<T, false>::
84 : : set (const T& x, flags f)
85 : : {
86 : : // We always do a fresh copy because T may not be x's
87 : : // dynamic type.
88 : : //
89 : 0 : T* r (x._clone (f, container_));
90 : :
91 [ # # ][ # # ]: 0 : delete x_;
[ # # ][ # # ]
92 : 0 : x_ = r;
93 : : }
94 : :
95 : : template<typename T>
96 : 2 : void one<T, false>::
97 : : set (XSD_AUTO_PTR<T> x)
98 : : {
99 : : T* r (0);
100 : :
101 [ + - ][ + - ]: 2 : if (x.get () != 0)
102 : : {
103 [ - + ][ - + ]: 2 : if (x->_container () != container_)
104 : 0 : x->_container (container_);
105 : :
106 : : r = x.release ();
107 : : }
108 : :
109 [ - + ][ - + ]: 2 : delete x_;
110 : 2 : x_ = r;
111 : 2 : }
112 : :
113 : : // optional
114 : : //
115 : : template <typename T>
116 : : optional<T, false>::
117 : : ~optional ()
118 : : {
119 [ + - ][ # # ]: 4 : delete x_;
120 : 0 : }
121 : :
122 : : template <typename T>
123 : : optional<T, false>::
124 : : optional (container* c)
125 : 4 : : x_ (0), container_ (c)
126 : : {
127 : : }
128 : :
129 : : template <typename T>
130 : : optional<T, false>::
131 : : optional (const T& x, container* c)
132 : : : x_ (0), container_ (c)
133 : : {
134 : : set (x);
135 : : }
136 : :
137 : : template <typename T>
138 : : optional<T, false>::
139 : : optional (XSD_AUTO_PTR<T> x, container* c)
140 : : : x_ (0), container_ (c)
141 : : {
142 : : #ifdef XSD_CXX11
143 : : set (std::move (x));
144 : : #else
145 : : set (x);
146 : : #endif
147 : : }
148 : :
149 : : template <typename T>
150 : 0 : optional<T, false>::
151 : : optional (const optional<T, false>& x, flags f, container* c)
152 : 0 : : x_ (0), container_ (c)
153 : : {
154 [ # # ]: 0 : if (x)
155 : : set (*x, f);
156 : 0 : }
157 : :
158 : : template <typename T>
159 : : optional<T, false>& optional<T, false>::
160 : : operator= (const T& x)
161 : : {
162 : : if (x_ == &x)
163 : : return *this;
164 : :
165 : : set (x);
166 : :
167 : : return *this;
168 : : }
169 : :
170 : : template <typename T>
171 : 0 : optional<T, false>& optional<T, false>::
172 : : operator= (const optional<T, false>& x)
173 : : {
174 [ # # ]: 0 : if (this == &x)
175 : : return *this;
176 : :
177 [ # # ]: 0 : if (x)
178 : 0 : set (*x);
179 : : else
180 : : reset ();
181 : :
182 : : return *this;
183 : : }
184 : :
185 : : template <typename T>
186 : : void optional<T, false>::
187 : : set (const T& x, flags f)
188 : : {
189 : : // We always do a fresh copy because T may not be x's
190 : : // dynamic type.
191 : : //
192 : 0 : T* r (x._clone (f, container_));
193 : :
194 [ # # ][ # # ]: 0 : delete x_;
195 : 0 : x_ = r;
196 : : }
197 : :
198 : : template <typename T>
199 : 4 : void optional<T, false>::
200 : : set (XSD_AUTO_PTR<T> x)
201 : : {
202 : : T* r (0);
203 : :
204 [ + - ]: 4 : if (x.get () != 0)
205 : : {
206 [ - + ]: 4 : if (x->_container () != container_)
207 : 0 : x->_container (container_);
208 : :
209 : : r = x.release ();
210 : : }
211 : :
212 [ - + ]: 4 : delete x_;
213 : 4 : x_ = r;
214 : 4 : }
215 : :
216 : : template <typename T>
217 : : void optional<T, false>::
218 : : reset ()
219 : : {
220 [ # # ]: 0 : delete x_;
221 : 0 : x_ = 0;
222 : : }
223 : :
224 : : template <typename T>
225 : 0 : void optional<T, false>::
226 : : true_ ()
227 : : {
228 : 0 : }
229 : :
230 : :
231 : : // optional
232 : : //
233 : : template <typename T>
234 : : optional<T, true>::
235 : : optional (const T& y, container*)
236 : : : present_ (false)
237 : : {
238 : : set (y);
239 : : }
240 : :
241 : : template <typename T>
242 : : optional<T, true>::
243 : : optional (const optional<T, true>& y, flags, container*)
244 : : : present_ (false)
245 : : {
246 : : if (y)
247 : : set (*y);
248 : : }
249 : :
250 : : template <typename T>
251 : : optional<T, true>& optional<T, true>::
252 : : operator= (const T& y)
253 : : {
254 : : if (&x_ == &y)
255 : : return *this;
256 : :
257 : : set (y);
258 : :
259 : : return *this;
260 : : }
261 : :
262 : : template <typename T>
263 : : optional<T, true>& optional<T, true>::
264 : : operator= (const optional<T, true>& y)
265 : : {
266 : : if (this == &y)
267 : : return *this;
268 : :
269 : : if (y)
270 : : set (*y);
271 : : else
272 : : reset ();
273 : :
274 : : return *this;
275 : : }
276 : :
277 : : template <typename T>
278 : : void optional<T, true>::
279 : : true_ ()
280 : : {
281 : : }
282 : :
283 : : template <typename C, typename T, bool fund>
284 : : std::basic_ostream<C>&
285 : : operator<< (std::basic_ostream<C>& os, const optional<T, fund>& x)
286 : : {
287 : : if (x)
288 : : os << *x;
289 : : else
290 : : os << bits::not_present<C> ();
291 : :
292 : : return os;
293 : : }
294 : : }
295 : : }
296 : : }
|