forked from ThePhD/sol2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional_implementation.hpp
2301 lines (1998 loc) · 81 KB
/
optional_implementation.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Taken from: TartanLlama/optional on Github, because
// holy shit am I done dealing with C++11 constexpr
///
// optional - An implementation of std::optional with extensions
// Written in 2017 by Simon Brand (@TartanLlama)
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software. If not, see
// <http://creativecommons.org/publicdomain/zero/1.0/>.
///
#ifndef SOL_TL_OPTIONAL_HPP
#define SOL_TL_OPTIONAL_HPP
#include <sol/version.hpp>
#include <sol/in_place.hpp>
#define SOL_TL_OPTIONAL_VERSION_MAJOR 0
#define SOL_TL_OPTIONAL_VERSION_MINOR 5
#include <exception>
#include <functional>
#include <new>
#include <type_traits>
#include <utility>
#include <cstdlib>
#include <optional>
#if (defined(_MSC_VER) && _MSC_VER == 1900)
#define SOL_TL_OPTIONAL_MSVC2015
#endif
#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
#define SOL_TL_OPTIONAL_GCC49
#endif
#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && !defined(__clang__))
#define SOL_TL_OPTIONAL_GCC54
#endif
#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && !defined(__clang__))
#define SOL_TL_OPTIONAL_GCC55
#endif
#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
// GCC < 5 doesn't support overloading on const&& for member functions
#define SOL_TL_OPTIONAL_NO_CONSTRR
// GCC < 5 doesn't support some standard C++11 type traits
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::has_trivial_copy_constructor<T>::value
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign<T>::value
// This one will be different for GCC 5.7 if it's ever supported
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector
// for non-copyable types
#elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__))
#ifndef SOL_TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
#define SOL_TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
namespace sol { namespace detail {
template <class T>
struct is_trivially_copy_constructible : std::is_trivially_copy_constructible<T> { };
#ifdef _GLIBCXX_VECTOR
template <class T, class A>
struct is_trivially_copy_constructible<std::vector<T, A>> : std::is_trivially_copy_constructible<T> { };
#endif
}} // namespace sol::detail
#endif
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) sol::detail::is_trivially_copy_constructible<T>::value
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable<T>::value
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
#else
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::is_trivially_copy_constructible<T>::value
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable<T>::value
#define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
#endif
#if __cplusplus > 201103L
#define SOL_TL_OPTIONAL_CXX14
#endif
// constexpr implies const in C++11, not C++14
#if (__cplusplus == 201103L || defined(SOL_TL_OPTIONAL_MSVC2015) || defined(SOL_TL_OPTIONAL_GCC49))
/// \exclude
#define SOL_TL_OPTIONAL_11_CONSTEXPR
#else
/// \exclude
#define SOL_TL_OPTIONAL_11_CONSTEXPR constexpr
#endif
namespace sol {
#ifndef SOL_TL_MONOSTATE_INPLACE_MUTEX
#define SOL_TL_MONOSTATE_INPLACE_MUTEX
/// \brief Used to represent an optional with no data; essentially a bool
class monostate { };
#endif
template <class T>
class optional;
/// \exclude
namespace detail {
#ifndef SOL_TL_TRAITS_MUTEX
#define SOL_TL_TRAITS_MUTEX
// C++14-style aliases for brevity
template <class T>
using remove_const_t = typename std::remove_const<T>::type;
template <class T>
using remove_reference_t = typename std::remove_reference<T>::type;
template <class T>
using decay_t = typename std::decay<T>::type;
template <bool E, class T = void>
using enable_if_t = typename std::enable_if<E, T>::type;
template <bool B, class T, class F>
using conditional_t = typename std::conditional<B, T, F>::type;
// std::conjunction from C++17
template <class...>
struct conjunction : std::true_type { };
template <class B>
struct conjunction<B> : B { };
template <class B, class... Bs>
struct conjunction<B, Bs...> : std::conditional<bool(B::value), conjunction<Bs...>, B>::type { };
#if defined(_LIBCPP_VERSION) && __cplusplus == 201103L
#define SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
#endif
// In C++11 mode, there's an issue in libc++'s std::mem_fn
// which results in a hard-error when using it in a noexcept expression
// in some cases. This is a check to workaround the common failing case.
#ifdef SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
template <class T>
struct is_pointer_to_non_const_member_func : std::false_type { };
template <class T, class Ret, class... Args>
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)> : std::true_type { };
template <class T, class Ret, class... Args>
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)&> : std::true_type { };
template <class T, class Ret, class... Args>
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &&> : std::true_type { };
template <class T, class Ret, class... Args>
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile> : std::true_type { };
template <class T, class Ret, class... Args>
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&> : std::true_type { };
template <class T, class Ret, class... Args>
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&&> : std::true_type { };
template <class T>
struct is_const_or_const_ref : std::false_type { };
template <class T>
struct is_const_or_const_ref<T const&> : std::true_type { };
template <class T>
struct is_const_or_const_ref<T const> : std::true_type { };
#endif
// std::invoke from C++17
// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
template <typename Fn, typename... Args,
#ifdef SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
typename = enable_if_t<!(is_pointer_to_non_const_member_func<Fn>::value && is_const_or_const_ref<Args...>::value)>,
#endif
typename = enable_if_t<std::is_member_pointer<decay_t<Fn>>::value>, int = 0>
constexpr auto invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
-> decltype(std::mem_fn(f)(std::forward<Args>(args)...)) {
return std::mem_fn(f)(std::forward<Args>(args)...);
}
template <typename Fn, typename... Args, typename = enable_if_t<!std::is_member_pointer<decay_t<Fn>>::value>>
constexpr auto invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::forward<Fn>(f)(std::forward<Args>(args)...)))
-> decltype(std::forward<Fn>(f)(std::forward<Args>(args)...)) {
return std::forward<Fn>(f)(std::forward<Args>(args)...);
}
// std::invoke_result from C++17
template <class F, class, class... Us>
struct invoke_result_impl;
template <class F, class... Us>
struct invoke_result_impl<F, decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...), void()), Us...> {
using type = decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...));
};
template <class F, class... Us>
using invoke_result = invoke_result_impl<F, void, Us...>;
template <class F, class... Us>
using invoke_result_t = typename invoke_result<F, Us...>::type;
#endif
// std::void_t from C++17
template <class...>
struct voider {
using type = void;
};
template <class... Ts>
using void_t = typename voider<Ts...>::type;
// Trait for checking if a type is a sol::optional
template <class T>
struct is_optional_impl : std::false_type { };
template <class T>
struct is_optional_impl<optional<T>> : std::true_type { };
template <class T>
using is_optional = is_optional_impl<decay_t<T>>;
// Change void to sol::monostate
template <class U>
using fixup_void = conditional_t<std::is_void<U>::value, monostate, U>;
template <class F, class U, class = invoke_result_t<F, U>>
using get_map_return = optional<fixup_void<invoke_result_t<F, U>>>;
// Check if invoking F for some Us returns void
template <class F, class = void, class... U>
struct returns_void_impl;
template <class F, class... U>
struct returns_void_impl<F, void_t<invoke_result_t<F, U...>>, U...> : std::is_void<invoke_result_t<F, U...>> { };
template <class F, class... U>
using returns_void = returns_void_impl<F, void, U...>;
template <class T, class... U>
using enable_if_ret_void = enable_if_t<returns_void<T&&, U...>::value>;
template <class T, class... U>
using disable_if_ret_void = enable_if_t<!returns_void<T&&, U...>::value>;
template <class T, class U>
using enable_forward_value = detail::enable_if_t<std::is_constructible<T, U&&>::value && !std::is_same<detail::decay_t<U>, in_place_t>::value
&& !std::is_same<optional<T>, detail::decay_t<U>>::value>;
template <class T, class U, class Other>
using enable_from_other = detail::enable_if_t<std::is_constructible<T, Other>::value && !std::is_constructible<T, optional<U>&>::value
&& !std::is_constructible<T, optional<U>&&>::value && !std::is_constructible<T, const optional<U>&>::value
&& !std::is_constructible<T, const optional<U>&&>::value && !std::is_convertible<optional<U>&, T>::value
&& !std::is_convertible<optional<U>&&, T>::value && !std::is_convertible<const optional<U>&, T>::value
&& !std::is_convertible<const optional<U>&&, T>::value>;
template <class T, class U>
using enable_assign_forward = detail::enable_if_t<!std::is_same<optional<T>, detail::decay_t<U>>::value
&& !detail::conjunction<std::is_scalar<T>, std::is_same<T, detail::decay_t<U>>>::value && std::is_constructible<T, U>::value
&& std::is_assignable<T&, U>::value>;
template <class T, class U, class Other>
using enable_assign_from_other = detail::enable_if_t<std::is_constructible<T, Other>::value && std::is_assignable<T&, Other>::value
&& !std::is_constructible<T, optional<U>&>::value && !std::is_constructible<T, optional<U>&&>::value
&& !std::is_constructible<T, const optional<U>&>::value && !std::is_constructible<T, const optional<U>&&>::value
&& !std::is_convertible<optional<U>&, T>::value && !std::is_convertible<optional<U>&&, T>::value
&& !std::is_convertible<const optional<U>&, T>::value && !std::is_convertible<const optional<U>&&, T>::value
&& !std::is_assignable<T&, optional<U>&>::value && !std::is_assignable<T&, optional<U>&&>::value
&& !std::is_assignable<T&, const optional<U>&>::value && !std::is_assignable<T&, const optional<U>&&>::value>;
#ifdef _MSC_VER
// TODO make a version which works with MSVC
template <class T, class U = T>
struct is_swappable : std::true_type { };
template <class T, class U = T>
struct is_nothrow_swappable : std::true_type { };
#else
// https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept
namespace swap_adl_tests {
// if swap ADL finds this then it would call std::swap otherwise (same
// signature)
struct tag { };
template <class T>
tag swap(T&, T&);
template <class T, std::size_t N>
tag swap(T (&a)[N], T (&b)[N]);
// helper functions to test if an unqualified swap is possible, and if it
// becomes std::swap
template <class, class>
std::false_type can_swap(...) noexcept(false);
template <class T, class U, class = decltype(swap(std::declval<T&>(), std::declval<U&>()))>
std::true_type can_swap(int) noexcept(noexcept(swap(std::declval<T&>(), std::declval<U&>())));
template <class, class>
std::false_type uses_std(...);
template <class T, class U>
std::is_same<decltype(swap(std::declval<T&>(), std::declval<U&>())), tag> uses_std(int);
template <class T>
struct is_std_swap_noexcept
: std::integral_constant<bool, std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value> { };
template <class T, std::size_t N>
struct is_std_swap_noexcept<T[N]> : is_std_swap_noexcept<T> { };
template <class T, class U>
struct is_adl_swap_noexcept : std::integral_constant<bool, noexcept(can_swap<T, U>(0))> { };
} // namespace swap_adl_tests
template <class T, class U = T>
struct is_swappable : std::integral_constant<bool,
decltype(detail::swap_adl_tests::can_swap<T, U>(0))::value
&& (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value
|| (std::is_move_assignable<T>::value && std::is_move_constructible<T>::value))> { };
template <class T, std::size_t N>
struct is_swappable<T[N], T[N]> : std::integral_constant<bool,
decltype(detail::swap_adl_tests::can_swap<T[N], T[N]>(0))::value
&& (!decltype(detail::swap_adl_tests::uses_std<T[N], T[N]>(0))::value || is_swappable<T, T>::value)> { };
template <class T, class U = T>
struct is_nothrow_swappable
: std::integral_constant<bool,
is_swappable<T, U>::value
&& ((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value&& detail::swap_adl_tests::is_std_swap_noexcept<T>::value)
|| (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value&& detail::swap_adl_tests::is_adl_swap_noexcept<T, U>::value))> { };
#endif
// The storage base manages the actual storage, and correctly propagates
// trivial destroyion from T. This case is for when T is not trivially
// destructible.
template <class T, bool = ::std::is_trivially_destructible<T>::value>
struct optional_storage_base {
SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {
}
template <class... U>
SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U&&... u) : m_value(std::forward<U>(u)...), m_has_value(true) {
}
~optional_storage_base() {
if (m_has_value) {
m_value.~T();
m_has_value = false;
}
}
struct dummy { };
union {
dummy m_dummy;
T m_value;
};
bool m_has_value;
};
// This case is for when T is trivially destructible.
template <class T>
struct optional_storage_base<T, true> {
SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {
}
template <class... U>
SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U&&... u) : m_value(std::forward<U>(u)...), m_has_value(true) {
}
// No destructor, so this class is trivially destructible
struct dummy { };
union {
dummy m_dummy;
T m_value;
};
bool m_has_value = false;
};
// This base class provides some handy member functions which can be used in
// further derived classes
template <class T>
struct optional_operations_base : optional_storage_base<T> {
using optional_storage_base<T>::optional_storage_base;
void hard_reset() noexcept {
get().~T();
this->m_has_value = false;
}
template <class... Args>
void construct(Args&&... args) noexcept {
new (std::addressof(this->m_value)) T(std::forward<Args>(args)...);
this->m_has_value = true;
}
template <class Opt>
void assign(Opt&& rhs) {
if (this->has_value()) {
if (rhs.has_value()) {
this->m_value = std::forward<Opt>(rhs).get();
}
else {
this->m_value.~T();
this->m_has_value = false;
}
}
else if (rhs.has_value()) {
construct(std::forward<Opt>(rhs).get());
}
}
bool has_value() const {
return this->m_has_value;
}
SOL_TL_OPTIONAL_11_CONSTEXPR T& get() & {
return this->m_value;
}
SOL_TL_OPTIONAL_11_CONSTEXPR const T& get() const& {
return this->m_value;
}
SOL_TL_OPTIONAL_11_CONSTEXPR T&& get() && {
return std::move(this->m_value);
}
#ifndef SOL_TL_OPTIONAL_NO_CONSTRR
constexpr const T&& get() const&& {
return std::move(this->m_value);
}
#endif
};
// This class manages conditionally having a trivial copy constructor
// This specialization is for when T is trivially copy constructible
template <class T, bool = SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)>
struct optional_copy_base : optional_operations_base<T> {
using optional_operations_base<T>::optional_operations_base;
};
// This specialization is for when T is not trivially copy constructible
template <class T>
struct optional_copy_base<T, false> : optional_operations_base<T> {
using base_t = optional_operations_base<T>;
using base_t::base_t;
optional_copy_base() = default;
optional_copy_base(const optional_copy_base& rhs) : base_t() {
if (rhs.has_value()) {
this->construct(rhs.get());
}
else {
this->m_has_value = false;
}
}
optional_copy_base(optional_copy_base&& rhs) = default;
optional_copy_base& operator=(const optional_copy_base& rhs) = default;
optional_copy_base& operator=(optional_copy_base&& rhs) = default;
};
// This class manages conditionally having a trivial move constructor
// Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it
// doesn't implement an analogue to std::is_trivially_move_constructible. We
// have to make do with a non-trivial move constructor even if T is trivially
// move constructible
#ifndef SOL_TL_OPTIONAL_GCC49
template <class T, bool = std::is_trivially_move_constructible<T>::value>
struct optional_move_base : optional_copy_base<T> {
using optional_copy_base<T>::optional_copy_base;
};
#else
template <class T, bool = false>
struct optional_move_base;
#endif
template <class T>
struct optional_move_base<T, false> : optional_copy_base<T> {
using optional_copy_base<T>::optional_copy_base;
optional_move_base() = default;
optional_move_base(const optional_move_base& rhs) = default;
optional_move_base(optional_move_base&& rhs) noexcept(std::is_nothrow_move_constructible<T>::value) {
if (rhs.has_value()) {
this->construct(std::move(rhs.get()));
}
else {
this->m_has_value = false;
}
}
optional_move_base& operator=(const optional_move_base& rhs) = default;
optional_move_base& operator=(optional_move_base&& rhs) = default;
};
// This class manages conditionally having a trivial copy assignment operator
template <class T,
bool = SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) && SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)
&& SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T)>
struct optional_copy_assign_base : optional_move_base<T> {
using optional_move_base<T>::optional_move_base;
};
template <class T>
struct optional_copy_assign_base<T, false> : optional_move_base<T> {
using optional_move_base<T>::optional_move_base;
optional_copy_assign_base() = default;
optional_copy_assign_base(const optional_copy_assign_base& rhs) = default;
optional_copy_assign_base(optional_copy_assign_base&& rhs) = default;
optional_copy_assign_base& operator=(const optional_copy_assign_base& rhs) {
this->assign(rhs);
return *this;
}
optional_copy_assign_base& operator=(optional_copy_assign_base&& rhs) = default;
};
// This class manages conditionally having a trivial move assignment operator
// Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it
// doesn't implement an analogue to std::is_trivially_move_assignable. We have
// to make do with a non-trivial move assignment operator even if T is trivially
// move assignable
#ifndef SOL_TL_OPTIONAL_GCC49
template <class T,
bool = std::is_trivially_destructible<T>::value&& std::is_trivially_move_constructible<T>::value&& std::is_trivially_move_assignable<T>::value>
struct optional_move_assign_base : optional_copy_assign_base<T> {
using optional_copy_assign_base<T>::optional_copy_assign_base;
};
#else
template <class T, bool = false>
struct optional_move_assign_base;
#endif
template <class T>
struct optional_move_assign_base<T, false> : optional_copy_assign_base<T> {
using optional_copy_assign_base<T>::optional_copy_assign_base;
optional_move_assign_base() = default;
optional_move_assign_base(const optional_move_assign_base& rhs) = default;
optional_move_assign_base(optional_move_assign_base&& rhs) = default;
optional_move_assign_base& operator=(const optional_move_assign_base& rhs) = default;
optional_move_assign_base& operator=(optional_move_assign_base&& rhs) noexcept(
std::is_nothrow_move_constructible<T>::value&& std::is_nothrow_move_assignable<T>::value) {
this->assign(std::move(rhs));
return *this;
}
};
// optional_delete_ctor_base will conditionally delete copy and move
// constructors depending on whether T is copy/move constructible
template <class T, bool EnableCopy = std::is_copy_constructible<T>::value, bool EnableMove = std::is_move_constructible<T>::value>
struct optional_delete_ctor_base {
optional_delete_ctor_base() = default;
optional_delete_ctor_base(const optional_delete_ctor_base&) = default;
optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = default;
optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
};
template <class T>
struct optional_delete_ctor_base<T, true, false> {
optional_delete_ctor_base() = default;
optional_delete_ctor_base(const optional_delete_ctor_base&) = default;
optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = delete;
optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
};
template <class T>
struct optional_delete_ctor_base<T, false, true> {
optional_delete_ctor_base() = default;
optional_delete_ctor_base(const optional_delete_ctor_base&) = delete;
optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = default;
optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
};
template <class T>
struct optional_delete_ctor_base<T, false, false> {
optional_delete_ctor_base() = default;
optional_delete_ctor_base(const optional_delete_ctor_base&) = delete;
optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = delete;
optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
};
// optional_delete_assign_base will conditionally delete copy and move
// constructors depending on whether T is copy/move constructible + assignable
template <class T, bool EnableCopy = (std::is_copy_constructible<T>::value && std::is_copy_assignable<T>::value),
bool EnableMove = (std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)>
struct optional_delete_assign_base {
optional_delete_assign_base() = default;
optional_delete_assign_base(const optional_delete_assign_base&) = default;
optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
optional_delete_assign_base& operator=(const optional_delete_assign_base&) = default;
optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = default;
};
template <class T>
struct optional_delete_assign_base<T, true, false> {
optional_delete_assign_base() = default;
optional_delete_assign_base(const optional_delete_assign_base&) = default;
optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
optional_delete_assign_base& operator=(const optional_delete_assign_base&) = default;
optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = delete;
};
template <class T>
struct optional_delete_assign_base<T, false, true> {
optional_delete_assign_base() = default;
optional_delete_assign_base(const optional_delete_assign_base&) = default;
optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
optional_delete_assign_base& operator=(const optional_delete_assign_base&) = delete;
optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = default;
};
template <class T>
struct optional_delete_assign_base<T, false, false> {
optional_delete_assign_base() = default;
optional_delete_assign_base(const optional_delete_assign_base&) = default;
optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
optional_delete_assign_base& operator=(const optional_delete_assign_base&) = delete;
optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = delete;
};
} // namespace detail
/// \brief A tag type to represent an empty optional
using nullopt_t = std::nullopt_t;
/// \brief Represents an empty optional
/// \synopsis static constexpr nullopt_t nullopt;
///
/// *Examples*:
/// ```
/// sol::optional<int> a = sol::nullopt;
/// void foo (sol::optional<int>);
/// foo(sol::nullopt); //pass an empty optional
/// ```
using std::nullopt;
/// @brief An exception for when an optional is accessed through specific methods while it is not engaged.
class bad_optional_access : public std::exception {
public:
/// @brief Default-constructs an optional exception.
bad_optional_access() = default;
/// @brief Returns a pointer to a null-terminated string containing the reason for the exception.
const char* what() const noexcept override {
return "Optional has no value";
}
};
/// An optional object is an object that contains the storage for another
/// object and manages the lifetime of this contained object, if any. The
/// contained object may be initialized after the optional object has been
/// initialized, and may be destroyed before the optional object has been
/// destroyed. The initialization state of the contained object is tracked by
/// the optional object.
template <class T>
class optional : private detail::optional_move_assign_base<T>,
private detail::optional_delete_ctor_base<T>,
private detail::optional_delete_assign_base<T> {
using base = detail::optional_move_assign_base<T>;
static_assert(!std::is_same<T, in_place_t>::value, "instantiation of optional with in_place_t is ill-formed");
static_assert(!std::is_same<detail::decay_t<T>, nullopt_t>::value, "instantiation of optional with nullopt_t is ill-formed");
public:
// The different versions for C++14 and 11 are needed because deduced return
// types are not SFINAE-safe. This provides better support for things like
// generic lambdas. C.f.
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html
#if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
/// \group and_then
/// Carries out some operation which returns an optional on the stored
/// object if there is one. \requires `std::invoke(std::forward<F>(f),
/// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
/// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
/// `std::optional<U>`. The return value is empty if `*this` is empty,
/// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
/// is returned.
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) &;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) & {
using result = detail::invoke_result_t<F, T&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
}
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) &&;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) && {
using result = detail::invoke_result_t<F, T&&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
}
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &;
template <class F>
constexpr auto and_then(F&& f) const& {
using result = detail::invoke_result_t<F, const T&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
}
#ifndef SOL_TL_OPTIONAL_NO_CONSTRR
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &&;
template <class F>
constexpr auto and_then(F&& f) const&& {
using result = detail::invoke_result_t<F, const T&&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
}
#endif
#else
/// \group and_then
/// Carries out some operation which returns an optional on the stored
/// object if there is one. \requires `std::invoke(std::forward<F>(f),
/// value())` returns a `std::optional<U>` for some `U`.
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
/// value())`. Returns a `std::optional<U>`. The return value is empty if
/// `*this` is empty, otherwise the return value of
/// `std::invoke(std::forward<F>(f), value())` is returned.
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) &;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) & {
using result = detail::invoke_result_t<F, T&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
}
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) &&;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&&> and_then(F&& f) && {
using result = detail::invoke_result_t<F, T&&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
}
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &;
template <class F>
constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const& {
using result = detail::invoke_result_t<F, const T&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
}
#ifndef SOL_TL_OPTIONAL_NO_CONSTRR
/// \group and_then
/// \synopsis template <class F> \n constexpr auto and_then(F &&f) const &&;
template <class F>
constexpr detail::invoke_result_t<F, const T&&> and_then(F&& f) const&& {
using result = detail::invoke_result_t<F, const T&&>;
static_assert(detail::is_optional<result>::value, "F must return an optional");
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
}
#endif
#endif
#if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
/// \brief Carries out some operation on the stored object if there is one.
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
/// value())`. Returns a `std::optional<U>`. The return value is empty if
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
/// return value of `std::invoke(std::forward<F>(f), value())` and is
/// returned.
///
/// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) &;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) & {
return optional_map_impl(*this, std::forward<F>(f));
}
/// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) &&;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) && {
return optional_map_impl(std::move(*this), std::forward<F>(f));
}
/// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) const&;
template <class F>
constexpr auto map(F&& f) const& {
return optional_map_impl(*this, std::forward<F>(f));
}
/// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) const&&;
template <class F>
constexpr auto map(F&& f) const&& {
return optional_map_impl(std::move(*this), std::forward<F>(f));
}
#else
/// \brief Carries out some operation on the stored object if there is one.
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
/// value())`. Returns a `std::optional<U>`. The return value is empty if
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
/// return value of `std::invoke(std::forward<F>(f), value())` and is
/// returned.
///
/// \group map
/// \synopsis template <class F> auto map(F &&f) &;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&>(), std::declval<F&&>())) map(F&& f) & {
return optional_map_impl(*this, std::forward<F>(f));
}
/// \group map
/// \synopsis template <class F> auto map(F &&f) &&;
template <class F>
SOL_TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&&>(), std::declval<F&&>())) map(F&& f) && {
return optional_map_impl(std::move(*this), std::forward<F>(f));
}
/// \group map
/// \synopsis template <class F> auto map(F &&f) const&;
template <class F>
constexpr decltype(optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) map(F&& f) const& {
return optional_map_impl(*this, std::forward<F>(f));
}
#ifndef SOL_TL_OPTIONAL_NO_CONSTRR
/// \group map
/// \synopsis template <class F> auto map(F &&f) const&&;
template <class F>
constexpr decltype(optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) map(F&& f) const&& {
return optional_map_impl(std::move(*this), std::forward<F>(f));
}
#endif
#endif
/// \brief Calls `f` if the optional is empty
/// \requires `std::invoke_result_t<F>` must be void or convertible to
/// `optional<T>`.
/// \effects If `*this` has a value, returns `*this`.
/// Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns
/// `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`.
///
/// \group or_else
/// \synopsis template <class F> optional<T> or_else (F &&f) &;
template <class F, detail::enable_if_ret_void<F>* = nullptr>
optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
if (has_value())
return *this;
std::forward<F>(f)();
return nullopt;
}
/// \exclude
template <class F, detail::disable_if_ret_void<F>* = nullptr>
optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
return has_value() ? *this : std::forward<F>(f)();
}
/// \group or_else
/// \synopsis template <class F> optional<T> or_else (F &&f) &&;
template <class F, detail::enable_if_ret_void<F>* = nullptr>
optional<T> or_else(F&& f) && {
if (has_value())
return std::move(*this);
std::forward<F>(f)();
return nullopt;
}
/// \exclude
template <class F, detail::disable_if_ret_void<F>* = nullptr>
optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) && {
return has_value() ? std::move(*this) : std::forward<F>(f)();
}
/// \group or_else
/// \synopsis template <class F> optional<T> or_else (F &&f) const &;
template <class F, detail::enable_if_ret_void<F>* = nullptr>
optional<T> or_else(F&& f) const& {
if (has_value())
return *this;
std::forward<F>(f)();
return nullopt;
}
/// \exclude
template <class F, detail::disable_if_ret_void<F>* = nullptr>
optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) const& {
return has_value() ? *this : std::forward<F>(f)();
}
#ifndef SOL_TL_OPTIONAL_NO_CONSTRR
/// \exclude
template <class F, detail::enable_if_ret_void<F>* = nullptr>
optional<T> or_else(F&& f) const&& {
if (has_value())
return std::move(*this);
std::forward<F>(f)();
return nullopt;
}
/// \exclude
template <class F, detail::disable_if_ret_void<F>* = nullptr>
optional<T> or_else(F&& f) const&& {
return has_value() ? std::move(*this) : std::forward<F>(f)();
}
#endif
/// \brief Maps the stored value with `f` if there is one, otherwise returns
/// `u`.
///
/// \details If there is a value stored, then `f` is called with `**this`
/// and the value is returned. Otherwise `u` is returned.
///
/// \group map_or
template <class F, class U>
U map_or(F&& f, U&& u) & {
return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
}
/// \group map_or
template <class F, class U>
U map_or(F&& f, U&& u) && {
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
}
/// \group map_or
template <class F, class U>
U map_or(F&& f, U&& u) const& {
return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
}
#ifndef SOL_TL_OPTIONAL_NO_CONSTRR
/// \group map_or
template <class F, class U>
U map_or(F&& f, U&& u) const&& {
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
}
#endif
/// \brief Maps the stored value with `f` if there is one, otherwise calls
/// `u` and returns the result.
///
/// \details If there is a value stored, then `f` is
/// called with `**this` and the value is returned. Otherwise
/// `std::forward<U>(u)()` is returned.
///
/// \group map_or_else
/// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u) &;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F&& f, U&& u) & {
return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
}
/// \group map_or_else
/// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
/// &&;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F&& f, U&& u) && {
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
}
/// \group map_or_else
/// \synopsis template <class F, class U> \n auto map_or_else(F &&f, U &&u)
/// const &;
template <class F, class U>
detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const& {
return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
}