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 | WTF_MAKE_FAST_ALLOCATED; |
1444 | typedef __variant_base<Variant<_Types...>,__all_trivially_destructible<_Types...>::__value> __base_type; |
1445 | friend __base_type; |
1446 | friend struct __copy_construct_op_table<Variant>; |
1447 | friend struct __copy_assign_op_table<Variant>; |
1448 | friend struct __move_construct_op_table<Variant>; |
1449 | friend struct __move_assign_op_table<Variant>; |
1450 | friend struct __destroy_op_table<Variant>; |
1451 | |
1452 | template<ptrdiff_t _Index,typename ... _Types2> |
1453 | friend struct __variant_accessor; |
1454 | |
1455 | friend struct __replace_construct_helper; |
1456 | |
1457 | typedef __variant_data<_Types...> __storage_type; |
1458 | __storage_type __storage; |
1459 | typename __discriminator_type<sizeof ... (_Types)>::__type __index; |
1460 | |
1461 | template<size_t _Index,typename ... _Args> |
1462 | size_t __emplace_construct(_Args&& ... __args){ |
1463 | new(&__storage) __storage_type( |
1464 | in_place<_Index>,std::forward<_Args>(__args)...); |
1465 | return _Index; |
1466 | } |
1467 | |
1468 | void __destroy_self(){ |
1469 | if(valueless_by_exception()) |
1470 | return; |
1471 | __destroy_op_table<Variant>::__apply[index()](this); |
1472 | __index=-1; |
1473 | } |
1474 | |
1475 | ptrdiff_t __move_construct(Variant& __other){ |
1476 | ptrdiff_t const __other_index=__other.index(); |
1477 | if(__other_index==-1) |
1478 | return -1; |
1479 | __move_construct_op_table<Variant>::__apply[__other_index](this,__other); |
1480 | __other.__destroy_self(); |
1481 | return __other_index; |
1482 | } |
1483 | |
1484 | ptrdiff_t __copy_construct(Variant const& __other){ |
1485 | ptrdiff_t const __other_index=__other.index(); |
1486 | if(__other_index==-1) |
1487 | return -1; |
1488 | __copy_construct_op_table<Variant>::__apply[__other_index](this,__other); |
1489 | return __other_index; |
1490 | } |
1491 | |
1492 | template<size_t _Index,typename ... _Args> |
1493 | void __replace_construct(_Args&& ... __args){ |
1494 | typedef typename __indexed_type<_Index,_Types...>::__type __this_type; |
1495 | __replace_construct_helper::__helper< |
1496 | _Index, |
1497 | __storage_nothrow_constructible<__this_type,_Args...>::__value || |
1498 | (sizeof...(_Types)==1), |
1499 | __storage_nothrow_move_constructible<__this_type>::__value, |
1500 | __other_storage_nothrow_move_constructible< |
1501 | _Index,_Types...>::__value |
1502 | >::__trampoline(*this,std::forward<_Args>(__args)...); |
1503 | } |
1504 | |
1505 | template<size_t _Index,typename ... _Args> |
1506 | void __two_stage_replace(_Args&& ... __args){ |
1507 | typedef typename __indexed_type<_Index,_Types...>::__type __type; |
1508 | __variant_data<__type> __local( |
1509 | in_place<0>,std::forward<_Args>(__args)...); |
1510 | __destroy_self(); |
1511 | __emplace_construct<_Index>( |
1512 | std::move(__local.__get(in_place<0>))); |
1513 | __index=_Index; |
1514 | __local.__destroy(in_place<0>); |
1515 | } |
1516 | |
1517 | template<size_t _Index,typename ... _Args> |
1518 | void __local_backup_replace(_Args&& ... __args){ |
1519 | __backup_storage<_Index,_Types...> __backup(__index,__storage); |
1520 | __emplace_construct<_Index>(std::forward<_Args>(__args)...); |
1521 | __index=_Index; |
1522 | __backup.__destroy(); |
1523 | } |
1524 | |
1525 | template<size_t _Index,typename ... _Args> |
1526 | void __direct_replace(_Args&& ... __args) { |
1527 | __destroy_self(); |
1528 | __emplace_construct<_Index>(std::forward<_Args>(__args)...); |
1529 | __index=_Index; |
1530 | } |
1531 | |
1532 | struct __private_type{}; |
1533 | |
1534 | public: |
1535 | constexpr Variant() |
1536 | __NOEXCEPT_(noexcept(typename __indexed_type<0,_Types...>::__type())): |
1537 | __storage(in_place<0>), |
1538 | __index(0) |
1539 | {} |
1540 | |
1541 | constexpr Variant(typename std::conditional<__all_move_constructible<_Types...>::value,Variant,__private_type>::type&& __other) |
1542 | __NOEXCEPT_(__noexcept_variant_move_construct<_Types...>::value): |
1543 | __index(__move_construct(__other)) |
1544 | {} |
1545 | |
1546 | constexpr Variant(typename std::conditional<!__all_move_constructible<_Types...>::value,Variant,__private_type>::type&& __other)=delete; |
1547 | |
1548 | constexpr Variant(typename std::conditional<__all_copy_constructible<_Types...>::value,Variant,__private_type>::type& __other) |
1549 | __NOEXCEPT_(__noexcept_variant_non_const_copy_construct<_Types...>::value): |
1550 | __index(__copy_construct(__other)) |
1551 | {} |
1552 | |
1553 | constexpr Variant(typename std::conditional<!__all_copy_constructible<_Types...>::value,Variant,__private_type>::type& __other)=delete; |
1554 | |
1555 | constexpr Variant(typename std::conditional<__all_copy_constructible<_Types...>::value,Variant,__private_type>::type const& __other) |
1556 | __NOEXCEPT_(__noexcept_variant_const_copy_construct<_Types...>::value): |
1557 | __index(__copy_construct(__other)) |
1558 | {} |
1559 | |
1560 | constexpr Variant(typename std::conditional<!__all_copy_constructible<_Types...>::value,Variant,__private_type>::type const& __other)=delete; |
1561 | |
1562 | template<typename _Type,typename ... _Args> |
1563 | explicit constexpr Variant(in_place_type_t<_Type>,_Args&& ... __args): |
1564 | __storage( |
1565 | in_place<__type_index<_Type,_Types...>::__value>, |
1566 | std::forward<_Args>(__args)...), |
1567 | __index(__type_index<_Type,_Types...>::__value) |
1568 | { |
1569 | static_assert(std::is_constructible<_Type,_Args...>::value,"Type must be constructible from args" ); |
1570 | } |
1571 | |
1572 | template<size_t _Index,typename ... _Args> |
1573 | explicit constexpr Variant(in_place_index_t<_Index>,_Args&& ... __args): |
1574 | __storage(in_place<_Index>,std::forward<_Args>(__args)...), |
1575 | __index(_Index) |
1576 | { |
1577 | static_assert(std::is_constructible<typename __indexed_type<_Index,_Types...>::__type,_Args...>::value,"Type must be constructible from args" ); |
1578 | } |
1579 | |
1580 | template<typename _Type> |
1581 | constexpr Variant(_Type&& __x): |
1582 | __storage( |
1583 | in_place< |
1584 | __type_index_to_construct<_Type,_Types...>::__value>, |
1585 | std::forward<_Type>(__x)), |
1586 | __index(__type_index_to_construct<_Type,_Types...>::__value) |
1587 | {} |
1588 | |
1589 | template<typename _Type, |
1590 | typename _Enable= |
1591 | typename std::enable_if< |
1592 | (__constructible_matches<std::initializer_list<_Type>,_Types...>::__type::__length>0) |
1593 | >::type> |
1594 | constexpr Variant(std::initializer_list<_Type> __x): |
1595 | __storage( |
1596 | in_place< |
1597 | __type_index_to_construct<std::initializer_list<_Type>,_Types...>::__value>, |
1598 | __x), |
1599 | __index(__type_index_to_construct<std::initializer_list<_Type>,_Types...>::__value) |
1600 | {} |
1601 | |
1602 | template<typename _Type> |
1603 | Variant& operator=(_Type&& __x){ |
1604 | constexpr size_t _Index= |
1605 | __type_index_to_construct<_Type,_Types...>::__value; |
1606 | if(_Index==__index){ |
1607 | get<_Index>(*this)=std::forward<_Type>(__x); |
1608 | } |
1609 | else{ |
1610 | __replace_construct<_Index>(std::forward<_Type>(__x)); |
1611 | } |
1612 | return *this; |
1613 | } |
1614 | |
1615 | Variant &operator=( |
1616 | typename std::conditional< |
1617 | !(__all_copy_constructible<_Types...>::value && |
1618 | __all_move_constructible<_Types...>::value && |
1619 | __all_copy_assignable<_Types...>::value), |
1620 | Variant, __private_type>::type const &__other) = delete; |
1621 | |
1622 | Variant &operator=( |
1623 | typename std::conditional< |
1624 | __all_copy_constructible<_Types...>::value && |
1625 | __all_move_constructible<_Types...>::value && |
1626 | __all_copy_assignable<_Types...>::value, |
1627 | Variant, __private_type>::type const &__other) { |
1628 | if (__other.valueless_by_exception()) { |
1629 | __destroy_self(); |
1630 | } |
1631 | else if(__other.index()==index()){ |
1632 | __copy_assign_op_table<Variant>::__apply[index()](this,__other); |
1633 | } |
1634 | else{ |
1635 | __replace_construct_helper::__op_table<Variant>::__copy_assign[ |
1636 | __other.index()](this,__other); |
1637 | } |
1638 | return *this; |
1639 | } |
1640 | Variant &operator=( |
1641 | typename std::conditional< |
1642 | !(__all_copy_constructible<_Types...>::value && |
1643 | __all_move_constructible<_Types...>::value && |
1644 | __all_copy_assignable<_Types...>::value), |
1645 | Variant, __private_type>::type &__other) = delete; |
1646 | |
1647 | Variant &operator=( |
1648 | typename std::conditional< |
1649 | __all_copy_constructible<_Types...>::value && |
1650 | __all_move_constructible<_Types...>::value && |
1651 | __all_copy_assignable<_Types...>::value, |
1652 | Variant, __private_type>::type &__other) { |
1653 | if(__other.valueless_by_exception()){ |
1654 | __destroy_self(); |
1655 | } |
1656 | else if(__other.index()==index()){ |
1657 | __copy_assign_op_table<Variant>::__apply[index()](this,__other); |
1658 | } |
1659 | else{ |
1660 | __replace_construct_helper::__op_table<Variant>::__copy_assign[ |
1661 | __other.index()](this,__other); |
1662 | } |
1663 | return *this; |
1664 | } |
1665 | Variant &operator=( |
1666 | typename std::conditional< |
1667 | !(__all_move_constructible<_Types...>::value && |
1668 | __all_move_assignable<_Types...>::value), |
1669 | Variant, __private_type>::type &&__other) = delete; |
1670 | |
1671 | Variant &operator=( |
1672 | typename std::conditional<__all_move_constructible<_Types...>::value && |
1673 | __all_move_assignable<_Types...>::value, |
1674 | Variant, __private_type>::type && |
1675 | __other) __NOEXCEPT_(__noexcept_variant_move_assign<_Types...>::value) { |
1676 | if (__other.valueless_by_exception()) { |
1677 | __destroy_self(); |
1678 | } |
1679 | else if(__other.index()==index()){ |
1680 | __move_assign_op_table<Variant>::__apply[index()](this,__other); |
1681 | __other.__destroy_self(); |
1682 | } |
1683 | else{ |
1684 | __replace_construct_helper::__op_table<Variant>::__move_assign[ |
1685 | __other.index()](this,__other); |
1686 | } |
1687 | return *this; |
1688 | } |
1689 | |
1690 | template<typename _Type,typename ... _Args> |
1691 | void emplace(_Args&& ... __args){ |
1692 | __direct_replace<__type_index<_Type,_Types...>::__value>( |
1693 | std::forward<_Args>(__args)...); |
1694 | } |
1695 | |
1696 | template<size_t _Index,typename ... _Args> |
1697 | void emplace(_Args&& ... __args){ |
1698 | __direct_replace<_Index>(std::forward<_Args>(__args)...); |
1699 | } |
1700 | |
1701 | constexpr bool valueless_by_exception() const __NOEXCEPT{ |
1702 | return __index==-1; |
1703 | } |
1704 | constexpr ptrdiff_t index() const __NOEXCEPT{ |
1705 | return __index; |
1706 | } |
1707 | |
1708 | void swap( |
1709 | typename std::conditional< |
1710 | __all_swappable<_Types...>::value && |
1711 | __all_move_constructible<_Types...>::value, |
1712 | Variant, __private_type>::type |
1713 | &__other) __NOEXCEPT_(__noexcept_variant_swap<_Types...>::value) { |
1714 | if (__other.index() == index()) { |
1715 | if(!valueless_by_exception()) |
1716 | __swap_op_table<Variant>::__apply[index()](*this,__other); |
1717 | } |
1718 | else{ |
1719 | Variant __temp(std::move(__other)); |
1720 | __other.__index=__other.__move_construct(*this); |
1721 | __index=__move_construct(__temp); |
1722 | } |
1723 | } |
1724 | }; |
1725 | |
1726 | template<> |
1727 | class Variant<>{ |
1728 | public: |
1729 | WTF_MAKE_FAST_ALLOCATED; |
1730 | Variant()=delete; |
1731 | |
1732 | constexpr bool valueless_by_exception() const __NOEXCEPT{ |
1733 | return true; |
1734 | } |
1735 | constexpr ptrdiff_t index() const __NOEXCEPT{ |
1736 | return -1; |
1737 | } |
1738 | |
1739 | void swap(Variant&){} |
1740 | }; |
1741 | |
1742 | template <typename... _Types> |
1743 | typename std::enable_if<__all_swappable<_Types...>::value && |
1744 | __all_move_constructible<_Types...>::value, |
1745 | void>::type |
1746 | swap(Variant<_Types...> &__lhs, Variant<_Types...> &__rhs) __NOEXCEPT_( |
1747 | __noexcept_variant_swap<_Types...>::value) { |
1748 | __lhs.swap(__rhs); |
1749 | } |
1750 | |
1751 | template<ptrdiff_t _Index,typename ... _Types> |
1752 | struct __variant_accessor{ |
1753 | typedef typename __indexed_type<_Index,_Types...>::__type __type; |
1754 | static constexpr __type& get(Variant<_Types...>& __v){ |
1755 | return __v.__storage.__get(in_place<_Index>); |
1756 | } |
1757 | static constexpr __type const& get(Variant<_Types...> const& __v){ |
1758 | return __v.__storage.__get(in_place<_Index>); |
1759 | } |
1760 | static constexpr __type&& get(Variant<_Types...>&& __v){ |
1761 | return __v.__storage.__get_rref(in_place<_Index>); |
1762 | } |
1763 | static constexpr const __type&& get(Variant<_Types...> const&& __v){ |
1764 | return __v.__storage.__get_rref(in_place<_Index>); |
1765 | } |
1766 | }; |
1767 | |
1768 | template<typename _Type,typename ... _Types> |
1769 | constexpr _Type& get(Variant<_Types...>& __v){ |
1770 | return get<__type_index<_Type,_Types...>::__value>(__v); |
1771 | } |
1772 | |
1773 | template<typename _Type,typename ... _Types> |
1774 | constexpr _Type&& get(Variant<_Types...>&& __v){ |
1775 | return get<__type_index<_Type,_Types...>::__value>(std::move(__v)); |
1776 | } |
1777 | |
1778 | template<typename _Type,typename ... _Types> |
1779 | constexpr _Type const& get(Variant<_Types...> const& __v){ |
1780 | return get<__type_index<_Type,_Types...>::__value>(__v); |
1781 | } |
1782 | |
1783 | template<typename _Type,typename ... _Types> |
1784 | constexpr const _Type&& get(Variant<_Types...> const&& __v){ |
1785 | return get<__type_index<_Type,_Types...>::__value>(std::move(__v)); |
1786 | } |
1787 | |
1788 | |
1789 | template<ptrdiff_t _Index,typename ... _Types> |
1790 | constexpr typename __indexed_type<_Index,_Types...>::__type const& get(Variant<_Types...> const& __v){ |
1791 | return *( |
1792 | (_Index!=__v.index()) |
1793 | ? std::addressof(__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type const&>("Bad Variant index in get" )) |
1794 | : std::addressof(__variant_accessor<_Index,_Types...>::get(__v)) |
1795 | ); |
1796 | } |
1797 | |
1798 | template<ptrdiff_t _Index,typename ... _Types> |
1799 | constexpr typename __indexed_type<_Index,_Types...>::__type& get(Variant<_Types...>& __v){ |
1800 | return *( |
1801 | (_Index!=__v.index()) |
1802 | ? std::addressof(__throw_bad_variant_access<typename __indexed_type<_Index,_Types...>::__type&>("Bad Variant index in get" )) |
1803 | : std::addressof(__variant_accessor<_Index,_Types...>::get(__v)) |
1804 | ); |
1805 | } |
1806 | |
1807 | template<ptrdiff_t _Index,typename ... _Types> |
1808 | constexpr typename __indexed_type<_Index,_Types...>::__type&& get(Variant<_Types...>&& __v){ |
1809 | return __variant_accessor<_Index,_Types...>::get( |
1810 | (((_Index!=__v.index()) ? __throw_bad_variant_access<int>("Bad Variant index in get" ) : 0), std::move(__v)) |
1811 | ); |
1812 | } |
1813 | |
1814 | template<ptrdiff_t _Index,typename ... _Types> |
1815 | constexpr const typename __indexed_type<_Index,_Types...>::__type&& get(Variant<_Types...> const&& __v){ |
1816 | return __variant_accessor<_Index,_Types...>::get( |
1817 | (((_Index!=__v.index()) ? __throw_bad_variant_access<int>("Bad Variant index in get" ) : 0), std::move(__v)) |
1818 | ); |
1819 | } |
1820 | |
1821 | template<typename _Type,typename ... _Types> |
1822 | constexpr std::add_pointer_t<_Type> get_if(Variant<_Types...>& __v){ |
1823 | return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:std::addressof(get<_Type>(__v)); |
1824 | } |
1825 | |
1826 | template<typename _Type,typename ... _Types> |
1827 | constexpr std::add_pointer_t<_Type const> get_if(Variant<_Types...> const& __v){ |
1828 | return (__type_index<_Type,_Types...>::__value!=__v.index())?nullptr:std::addressof(get<_Type>(__v)); |
1829 | } |
1830 | |
1831 | template<ptrdiff_t _Index,typename ... _Types> |
1832 | constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type> get_if(Variant<_Types...>& __v){ |
1833 | return ((_Index!=__v.index())?nullptr: |
1834 | std::addressof(__variant_accessor<_Index,_Types...>::get(__v))); |
1835 | } |
1836 | |
1837 | template<ptrdiff_t _Index,typename ... _Types> |
1838 | constexpr std::add_pointer_t<typename __indexed_type<_Index,_Types...>::__type const> get_if( |
1839 | Variant<_Types...> const& __v){ |
1840 | return ((_Index!=__v.index())?nullptr: |
1841 | std::addressof(__variant_accessor<_Index,_Types...>::get(__v))); |
1842 | } |
1843 | |
1844 | template<typename _Type,typename ... _Types> |
1845 | constexpr bool holds_alternative(Variant<_Types...> const& __v) __NOEXCEPT{ |
1846 | return __v.index()==__type_index<_Type,_Types...>::__value; |
1847 | } |
1848 | |
1849 | template<typename _Visitor,typename ... _Types> |
1850 | struct __visitor_return_type; |
1851 | |
1852 | template<typename _Visitor> |
1853 | struct __visitor_return_type<_Visitor>{ |
1854 | typedef decltype(std::declval<_Visitor&>()()) __type; |
1855 | }; |
1856 | |
1857 | template<typename _Visitor,typename _Head,typename ... _Rest> |
1858 | struct __visitor_return_type<_Visitor,_Head,_Rest...>{ |
1859 | typedef decltype(std::declval<_Visitor&>()(std::declval<_Head&>())) __type; |
1860 | }; |
1861 | |
1862 | template<typename _Visitor,typename ... _Types> |
1863 | struct __visitor_table{ |
1864 | typedef Variant<_Types...> __variant_type; |
1865 | typedef typename __visitor_return_type<_Visitor,_Types...>::__type __return_type; |
1866 | typedef __return_type (*__func_type)(_Visitor&,__variant_type&); |
1867 | |
1868 | template<typename _Type> |
1869 | static __return_type __trampoline_func(_Visitor& __visitor,__variant_type& __v){ |
1870 | return __visitor(get<_Type>(__v)); |
1871 | } |
1872 | |
1873 | static const __func_type __trampoline[sizeof...(_Types)]; |
1874 | }; |
1875 | |
1876 | template<typename _Visitor,typename ... _Types> |
1877 | const typename __visitor_table<_Visitor,_Types...>::__func_type __visitor_table<_Visitor,_Types...>::__trampoline[sizeof...(_Types)]={ |
1878 | &__trampoline_func<_Types>... |
1879 | }; |
1880 | |
1881 | template<typename _Visitor,typename ... _Types> |
1882 | constexpr typename __visitor_return_type<_Visitor,_Types...>::__type |
1883 | visit(_Visitor&& __visitor,Variant<_Types...>& __v){ |
1884 | return (__v.valueless_by_exception()) |
1885 | ? __throw_bad_variant_access<typename __visitor_return_type<_Visitor,_Types...>::__type>("Visiting of empty Variant" ) |
1886 | : __visitor_table<_Visitor,_Types...>::__trampoline[__v.index()](__visitor,__v); |
1887 | } |
1888 | |
1889 | template<typename _Visitor,typename ... _Variants> |
1890 | struct __multi_visitor_return_type{ |
1891 | typedef decltype(std::declval<_Visitor&>()(get<0>(std::declval<_Variants>())...)) |
1892 | __type; |
1893 | }; |
1894 | |
1895 | template<size_t _VariantIndex,typename _Indices> |
1896 | struct __visit_helper; |
1897 | |
1898 | template<ptrdiff_t ... _Indices> |
1899 | struct __visit_helper<0,__index_sequence<_Indices...>>{ |
1900 | template<typename _Visitor,typename ... _Variants> |
1901 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1902 | __visit(_Visitor& __visitor,_Variants& ... __v){ |
1903 | return __visitor(get<_Indices>(__v)...); |
1904 | } |
1905 | }; |
1906 | |
1907 | template<size_t _Index,typename ... _Args> |
1908 | struct __arg_selector_t; |
1909 | |
1910 | template<typename _Head,typename ... _Rest> |
1911 | struct __arg_selector_t<0,_Head,_Rest...>{ |
1912 | typedef _Head __type; |
1913 | |
1914 | static constexpr __type& __select(_Head& __head,_Rest& ...){ |
1915 | return __head; |
1916 | } |
1917 | }; |
1918 | |
1919 | template<size_t _Index,typename _Head,typename ... _Rest> |
1920 | struct __arg_selector_t<_Index,_Head,_Rest...>{ |
1921 | typedef typename __arg_selector_t<_Index-1,_Rest...>::__type __type; |
1922 | static constexpr __type& __select(_Head&,_Rest& ... __rest){ |
1923 | return __arg_selector_t<_Index-1,_Rest...>::__select(__rest...); |
1924 | } |
1925 | }; |
1926 | |
1927 | template<size_t _Index,typename ... _Args> |
1928 | constexpr typename __arg_selector_t<_Index,_Args...>::__type&& __arg_selector(_Args&& ... __args){ |
1929 | return std::forward<typename __arg_selector_t<_Index,_Args...>::__type>( |
1930 | __arg_selector_t<_Index,_Args...>::__select(__args...)); |
1931 | } |
1932 | |
1933 | template<ptrdiff_t _Index,size_t _VariantIndex,ptrdiff_t ... _Indices> |
1934 | struct __visit_helper2{ |
1935 | template<typename _Visitor,typename ... _Variants> |
1936 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1937 | __visit(_Visitor& __visitor,_Variants&& ... __v){ |
1938 | return (__arg_selector<_VariantIndex-1>(__v...).index()==_Index) |
1939 | ? __visit_helper<_VariantIndex-1,__index_sequence<_Index,_Indices...>>::__visit(__visitor,std::forward<_Variants>(__v)...) |
1940 | : __visit_helper2<_Index-1,_VariantIndex,_Indices...>::__visit(__visitor,std::forward<_Variants>(__v)...); |
1941 | } |
1942 | }; |
1943 | |
1944 | template<size_t _VariantIndex,ptrdiff_t ... _Indices> |
1945 | struct __visit_helper2<-1,_VariantIndex,_Indices...>{ |
1946 | template<typename _Visitor,typename ... _Variants> |
1947 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1948 | __visit(_Visitor&,_Variants&& ...){ |
1949 | return __throw_bad_variant_access<typename __multi_visitor_return_type<_Visitor,_Variants...>::__type>("Visiting of empty Variant" ); |
1950 | } |
1951 | }; |
1952 | |
1953 | template<typename _Variant> |
1954 | struct __variant_type_count; |
1955 | |
1956 | template<typename ... _Types> |
1957 | struct __variant_type_count<Variant<_Types...>>{ |
1958 | static constexpr size_t __value=sizeof...(_Types); |
1959 | }; |
1960 | |
1961 | template<typename _Variant> |
1962 | struct __variant_type_count<_Variant&>{ |
1963 | static constexpr size_t __value=__variant_type_count<_Variant>::__value; |
1964 | }; |
1965 | |
1966 | template<typename _Variant> |
1967 | struct __variant_type_count<_Variant const&>{ |
1968 | static constexpr size_t __value=__variant_type_count<_Variant>::__value; |
1969 | }; |
1970 | |
1971 | template<size_t _VariantIndex,ptrdiff_t ... _Indices> |
1972 | struct __visit_helper<_VariantIndex,__index_sequence<_Indices...>>{ |
1973 | |
1974 | template<typename _Visitor,typename ... _Variants> |
1975 | static constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1976 | __visit(_Visitor& __visitor,_Variants&& ... __v){ |
1977 | return __visit_helper2< |
1978 | __variant_type_count< |
1979 | typename __arg_selector_t< |
1980 | _VariantIndex-1,_Variants...>::__type>::__value-1, |
1981 | _VariantIndex,_Indices...>::__visit( |
1982 | __visitor,std::forward<_Variants&&>(__v)...); |
1983 | } |
1984 | }; |
1985 | |
1986 | template<typename _Visitor,typename ... _Variants> |
1987 | constexpr typename __multi_visitor_return_type<_Visitor,_Variants...>::__type |
1988 | visit(_Visitor&& __visitor,_Variants&& ... __v){ |
1989 | return __visit_helper<sizeof...(_Variants),__index_sequence<>>::__visit( |
1990 | __visitor,std::forward<_Variants>(__v)...); |
1991 | } |
1992 | |
1993 | template<typename ... _Types> |
1994 | constexpr bool operator==(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
1995 | return (__lhs.index()==__rhs.index()) && |
1996 | ((__lhs.index()==-1) || |
1997 | __equality_op_table<Variant<_Types...>>::__equality_compare[__lhs.index()]( |
1998 | __lhs,__rhs)); |
1999 | } |
2000 | |
2001 | template<typename ... _Types> |
2002 | constexpr bool operator!=(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2003 | return !(__lhs==__rhs); |
2004 | } |
2005 | |
2006 | template<typename ... _Types> |
2007 | constexpr bool operator<(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2008 | return (__lhs.index()<__rhs.index()) || |
2009 | ((__lhs.index()==__rhs.index()) && |
2010 | ((__lhs.index()!=-1) && |
2011 | __less_than_op_table<Variant<_Types...>>:: |
2012 | __less_than_compare[__lhs.index()](__lhs,__rhs))); |
2013 | } |
2014 | |
2015 | template<typename ... _Types> |
2016 | constexpr bool operator>(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2017 | return __rhs<__lhs; |
2018 | } |
2019 | |
2020 | template<typename ... _Types> |
2021 | constexpr bool operator>=(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2022 | return !(__lhs<__rhs); |
2023 | } |
2024 | |
2025 | template<typename ... _Types> |
2026 | constexpr bool operator<=(Variant<_Types...> const& __lhs,Variant<_Types...> const& __rhs){ |
2027 | return !(__lhs>__rhs); |
2028 | } |
2029 | |
2030 | struct Monostate{}; |
2031 | |
2032 | constexpr bool operator==(Monostate const&, Monostate const&) { return true; } |
2033 | constexpr bool operator!=(Monostate const&, Monostate const&) { return false; } |
2034 | constexpr bool operator>=(Monostate const&, Monostate const&) { return true; } |
2035 | constexpr bool operator<=(Monostate const&, Monostate const&) { return true; } |
2036 | constexpr bool operator>(Monostate const&, Monostate const&) { return false; } |
2037 | constexpr bool operator<(Monostate const&, Monostate const&) { return false; } |
2038 | |
2039 | struct __hash_visitor{ |
2040 | template<typename _Type> |
2041 | size_t operator()(_Type const& __x){ |
2042 | return std::hash<_Type>()(__x); |
2043 | } |
2044 | }; |
2045 | |
2046 | // -- WebKit Additions -- |
2047 | |
2048 | template<class V, class... F> |
2049 | auto switchOn(V&& v, F&&... f) -> decltype(WTF::visit(makeVisitor(std::forward<F>(f)...), std::forward<V>(v))) |
2050 | { |
2051 | return WTF::visit(makeVisitor(std::forward<F>(f)...), std::forward<V>(v)); |
2052 | } |
2053 | |
2054 | } // namespace WTF |
2055 | |
2056 | namespace std { |
2057 | |
2058 | template<> |
2059 | struct hash<WTF::Monostate>{ |
2060 | size_t operator()(WTF::Monostate) __NOEXCEPT{ |
2061 | return 42; |
2062 | } |
2063 | }; |
2064 | |
2065 | template<typename ... _Types> |
2066 | struct hash<WTF::Variant<_Types...>>{ |
2067 | size_t operator()(WTF::Variant<_Types...> const &v) __NOEXCEPT { |
2068 | return std::hash<ptrdiff_t>()(v.index()) ^ WTF::visit(WTF::__hash_visitor(), v); |
2069 | } |
2070 | }; |
2071 | |
2072 | } // namespace std |
2073 | |
2074 | using WTF::Monostate; |
2075 | using WTF::Variant; |
2076 | |
2077 | #endif // !COMPILER(CLANG) || WTF_CPP_STD_VER >= 14 |
2078 | |
2079 | #if COMPILER(MSVC) |
2080 | #pragma warning(pop) |
2081 | #endif |
2082 | |