// Copyright 2018 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // ----------------------------------------------------------------------------- // variant.h // ----------------------------------------------------------------------------- // // This header file defines an `absl::variant` type for holding a type-safe // value of some prescribed set of types (noted as alternative types), and // associated functions for managing variants. // // The `absl::variant` type is a form of type-safe union. An `absl::variant` // should always hold a value of one of its alternative types (except in the // "valueless by exception state" -- see below). A default-constructed // `absl::variant` will hold the value of its first alternative type, provided // it is default-constructible. // // In exceptional cases due to error, an `absl::variant` can hold no // value (known as a "valueless by exception" state), though this is not the // norm. // // As with `absl::optional`, an `absl::variant` -- when it holds a value -- // allocates a value of that type directly within the `variant` itself; it // cannot hold a reference, array, or the type `void`; it can, however, hold a // pointer to externally managed memory. // // `absl::variant` is a C++11 compatible version of the C++17 `std::variant` // abstraction and is designed to be a drop-in replacement for code compliant // with C++17. #ifndef ABSL_TYPES_VARIANT_H_ #define ABSL_TYPES_VARIANT_H_ #include "absl/base/config.h" #include "absl/utility/utility.h" #ifdef ABSL_USES_STD_VARIANT #include // IWYU pragma: export namespace absl { using std::bad_variant_access; using std::get; using std::get_if; using std::holds_alternative; using std::monostate; using std::variant; using std::variant_alternative; using std::variant_alternative_t; using std::variant_npos; using std::variant_size; using std::variant_size_v; using std::visit; } // namespace absl #else // ABSL_USES_STD_VARIANT #include #include #include #include #include "absl/base/macros.h" #include "absl/base/port.h" #include "absl/meta/type_traits.h" #include "absl/types/internal/variant.h" namespace absl { // ----------------------------------------------------------------------------- // absl::variant // ----------------------------------------------------------------------------- // // An `absl::variant` type is a form of type-safe union. An `absl::variant` -- // except in exceptional cases -- always holds a value of one of its alternative // types. // // Example: // // // Construct a variant that holds either an integer or a std::string and // // assign it to a std::string. // absl::variant v = std::string("abc"); // // // A default-constructed variant will hold a value-initialized value of // // the first alternative type. // auto a = absl::variant(); // Holds an int of value '0'. // // // variants are assignable. // // // copy assignment // auto v1 = absl::variant("abc"); // auto v2 = absl::variant(10); // v2 = v1; // copy assign // // // move assignment // auto v1 = absl::variant("abc"); // v1 = absl::variant(10); // // // assignment through type conversion // a = 128; // variant contains int // a = "128"; // variant contains std::string // // An `absl::variant` holding a value of one of its alternative types `T` holds // an allocation of `T` directly within the variant itself. An `absl::variant` // is not allowed to allocate additional storage, such as dynamic memory, to // allocate the contained value. The contained value shall be allocated in a // region of the variant storage suitably aligned for all alternative types. template class variant; // swap() // // Swaps two `absl::variant` values. This function is equivalent to `v.swap(w)` // where `v` and `w` are `absl::variant` types. // // Note that this function requires all alternative types to be both swappable // and move-constructible, because any two variants may refer to either the same // type (in which case, they will be swapped) or to two different types (in // which case the values will need to be moved). // template < typename... Ts, absl::enable_if_t< absl::conjunction..., type_traits_internal::IsSwappable...>::value, int> = 0> void swap(variant& v, variant& w) noexcept(noexcept(v.swap(w))) { v.swap(w); } // variant_size // // Returns the number of alternative types available for a given `absl::variant` // type as a compile-time constant expression. As this is a class template, it // is not generally useful for accessing the number of alternative types of // any given `absl::variant` instance. // // Example: // // auto a = absl::variant; // constexpr int num_types = // absl::variant_size>(); // // // You can also use the member constant `value`. // constexpr int num_types = // absl::variant_size>::value; // // // `absl::variant_size` is more valuable for use in generic code: // template // constexpr bool IsVariantMultivalue() { // return absl::variant_size() > 1; // } // // Note that the set of cv-qualified specializations of `variant_size` are // provided to ensure that those specializations compile (especially when passed // within template logic). template struct variant_size; template struct variant_size> : std::integral_constant {}; // Specialization of `variant_size` for const qualified variants. template struct variant_size : variant_size::type {}; // Specialization of `variant_size` for volatile qualified variants. template struct variant_size : variant_size::type {}; // Specialization of `variant_size` for const volatile qualified variants. template struct variant_size : variant_size::type {}; // variant_alternative // // Returns the alternative type for a given `absl::variant` at the passed // index value as a compile-time constant expression. As this is a class // template resulting in a type, it is not useful for access of the run-time // value of any given `absl::variant` variable. // // Example: // // // The type of the 0th alternative is "int". // using alternative_type_0 // = absl::variant_alternative<0, absl::variant>::type; // // static_assert(std::is_same::value, ""); // // // `absl::variant_alternative` is more valuable for use in generic code: // template // constexpr bool IsFirstElementTrivial() { // return std::is_trivial_v::type>; // } // // Note that the set of cv-qualified specializations of `variant_alternative` // are provided to ensure that those specializations compile (especially when // passed within template logic). template struct variant_alternative; template struct variant_alternative> { using type = variant_internal::VariantAlternativeSfinaeT>; }; // Specialization of `variant_alternative` for const qualified variants. template struct variant_alternative { using type = const typename variant_alternative::type; }; // Specialization of `variant_alternative` for volatile qualified variants. template struct variant_alternative { using type = volatile typename variant_alternative::type; }; // Specialization of `variant_alternative` for const volatile qualified // variants. template struct variant_alternative { using type = const volatile typename variant_alternative::type; }; // Template type alias for variant_alternative::type. // // Example: // // using alternative_type_0 // = absl::variant_alternative_t<0, absl::variant>; // static_assert(std::is_same::value, ""); template using variant_alternative_t = typename variant_alternative::type; // holds_alternative() // // Checks whether the given variant currently holds a given alternative type, // returning `true` if so. // // Example: // // absl::variant foo = 42; // if (absl::holds_alternative(foo)) { // std::cout << "The variant holds an integer"; // } template constexpr bool holds_alternative(const variant& v) noexcept { static_assert( variant_internal::UnambiguousIndexOfImpl, T, 0>::value != sizeof...(Types), "The type T must occur exactly once in Types..."); return v.index() == variant_internal::UnambiguousIndexOf, T>::value; } // get() // // Returns a reference to the value currently within a given variant, using // either a unique alternative type amongst the variant's set of alternative // types, or the variant's index value. Attempting to get a variant's value // using a type that is not unique within the variant's set of alternative types // is a compile-time error. If the index of the alternative being specified is // different from the index of the alternative that is currently stored, throws // `absl::bad_variant_access`. // // Example: // // auto a = absl::variant; // // // Get the value by type (if unique). // int i = absl::get(a); // // auto b = absl::variant; // // // Getting the value by a type that is not unique is ill-formed. // int j = absl::get(b); // Compile Error! // // // Getting value by index not ambiguous and allowed. // int k = absl::get<1>(b); // Overload for getting a variant's lvalue by type. template constexpr T& get(variant& v) { // NOLINT return variant_internal::VariantCoreAccess::CheckedAccess< variant_internal::IndexOf::value>(v); } // Overload for getting a variant's rvalue by type. // Note: `absl::move()` is required to allow use of constexpr in C++11. template constexpr T&& get(variant&& v) { return variant_internal::VariantCoreAccess::CheckedAccess< variant_internal::IndexOf::value>(absl::move(v)); } // Overload for getting a variant's const lvalue by type. template constexpr const T& get(const variant& v) { return variant_internal::VariantCoreAccess::CheckedAccess< variant_internal::IndexOf::value>(v); } // Overload for getting a variant's const rvalue by type. // Note: `absl::move()` is required to allow use of constexpr in C++11. template constexpr const T&& get(const variant&& v) { return variant_internal::VariantCoreAccess::CheckedAccess< variant_internal::IndexOf::value>(absl::move(v)); } // Overload for getting a variant's lvalue by index. template constexpr variant_alternative_t>& get( variant& v) { // NOLINT return variant_internal::VariantCoreAccess::CheckedAccess(v); } // Overload for getting a variant's rvalue by index. // Note: `absl::move()` is required to allow use of constexpr in C++11. template constexpr variant_alternative_t>&& get( variant&& v) { return variant_internal::VariantCoreAccess::CheckedAccess(absl::move(v)); } // Overload for getting a variant's const lvalue by index. template constexpr const variant_alternative_t>& get( const variant& v) { return variant_internal::VariantCoreAccess::CheckedAccess(v); } // Overload for getting a variant's const rvalue by index. // Note: `absl::move()` is required to allow use of constexpr in C++11. template constexpr const variant_alternative_t>&& get( const variant&& v) { return variant_internal::VariantCoreAccess::CheckedAccess(absl::move(v)); } // get_if() // // Returns a pointer to the value currently stored within a given variant, if // present, using either a unique alternative type amongst the variant's set of // alternative types, or the variant's index value. If such a value does not // exist, returns `nullptr`. // // As with `get`, attempting to get a variant's value using a type that is not // unique within the variant's set of alternative types is a compile-time error. // Overload for getting a pointer to the value stored in the given variant by // index. template constexpr absl::add_pointer_t>> get_if(variant* v) noexcept { return (v != nullptr && v->index() == I) ? std::addressof( variant_internal::VariantCoreAccess::Access(*v)) : nullptr; } // Overload for getting a pointer to the const value stored in the given // variant by index. template constexpr absl::add_pointer_t>> get_if(const variant* v) noexcept { return (v != nullptr && v->index() == I) ? std::addressof( variant_internal::VariantCoreAccess::Access(*v)) : nullptr; } // Overload for getting a pointer to the value stored in the given variant by // type. template constexpr absl::add_pointer_t get_if(variant* v) noexcept { return absl::get_if::value>(v); } // Overload for getting a pointer to the const value stored in the given variant // by type. template constexpr absl::add_pointer_t get_if( const variant* v) noexcept { return absl::get_if::value>(v); } // visit() // // Calls a provided functor on a given set of variants. `absl::visit()` is // commonly used to conditionally inspect the state of a given variant (or set // of variants). // // The functor must return the same type when called with any of the variants' // alternatives. // // Example: // // // Define a visitor functor // struct GetVariant { // template // void operator()(const T& i) const { // std::cout << "The variant's value is: " << i; // } // }; // // // Declare our variant, and call `absl::visit()` on it. // // Note that `GetVariant()` returns void in either case. // absl::variant foo = std::string("foo"); // GetVariant visitor; // absl::visit(visitor, foo); // Prints `The variant's value is: foo' template variant_internal::VisitResult visit(Visitor&& vis, Variants&&... vars) { return variant_internal:: VisitIndices >::value...>::Run( variant_internal::PerformVisitation{ std::forward_as_tuple(absl::forward(vars)...), absl::forward(vis)}, vars.index()...); } // monostate // // The monostate class serves as a first alternative type for a variant for // which the first variant type is otherwise not default-constructible. struct monostate {}; // `absl::monostate` Relational Operators constexpr bool operator<(monostate, monostate) noexcept { return false; } constexpr bool operator>(monostate, monostate) noexcept { return false; } constexpr bool operator<=(monostate, monostate) noexcept { return true; } constexpr bool operator>=(monostate, monostate) noexcept { return true; } constexpr bool operator==(monostate, monostate) noexcept { return true; } constexpr bool operator!=(monostate, monostate) noexcept { return false; } //------------------------------------------------------------------------------ // `absl::variant` Template Definition //------------------------------------------------------------------------------ template class variant : private variant_internal::VariantBase { static_assert(absl::conjunction, std::is_object...>::value, "Attempted to instantiate a variant containing a non-object " "type."); // Intentionally not qualifying `negation` with `absl::` to work around a bug // in MSVC 2015 with inline namespace and variadic template. static_assert(absl::conjunction >, negation >...>::value, "Attempted to instantiate a variant containing an array type."); static_assert(absl::conjunction, std::is_nothrow_destructible...>::value, "Attempted to instantiate a variant containing a non-nothrow " "destructible type."); friend struct variant_internal::VariantCoreAccess; private: using Base = variant_internal::VariantBase; public: // Constructors // Constructs a variant holding a default-initialized value of the first // alternative type. constexpr variant() /*noexcept(see 111above)*/ = default; // Copy constructor, standard semantics variant(const variant& other) = default; // Move constructor, standard semantics variant(variant&& other) /*noexcept(see above)*/ = default; // Constructs a variant of an alternative type specified by overload // resolution of the provided forwarding arguments through // direct-initialization. // // Note: If the selected constructor is a constexpr constructor, this // constructor shall be a constexpr constructor. // // NOTE: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0608r1.html // has been voted passed the design phase in the C++ standard meeting in Mar // 2018. It will be implemented and integrated into `absl::variant`. template < class T, std::size_t I = std::enable_if< variant_internal::IsNeitherSelfNorInPlace>::value, variant_internal::IndexOfConstructedType>::type::value, class Tj = absl::variant_alternative_t, absl::enable_if_t::value>* = nullptr> constexpr variant(T&& t) noexcept(std::is_nothrow_constructible::value) : Base(variant_internal::EmplaceTag(), absl::forward(t)) {} // Constructs a variant of an alternative type from the arguments through // direct-initialization. // // Note: If the selected constructor is a constexpr constructor, this // constructor shall be a constexpr constructor. template , Args...>::value>::type* = nullptr> constexpr explicit variant(in_place_type_t, Args&&... args) : Base(variant_internal::EmplaceTag< variant_internal::UnambiguousIndexOf::value>(), absl::forward(args)...) {} // Constructs a variant of an alternative type from an initializer list // and other arguments through direct-initialization. // // Note: If the selected constructor is a constexpr constructor, this // constructor shall be a constexpr constructor. template , std::initializer_list&, Args...>::value>::type* = nullptr> constexpr explicit variant(in_place_type_t, std::initializer_list il, Args&&... args) : Base(variant_internal::EmplaceTag< variant_internal::UnambiguousIndexOf::value>(), il, absl::forward(args)...) {} // Constructs a variant of an alternative type from a provided index, // through value-initialization using the provided forwarded arguments. template , Args...>::value>::type* = nullptr> constexpr explicit variant(in_place_index_t, Args&&... args) : Base(variant_internal::EmplaceTag(), absl::forward(args)...) {} // Constructs a variant of an alternative type from a provided index, // through value-initialization of an initializer list and the provided // forwarded arguments. template , std::initializer_list&, Args...>::value>::type* = nullptr> constexpr explicit variant(in_place_index_t, std::initializer_list il, Args&&... args) : Base(variant_internal::EmplaceTag(), il, absl::forward(args)...) {} // Destructors // Destroys the variant's currently contained value, provided that // `absl::valueless_by_exception()` is false. ~variant() = default; // Assignment Operators // Copy assignment operator variant& operator=(const variant& other) = default; // Move assignment operator variant& operator=(variant&& other) /*noexcept(see above)*/ = default; // Converting assignment operator // // NOTE: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0608r1.html // has been voted passed the design phase in the C++ standard meeting in Mar // 2018. It will be implemented and integrated into `absl::variant`. template < class T, std::size_t I = std::enable_if< !std::is_same, variant>::value, variant_internal::IndexOfConstructedType>::type::value, class Tj = absl::variant_alternative_t, typename std::enable_if::value && std::is_constructible::value>::type* = nullptr> variant& operator=(T&& t) noexcept( std::is_nothrow_assignable::value&& std::is_nothrow_constructible::value) { variant_internal::VisitIndices::Run( variant_internal::VariantCoreAccess::MakeConversionAssignVisitor( this, absl::forward(t)), index()); return *this; } // emplace() Functions // Constructs a value of the given alternative type T within the variant. // // Example: // // absl::variant, int, std::string> v; // v.emplace(99); // v.emplace("abc"); template < class T, class... Args, typename std::enable_if::value, variant>, Args...>::value>::type* = nullptr> T& emplace(Args&&... args) { return variant_internal::VariantCoreAccess::Replace< variant_internal::UnambiguousIndexOf::value>( this, absl::forward(args)...); } // Constructs a value of the given alternative type T within the variant using // an initializer list. // // Example: // // absl::variant, int, std::string> v; // v.emplace>({0, 1, 2}); template < class T, class U, class... Args, typename std::enable_if::value, variant>, std::initializer_list&, Args...>::value>::type* = nullptr> T& emplace(std::initializer_list il, Args&&... args) { return variant_internal::VariantCoreAccess::Replace< variant_internal::UnambiguousIndexOf::value>( this, il, absl::forward(args)...); } // Destroys the current value of the variant (provided that // `absl::valueless_by_exception()` is false, and constructs a new value at // the given index. // // Example: // // absl::variant, int, int> v; // v.emplace<1>(99); // v.emplace<2>(98); // v.emplace(99); // Won't compile. 'int' isn't a unique type. template , Args...>::value>::type* = nullptr> absl::variant_alternative_t& emplace(Args&&... args) { return variant_internal::VariantCoreAccess::Replace( this, absl::forward(args)...); } // Destroys the current value of the variant (provided that // `absl::valueless_by_exception()` is false, and constructs a new value at // the given index using an initializer list and the provided arguments. // // Example: // // absl::variant, int, int> v; // v.emplace<0>({0, 1, 2}); template , std::initializer_list&, Args...>::value>::type* = nullptr> absl::variant_alternative_t& emplace(std::initializer_list il, Args&&... args) { return variant_internal::VariantCoreAccess::Replace( this, il, absl::forward(args)...); } // variant::valueless_by_exception() // // Returns false if and only if the variant currently holds a valid value. constexpr bool valueless_by_exception() const noexcept { return this->index_ == absl::variant_npos; } // variant::index() // // Returns the index value of the variant's currently selected alternative // type. constexpr std::size_t index() const noexcept { return this->index_; } // variant::swap() // // Swaps the values of two variant objects. // void swap(variant& rhs) noexcept( absl::conjunction< std::is_nothrow_move_constructible, std::is_nothrow_move_constructible..., type_traits_internal::IsNothrowSwappable, type_traits_internal::IsNothrowSwappable...>::value) { return variant_internal::VisitIndices::Run( variant_internal::Swap{this, &rhs}, rhs.index()); } }; // We need a valid declaration of variant<> for SFINAE and overload resolution // to work properly above, but we don't need a full declaration since this type // will never be constructed. This declaration, though incomplete, suffices. template <> class variant<>; //------------------------------------------------------------------------------ // Relational Operators //------------------------------------------------------------------------------ // // If neither operand is in the `variant::valueless_by_exception` state: // // * If the index of both variants is the same, the relational operator // returns the result of the corresponding relational operator for the // corresponding alternative type. // * If the index of both variants is not the same, the relational operator // returns the result of that operation applied to the value of the left // operand's index and the value of the right operand's index. // * If at least one operand is in the valueless_by_exception state: // - A variant in the valueless_by_exception state is only considered equal // to another variant in the valueless_by_exception state. // - If exactly one operand is in the valueless_by_exception state, the // variant in the valueless_by_exception state is less than the variant // that is not in the valueless_by_exception state. // // Note: The value 1 is added to each index in the relational comparisons such // that the index corresponding to the valueless_by_exception state wraps around // to 0 (the lowest value for the index type), and the remaining indices stay in // the same relative order. // Equal-to operator template constexpr variant_internal::RequireAllHaveEqualT operator==( const variant& a, const variant& b) { return (a.index() == b.index()) && variant_internal::VisitIndices::Run( variant_internal::EqualsOp{&a, &b}, a.index()); } // Not equal operator template constexpr variant_internal::RequireAllHaveNotEqualT operator!=( const variant& a, const variant& b) { return (a.index() != b.index()) || variant_internal::VisitIndices::Run( variant_internal::NotEqualsOp{&a, &b}, a.index()); } // Less-than operator template constexpr variant_internal::RequireAllHaveLessThanT operator<( const variant& a, const variant& b) { return (a.index() != b.index()) ? (a.index() + 1) < (b.index() + 1) : variant_internal::VisitIndices::Run( variant_internal::LessThanOp{&a, &b}, a.index()); } // Greater-than operator template constexpr variant_internal::RequireAllHaveGreaterThanT operator>( const variant& a, const variant& b) { return (a.index() != b.index()) ? (a.index() + 1) > (b.index() + 1) : variant_internal::VisitIndices::Run( variant_internal::GreaterThanOp{&a, &b}, a.index()); } // Less-than or equal-to operator template constexpr variant_internal::RequireAllHaveLessThanOrEqualT operator<=( const variant& a, const variant& b) { return (a.index() != b.index()) ? (a.index() + 1) < (b.index() + 1) : variant_internal::VisitIndices::Run( variant_internal::LessThanOrEqualsOp{&a, &b}, a.index()); } // Greater-than or equal-to operator template constexpr variant_internal::RequireAllHaveGreaterThanOrEqualT operator>=(const variant& a, const variant& b) { return (a.index() != b.index()) ? (a.index() + 1) > (b.index() + 1) : variant_internal::VisitIndices::Run( variant_internal::GreaterThanOrEqualsOp{&a, &b}, a.index()); } } // namespace absl namespace std { // hash() template <> // NOLINT struct hash { std::size_t operator()(absl::monostate) const { return 0; } }; template // NOLINT struct hash> : absl::variant_internal::VariantHashBase, void, absl::remove_const_t...> {}; } // namespace std #endif // ABSL_USES_STD_VARIANT namespace absl { namespace variant_internal { // Helper visitor for converting a variant` into another type (mostly // variant) that can be constructed from any type. template struct ConversionVisitor { template To operator()(T&& v) const { return To(std::forward(v)); } }; } // namespace variant_internal // ConvertVariantTo() // // Helper functions to convert an `absl::variant` to a variant of another set of // types, provided that the alternative type of the new variant type can be // converted from any type in the source variant. // // Example: // // absl::variant InternalReq(const Req&); // // // name1 and name2 are convertible to name // absl::variant ExternalReq(const Req& req) { // return absl::ConvertVariantTo>( // InternalReq(req)); // } template To ConvertVariantTo(Variant&& variant) { return absl::visit(variant_internal::ConversionVisitor{}, std::forward(variant)); } } // namespace absl #endif // ABSL_TYPES_VARIANT_H_