1 | // Copyright (c) 2015, Just Software Solutions Ltd |
2 | // All rights reserved. |
3 | // |
4 | // Redistribution and use in source and binary forms, with or |
5 | // without modification, are permitted provided that the |
6 | // following conditions are met: |
7 | // |
8 | // 1. Redistributions of source code must retain the above |
9 | // copyright notice, this list of conditions and the following |
10 | // disclaimer. |
11 | // |
12 | // 2. Redistributions in binary form must reproduce the above |
13 | // copyright notice, this list of conditions and the following |
14 | // disclaimer in the documentation and/or other materials |
15 | // provided with the distribution. |
16 | // |
17 | // 3. Neither the name of the copyright holder nor the names of |
18 | // its contributors may be used to endorse or promote products |
19 | // derived from this software without specific prior written |
20 | // permission. |
21 | // |
22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
23 | // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
24 | // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
25 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
27 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
29 | // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
31 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
32 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
33 | // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
34 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
35 | |
36 | // Copied from https://bitbucket.org/anthonyw/variant/src (5bce47fa788648f79e5ea1d77b0eef2e8f0b2999) |
37 | |
38 | // Modified to make it compile with exceptions disabled. |
39 | |
40 | #pragma once |
41 | |
42 | #include <functional> |
43 | #include <limits.h> |
44 | #include <new> |
45 | #include <stddef.h> |
46 | #include <stdexcept> |
47 | #include <string> |
48 | #include <type_traits> |
49 | #include <utility> |
50 | #include <wtf/Compiler.h> |
51 | #include <wtf/StdLibExtras.h> |
52 | |
53 | #if COMPILER(MSVC) |
54 | #pragma warning(push) |
55 | #pragma warning(disable:4245) |
56 | #pragma warning(disable:4521) |
57 | #pragma warning(disable:4522) |
58 | #pragma warning(disable:4814) |
59 | #endif |
60 | |
61 | #if !COMPILER(CLANG) || WTF_CPP_STD_VER >= 14 |
62 | |
63 | namespace WTF { |
64 | |
65 | #if COMPILER_SUPPORTS(EXCEPTIONS) |
66 | #define __THROW_EXCEPTION(__exception) throw __exception; |
67 | #define __NOEXCEPT noexcept |
68 | #define __NOEXCEPT_(__exception) noexcept(__exception) |
69 | #else |
70 | #define __THROW_EXCEPTION(__exception) do { (void)__exception; CRASH(); } while (0); |
71 | #define __NOEXCEPT |
72 | #define __NOEXCEPT_(...) |
73 | #endif |
74 | |
75 | struct __in_place_private{ |
76 | template<typename> |
77 | struct __type_holder; |
78 | |
79 | template<size_t> |
80 | struct __value_holder; |
81 | }; |
82 | |
83 | |
84 | struct in_place_tag { |
85 | in_place_tag() = delete; |
86 | }; |
87 | |
88 | using in_place_t = in_place_tag(&)(__in_place_private&); |
89 | |
90 | template <class _Type> |
91 | using in_place_type_t = in_place_tag(&)(__in_place_private::__type_holder<_Type>&); |
92 | |
93 | template <size_t _Index> |
94 | using in_place_index_t = in_place_tag(&)(__in_place_private::__value_holder<_Index>&); |
95 | |
96 | in_place_tag in_place(__in_place_private&); |
97 | |
98 | template <class _Type> |
99 | in_place_tag in_place(__in_place_private::__type_holder<_Type> &) { |
100 | __THROW_EXCEPTION(__in_place_private()); |
101 | } |
102 | |
103 | template <size_t _Index> |
104 | in_place_tag in_place(__in_place_private::__value_holder<_Index> &) { |
105 | __THROW_EXCEPTION(__in_place_private()); |
106 | } |
107 | |
108 | class bad_variant_access: public std::logic_error{ |
109 | public: |
110 | explicit bad_variant_access(const std::string& what_arg): |
111 | std::logic_error(what_arg) |
112 | {} |
113 | explicit bad_variant_access(const char* what_arg): |
114 | std::logic_error(what_arg) |
115 | {} |
116 | }; |
117 | |
118 | template<typename T> |
119 | NO_RETURN_DUE_TO_CRASH inline T __throw_bad_variant_access(const char* what_arg){ |
120 | __THROW_EXCEPTION(bad_variant_access(what_arg)) |
121 | } |
122 | |
123 | template<ptrdiff_t _Offset,typename _Type,typename ... _Types> |
124 | struct __type_index_helper; |
125 | |
126 | template<ptrdiff_t _Offset,typename _Type,typename _Head,typename ... _Rest> |
127 | struct __type_index_helper<_Offset,_Type,_Head,_Rest...>{ |
128 | static constexpr ptrdiff_t __value= |
129 | __type_index_helper<_Offset+1,_Type,_Rest...>::__value; |
130 | }; |
131 | |
132 | template<ptrdiff_t _Offset,typename _Type,typename ... _Rest> |
133 | struct __type_index_helper<_Offset,_Type,_Type,_Rest...>{ |
134 | static constexpr ptrdiff_t __value=_Offset; |
135 | }; |
136 | |
137 | template<typename _Type,typename ... _Types> |
138 | struct __type_index{ |
139 | static constexpr ptrdiff_t __value= |
140 | __type_index_helper<0,_Type,_Types...>::__value; |
141 | }; |
142 | |
143 | template<ptrdiff_t _Index,typename ... _Types> |
144 | struct __indexed_type; |
145 | |
146 | template<typename _Head,typename ... _Rest> |
147 | struct __indexed_type<0,_Head,_Rest...>{ |
148 | typedef _Head __type; |
149 | }; |
150 | |
151 | template<typename _Head,typename ... _Rest> |
152 | struct __indexed_type<-1,_Head,_Rest...>{ |
153 | typedef void __type; |
154 | }; |
155 | |
156 | template<ptrdiff_t _Index,typename _Head,typename ... _Rest> |
157 | struct __indexed_type<_Index,_Head,_Rest...>{ |
158 | typedef typename __indexed_type<_Index-1,_Rest...>::__type __type; |
159 | }; |
160 | |
161 | template<ptrdiff_t _Index,typename ..._Types> |
162 | struct __next_index{ |
163 | static constexpr ptrdiff_t __value= |
164 | (_Index>=ptrdiff_t(sizeof...(_Types)-1))?-1:_Index+1; |
165 | }; |
166 | |
167 | template<typename ... _Types> |
168 | class Variant; |
169 | |
170 | template<typename> |
171 | struct variant_size; |
172 | |
173 | template <typename _Type> |
174 | struct variant_size<const _Type> : variant_size<_Type> {}; |
175 | |
176 | template <typename _Type> |
177 | struct variant_size<volatile _Type> : variant_size<_Type> {}; |
178 | |
179 | template <typename _Type> |
180 | struct variant_size<const volatile _Type> : variant_size<_Type> {}; |
181 | |
182 | template <typename... _Types> |
183 | struct variant_size<Variant<_Types...>> |
184 | : std::integral_constant<size_t, sizeof...(_Types)> {}; |
185 | |
186 | template<size_t _Index,typename _Type> |
187 | struct variant_alternative; |
188 | |
189 | template<size_t _Index,typename _Type> |
190 | using variant_alternative_t=typename variant_alternative<_Index,_Type>::type; |
191 | |
192 | template <size_t _Index, typename _Type> |
193 | struct variant_alternative<_Index, const _Type>{ |
194 | using type=std::add_const_t<variant_alternative_t<_Index,_Type>>; |
195 | }; |
196 | |
197 | template <size_t _Index, typename _Type> |
198 | struct variant_alternative<_Index, volatile _Type>{ |
199 | using type=std::add_volatile_t<variant_alternative_t<_Index,_Type>>; |
200 | }; |
201 | |
202 | template <size_t _Index, typename _Type> |
203 | struct variant_alternative<_Index, volatile const _Type>{ |
204 | using type=std::add_volatile_t<std::add_const_t<variant_alternative_t<_Index,_Type>>>; |
205 | }; |
206 | |
207 | template<size_t _Index,typename ... _Types> |
208 | struct variant_alternative<_Index,Variant<_Types...>>{ |
209 | using type=typename __indexed_type<_Index,_Types...>::__type; |
210 | }; |
211 | |
212 | constexpr size_t variant_npos=-1; |
213 | |
214 | template<typename _Type,typename ... _Types> |
215 | constexpr _Type& get(Variant<_Types...>&); |
216 | |
217 | template<typename _Type,typename ... _Types> |
218 | constexpr _Type const& get(Variant<_Types...> const&); |
219 | |
220 | template<typename _Type,typename ... _Types> |
221 | constexpr _Type&& get(Variant<_Types...>&&); |
222 | |
223 | template<typename _Type,typename ... _Types> |
224 | constexpr const _Type&& get(Variant<_Types...> const&&); |
225 | |
226 | template<ptrdiff_t _Index,typename ... _Types> |
227 | constexpr typename __indexed_type<_Index,_Types...>::__type& get(Variant<_Types...>&); |
228 | |
229 | template<ptrdiff_t _Index,typename ... _Types> |
230 | constexpr typename __indexed_type<_Index,_Types...>::__type&& get(Variant<_Types...>&&); |
231 | |
232 | template<ptrdiff_t _Index,typename ... _Types> |
233 | constexpr typename __indexed_type<_Index,_Types...>::__type const& get( |
234 | Variant<_Types...> const&); |
235 | |
236 | template <ptrdiff_t _Index, typename... _Types> |
237 | constexpr const typename __indexed_type<_Index, _Types...>::__type && |
238 | get(Variant<_Types...> const &&); |
239 | |
240 | template<typename _Type,typename ... _Types> |
241 | constexpr std::add_pointer_t<_Type> get_if(Variant<_Types...>&); |
242 | |
243 | template<typename _Type,typename ... _Types> |
244 | constexpr std::add_pointer_t<_Type const> get_if(Variant<_Types...> const&); |
245 | |
246 | template<ptrdiff_t _Index,typename ... _Types> |
247 | constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type> get_if(Variant<_Types...>&); |
248 | |
249 | template<ptrdiff_t _Index,typename ... _Types> |
250 | constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type const> get_if( |
251 | Variant<_Types...> const&); |
252 | |
253 | template<ptrdiff_t _Index,typename ... _Types> |
254 | struct __variant_accessor; |
255 | |
256 | template<size_t __count, |
257 | bool __larger_than_char=(__count>SCHAR_MAX), |
258 | bool __larger_than_short=(__count>SHRT_MAX), |
259 | bool __larger_than_int=(__count>INT_MAX)> |
260 | struct __discriminator_type{ |
261 | typedef signed char __type; |
262 | }; |
263 | |
264 | template<size_t __count> |
265 | struct __discriminator_type<__count,true,false,false>{ |
266 | typedef signed short __type; |
267 | }; |
268 | |
269 | template<size_t __count> |
270 | struct __discriminator_type<__count,true,true,false>{ |
271 | typedef int __type; |
272 | }; |
273 | template<size_t __count> |
274 | struct __discriminator_type<__count,true,true,true>{ |
275 | typedef signed long __type; |
276 | }; |
277 | |
278 | template<typename _Type> |
279 | struct __stored_type{ |
280 | typedef _Type __type; |
281 | }; |
282 | |
283 | template<typename _Type> |
284 | struct __stored_type<_Type&>{ |
285 | typedef _Type* __type; |
286 | }; |
287 | |
288 | template<typename ... _Types> |
289 | struct __all_trivially_destructible; |
290 | |
291 | template<> |
292 | struct __all_trivially_destructible<> { |
293 | static constexpr bool __value=true; |
294 | }; |
295 | |
296 | template<typename _Type> |
297 | struct __all_trivially_destructible<_Type> { |
298 | static constexpr bool __value= |
299 | std::is_trivially_destructible<typename __stored_type<_Type>::__type>::value; |
300 | }; |
301 | |
302 | template<typename _Head,typename ... _Rest> |
303 | struct __all_trivially_destructible<_Head,_Rest...> { |
304 | static constexpr bool __value= |
305 | __all_trivially_destructible<_Head>::__value && |
306 | __all_trivially_destructible<_Rest...>::__value; |
307 | }; |
308 | |
309 | template<typename _Target,typename ... _Args> |
310 | struct __storage_nothrow_constructible{ |
311 | static const bool __value= |
312 | std::is_nothrow_constructible<_Target, _Args...>::value; |
313 | }; |
314 | |
315 | template<typename ... _Types> |
316 | struct __storage_nothrow_move_constructible; |
317 | |
318 | template<> |
319 | struct __storage_nothrow_move_constructible<> { |
320 | static constexpr bool __value=true; |
321 | }; |
322 | |
323 | template<typename _Type> |
324 | struct __storage_nothrow_move_constructible<_Type> { |
325 | static constexpr bool __value= |
326 | std::is_nothrow_move_constructible< |
327 | typename __stored_type<_Type>::__type>::value; |
328 | }; |
329 | |
330 | template<typename _Head,typename ... _Rest> |
331 | struct __storage_nothrow_move_constructible<_Head,_Rest...> { |
332 | static constexpr bool __value= |
333 | __storage_nothrow_move_constructible<_Head>::__value && |
334 | __storage_nothrow_move_constructible<_Rest...>::__value; |
335 | }; |
336 | |
337 | template<ptrdiff_t _Index,typename ... _Types> |
338 | struct __other_storage_nothrow_move_constructible; |
339 | |
340 | template<typename _Head,typename ... _Rest> |
341 | struct __other_storage_nothrow_move_constructible<0,_Head,_Rest...>{ |
342 | static const bool __value=__storage_nothrow_move_constructible<_Rest...>::__value; |
343 | }; |
344 | |
345 | template<typename _Head,typename ... _Rest> |
346 | struct __other_storage_nothrow_move_constructible<-1,_Head,_Rest...>{ |
347 | static const bool __value= |
348 | __storage_nothrow_move_constructible<_Head,_Rest...>::__value; |
349 | }; |
350 | |
351 | template<ptrdiff_t _Index,typename _Head,typename ... _Rest> |
352 | struct __other_storage_nothrow_move_constructible<_Index,_Head,_Rest...>{ |
353 | static const bool __value= |
354 | __storage_nothrow_move_constructible<_Head>::__value && |
355 | __other_storage_nothrow_move_constructible<_Index-1,_Rest...>::__value; |
356 | }; |
357 | |
358 | template<ptrdiff_t _Index,typename ... _Types> |
359 | struct __backup_storage_required{ |
360 | static const bool __value= |
361 | !__storage_nothrow_move_constructible< |
362 | typename __indexed_type<_Index,_Types...>::__type>::__value && |
363 | !__other_storage_nothrow_move_constructible<_Index,_Types...>::__value; |
364 | }; |
365 | |
366 | template<ptrdiff_t _Index,ptrdiff_t _Count,typename ... _Types> |
367 | struct __any_backup_storage_required_impl{ |
368 | static const bool __value= |
369 | __backup_storage_required<_Index,_Types...>::__value || |
370 | __any_backup_storage_required_impl<_Index+1,_Count-1,_Types...>::__value; |
371 | }; |
372 | |
373 | template<ptrdiff_t _Index,typename ... _Types> |
374 | struct __any_backup_storage_required_impl<_Index,0,_Types...>{ |
375 | static const bool __value=false; |
376 | }; |
377 | |
378 | template<typename _Variant> |
379 | struct __any_backup_storage_required; |
380 | |
381 | template<typename ... _Types> |
382 | struct __any_backup_storage_required<Variant<_Types...> >{ |
383 | static const bool __value= |
384 | __any_backup_storage_required_impl<0,sizeof...(_Types),_Types...>::__value; |
385 | }; |
386 | |
387 | template<typename ... _Types> |
388 | union __variant_data; |
389 | |
390 | template<typename _Type,bool=std::is_literal_type<_Type>::value> |
391 | struct __variant_storage{ |
392 | typedef _Type __type; |
393 | |
394 | static constexpr _Type& __get(__type& __val){ |
395 | return __val; |
396 | } |
397 | static constexpr _Type&& __get_rref(__type& __val){ |
398 | return std::move(__val); |
399 | } |
400 | static constexpr const _Type& __get(__type const& __val){ |
401 | return __val; |
402 | } |
403 | static constexpr const _Type&& __get_rref(__type const& __val){ |
404 | return std::move(__val); |
405 | } |
406 | static void __destroy(__type&){} |
407 | }; |
408 | |
409 | template<typename _Type> |
410 | struct __storage_wrapper{ |
411 | typename std::aligned_storage<sizeof(_Type),alignof(_Type)>::type __storage; |
412 | |
413 | template<typename ... _Args> |
414 | static constexpr void __construct(void* __p,_Args&& ... __args){ |
415 | new (__p) _Type(std::forward<_Args>(__args)...); |
416 | } |
417 | |
418 | template <typename _Dummy = _Type> |
419 | __storage_wrapper( |
420 | typename std::enable_if<std::is_default_constructible<_Dummy>::value, |
421 | void (__storage_wrapper::*)()>::type = nullptr) { |
422 | __construct(&__storage); |
423 | } |
424 | |
425 | template <typename _Dummy = _Type> |
426 | __storage_wrapper( |
427 | typename std::enable_if<!std::is_default_constructible<_Dummy>::value, |
428 | void (__storage_wrapper::*)()>::type = nullptr) { |
429 | } |
430 | |
431 | template<typename _First,typename ... _Args> |
432 | __storage_wrapper(_First&& __first,_Args&& ... __args){ |
433 | __construct(&__storage,std::forward<_First>(__first),std::forward<_Args>(__args)...); |
434 | } |
435 | |
436 | _Type& __get(){ |
437 | return *static_cast<_Type*>(static_cast<void*>(&__storage)); |
438 | } |
439 | constexpr _Type const& __get() const{ |
440 | return *static_cast<_Type const*>(static_cast<void const*>(&__storage)); |
441 | } |
442 | void __destroy(){ |
443 | __get().~_Type(); |
444 | } |
445 | }; |
446 | |
447 | template<typename _Type> |
448 | struct __storage_wrapper<_Type&>{ |
449 | _Type* __storage; |
450 | |
451 | template<typename _Arg> |
452 | constexpr __storage_wrapper(_Arg& __arg): |
453 | __storage(&__arg){} |
454 | |
455 | _Type& __get(){ |
456 | return *__storage; |
457 | } |
458 | constexpr _Type const& __get() const{ |
459 | return *__storage; |
460 | } |
461 | }; |
462 | |
463 | template<typename _Type> |
464 | struct __variant_storage<_Type,false>{ |
465 | typedef __storage_wrapper<_Type> __type; |
466 | |
467 | static constexpr _Type& __get(__type& __val){ |
468 | return __val.__get(); |
469 | } |
470 | static constexpr _Type&& __get_rref(__type& __val){ |
471 | return std::move(__val.__get()); |
472 | } |
473 | static constexpr const _Type& __get(__type const& __val){ |
474 | return __val.__get(); |
475 | } |
476 | static constexpr const _Type&& __get_rref(__type const& __val){ |
477 | return std::move(__val.__get()); |
478 | } |
479 | static void __destroy(__type& __val){ |
480 | __val.__destroy(); |
481 | } |
482 | }; |
483 | |
484 | template<typename _Type,bool __b> |
485 | struct __variant_storage<_Type&,__b>{ |
486 | typedef _Type* __type; |
487 | |
488 | static constexpr _Type& __get(__type& __val){ |
489 | return *__val; |
490 | } |
491 | static constexpr _Type& __get_rref(__type& __val){ |
492 | return *__val; |
493 | } |
494 | static constexpr _Type& __get(__type const& __val){ |
495 | return *__val; |
496 | } |
497 | static constexpr _Type& __get_rref(__type const& __val){ |
498 | return *__val; |
499 | } |
500 | static void __destroy(__type&){} |
501 | }; |
502 | |
503 | template<typename _Type,bool __b> |
504 | struct __variant_storage<_Type&&,__b>{ |
505 | typedef _Type* __type; |
506 | |
507 | static constexpr _Type&& __get(__type& __val){ |
508 | return static_cast<_Type&&>(*__val); |
509 | } |
510 | static constexpr _Type&& __get_rref(__type& __val){ |
511 | return static_cast<_Type&&>(*__val); |
512 | } |
513 | static constexpr _Type&& __get(__type const& __val){ |
514 | return static_cast<_Type&&>(*__val); |
515 | } |
516 | static constexpr _Type&& __get_rref(__type const& __val){ |
517 | return static_cast<_Type&&>(*__val); |
518 | } |
519 | static void __destroy(__type&){} |
520 | }; |
521 | |
522 | template<> |
523 | union __variant_data<>{ |
524 | constexpr __variant_data(){} |
525 | }; |
526 | |
527 | template<typename _Type> |
528 | union __variant_data<_Type>{ |
529 | typename __variant_storage<_Type>::__type __val; |
530 | struct __dummy_type{} __dummy; |
531 | |
532 | constexpr __variant_data():__dummy(){} |
533 | |
534 | template<typename ... _Args> |
535 | constexpr __variant_data(in_place_index_t<0>,_Args&& ... __args): |
536 | __val(std::forward<_Args>(__args)...){} |
537 | |
538 | _Type& __get(in_place_index_t<0>){ |
539 | return __variant_storage<_Type>::__get(__val); |
540 | } |
541 | /*constexpr*/ _Type&& __get_rref(in_place_index_t<0>){ |
542 | return __variant_storage<_Type>::__get_rref(__val); |
543 | } |
544 | constexpr const _Type& __get(in_place_index_t<0>) const{ |
545 | return __variant_storage<_Type>::__get(__val); |
546 | } |
547 | constexpr const _Type&& __get_rref(in_place_index_t<0>) const{ |
548 | return __variant_storage<_Type>::__get_rref(__val); |
549 | } |
550 | void __destroy(in_place_index_t<0>){ |
551 | __variant_storage<_Type>::__destroy(__val); |
552 | } |
553 | }; |
554 | |
555 | template<typename _Type> |
556 | union __variant_data<_Type&>{ |
557 | typename __variant_storage<_Type&>::__type __val; |
558 | struct __dummy_type{} __dummy; |
559 | |
560 | constexpr __variant_data():__dummy(){} |
561 | |
562 | template<typename ... _Args> |
563 | constexpr __variant_data(in_place_index_t<0>,_Args&& ... __args): |
564 | __val(&std::forward<_Args>(__args)...){} |
565 | |
566 | _Type& __get(in_place_index_t<0>){ |
567 | return __variant_storage<_Type&>::__get(__val); |
568 | } |
569 | constexpr _Type& __get(in_place_index_t<0>) const{ |
570 | return __variant_storage<_Type&>::__get(__val); |
571 | } |
572 | |
573 | _Type& __get_rref(in_place_index_t<0>){ |
574 | return __variant_storage<_Type&>::__get_rref(__val); |
575 | } |
576 | constexpr _Type& __get_rref(in_place_index_t<0>) const{ |
577 | return __variant_storage<_Type&>::__get_rref(__val); |
578 | } |
579 | |
580 | void __destroy(in_place_index_t<0>){ |
581 | __variant_storage<_Type&>::__destroy(__val); |
582 | } |
583 | }; |
584 | |
585 | template<typename _Type> |
586 | union __variant_data<_Type&&>{ |
587 | typename __variant_storage<_Type&&>::__type __val; |
588 | struct __dummy_type{} __dummy; |
589 | |
590 | constexpr __variant_data():__dummy(){} |
591 | |
592 | template<typename _Arg> |
593 | __variant_data(in_place_index_t<0>,_Arg&& __arg): |
594 | __val(&__arg){} |
595 | |
596 | _Type&& __get(in_place_index_t<0>){ |
597 | return __variant_storage<_Type&&>::__get(__val); |
598 | } |
599 | constexpr _Type&& __get(in_place_index_t<0>) const{ |
600 | return __variant_storage<_Type&&>::__get(__val); |
601 | } |
602 | _Type&& __get_rref(in_place_index_t<0>){ |
603 | return __variant_storage<_Type&&>::__get_rref(__val); |
604 | } |
605 | constexpr _Type&& __get_rref(in_place_index_t<0>) const{ |
606 | return __variant_storage<_Type&&>::__get_rref(__val); |
607 | } |
608 | void __destroy(in_place_index_t<0>){ |
609 | __variant_storage<_Type&&>::__destroy(__val); |
610 | } |
611 | }; |
612 | |
613 | template<typename _Head,typename ... _Rest> |
614 | union __variant_data<_Head,_Rest...>{ |
615 | __variant_data<_Head> __head; |
616 | __variant_data<_Rest...> __rest; |
617 | |
618 | constexpr __variant_data(): |
619 | __head(){} |
620 | |
621 | template<typename ... _Args> |
622 | constexpr __variant_data(in_place_index_t<0>,_Args&& ... __args): |
623 | __head(in_place<0>,std::forward<_Args>(__args)...){} |
624 | template<size_t _Index,typename ... _Args> |
625 | constexpr __variant_data(in_place_index_t<_Index>,_Args&& ... __args): |
626 | __rest(in_place<_Index-1>,std::forward<_Args>(__args)...){} |
627 | |
628 | _Head& __get(in_place_index_t<0>){ |
629 | return __head.__get(in_place<0>); |
630 | } |
631 | |
632 | /*constexpr*/ _Head&& __get_rref(in_place_index_t<0>){ |
633 | return __head.__get_rref(in_place<0>); |
634 | } |
635 | |
636 | constexpr const _Head& __get(in_place_index_t<0>) const{ |
637 | return __head.__get(in_place<0>); |
638 | } |
639 | |
640 | constexpr const _Head&& __get_rref(in_place_index_t<0>) const{ |
641 | return __head.__get_rref(in_place<0>); |
642 | } |
643 | |
644 | template<size_t _Index> |
645 | typename __indexed_type<_Index-1,_Rest...>::__type& __get( |
646 | in_place_index_t<_Index>){ |
647 | return __rest.__get(in_place<_Index-1>); |
648 | } |
649 | |
650 | template<size_t _Index> |
651 | /*constexpr*/ typename __indexed_type<_Index-1,_Rest...>::__type&& __get_rref( |
652 | in_place_index_t<_Index>){ |
653 | return __rest.__get_rref(in_place<_Index-1>); |
654 | } |
655 | |
656 | template<size_t _Index> |
657 | constexpr const typename __indexed_type<_Index-1,_Rest...>::__type& __get( |
658 | in_place_index_t<_Index>) const{ |
659 | return __rest.__get(in_place<_Index-1>); |
660 | } |
661 | |
662 | template<size_t _Index> |
663 | constexpr const typename __indexed_type<_Index-1,_Rest...>::__type&& __get_rref( |
664 | in_place_index_t<_Index>) const{ |
665 | return __rest.__get_rref(in_place<_Index-1>); |
666 | } |
667 | |
668 | |
669 | void __destroy(in_place_index_t<0>){ |
670 | __head.__destroy(in_place<0>); |
671 | } |
672 | template<size_t _Index> |
673 | void __destroy(in_place_index_t<_Index>){ |
674 | __rest.__destroy(in_place<_Index-1>); |
675 | } |
676 | }; |
677 | |
678 | |
679 | template<ptrdiff_t... _Indices> |
680 | struct __index_sequence{ |
681 | typedef __index_sequence<_Indices...,sizeof...(_Indices)> __next; |
682 | static constexpr size_t __length=sizeof...(_Indices); |
683 | }; |
684 | |
685 | template<typename ... _Types> |
686 | struct __type_indices; |
687 | |
688 | template<> |
689 | struct __type_indices<>{ |
690 | typedef __index_sequence<> __type; |
691 | }; |
692 | |
693 | template<typename _Type> |
694 | struct __type_indices<_Type>{ |
695 | typedef __index_sequence<0> __type; |
696 | }; |
697 | |
698 | template<typename _Type,typename ... _Rest> |
699 | struct __type_indices<_Type,_Rest...>{ |
700 | typedef typename __type_indices<_Rest...>::__type::__next __type; |
701 | }; |
702 | |
703 | template<typename _Variant> |
704 | struct __variant_indices; |
705 | |
706 | template<typename ... _Types> |
707 | struct __variant_indices<Variant<_Types...>>{ |
708 | typedef typename __type_indices<_Types...>::__type __type; |
709 | }; |
710 | |
711 | template<typename _Variant, |
712 | typename _Indices=typename __variant_indices<_Variant>::__type> |
713 | struct __move_construct_op_table; |
714 | |
715 | template<typename _Variant,ptrdiff_t ... _Indices> |
716 | struct __move_construct_op_table<_Variant,__index_sequence<_Indices...>>{ |
717 | typedef void(* const __func_type)(_Variant*,_Variant&); |
718 | |
719 | template<ptrdiff_t _Index> |
720 | static void __move_construct_func( |
721 | _Variant * __lhs,_Variant& __rhs){ |
722 | __lhs->template __emplace_construct<_Index>( |
723 | std::move(get<_Index>(__rhs))); |
724 | } |
725 | |
726 | static const __func_type __apply[sizeof...(_Indices)]; |
727 | }; |
728 | |
729 | template<typename _Variant,ptrdiff_t ... _Indices> |
730 | const typename __move_construct_op_table<_Variant,__index_sequence<_Indices...>>:: |
731 | __func_type |
732 | __move_construct_op_table<_Variant,__index_sequence<_Indices...>>::__apply[ |
733 | sizeof...(_Indices)]={ |
734 | &__move_construct_func<_Indices>... |
735 | }; |
736 | |
737 | template<typename _Variant, |
738 | typename _Indices=typename __variant_indices<_Variant>::__type> |
739 | struct __move_assign_op_table; |
740 | |
741 | template<typename _Variant,ptrdiff_t ... _Indices> |
742 | struct __move_assign_op_table<_Variant,__index_sequence<_Indices...>>{ |
743 | typedef void(* const __func_type)(_Variant*,_Variant&); |
744 | |
745 | template<ptrdiff_t _Index> |
746 | static void __move_assign_func( |
747 | _Variant * __lhs,_Variant& __rhs){ |
748 | get<_Index>(*__lhs)=std::move(get<_Index>(__rhs)); |
749 | } |
750 | |
751 | static const __func_type __apply[sizeof...(_Indices)]; |
752 | }; |
753 | |
754 | template<typename _Variant,ptrdiff_t ... _Indices> |
755 | const typename __move_assign_op_table<_Variant,__index_sequence<_Indices...>>:: |
756 | __func_type |
757 | __move_assign_op_table<_Variant,__index_sequence<_Indices...>>::__apply[ |
758 | sizeof...(_Indices)]={ |
759 | &__move_assign_func<_Indices>... |
760 | }; |
761 | |
762 | template<typename _Variant, |
763 | typename _Indices=typename __variant_indices<_Variant>::__type> |
764 | struct __copy_construct_op_table; |
765 | |
766 | template<typename _Variant,ptrdiff_t ... _Indices> |
767 | struct __copy_construct_op_table<_Variant,__index_sequence<_Indices...>>{ |
768 | typedef void(* const __func_type)(_Variant*,_Variant const&); |
769 | |
770 | template<ptrdiff_t _Index> |
771 | static void __copy_construct_func( |
772 | _Variant * __lhs,_Variant const& __rhs){ |
773 | __lhs->template __emplace_construct<_Index>( |
774 | get<_Index>(__rhs)); |
775 | } |
776 | |
777 | static const __func_type __apply[sizeof...(_Indices)]; |
778 | }; |
779 | |
780 | template<typename _Variant,ptrdiff_t ... _Indices> |
781 | const typename __copy_construct_op_table<_Variant,__index_sequence<_Indices...>>:: |
782 | __func_type |
783 | __copy_construct_op_table<_Variant,__index_sequence<_Indices...>>::__apply[ |
784 | sizeof...(_Indices)]={ |
785 | &__copy_construct_func<_Indices>... |
786 | }; |
787 | |
788 | template<typename _Variant, |
789 | typename _Indices=typename __variant_indices<_Variant>::__type> |
790 | struct __copy_assign_op_table; |
791 | |
792 | template<typename _Variant,ptrdiff_t ... _Indices> |
793 | struct __copy_assign_op_table<_Variant,__index_sequence<_Indices...>>{ |
794 | typedef void(* const __func_type)(_Variant*,_Variant const&); |
795 | |
796 | template<ptrdiff_t _Index> |
797 | static void __copy_assign_func( |
798 | _Variant * __lhs,_Variant const& __rhs){ |
799 | get<_Index>(*__lhs)=get<_Index>(__rhs); |
800 | } |
801 | |
802 | static const __func_type __apply[sizeof...(_Indices)]; |
803 | }; |
804 | |
805 | template<typename _Variant,ptrdiff_t ... _Indices> |
806 | const typename __copy_assign_op_table<_Variant,__index_sequence<_Indices...>>:: |
807 | __func_type |
808 | __copy_assign_op_table<_Variant,__index_sequence<_Indices...>>::__apply[ |
809 | sizeof...(_Indices)]={ |
810 | &__copy_assign_func<_Indices>... |
811 | }; |
812 | |
813 | template<typename _Variant, |
814 | typename _Indices=typename __variant_indices<_Variant>::__type> |
815 | struct __destroy_op_table; |
816 | |
817 | template<typename _Variant,ptrdiff_t ... _Indices> |
818 | struct __destroy_op_table<_Variant,__index_sequence<_Indices...>>{ |
819 | typedef void(* const __func_type)(_Variant*); |
820 | |
821 | template<ptrdiff_t _Index> |
822 | static void __destroy_func( |
823 | _Variant * __self){ |
824 | if(__self->__index>=0){ |
825 | __self->__storage.__destroy(in_place<_Index>); |
826 | } |
827 | } |
828 | |
829 | static const __func_type __apply[sizeof...(_Indices)]; |
830 | }; |
831 | |
832 | template<typename _Variant,ptrdiff_t ... _Indices> |
833 | const typename __destroy_op_table<_Variant,__index_sequence<_Indices...>>:: |
834 | __func_type |
835 | __destroy_op_table<_Variant,__index_sequence<_Indices...>>::__apply[ |
836 | sizeof...(_Indices)]={ |
837 | &__destroy_func<_Indices>... |
838 | }; |
839 | |
840 | template<typename _Variant, |
841 | typename _Indices=typename __variant_indices<_Variant>::__type> |
842 | struct __swap_op_table; |
843 | |
844 | template<typename _Variant,ptrdiff_t ... _Indices> |
845 | struct __swap_op_table<_Variant,__index_sequence<_Indices...>>{ |
846 | typedef void(* const __func_type)(_Variant&,_Variant&); |
847 | |
848 | template<ptrdiff_t _Index> |
849 | static void __swap_func( |
850 | _Variant & __lhs,_Variant & __rhs){ |
851 | swap(get<_Index>(__lhs),get<_Index>(__rhs)); |
852 | } |
853 | |
854 | static const __func_type __apply[sizeof...(_Indices)]; |
855 | }; |
856 | |
857 | template<typename _Variant,ptrdiff_t ... _Indices> |
858 | const typename __swap_op_table<_Variant,__index_sequence<_Indices...>>:: |
859 | __func_type |
860 | __swap_op_table<_Variant,__index_sequence<_Indices...>>::__apply[ |
861 | sizeof...(_Indices)]={ |
862 | &__swap_func<_Indices>... |
863 | }; |
864 | |
865 | template<typename _Variant, |
866 | typename _Indices=typename __variant_indices<_Variant>::__type> |
867 | struct __equality_op_table; |
868 | |
869 | template<typename _Variant,ptrdiff_t ... _Indices> |
870 | struct __equality_op_table<_Variant,__index_sequence<_Indices...>>{ |
871 | typedef bool(* const __compare_func_type)(_Variant const&,_Variant const&); |
872 | |
873 | template<ptrdiff_t _Index> |
874 | static constexpr bool __equality_compare_func( |
875 | _Variant const& __lhs,_Variant const& __rhs){ |
876 | return get<_Index>(__lhs)==get<_Index>(__rhs); |
877 | } |
878 | |
879 | static constexpr __compare_func_type __equality_compare[sizeof...(_Indices)]={ |
880 | &__equality_compare_func<_Indices>... |
881 | }; |
882 | }; |
883 | |
884 | template<typename _Variant,ptrdiff_t ... _Indices> |
885 | constexpr typename __equality_op_table<_Variant,__index_sequence<_Indices...>>:: |
886 | __compare_func_type |
887 | __equality_op_table<_Variant,__index_sequence<_Indices...>>::__equality_compare[ |
888 | sizeof...(_Indices)]; |
889 | |
890 | template<typename _Variant, |
891 | typename _Indices=typename __variant_indices<_Variant>::__type> |
892 | struct __less_than_op_table; |
893 | |
894 | template<typename _Variant,ptrdiff_t ... _Indices> |
895 | struct __less_than_op_table<_Variant,__index_sequence<_Indices...>>{ |
896 | typedef bool(* const __compare_func_type)(_Variant const&,_Variant const&); |
897 | |
898 | template<ptrdiff_t _Index> |
899 | static constexpr bool __less_than_compare_func( |
900 | _Variant const& __lhs,_Variant const& __rhs){ |
901 | return get<_Index>(__lhs)<get<_Index>(__rhs); |
902 | } |
903 | |
904 | static constexpr __compare_func_type __less_than_compare[sizeof...(_Indices)]={ |
905 | &__less_than_compare_func<_Indices>... |
906 | }; |
907 | }; |
908 | |
909 | template<typename _Variant,ptrdiff_t ... _Indices> |
910 | constexpr typename __less_than_op_table<_Variant,__index_sequence<_Indices...>>:: |
911 | __compare_func_type |
912 | __less_than_op_table<_Variant,__index_sequence<_Indices...>>::__less_than_compare[ |
913 | sizeof...(_Indices)]; |
914 | |
915 | template<typename _Variant> |
916 | struct __variant_storage_type; |
917 | |
918 | template<typename _Derived,bool __trivial_destructor> |
919 | struct __variant_base |
920 | { |
921 | ~__variant_base(){ |
922 | static_cast<_Derived*>(this)->__destroy_self(); |
923 | } |
924 | }; |
925 | |
926 | template<typename _Derived> |
927 | struct __variant_base<_Derived,true>{ |
928 | }; |
929 | |
930 | |
931 | template<ptrdiff_t _Offset,typename _CurrentSequence, |
932 | typename _Type,typename ... _Types> |
933 | struct __all_indices_helper; |
934 | |
935 | template<ptrdiff_t _Offset,ptrdiff_t ... _Indices, |
936 | typename _Type,typename ... _Rest> |
937 | struct __all_indices_helper< |
938 | _Offset,__index_sequence<_Indices...>, |
939 | _Type,_Type,_Rest...>{ |
940 | typedef typename __all_indices_helper< |
941 | _Offset+1,__index_sequence<_Indices...,_Offset>,_Type,_Rest...>::__type |
942 | __type; |
943 | }; |
944 | |
945 | template<ptrdiff_t _Offset,typename _CurrentSequence, |
946 | typename _Type,typename _Head,typename ... _Rest> |
947 | struct __all_indices_helper<_Offset,_CurrentSequence,_Type,_Head,_Rest...>{ |
948 | typedef typename __all_indices_helper< |
949 | _Offset+1,_CurrentSequence,_Type,_Rest...>::__type __type; |
950 | }; |
951 | |
952 | template<ptrdiff_t _Offset,typename _CurrentSequence,typename _Type> |
953 | struct __all_indices_helper<_Offset,_CurrentSequence,_Type>{ |
954 | typedef _CurrentSequence __type; |
955 | }; |
956 | |
957 | template<typename _Type,typename ... _Types> |
958 | struct __all_indices{ |
959 | typedef typename __all_indices_helper< |
960 | 0,__index_sequence<>,_Type,_Types...>::__type __type; |
961 | }; |
962 | |
963 | template<typename ... _Sequences> |
964 | struct __combine_sequences; |
965 | |
966 | template<ptrdiff_t ... _Indices1,ptrdiff_t ... _Indices2> |
967 | struct __combine_sequences< |
968 | __index_sequence<_Indices1...>,__index_sequence<_Indices2...>>{ |
969 | typedef __index_sequence<_Indices1...,_Indices2...> __type; |
970 | }; |
971 | |
972 | template<typename _Sequence,typename ... _Rest> |
973 | struct __combine_sequences<_Sequence,_Rest...>{ |
974 | typedef typename __combine_sequences< |
975 | _Sequence, |
976 | typename __combine_sequences<_Rest...>::__type>::__type __type; |
977 | }; |
978 | |
979 | template<typename _Indices> |
980 | struct __first_index; |
981 | |
982 | template<ptrdiff_t _FirstIndex,ptrdiff_t ... _Rest> |
983 | struct __first_index<__index_sequence<_FirstIndex,_Rest...>>{ |
984 | static constexpr ptrdiff_t __value=_FirstIndex; |
985 | }; |
986 | |
987 | template<ptrdiff_t _Offset,typename _CurrentSequence, |
988 | typename _Type,typename ... _Types> |
989 | struct __constructible_matches_helper; |
990 | |
991 | template<ptrdiff_t _Offset,typename _Sequence,typename _Type> |
992 | struct __constructible_matches_helper< |
993 | _Offset,_Sequence,_Type>{ |
994 | typedef _Sequence __type; |
995 | }; |
996 | |
997 | template<bool _Accept,ptrdiff_t _Entry> |
998 | struct __sequence_or_empty{ |
999 | typedef __index_sequence<> __type; |
1000 | }; |
1001 | |
1002 | template<ptrdiff_t _Entry> |
1003 | struct __sequence_or_empty<true,_Entry>{ |
1004 | typedef __index_sequence<_Entry> __type; |
1005 | }; |
1006 | |
1007 | template<ptrdiff_t _Offset,typename _CurrentSequence, |
1008 | typename _Type,typename _Head,typename ... _Rest> |
1009 | struct __constructible_matches_helper< |
1010 | _Offset,_CurrentSequence,_Type,_Head,_Rest...>{ |
1011 | typedef |
1012 | typename __constructible_matches_helper< |
1013 | _Offset+1, |
1014 | typename __combine_sequences< |
1015 | _CurrentSequence, |
1016 | typename __sequence_or_empty< |
1017 | std::is_constructible<_Head,_Type>::value, |
1018 | _Offset>::__type>::__type, |
1019 | _Type,_Rest...>::__type __type; |
1020 | }; |
1021 | |
1022 | template<typename _Type,typename ... _Types> |
1023 | struct __constructible_matches{ |
1024 | typedef typename __constructible_matches_helper< |
1025 | 0,__index_sequence<>,_Type,_Types...>::__type __type; |
1026 | }; |
1027 | |
1028 | template<typename _Type,typename ... _Types> |
1029 | struct __type_index_to_construct{ |
1030 | typedef typename __all_indices<_Type,_Types...>::__type __direct_matches; |
1031 | typedef typename __all_indices< |
1032 | typename std::remove_const< |
1033 | typename std::remove_reference<_Type>::type |
1034 | >::type,_Types...>::__type __value_matches; |
1035 | typedef typename __all_indices< |
1036 | _Type, |
1037 | typename std::remove_const< |
1038 | typename std::remove_reference<_Types>::type |
1039 | >::type...>::__type __rref_matches; |
1040 | |
1041 | typedef typename __constructible_matches<_Type,_Types...>::__type |
1042 | __constructibles; |
1043 | |
1044 | static_assert( |
1045 | (__direct_matches::__length>0) || |
1046 | (__value_matches::__length>0) || |
1047 | (__rref_matches::__length>0) || |
1048 | (__constructibles::__length==1), |
1049 | "For conversion construction of variants, exactly one type must be constructible" ); |
1050 | |
1051 | typedef typename __combine_sequences< |
1052 | __direct_matches,__value_matches,__rref_matches, |
1053 | __constructibles>::__type __all_matches; |
1054 | |
1055 | static constexpr ptrdiff_t __value=__first_index<__all_matches>::__value; |
1056 | }; |
1057 | |
1058 | struct __replace_construct_helper{ |
1059 | template< |
1060 | ptrdiff_t _Index, |
1061 | bool __construct_directly, |
1062 | bool __indexed_type_has_nothrow_move, |
1063 | bool __other_types_have_nothrow_move> |
1064 | struct __helper; |
1065 | |
1066 | template<typename _Variant, |
1067 | typename _Indices=typename __variant_indices<_Variant>::__type> |
1068 | struct __op_table; |
1069 | }; |
1070 | |
1071 | template< |
1072 | ptrdiff_t _Index, |
1073 | bool __other_types_have_nothrow_move> |
1074 | struct __replace_construct_helper::__helper< |
1075 | _Index,false,true,__other_types_have_nothrow_move>{ |
1076 | |
1077 | template<typename _Variant,typename ... _Args> |
1078 | static void __trampoline(_Variant& __v,_Args&& ... __args){ |
1079 | __v.template __two_stage_replace<_Index>(__args...); |
1080 | } |
1081 | }; |
1082 | |
1083 | template< |
1084 | ptrdiff_t _Index, |
1085 | bool __indexed_type_has_nothrow_move, |
1086 | bool __other_types_have_nothrow_move> |
1087 | struct __replace_construct_helper::__helper< |
1088 | _Index,true,__indexed_type_has_nothrow_move, |
1089 | __other_types_have_nothrow_move>{ |
1090 | |
1091 | template<typename _Variant,typename ... _Args> |
1092 | static void __trampoline(_Variant& __v,_Args&& ... __args){ |
1093 | __v.template __direct_replace<_Index>(std::forward<_Args>(__args)...); |
1094 | } |
1095 | }; |
1096 | |
1097 | |
1098 | template< |
1099 | ptrdiff_t _Index> |
1100 | struct __replace_construct_helper::__helper< |
1101 | _Index,false,false,true>{ |
1102 | |
1103 | template<typename _Variant,typename ... _Args> |
1104 | static void __trampoline(_Variant& __v,_Args&& ... __args){ |
1105 | __v.template __local_backup_replace<_Index>(std::forward<_Args>(__args)...); |
1106 | } |
1107 | }; |
1108 | |
1109 | template< |
1110 | ptrdiff_t _Index> |
1111 | struct __replace_construct_helper::__helper< |
1112 | _Index,false,false,false>{ |
1113 | |
1114 | template<typename _Variant,typename ... _Args> |
1115 | static void __trampoline(_Variant& __v,_Args&& ... __args){ |
1116 | __v.template __direct_replace<_Index>(std::forward<_Args>(__args)...); |
1117 | } |
1118 | }; |
1119 | |
1120 | template<typename _Variant,ptrdiff_t ... _Indices> |
1121 | struct __replace_construct_helper::__op_table<_Variant,__index_sequence<_Indices...>>{ |
1122 | typedef void(* const __move_func_type)(_Variant*,_Variant&); |
1123 | typedef void(* const __copy_func_type)(_Variant*,_Variant const&); |
1124 | |
1125 | template<ptrdiff_t _Index> |
1126 | static void __move_assign_func( |
1127 | _Variant * __lhs,_Variant& __rhs){ |
1128 | __lhs->template __replace_construct<_Index>(std::move(get<_Index>(__rhs))); |
1129 | __rhs.__destroy_self(); |
1130 | } |
1131 | |
1132 | template<ptrdiff_t _Index> |
1133 | static void __copy_assign_func( |
1134 | _Variant * __lhs,_Variant const& __rhs){ |
1135 | __lhs->template __replace_construct<_Index>(get<_Index>(__rhs)); |
1136 | } |
1137 | |
1138 | static const __move_func_type __move_assign[sizeof...(_Indices)]; |
1139 | static const __copy_func_type __copy_assign[sizeof...(_Indices)]; |
1140 | }; |
1141 | |
1142 | template<typename _Variant,ptrdiff_t ... _Indices> |
1143 | const typename __replace_construct_helper::__op_table< |
1144 | _Variant,__index_sequence<_Indices...>>::__move_func_type |
1145 | __replace_construct_helper::__op_table< |
1146 | _Variant,__index_sequence<_Indices...>>::__move_assign[ |
1147 | sizeof...(_Indices)]={ |
1148 | &__move_assign_func<_Indices>... |
1149 | }; |
1150 | |
1151 | template<typename _Variant,ptrdiff_t ... _Indices> |
1152 | const typename __replace_construct_helper::__op_table< |
1153 | _Variant,__index_sequence<_Indices...>>::__copy_func_type |
1154 | __replace_construct_helper::__op_table< |
1155 | _Variant,__index_sequence<_Indices...>>::__copy_assign[ |
1156 | sizeof...(_Indices)]={ |
1157 | &__copy_assign_func<_Indices>... |
1158 | }; |
1159 | |
1160 | template<ptrdiff_t _Index,ptrdiff_t _MaskIndex,typename _Storage> |
1161 | struct __backup_storage_ops{ |
1162 | static void __move_construct_func( |
1163 | _Storage * __dest,_Storage& __source){ |
1164 | new(__dest) _Storage( |
1165 | in_place<_Index>, |
1166 | std::move(__source.__get(in_place<_Index>))); |
1167 | } |
1168 | static void __destroy_func(_Storage * __obj){ |
1169 | __obj->__destroy(in_place<_Index>); |
1170 | }; |
1171 | }; |
1172 | |
1173 | template<ptrdiff_t _Index,typename _Storage> |
1174 | struct __backup_storage_ops<_Index,_Index,_Storage>{ |
1175 | static void __move_construct_func(_Storage *,_Storage&){ |
1176 | __THROW_EXCEPTION(std::bad_alloc()); |
1177 | }; |
1178 | static void __destroy_func(_Storage *){ |
1179 | __THROW_EXCEPTION(std::bad_alloc()); |
1180 | }; |
1181 | }; |
1182 | |
1183 | template<ptrdiff_t _MaskIndex,typename _Storage,typename _Indices> |
1184 | struct __backup_storage_op_table; |
1185 | |
1186 | template<ptrdiff_t _MaskIndex,typename _Storage,ptrdiff_t ... _Indices> |
1187 | struct __backup_storage_op_table< |
1188 | _MaskIndex,_Storage,__index_sequence<_Indices...> > |
1189 | { |
1190 | typedef void (*__move_func_type)(_Storage * __dest,_Storage& __source); |
1191 | typedef void (*__destroy_func_type)(_Storage * __obj); |
1192 | |
1193 | template<size_t _Index> |
1194 | struct __helper{ |
1195 | typedef __backup_storage_ops<_Index,_MaskIndex,_Storage> __ops; |
1196 | }; |
1197 | |
1198 | static const __move_func_type __move_ops[sizeof...(_Indices)]; |
1199 | static const __destroy_func_type __destroy_ops[sizeof...(_Indices)]; |
1200 | }; |
1201 | |
1202 | template<ptrdiff_t _MaskIndex,typename _Storage,ptrdiff_t ... _Indices> |
1203 | const typename __backup_storage_op_table< |
1204 | _MaskIndex,_Storage,__index_sequence<_Indices...> >::__move_func_type |
1205 | __backup_storage_op_table< |
1206 | _MaskIndex,_Storage,__index_sequence<_Indices...> >::__move_ops[ |
1207 | sizeof...(_Indices)]={ |
1208 | &__helper<_Indices>::__ops::__move_construct_func... |
1209 | }; |
1210 | |
1211 | template<ptrdiff_t _MaskIndex,typename _Storage,ptrdiff_t ... _Indices> |
1212 | const typename __backup_storage_op_table< |
1213 | _MaskIndex,_Storage,__index_sequence<_Indices...> >::__destroy_func_type |
1214 | __backup_storage_op_table< |
1215 | _MaskIndex,_Storage,__index_sequence<_Indices...> >::__destroy_ops[ |
1216 | sizeof...(_Indices)]={ |
1217 | &__helper<_Indices>::__ops::__destroy_func... |
1218 | }; |
1219 | |
1220 | template<ptrdiff_t _Index,typename ... _Types> |
1221 | struct __backup_storage{ |
1222 | typedef __variant_data<_Types...> __storage_type; |
1223 | |
1224 | typedef __backup_storage_op_table< |
1225 | _Index,__storage_type,typename __type_indices<_Types...>::__type> |
1226 | __op_table_type; |
1227 | |
1228 | ptrdiff_t __backup_index; |
1229 | __storage_type& __live_storage; |
1230 | __storage_type __backup; |
1231 | |
1232 | __backup_storage(ptrdiff_t __live_index_,__storage_type& __live_storage_): |
1233 | __backup_index(__live_index_),__live_storage(__live_storage_){ |
1234 | if(__backup_index>=0){ |
1235 | __op_table_type::__move_ops[__backup_index]( |
1236 | &__backup,__live_storage); |
1237 | __op_table_type::__destroy_ops[__backup_index]( |
1238 | &__live_storage); |
1239 | } |
1240 | } |
1241 | void __destroy(){ |
1242 | if(__backup_index>=0) |
1243 | __op_table_type::__destroy_ops[__backup_index]( |
1244 | &__backup); |
1245 | __backup_index=-1; |
1246 | } |
1247 | |
1248 | ~__backup_storage(){ |
1249 | if(__backup_index>=0){ |
1250 | __op_table_type::__move_ops[__backup_index]( |
1251 | &__live_storage,__backup); |
1252 | __destroy(); |
1253 | } |
1254 | } |
1255 | }; |
1256 | |
1257 | template<typename ... _Types> |
1258 | struct __all_move_constructible; |
1259 | |
1260 | template<typename _Head,typename ... _Rest> |
1261 | struct __all_move_constructible<_Head,_Rest...> |
1262 | { |
1263 | static constexpr bool value=std::is_move_constructible<_Head>::value && __all_move_constructible<_Rest...>::value; |
1264 | }; |
1265 | |
1266 | template<> |
1267 | struct __all_move_constructible<>: |
1268 | std::true_type{}; |
1269 | |
1270 | template<typename ... _Types> |
1271 | struct __all_move_assignable; |
1272 | |
1273 | template<typename _Head,typename ... _Rest> |
1274 | struct __all_move_assignable<_Head,_Rest...> |
1275 | { |
1276 | static constexpr bool value=std::is_move_assignable<_Head>::value && __all_move_assignable<_Rest...>::value; |
1277 | }; |
1278 | |
1279 | template<> |
1280 | struct __all_move_assignable<>: |
1281 | std::true_type{}; |
1282 | |
1283 | template<typename ... _Types> |
1284 | struct __all_copy_assignable; |
1285 | |
1286 | template<typename _Head,typename ... _Rest> |
1287 | struct __all_copy_assignable<_Head,_Rest...> |
1288 | { |
1289 | static constexpr bool value=std::is_copy_assignable<_Head>::value && __all_copy_assignable<_Rest...>::value; |
1290 | }; |
1291 | |
1292 | template<> |
1293 | struct __all_copy_assignable<>: |
1294 | std::true_type{}; |
1295 | |
1296 | namespace __swap_test_detail{ |
1297 | using std::swap; |
1298 | |
1299 | template<typename _Other> |
1300 | struct __swap_result{}; |
1301 | |
1302 | template<typename> |
1303 | static char __test(...); |
1304 | template <typename _Other> |
1305 | static std::pair<char, std::pair<char, __swap_result<decltype( |
1306 | swap(std::declval<_Other &>(),std::declval<_Other &>()))>>> |
1307 | __test(_Other *); |
1308 | } |
1309 | |
1310 | template <typename _Type> struct __is_swappable { |
1311 | static constexpr bool value = |
1312 | sizeof(__swap_test_detail::__test<_Type>(0)) != 1; |
1313 | }; |
1314 | |
1315 | template<typename ... _Types> |
1316 | struct __all_swappable; |
1317 | |
1318 | template<typename _Head,typename ... _Rest> |
1319 | struct __all_swappable<_Head,_Rest...> |
1320 | { |
1321 | static constexpr bool value=__is_swappable<_Head>::value && __all_swappable<_Rest...>::value; |
1322 | }; |
1323 | |
1324 | template<> |
1325 | struct __all_swappable<>: |
1326 | std::true_type{}; |
1327 | |
1328 | template<bool _MoveConstructible,typename ... _Types> |
1329 | struct __noexcept_variant_move_construct_impl{}; |
1330 | |
1331 | template<typename _Head,typename ... _Rest> |
1332 | struct __noexcept_variant_move_construct_impl<true,_Head,_Rest...>{ |
1333 | static constexpr bool value=noexcept(_Head(std::declval<_Head&&>())) && __noexcept_variant_move_construct_impl<true,_Rest...>::value; |
1334 | }; |
1335 | |
1336 | template<> |
1337 | struct __noexcept_variant_move_construct_impl<true>{ |
1338 | static constexpr bool value=true; |
1339 | }; |
1340 | |
1341 | template<typename ... _Types> |
1342 | struct __noexcept_variant_move_construct: |
1343 | __noexcept_variant_move_construct_impl<__all_move_constructible<_Types...>::value,_Types...> |
1344 | {}; |
1345 | |
1346 | template<bool _MoveAssignable,typename ... _Types> |
1347 | struct __noexcept_variant_move_assign_impl{}; |
1348 | |
1349 | template <typename _Head, typename... _Rest> |
1350 | struct __noexcept_variant_move_assign_impl<true, _Head, _Rest...> { |
1351 | static constexpr bool value = |
1352 | std::is_nothrow_move_assignable<_Head>::value && |
1353 | std::is_nothrow_move_constructible<_Head>::value && |
1354 | __noexcept_variant_move_assign_impl<true, _Rest...>::value; |
1355 | }; |
1356 | |
1357 | template<> |
1358 | struct __noexcept_variant_move_assign_impl<true>{ |
1359 | static constexpr bool value=true; |
1360 | }; |
1361 | |
1362 | template <typename... _Types> |
1363 | struct __noexcept_variant_move_assign |
1364 | : __noexcept_variant_move_assign_impl< |
1365 | __all_move_assignable<_Types...>::value && |
1366 | __all_move_constructible<_Types...>::value, |
1367 | _Types...> {}; |
1368 | |
1369 | template<typename ... _Types> |
1370 | struct __all_copy_constructible; |
1371 | |
1372 | template<typename _Head,typename ... _Rest> |
1373 | struct __all_copy_constructible<_Head,_Rest...> |
1374 | { |
1375 | static constexpr bool value=std::is_copy_constructible<_Head>::value && __all_copy_constructible<_Rest...>::value; |
1376 | }; |
1377 | |
1378 | template<> |
1379 | struct __all_copy_constructible<>: |
1380 | std::true_type{}; |
1381 | |
1382 | template<bool _CopyConstructible,typename ... _Types> |
1383 | struct __noexcept_variant_const_copy_construct_impl{}; |
1384 | |
1385 | template<typename _Head,typename ... _Rest> |
1386 | struct __noexcept_variant_const_copy_construct_impl<true,_Head,_Rest...>{ |
1387 | static constexpr bool value=noexcept(_Head(std::declval<_Head const&>())) && __noexcept_variant_const_copy_construct_impl<true,_Rest...>::value; |
1388 | }; |
1389 | |
1390 | template<> |
1391 | struct __noexcept_variant_const_copy_construct_impl<true>{ |
1392 | static constexpr bool value=true; |
1393 | }; |
1394 | |
1395 | template<typename ... _Types> |
1396 | struct __noexcept_variant_const_copy_construct: |
1397 | __noexcept_variant_const_copy_construct_impl<__all_copy_constructible<_Types...>::value,_Types...> |
1398 | {}; |
1399 | |
1400 | template<bool _CopyNon_Constructible,typename ... _Types> |
1401 | struct __noexcept_variant_non_const_copy_construct_impl{}; |
1402 | |
1403 | template<typename _Head,typename ... _Rest> |
1404 | struct __noexcept_variant_non_const_copy_construct_impl<true,_Head,_Rest...>{ |
1405 | static constexpr bool value=noexcept(_Head(std::declval<_Head&>())) && __noexcept_variant_non_const_copy_construct_impl<true,_Rest...>::value; |
1406 | }; |
1407 | |
1408 | template<> |
1409 | struct __noexcept_variant_non_const_copy_construct_impl<true>{ |
1410 | static constexpr bool value=true; |
1411 | }; |
1412 | |
1413 | template<typename ... _Types> |
1414 | struct __noexcept_variant_non_const_copy_construct: |
1415 | __noexcept_variant_non_const_copy_construct_impl<__all_copy_constructible<_Types...>::value,_Types...> |
1416 | {}; |
1417 | |
1418 | template<bool _Swappable,typename ... _Types> |
1419 | struct __noexcept_variant_swap_impl{}; |
1420 | |
1421 | template <typename _Head, typename... _Rest> |
1422 | struct __noexcept_variant_swap_impl<true, _Head, _Rest...> { |
1423 | static constexpr bool value = |
1424 | noexcept(swap(std::declval<_Head&>(),std::declval<_Head&>())) && |
1425 | __noexcept_variant_swap_impl<true, _Rest...>::value; |
1426 | }; |
1427 | |
1428 | template<> |
1429 | struct __noexcept_variant_swap_impl<true>{ |
1430 | static constexpr bool value=true; |
1431 | }; |
1432 | |
1433 | template<typename ... _Types> |
1434 | struct __noexcept_variant_swap: |
1435 | __noexcept_variant_swap_impl<__all_swappable<_Types...>::value,_Types...> |
1436 | {}; |
1437 | |
1438 | template<typename ... _Types> |
1439 | class Variant: |
1440 | private __variant_base< |
1441 | Variant<_Types...>,__all_trivially_destructible<_Types...>::__value> |
1442 | { |
1443 | typedef __variant_base<Variant<_Types...>,__all_trivially_destructible<_Types...>::__value> __base_type; |
1444 | friend __base_type; |
1445 | friend struct __copy_construct_op_table<Variant>; |
1446 | friend struct __copy_assign_op_table<Variant>; |
1447 | friend struct __move_construct_op_table<Variant>; |
1448 | friend struct __move_assign_op_table<Variant>; |
1449 | friend struct __destroy_op_table<Variant>; |
1450 | |
1451 | template<ptrdiff_t _Index,typename ... _Types2> |
1452 | friend struct __variant_accessor; |
1453 | |
1454 | friend struct __replace_construct_helper; |
1455 | |
1456 | typedef __variant_data<_Types...> __storage_type; |
1457 | __storage_type __storage; |
1458 | typename __discriminator_type<sizeof ... (_Types)>::__type __index; |
1459 | |
1460 | template<size_t _Index,typename ... _Args> |
1461 | size_t __emplace_construct(_Args&& ... __args){ |
1462 | new(&__storage) __storage_type( |
1463 | in_place<_Index>,std::forward<_Args>(__args)...); |
1464 | return _Index; |
1465 | } |
1466 | |
1467 | void __destroy_self(){ |
1468 | if(valueless_by_exception()) |
1469 | return; |
1470 | __destroy_op_table<Variant>::__apply[index()](this); |
1471 | __index=-1; |
1472 | } |
1473 | |
1474 | ptrdiff_t __move_construct(Variant& __other){ |
1475 | ptrdiff_t const __other_index=__other.index(); |
1476 | if(__other_index==-1) |
1477 | return -1; |
1478 | __move_construct_op_table<Variant>::__apply[__other_index](this,__other); |
1479 | __other.__destroy_self(); |
1480 | return __other_index; |
1481 | } |
1482 | |
1483 | ptrdiff_t __copy_construct(Variant const& __other){ |
1484 | ptrdiff_t const __other_index=__other.index(); |
1485 | if(__other_index==-1) |
1486 | return -1; |
1487 | __copy_construct_op_table<Variant>::__apply[__other_index](this,__other); |
1488 | return __other_index; |
1489 | } |
1490 | |
1491 | template<size_t _Index,typename ... _Args> |
1492 | void __replace_construct(_Args&& ... __args){ |
1493 | typedef typename __indexed_type<_Index,_Types...>::__type __this_type; |
1494 | __replace_construct_helper::__helper< |
1495 | _Index, |
1496 | __storage_nothrow_constructible<__this_type,_Args...>::__value || |
1497 | (sizeof...(_Types)==1), |
1498 | __storage_nothrow_move_constructible<__this_type>::__value, |
1499 | __other_storage_nothrow_move_constructible< |
1500 | _Index,_Types...>::__value |
1501 | >::__trampoline(*this,std::forward<_Args>(__args)...); |
1502 | } |
1503 | |
1504 | template<size_t _Index,typename ... _Args> |
1505 | void __two_stage_replace(_Args&& ... __args){ |
1506 | typedef typename __indexed_type<_Index,_Types...>::__type __type; |
1507 | __variant_data<__type> __local( |
1508 | in_place<0>,std::forward<_Args>(__args)...); |
1509 | __destroy_self(); |
1510 | __emplace_construct<_Index>( |
1511 | std::move(__local.__get(in_place<0>))); |
1512 | __index=_Index; |
1513 | __local.__destroy(in_place<0>); |
1514 | } |
1515 | |
1516 | template<size_t _Index,typename ... _Args> |
1517 | void __local_backup_replace(_Args&& ... __args){ |
1518 | __backup_storage<_Index,_Types...> __backup(__index,__storage); |
1519 | __emplace_construct<_Index>(std::forward<_Args>(__args)...); |
1520 | __index=_Index; |
1521 | __backup.__destroy(); |
1522 | } |
1523 | |
1524 | template<size_t _Index,typename ... _Args> |
1525 | void __direct_replace(_Args&& ... __args) { |
1526 | __destroy_self(); |
1527 | __emplace_construct<_Index>(std::forward<_Args>(__args)...); |
1528 | __index=_Index; |
1529 | } |
1530 | |
1531 | struct __private_type{}; |
1532 | |
1533 | public: |
1534 | constexpr Variant() |
1535 | __NOEXCEPT_(noexcept(typename __indexed_type<0,_Types...>::__type())): |
1536 | __storage(in_place<0>), |
1537 | __index(0) |
1538 | {} |
1539 | |
1540 | constexpr Variant(typename std::conditional<__all_move_constructible<_Types...>::value,Variant,__private_type>::type&& __other) |
1541 | __NOEXCEPT_(__noexcept_variant_move_construct<_Types...>::value): |
1542 | __index(__move_construct(__other)) |
1543 | {} |
1544 | |
1545 | constexpr Variant(typename std::conditional<!__all_move_constructible<_Types...>::value,Variant,__private_type>::type&& __other)=delete; |
1546 | |
1547 | constexpr Variant(typename std::conditional<__all_copy_constructible<_Types...>::value,Variant,__private_type>::type& __other) |
1548 | __NOEXCEPT_(__noexcept_variant_non_const_copy_construct<_Types...>::value): |
1549 | __index(__copy_construct(__other)) |
1550 | {} |
1551 | |
1552 | constexpr Variant(typename std::conditional<!__all_copy_constructible<_Types...>::value,Variant,__private_type>::type& __other)=delete; |
1553 | |
1554 | constexpr Variant(typename std::conditional<__all_copy_constructible<_Types...>::value,Variant,__private_type>::type const& __other) |
1555 | __NOEXCEPT_(__noexcept_variant_const_copy_construct<_Types...>::value): |
1556 | __index(__copy_construct(__other)) |
1557 | {} |
1558 | |
1559 | constexpr Variant(typename std::conditional<!__all_copy_constructible<_Types...>::value,Variant,__private_type>::type const& __other)=delete; |
1560 | |
1561 | template<typename _Type,typename ... _Args> |
1562 | explicit constexpr Variant(in_place_type_t<_Type>,_Args&& ... __args): |
1563 | __storage( |
1564 | in_place<__type_index<_Type,_Types...>::__value>, |
1565 | std::forward<_Args>(__args)...), |
1566 | __index(__type_index<_Type,_Types...>::__value) |
1567 | { |
1568 | static_assert(std::is_constructible<_Type,_Args...>::value,"Type must be constructible from args" ); |
1569 | } |
1570 | |
1571 | template<size_t _Index,typename ... _Args> |
1572 | explicit constexpr Variant(in_place_index_t<_Index>,_Args&& ... __args): |
1573 | __storage(in_place<_Index>,std::forward<_Args>(__args)...), |
1574 | __index(_Index) |
1575 | { |
1576 | static_assert(std::is_constructible<typename __indexed_type<_Index,_Types...>::__type,_Args...>::value,"Type must be constructible from args" ); |
1577 | } |
1578 | |
1579 | template<typename _Type> |
1580 | constexpr Variant(_Type&& __x): |
1581 | __storage( |
1582 | in_place< |
1583 | __type_index_to_construct<_Type,_Types...>::__value>, |
1584 | std::forward<_Type>(__x)), |
1585 | __index(__type_index_to_construct<_Type,_Types...>::__value) |
1586 | {} |
1587 | |
1588 | template<typename _Type, |
1589 | typename _Enable= |
1590 | typename std::enable_if< |
1591 | (__constructible_matches<std::initializer_list<_Type>,_Types...>::__type::__length>0) |
1592 | >::type> |
1593 | constexpr Variant(std::initializer_list<_Type> __x): |
1594 | __storage( |
1595 | in_place< |
1596 | __type_index_to_construct<std::initializer_list<_Type>,_Types...>::__value>, |
1597 | __x), |
1598 | __index(__type_index_to_construct<std::initializer_list<_Type>,_Types...>::__value) |
1599 | {} |
1600 | |
1601 | template<typename _Type> |
1602 | Variant& operator=(_Type&& __x){ |
1603 | constexpr size_t _Index= |
1604 | __type_index_to_construct<_Type,_Types...>::__value; |
1605 | if(_Index==__index){ |
1606 | get<_Index>(*this)=std::forward<_Type>(__x); |
1607 | } |
1608 | else{ |
1609 | __replace_construct<_Index>(std::forward<_Type>(__x)); |
1610 | } |
1611 | return *this; |
1612 | } |
1613 | |
1614 | Variant &operator=( |
1615 | typename std::conditional< |
1616 | !(__all_copy_constructible<_Types...>::value && |
1617 | __all_move_constructible<_Types...>::value && |
1618 | __all_copy_assignable<_Types...>::value), |
1619 | Variant, __private_type>::type const &__other) = delete; |
1620 | |
1621 | Variant &operator=( |
1622 | typename std::conditional< |
1623 | __all_copy_constructible<_Types...>::value && |
1624 | __all_move_constructible<_Types...>::value && |
1625 | __all_copy_assignable<_Types...>::value, |
1626 | Variant, __private_type>::type const &__other) { |
1627 | if (__other.valueless_by_exception()) { |
1628 | __destroy_self(); |
1629 | } |
1630 | else if(__other.index()==index()){ |
1631 | __copy_assign_op_table<Variant>::__apply[index()](this,__other); |
1632 | } |
1633 | else{ |
1634 | __replace_construct_helper::__op_table<Variant>::__copy_assign[ |
1635 | __other.index()](this,__other); |
1636 | } |
1637 | return *this; |
1638 | } |
1639 | Variant &operator=( |
1640 | typename std::conditional< |
1641 | !(__all_copy_constructible<_Types...>::value && |
1642 | __all_move_constructible<_Types...>::value && |
1643 | __all_copy_assignable<_Types...>::value), |
1644 | Variant, __private_type>::type &__other) = delete; |
1645 | |
1646 | Variant &operator=( |
1647 | typename std::conditional< |
1648 | __all_copy_constructible<_Types...>::value && |
1649 | __all_move_constructible<_Types...>::value && |
1650 | __all_copy_assignable<_Types...>::value, |
1651 | Variant, __private_type>::type &__other) { |
1652 | if(__other.valueless_by_exception()){ |
1653 | __destroy_self(); |
1654 | } |
1655 | else if(__other.index()==index()){ |
1656 | __copy_assign_op_table<Variant>::__apply[index()](this,__other); |
1657 | } |
1658 | else{ |
1659 | __replace_construct_helper::__op_table<Variant>::__copy_assign[ |
1660 | __other.index()](this,__other); |
1661 | } |
1662 | return *this; |
1663 | } |
1664 | Variant &operator=( |
1665 | typename std::conditional< |
1666 | !(__all_move_constructible<_Types...>::value && |
1667 | __all_move_assignable<_Types...>::value), |
1668 | Variant, __private_type>::type &&__other) = delete; |
1669 | |
1670 | Variant &operator=( |
1671 | typename std::conditional<__all_move_constructible<_Types...>::value && |
1672 | __all_move_assignable<_Types...>::value, |
1673 | Variant, __private_type>::type && |
1674 | __other) __NOEXCEPT_(__noexcept_variant_move_assign<_Types...>::value) { |
1675 | if (__other.valueless_by_exception()) { |
1676 | __destroy_self(); |
1677 | } |
1678 | else if(__other.index()==index()){ |
1679 | __move_assign_op_table<Variant>::__apply[index()](this,__other); |
1680 | __other.__destroy_self(); |
1681 | } |
1682 | else{ |
1683 | __replace_construct_helper::__op_table<Variant>::__move_assign[ |
1684 | __other.index()](this,__other); |
1685 | } |
1686 | return *this; |
1687 | } |
1688 | |
1689 | template<typename _Type,typename ... _Args> |
1690 | void emplace(_Args&& ... __args){ |
1691 | __direct_replace<__type_index<_Type,_Types...>::__value>( |
1692 | std::forward<_Args>(__args)...); |
1693 | } |
1694 | |
1695 | template<size_t _Index,typename ... _Args> |
1696 | void emplace(_Args&& ... __args){ |
1697 | __direct_replace<_Index>(std::forward<_Args>(__args)...); |
1698 | } |
1699 | |
1700 | constexpr bool valueless_by_exception() const __NOEXCEPT{ |
1701 | return __index==-1; |
1702 | } |
1703 | constexpr ptrdiff_t index() const __NOEXCEPT{ |
1704 | return __index; |
1705 | } |
1706 | |
1707 | void swap( |
1708 | typename std::conditional< |
1709 | __all_swappable<_Types...>::value && |
1710 | __all_move_constructible<_Types...>::value, |
1711 | Variant, __private_type>::type |
1712 | &__other) __NOEXCEPT_(__noexcept_variant_swap<_Types...>::value) { |
1713 | if (__other.index() == index()) { |
1714 | if(!valueless_by_exception()) |
1715 | __swap_op_table<Variant>::__apply[index()](*this,__other); |
1716 | } |
1717 | else{ |
1718 | Variant __temp(std::move(__other)); |
1719 | __other.__index=__other.__move_construct(*this); |
1720 | __index=__move_construct(__temp); |
1721 | } |
1722 | } |
1723 | }; |
1724 | |
1725 | template<> |
1726 | class Variant<>{ |
1727 | public: |
1728 | Variant()=delete; |
1729 | |
1730 | constexpr bool valueless_by_exception() const __NOEXCEPT{ |
1731 | return true; |
1732 | } |
1733 | constexpr ptrdiff_t index() const __NOEXCEPT{ |
1734 | return -1; |
1735 | } |
1736 | |
1737 | void swap(Variant&){} |
1738 | }; |
1739 | |
1740 | template <typename... _Types> |
1741 | typename std::enable_if<__all_swappable<_Types...>::value && |
1742 | __all_move_constructible<_Types...>::value, |
1743 | void>::type |
1744 | swap(Variant<_Types...> &__lhs, Variant<_Types...> &__rhs) __NOEXCEPT_( |
1745 | __noexcept_variant_swap<_Types...>::value) { |
1746 | __lhs.swap(__rhs); |
1747 | } |
1748 | |
1749 | template<ptrdiff_t _Index,typename ... _Types> |
1750 | struct __variant_accessor{ |
1751 | typedef typename __indexed_type<_Index,_Types...>::__type __type; |
1752 | static constexpr __type& get(Variant<_Types...>& __v){ |
1753 | return __v.__storage.__get(in_place<_Index>); |
1754 | } |
1755 | static constexpr __type const& get(Variant<_Types...> const& __v){ |
1756 | return __v.__storage.__get(in_place<_Index>); |
1757 | } |
1758 | static constexpr __type&& get(Variant<_Types...>&& __v){ |
1759 | return __v.__storage.__get_rref(in_place<_Index>); |
1760 | } |
1761 | static constexpr const __type&& get(Variant<_Types...> const&& __v){ |
1762 | return __v.__storage.__get_rref(in_place<_Index>); |
1763 | } |
1764 | }; |
1765 | |
1766 | template<typename _Type,typename ... _Types> |
1767 | constexpr _Type& get(Variant<_Types...>& __v){ |
1768 | return get<__type_index<_Type,_Types...>::__value>(__v); |
1769 | } |
1770 | |
1771 | template<typename _Type,typename ... _Types> |
1772 | constexpr _Type&& get(Variant<_Types...>&& __v){ |
1773 | return get<__type_index<_Type,_Types...>::__value>(std::move(__v)); |
1774 | } |
1775 | |
1776 | template<typename _Type,typename ... _Types> |
1777 | constexpr _Type const& get(Variant<_Types...> const& __v){ |
1778 | return get<__type_index<_Type,_Types...>::__value>(__v); |
1779 | } |
1780 | |
1781 | template<typename _Type,typename ... _Types> |
1782 | constexpr const _Type&& get(Variant<_Types...> const&& __v){ |
1783 | return get<__type_index<_Type,_Types...>::__value>(std::move(__v)); |
1784 | } |
1785 | |
1786 | |
1787 | template<ptrdiff_t _Index,typename ... _Types> |
1788 | constexpr typename __indexed_type<_Index,_Types...>::__type const& get(Variant<_Types...> const& __v){ |
1789 | return *( |
1790 | (_Index!=__v.index()) |
1791 | ? std::addressof(__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type const&>("Bad Variant index in get" )) |
1792 | : std::addressof(__variant_accessor<_Index,_Types...>::get(__v)) |
1793 | ); |
1794 | } |
1795 | |
1796 | template<ptrdiff_t _Index,typename ... _Types> |
1797 | constexpr typename __indexed_type<_Index,_Types...>::__type& get(Variant<_Types...>& __v){ |
1798 | return *( |
1799 | (_Index!=__v.index()) |
1800 | ? std::addressof(__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type&>("Bad Variant index in get" )) |
1801 | : std::addressof(__variant_accessor<_Index,_Types...>::get(__v)) |
1802 | ); |
1803 | } |
1804 | |
1805 | template<ptrdiff_t _Index,typename ... _Types> |
1806 | constexpr typename __indexed_type<_Index,_Types...>::__type&& get(Variant<_Types...>&& __v){ |
1807 | return __variant_accessor<_Index,_Types...>::get( |
1808 | (((_Index!=__v.index()) ? __throw_bad_variant_access<int>("Bad Variant index in get" ) : 0), std::move(__v)) |
1809 | ); |
1810 | } |
1811 | |
1812 | template<ptrdiff_t _Index,typename ... _Types> |
1813 | constexpr const typename __indexed_type<_Index,_Types...>::__type&& get(Variant<_Types...> const&& __v){ |
1814 | return __variant_accessor<_Index,_Types...>::get( |
1815 | (((_Index!=__v.index()) ? __throw_bad_variant_access<int>("Bad Variant index in get" ) : 0), std::move(__v)) |
1816 | ); |
1817 | } |
1818 | |
1819 | template<typename _Type,typename ... _Types> |
1820 | constexpr std::add_pointer_t<_Type> get_if(Variant<_Types...>& __v){ |
1821 | return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:std::addressof(get<_Type>(__v)); |
1822 | } |
1823 | |
1824 | template<typename _Type,typename ... _Types> |
1825 | constexpr std::add_pointer_t<_Type const> get_if(Variant<_Types...> const& __v){ |
1826 | return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:std::addressof(get<_Type>(__v)); |
1827 | } |
1828 | |
1829 | template<ptrdiff_t _Index,typename ... _Types> |
1830 | constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type> get_if(Variant<_Types...>& __v){ |
1831 | return ((_Index!=__v.index())?nullptr: |
1832 | std::addressof(__variant_accessor<_Index,_Types...>::get(__v))); |
1833 | } |
1834 | |
1835 | template<ptrdiff_t _Index,typename ... _Types> |
1836 | constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type const> get_if( |
1837 | Variant<_Types...> const& __v){ |
1838 | return ((_Index!=__v.index())?nullptr: |
1839 | std::addressof(__variant_accessor<_Index,_Types...>::get(__v))); |
1840 | } |
1841 | |
1842 | template<typename _Type,typename ... _Types> |
1843 | constexpr bool holds_alternative(Variant<_Types...> const& __v) __NOEXCEPT{ |
1844 | return __v.index()==__type_index<_Type,_Types...>::__value; |
1845 | } |
1846 | |
1847 | template<typename _Visitor,typename ... _Types> |
1848 | struct __visitor_return_type; |
1849 | |
1850 | template<typename _Visitor> |
1851 | struct __visitor_return_type<_Visitor>{ |
1852 | typedef decltype(std::declval<_Visitor&>()()) __type; |
1853 | }; |
1854 | |
1855 | template<typename _Visitor,typename _Head,typename ... _Rest> |
1856 | struct __visitor_return_type<_Visitor,_Head,_Rest...>{ |
1857 | typedef decltype(std::declval<_Visitor&>()(std::declval<_Head&>())) __type; |
1858 | }; |
1859 | |
1860 | template<typename _Visitor,typename ... _Types> |
1861 | struct __visitor_table{ |
1862 | typedef Variant<_Types...> __variant_type; |
1863 | typedef typename __visitor_return_type<_Visitor,_Types...>::__type __return_type; |
1864 | typedef __return_type (*__func_type)(_Visitor&,__variant_type&); |
1865 | |
1866 | template<typename _Type> |
1867 | static __return_type __trampoline_func(_Visitor& __visitor,__variant_type& __v){ |
1868 | return __visitor(get<_Type>(__v)); |
1869 | } |
1870 | |
1871 | static const __func_type __trampoline[sizeof...(_Types)]; |
1872 | }; |
1873 | |
1874 | template<typename _Visitor,typename ... _Types> |
1875 | const typename __visitor_table<_Visitor,_Types...>::__func_type __visitor_table<_Visitor,_Types...>::__trampoline[sizeof...(_Types)]={ |
1876 | &__trampoline_func<_Types>... |
1877 | }; |
1878 | |
1879 | template<typename _Visitor,typename ... _Types> |
1880 | constexpr typename __visitor_return_type<_Visitor,_Types...>::__type |
1881 | visit(_Visitor&& __visitor,Variant<_Types...>& __v){ |
1882 | return (__v.valueless_by_exception()) |
1883 | ? __throw_bad_variant_access<typename __visitor_return_type<_Visitor,_Types...>::__type>("Visiting of empty Variant" ) |
1884 | : __visitor_table<_Visitor,_Types...>::__trampoline[__v.index()](__visitor,__v); |
1885 | } |
1886 | |
1887 | template<typename _Visitor,typename ... _Variants> |
1888 | struct __multi_visitor_return_type{ |
1889 | typedef decltype(std::declval<_Visitor&>()(get<0>(std::declval<_Variants>())...)) |
1890 | __type; |
1891 | }; |
1892 | |
1893 | template<size_t _VariantIndex,typename _Indices> |
1894 | struct __visit_helper; |
1895 | |
1896 | template<ptrdiff_t ... _Indices> |
1897 | struct __visit_helper<0,__index_sequence<_Indices...>>{ |
1898 | template<typename _Visitor,typename ... _Variants> |
1899 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1900 | __visit(_Visitor& __visitor,_Variants& ... __v){ |
1901 | return __visitor(get<_Indices>(__v)...); |
1902 | } |
1903 | }; |
1904 | |
1905 | template<size_t _Index,typename ... _Args> |
1906 | struct __arg_selector_t; |
1907 | |
1908 | template<typename _Head,typename ... _Rest> |
1909 | struct __arg_selector_t<0,_Head,_Rest...>{ |
1910 | typedef _Head __type; |
1911 | |
1912 | static constexpr __type& __select(_Head& __head,_Rest& ...){ |
1913 | return __head; |
1914 | } |
1915 | }; |
1916 | |
1917 | template<size_t _Index,typename _Head,typename ... _Rest> |
1918 | struct __arg_selector_t<_Index,_Head,_Rest...>{ |
1919 | typedef typename __arg_selector_t<_Index-1,_Rest...>::__type __type; |
1920 | static constexpr __type& __select(_Head&,_Rest& ... __rest){ |
1921 | return __arg_selector_t<_Index-1,_Rest...>::__select(__rest...); |
1922 | } |
1923 | }; |
1924 | |
1925 | template<size_t _Index,typename ... _Args> |
1926 | constexpr typename __arg_selector_t<_Index,_Args...>::__type&& __arg_selector(_Args&& ... __args){ |
1927 | return std::forward<typename __arg_selector_t<_Index,_Args...>::__type>( |
1928 | __arg_selector_t<_Index,_Args...>::__select(__args...)); |
1929 | } |
1930 | |
1931 | template<ptrdiff_t _Index,size_t _VariantIndex,ptrdiff_t ... _Indices> |
1932 | struct __visit_helper2{ |
1933 | template<typename _Visitor,typename ... _Variants> |
1934 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1935 | __visit(_Visitor& __visitor,_Variants&& ... __v){ |
1936 | return (__arg_selector<_VariantIndex-1>(__v...).index()==_Index) |
1937 | ? __visit_helper<_VariantIndex-1,__index_sequence<_Index,_Indices...>>::__visit(__visitor,std::forward<_Variants>(__v)...) |
1938 | : __visit_helper2<_Index-1,_VariantIndex,_Indices...>::__visit(__visitor,std::forward<_Variants>(__v)...); |
1939 | } |
1940 | }; |
1941 | |
1942 | template<size_t _VariantIndex,ptrdiff_t ... _Indices> |
1943 | struct __visit_helper2<-1,_VariantIndex,_Indices...>{ |
1944 | template<typename _Visitor,typename ... _Variants> |
1945 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1946 | __visit(_Visitor&,_Variants&& ...){ |
1947 | return __throw_bad_variant_access<typename __multi_visitor_return_type<_Visitor,_Variants...>::__type>("Visiting of empty Variant" ); |
1948 | } |
1949 | }; |
1950 | |
1951 | template<typename _Variant> |
1952 | struct __variant_type_count; |
1953 | |
1954 | template<typename ... _Types> |
1955 | struct __variant_type_count<Variant<_Types...>>{ |
1956 | static constexpr size_t __value=sizeof...(_Types); |
1957 | }; |
1958 | |
1959 | template<typename _Variant> |
1960 | struct __variant_type_count<_Variant&>{ |
1961 | static constexpr size_t __value=__variant_type_count<_Variant>::__value; |
1962 | }; |
1963 | |
1964 | template<typename _Variant> |
1965 | struct __variant_type_count<_Variant const&>{ |
1966 | static constexpr size_t __value=__variant_type_count<_Variant>::__value; |
1967 | }; |
1968 | |
1969 | template<size_t _VariantIndex,ptrdiff_t ... _Indices> |
1970 | struct __visit_helper<_VariantIndex,__index_sequence<_Indices...>>{ |
1971 | |
1972 | template<typename _Visitor,typename ... _Variants> |
1973 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1974 | __visit(_Visitor& __visitor,_Variants&& ... __v){ |
1975 | return __visit_helper2< |
1976 | __variant_type_count< |
1977 | typename __arg_selector_t< |
1978 | _VariantIndex-1,_Variants...>::__type>::__value-1, |
1979 | _VariantIndex,_Indices...>::__visit( |
1980 | __visitor,std::forward<_Variants&&>(__v)...); |
1981 | } |
1982 | }; |
1983 | |
1984 | template<typename _Visitor,typename ... _Variants> |
1985 | constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1986 | visit(_Visitor&& __visitor,_Variants&& ... __v){ |
1987 | return __visit_helper<sizeof...(_Variants),__index_sequence<>>::__visit( |
1988 | __visitor,std::forward<_Variants>(__v)...); |
1989 | } |
1990 | |
1991 | template<typename ... _Types> |
1992 | constexpr bool operator==(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
1993 | return (__lhs.index()==__rhs.index()) && |
1994 | ((__lhs.index()==-1) || |
1995 | __equality_op_table<Variant<_Types...>>::__equality_compare[__lhs.index()]( |
1996 | __lhs,__rhs)); |
1997 | } |
1998 | |
1999 | template<typename ... _Types> |
2000 | constexpr bool operator!=(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2001 | return !(__lhs==__rhs); |
2002 | } |
2003 | |
2004 | template<typename ... _Types> |
2005 | constexpr bool operator<(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2006 | return (__lhs.index()<__rhs.index()) || |
2007 | ((__lhs.index()==__rhs.index()) && |
2008 | ((__lhs.index()!=-1) && |
2009 | __less_than_op_table<Variant<_Types...>>:: |
2010 | __less_than_compare[__lhs.index()](__lhs,__rhs))); |
2011 | } |
2012 | |
2013 | template<typename ... _Types> |
2014 | constexpr bool operator>(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2015 | return __rhs<__lhs; |
2016 | } |
2017 | |
2018 | template<typename ... _Types> |
2019 | constexpr bool operator>=(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2020 | return !(__lhs<__rhs); |
2021 | } |
2022 | |
2023 | template<typename ... _Types> |
2024 | constexpr bool operator<=(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2025 | return !(__lhs>__rhs); |
2026 | } |
2027 | |
2028 | struct Monostate{}; |
2029 | |
2030 | constexpr bool operator==(Monostate const&, Monostate const&) { return true; } |
2031 | constexpr bool operator!=(Monostate const&, Monostate const&) { return false; } |
2032 | constexpr bool operator>=(Monostate const&, Monostate const&) { return true; } |
2033 | constexpr bool operator<=(Monostate const&, Monostate const&) { return true; } |
2034 | constexpr bool operator>(Monostate const&, Monostate const&) { return false; } |
2035 | constexpr bool operator<(Monostate const&, Monostate const&) { return false; } |
2036 | |
2037 | struct __hash_visitor{ |
2038 | template<typename _Type> |
2039 | size_t operator()(_Type const& __x){ |
2040 | return std::hash<_Type>()(__x); |
2041 | } |
2042 | }; |
2043 | |
2044 | // -- WebKit Additions -- |
2045 | |
2046 | template<class V, class... F> |
2047 | auto switchOn(V&& v, F&&... f) -> decltype(WTF::visit(makeVisitor(std::forward<F>(f)...), std::forward<V>(v))) |
2048 | { |
2049 | return WTF::visit(makeVisitor(std::forward<F>(f)...), std::forward<V>(v)); |
2050 | } |
2051 | |
2052 | } // namespace WTF |
2053 | |
2054 | namespace std { |
2055 | |
2056 | template<> |
2057 | struct hash<WTF::Monostate>{ |
2058 | size_t operator()(WTF::Monostate) __NOEXCEPT{ |
2059 | return 42; |
2060 | } |
2061 | }; |
2062 | |
2063 | template<typename ... _Types> |
2064 | struct hash<WTF::Variant<_Types...>>{ |
2065 | size_t operator()(WTF::Variant<_Types...> const &v) __NOEXCEPT { |
2066 | return std::hash<ptrdiff_t>()(v.index()) ^ WTF::visit(WTF::__hash_visitor(), v); |
2067 | } |
2068 | }; |
2069 | |
2070 | } // namespace std |
2071 | |
2072 | using WTF::Monostate; |
2073 | using WTF::Variant; |
2074 | |
2075 | #endif // !COMPILER(CLANG) || WTF_CPP_STD_VER >= 14 |
2076 | |
2077 | #if COMPILER(MSVC) |
2078 | #pragma warning(pop) |
2079 | #endif |
2080 | |