File indexing completed on 2024-11-16 09:03:44
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 #ifndef ABSL_TYPES_VARIANT_H_
0043 #define ABSL_TYPES_VARIANT_H_
0044
0045 #include "absl/base/config.h"
0046 #include "absl/utility/utility.h"
0047
0048 #ifdef ABSL_USES_STD_VARIANT
0049
0050 #include <variant> // IWYU pragma: export
0051
0052 namespace absl {
0053 ABSL_NAMESPACE_BEGIN
0054 using std::bad_variant_access;
0055 using std::get;
0056 using std::get_if;
0057 using std::holds_alternative;
0058 using std::monostate;
0059 using std::variant;
0060 using std::variant_alternative;
0061 using std::variant_alternative_t;
0062 using std::variant_npos;
0063 using std::variant_size;
0064 using std::variant_size_v;
0065 using std::visit;
0066 ABSL_NAMESPACE_END
0067 }
0068
0069 #else
0070
0071 #include <functional>
0072 #include <new>
0073 #include <type_traits>
0074 #include <utility>
0075
0076 #include "absl/base/macros.h"
0077 #include "absl/base/port.h"
0078 #include "absl/meta/type_traits.h"
0079 #include "absl/types/internal/variant.h"
0080
0081 namespace absl {
0082 ABSL_NAMESPACE_BEGIN
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 template <typename... Ts>
0123 class variant;
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 template <
0136 typename... Ts,
0137 absl::enable_if_t<
0138 absl::conjunction<std::is_move_constructible<Ts>...,
0139 type_traits_internal::IsSwappable<Ts>...>::value,
0140 int> = 0>
0141 void swap(variant<Ts...>& v, variant<Ts...>& w) noexcept(noexcept(v.swap(w))) {
0142 v.swap(w);
0143 }
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 template <class T>
0172 struct variant_size;
0173
0174 template <class... Ts>
0175 struct variant_size<variant<Ts...>>
0176 : std::integral_constant<std::size_t, sizeof...(Ts)> {};
0177
0178
0179 template <class T>
0180 struct variant_size<const T> : variant_size<T>::type {};
0181
0182
0183 template <class T>
0184 struct variant_size<volatile T> : variant_size<T>::type {};
0185
0186
0187 template <class T>
0188 struct variant_size<const volatile T> : variant_size<T>::type {};
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214 template <std::size_t I, class T>
0215 struct variant_alternative;
0216
0217 template <std::size_t I, class... Types>
0218 struct variant_alternative<I, variant<Types...>> {
0219 using type =
0220 variant_internal::VariantAlternativeSfinaeT<I, variant<Types...>>;
0221 };
0222
0223
0224 template <std::size_t I, class T>
0225 struct variant_alternative<I, const T> {
0226 using type = const typename variant_alternative<I, T>::type;
0227 };
0228
0229
0230 template <std::size_t I, class T>
0231 struct variant_alternative<I, volatile T> {
0232 using type = volatile typename variant_alternative<I, T>::type;
0233 };
0234
0235
0236
0237 template <std::size_t I, class T>
0238 struct variant_alternative<I, const volatile T> {
0239 using type = const volatile typename variant_alternative<I, T>::type;
0240 };
0241
0242
0243
0244
0245
0246
0247
0248
0249 template <std::size_t I, class T>
0250 using variant_alternative_t = typename variant_alternative<I, T>::type;
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 template <class T, class... Types>
0264 constexpr bool holds_alternative(const variant<Types...>& v) noexcept {
0265 static_assert(
0266 variant_internal::UnambiguousIndexOfImpl<variant<Types...>, T,
0267 0>::value != sizeof...(Types),
0268 "The type T must occur exactly once in Types...");
0269 return v.index() ==
0270 variant_internal::UnambiguousIndexOf<variant<Types...>, T>::value;
0271 }
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 template <class T, class... Types>
0300 constexpr T& get(variant<Types...>& v) {
0301 return variant_internal::VariantCoreAccess::CheckedAccess<
0302 variant_internal::IndexOf<T, Types...>::value>(v);
0303 }
0304
0305
0306
0307 template <class T, class... Types>
0308 constexpr T&& get(variant<Types...>&& v) {
0309 return variant_internal::VariantCoreAccess::CheckedAccess<
0310 variant_internal::IndexOf<T, Types...>::value>(absl::move(v));
0311 }
0312
0313
0314 template <class T, class... Types>
0315 constexpr const T& get(const variant<Types...>& v) {
0316 return variant_internal::VariantCoreAccess::CheckedAccess<
0317 variant_internal::IndexOf<T, Types...>::value>(v);
0318 }
0319
0320
0321
0322 template <class T, class... Types>
0323 constexpr const T&& get(const variant<Types...>&& v) {
0324 return variant_internal::VariantCoreAccess::CheckedAccess<
0325 variant_internal::IndexOf<T, Types...>::value>(absl::move(v));
0326 }
0327
0328
0329 template <std::size_t I, class... Types>
0330 constexpr variant_alternative_t<I, variant<Types...>>& get(
0331 variant<Types...>& v) {
0332 return variant_internal::VariantCoreAccess::CheckedAccess<I>(v);
0333 }
0334
0335
0336
0337 template <std::size_t I, class... Types>
0338 constexpr variant_alternative_t<I, variant<Types...>>&& get(
0339 variant<Types...>&& v) {
0340 return variant_internal::VariantCoreAccess::CheckedAccess<I>(absl::move(v));
0341 }
0342
0343
0344 template <std::size_t I, class... Types>
0345 constexpr const variant_alternative_t<I, variant<Types...>>& get(
0346 const variant<Types...>& v) {
0347 return variant_internal::VariantCoreAccess::CheckedAccess<I>(v);
0348 }
0349
0350
0351
0352 template <std::size_t I, class... Types>
0353 constexpr const variant_alternative_t<I, variant<Types...>>&& get(
0354 const variant<Types...>&& v) {
0355 return variant_internal::VariantCoreAccess::CheckedAccess<I>(absl::move(v));
0356 }
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 template <std::size_t I, class... Types>
0371 constexpr absl::add_pointer_t<variant_alternative_t<I, variant<Types...>>>
0372 get_if(variant<Types...>* v) noexcept {
0373 return (v != nullptr && v->index() == I)
0374 ? std::addressof(
0375 variant_internal::VariantCoreAccess::Access<I>(*v))
0376 : nullptr;
0377 }
0378
0379
0380
0381 template <std::size_t I, class... Types>
0382 constexpr absl::add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
0383 get_if(const variant<Types...>* v) noexcept {
0384 return (v != nullptr && v->index() == I)
0385 ? std::addressof(
0386 variant_internal::VariantCoreAccess::Access<I>(*v))
0387 : nullptr;
0388 }
0389
0390
0391
0392 template <class T, class... Types>
0393 constexpr absl::add_pointer_t<T> get_if(variant<Types...>* v) noexcept {
0394 return absl::get_if<variant_internal::IndexOf<T, Types...>::value>(v);
0395 }
0396
0397
0398
0399 template <class T, class... Types>
0400 constexpr absl::add_pointer_t<const T> get_if(
0401 const variant<Types...>* v) noexcept {
0402 return absl::get_if<variant_internal::IndexOf<T, Types...>::value>(v);
0403 }
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429 template <typename Visitor, typename... Variants>
0430 variant_internal::VisitResult<Visitor, Variants...> visit(Visitor&& vis,
0431 Variants&&... vars) {
0432 return variant_internal::
0433 VisitIndices<variant_size<absl::decay_t<Variants> >::value...>::Run(
0434 variant_internal::PerformVisitation<Visitor, Variants...>{
0435 std::forward_as_tuple(absl::forward<Variants>(vars)...),
0436 absl::forward<Visitor>(vis)},
0437 vars.index()...);
0438 }
0439
0440
0441
0442
0443
0444 struct monostate {};
0445
0446
0447
0448 constexpr bool operator<(monostate, monostate) noexcept { return false; }
0449 constexpr bool operator>(monostate, monostate) noexcept { return false; }
0450 constexpr bool operator<=(monostate, monostate) noexcept { return true; }
0451 constexpr bool operator>=(monostate, monostate) noexcept { return true; }
0452 constexpr bool operator==(monostate, monostate) noexcept { return true; }
0453 constexpr bool operator!=(monostate, monostate) noexcept { return false; }
0454
0455
0456
0457
0458
0459 template <typename T0, typename... Tn>
0460 class variant<T0, Tn...> : private variant_internal::VariantBase<T0, Tn...> {
0461 static_assert(absl::conjunction<std::is_object<T0>,
0462 std::is_object<Tn>...>::value,
0463 "Attempted to instantiate a variant containing a non-object "
0464 "type.");
0465
0466
0467 static_assert(absl::conjunction<negation<std::is_array<T0> >,
0468 negation<std::is_array<Tn> >...>::value,
0469 "Attempted to instantiate a variant containing an array type.");
0470 static_assert(absl::conjunction<std::is_nothrow_destructible<T0>,
0471 std::is_nothrow_destructible<Tn>...>::value,
0472 "Attempted to instantiate a variant containing a non-nothrow "
0473 "destructible type.");
0474
0475 friend struct variant_internal::VariantCoreAccess;
0476
0477 private:
0478 using Base = variant_internal::VariantBase<T0, Tn...>;
0479
0480 public:
0481
0482
0483
0484
0485 constexpr variant() = default;
0486
0487
0488 variant(const variant& other) = default;
0489
0490
0491 variant(variant&& other) = default;
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 template <
0504 class T,
0505 std::size_t I = std::enable_if<
0506 variant_internal::IsNeitherSelfNorInPlace<variant,
0507 absl::decay_t<T>>::value,
0508 variant_internal::IndexOfConstructedType<variant, T>>::type::value,
0509 class Tj = absl::variant_alternative_t<I, variant>,
0510 absl::enable_if_t<std::is_constructible<Tj, T>::value>* =
0511 nullptr>
0512 constexpr variant(T&& t) noexcept(std::is_nothrow_constructible<Tj, T>::value)
0513 : Base(variant_internal::EmplaceTag<I>(), absl::forward<T>(t)) {}
0514
0515
0516
0517
0518
0519
0520 template <class T, class... Args,
0521 typename std::enable_if<std::is_constructible<
0522 variant_internal::UnambiguousTypeOfT<variant, T>,
0523 Args...>::value>::type* = nullptr>
0524 constexpr explicit variant(in_place_type_t<T>, Args&&... args)
0525 : Base(variant_internal::EmplaceTag<
0526 variant_internal::UnambiguousIndexOf<variant, T>::value>(),
0527 absl::forward<Args>(args)...) {}
0528
0529
0530
0531
0532
0533
0534 template <class T, class U, class... Args,
0535 typename std::enable_if<std::is_constructible<
0536 variant_internal::UnambiguousTypeOfT<variant, T>,
0537 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0538 constexpr explicit variant(in_place_type_t<T>, std::initializer_list<U> il,
0539 Args&&... args)
0540 : Base(variant_internal::EmplaceTag<
0541 variant_internal::UnambiguousIndexOf<variant, T>::value>(),
0542 il, absl::forward<Args>(args)...) {}
0543
0544
0545
0546 template <std::size_t I, class... Args,
0547 typename std::enable_if<std::is_constructible<
0548 variant_internal::VariantAlternativeSfinaeT<I, variant>,
0549 Args...>::value>::type* = nullptr>
0550 constexpr explicit variant(in_place_index_t<I>, Args&&... args)
0551 : Base(variant_internal::EmplaceTag<I>(), absl::forward<Args>(args)...) {}
0552
0553
0554
0555
0556 template <std::size_t I, class U, class... Args,
0557 typename std::enable_if<std::is_constructible<
0558 variant_internal::VariantAlternativeSfinaeT<I, variant>,
0559 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0560 constexpr explicit variant(in_place_index_t<I>, std::initializer_list<U> il,
0561 Args&&... args)
0562 : Base(variant_internal::EmplaceTag<I>(), il,
0563 absl::forward<Args>(args)...) {}
0564
0565
0566
0567
0568
0569 ~variant() = default;
0570
0571
0572
0573
0574 variant& operator=(const variant& other) = default;
0575
0576
0577 variant& operator=(variant&& other) = default;
0578
0579
0580
0581
0582
0583
0584 template <
0585 class T,
0586 std::size_t I = std::enable_if<
0587 !std::is_same<absl::decay_t<T>, variant>::value,
0588 variant_internal::IndexOfConstructedType<variant, T>>::type::value,
0589 class Tj = absl::variant_alternative_t<I, variant>,
0590 typename std::enable_if<std::is_assignable<Tj&, T>::value &&
0591 std::is_constructible<Tj, T>::value>::type* =
0592 nullptr>
0593 variant& operator=(T&& t) noexcept(
0594 std::is_nothrow_assignable<Tj&, T>::value&&
0595 std::is_nothrow_constructible<Tj, T>::value) {
0596 variant_internal::VisitIndices<sizeof...(Tn) + 1>::Run(
0597 variant_internal::VariantCoreAccess::MakeConversionAssignVisitor(
0598 this, absl::forward<T>(t)),
0599 index());
0600
0601 return *this;
0602 }
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617 template <
0618 class T, class... Args,
0619 typename std::enable_if<std::is_constructible<
0620 absl::variant_alternative_t<
0621 variant_internal::UnambiguousIndexOf<variant, T>::value, variant>,
0622 Args...>::value>::type* = nullptr>
0623 T& emplace(Args&&... args) {
0624 return variant_internal::VariantCoreAccess::Replace<
0625 variant_internal::UnambiguousIndexOf<variant, T>::value>(
0626 this, absl::forward<Args>(args)...);
0627 }
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638 template <
0639 class T, class U, class... Args,
0640 typename std::enable_if<std::is_constructible<
0641 absl::variant_alternative_t<
0642 variant_internal::UnambiguousIndexOf<variant, T>::value, variant>,
0643 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0644 T& emplace(std::initializer_list<U> il, Args&&... args) {
0645 return variant_internal::VariantCoreAccess::Replace<
0646 variant_internal::UnambiguousIndexOf<variant, T>::value>(
0647 this, il, absl::forward<Args>(args)...);
0648 }
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 template <std::size_t I, class... Args,
0661 typename std::enable_if<
0662 std::is_constructible<absl::variant_alternative_t<I, variant>,
0663 Args...>::value>::type* = nullptr>
0664 absl::variant_alternative_t<I, variant>& emplace(Args&&... args) {
0665 return variant_internal::VariantCoreAccess::Replace<I>(
0666 this, absl::forward<Args>(args)...);
0667 }
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677 template <std::size_t I, class U, class... Args,
0678 typename std::enable_if<std::is_constructible<
0679 absl::variant_alternative_t<I, variant>,
0680 std::initializer_list<U>&, Args...>::value>::type* = nullptr>
0681 absl::variant_alternative_t<I, variant>& emplace(std::initializer_list<U> il,
0682 Args&&... args) {
0683 return variant_internal::VariantCoreAccess::Replace<I>(
0684 this, il, absl::forward<Args>(args)...);
0685 }
0686
0687
0688
0689
0690 constexpr bool valueless_by_exception() const noexcept {
0691 return this->index_ == absl::variant_npos;
0692 }
0693
0694
0695
0696
0697
0698 constexpr std::size_t index() const noexcept { return this->index_; }
0699
0700
0701
0702
0703
0704 void swap(variant& rhs) noexcept(
0705 absl::conjunction<
0706 std::is_nothrow_move_constructible<T0>,
0707 std::is_nothrow_move_constructible<Tn>...,
0708 type_traits_internal::IsNothrowSwappable<T0>,
0709 type_traits_internal::IsNothrowSwappable<Tn>...>::value) {
0710 return variant_internal::VisitIndices<sizeof...(Tn) + 1>::Run(
0711 variant_internal::Swap<T0, Tn...>{this, &rhs}, rhs.index());
0712 }
0713 };
0714
0715
0716
0717
0718 template <>
0719 class variant<>;
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746 template <typename... Types>
0747 constexpr variant_internal::RequireAllHaveEqualT<Types...> operator==(
0748 const variant<Types...>& a, const variant<Types...>& b) {
0749 return (a.index() == b.index()) &&
0750 variant_internal::VisitIndices<sizeof...(Types)>::Run(
0751 variant_internal::EqualsOp<Types...>{&a, &b}, a.index());
0752 }
0753
0754
0755 template <typename... Types>
0756 constexpr variant_internal::RequireAllHaveNotEqualT<Types...> operator!=(
0757 const variant<Types...>& a, const variant<Types...>& b) {
0758 return (a.index() != b.index()) ||
0759 variant_internal::VisitIndices<sizeof...(Types)>::Run(
0760 variant_internal::NotEqualsOp<Types...>{&a, &b}, a.index());
0761 }
0762
0763
0764 template <typename... Types>
0765 constexpr variant_internal::RequireAllHaveLessThanT<Types...> operator<(
0766 const variant<Types...>& a, const variant<Types...>& b) {
0767 return (a.index() != b.index())
0768 ? (a.index() + 1) < (b.index() + 1)
0769 : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0770 variant_internal::LessThanOp<Types...>{&a, &b}, a.index());
0771 }
0772
0773
0774 template <typename... Types>
0775 constexpr variant_internal::RequireAllHaveGreaterThanT<Types...> operator>(
0776 const variant<Types...>& a, const variant<Types...>& b) {
0777 return (a.index() != b.index())
0778 ? (a.index() + 1) > (b.index() + 1)
0779 : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0780 variant_internal::GreaterThanOp<Types...>{&a, &b},
0781 a.index());
0782 }
0783
0784
0785 template <typename... Types>
0786 constexpr variant_internal::RequireAllHaveLessThanOrEqualT<Types...> operator<=(
0787 const variant<Types...>& a, const variant<Types...>& b) {
0788 return (a.index() != b.index())
0789 ? (a.index() + 1) < (b.index() + 1)
0790 : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0791 variant_internal::LessThanOrEqualsOp<Types...>{&a, &b},
0792 a.index());
0793 }
0794
0795
0796 template <typename... Types>
0797 constexpr variant_internal::RequireAllHaveGreaterThanOrEqualT<Types...>
0798 operator>=(const variant<Types...>& a, const variant<Types...>& b) {
0799 return (a.index() != b.index())
0800 ? (a.index() + 1) > (b.index() + 1)
0801 : variant_internal::VisitIndices<sizeof...(Types)>::Run(
0802 variant_internal::GreaterThanOrEqualsOp<Types...>{&a, &b},
0803 a.index());
0804 }
0805
0806 ABSL_NAMESPACE_END
0807 }
0808
0809 namespace std {
0810
0811
0812 template <>
0813 struct hash<absl::monostate> {
0814 std::size_t operator()(absl::monostate) const { return 0; }
0815 };
0816
0817 template <class... T>
0818 struct hash<absl::variant<T...>>
0819 : absl::variant_internal::VariantHashBase<absl::variant<T...>, void,
0820 absl::remove_const_t<T>...> {};
0821
0822 }
0823
0824 #endif
0825
0826 namespace absl {
0827 ABSL_NAMESPACE_BEGIN
0828 namespace variant_internal {
0829
0830
0831
0832 template <typename To>
0833 struct ConversionVisitor {
0834 template <typename T>
0835 To operator()(T&& v) const {
0836 return To(std::forward<T>(v));
0837 }
0838 };
0839
0840 }
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857 template <typename To, typename Variant>
0858 To ConvertVariantTo(Variant&& variant) {
0859 return absl::visit(variant_internal::ConversionVisitor<To>{},
0860 std::forward<Variant>(variant));
0861 }
0862
0863 ABSL_NAMESPACE_END
0864 }
0865
0866 #endif