2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_DETAIL_VARIANT_HPP
11 #define BOOST_BEAST_DETAIL_VARIANT_HPP
13 #include <boost/beast/core/detail/type_traits.hpp>
14 #include <boost/assert.hpp>
17 #include <type_traits>
23 // This simple variant gets the job done without
24 // causing too much trouble with template depth:
26 // * Always allows an empty state I==0
27 // * emplace() and get() support 1-based indexes only
28 // * Basic exception guarantee
34 typename std::aligned_storage<
40 template<std::size_t I>
41 using type = typename std::tuple_element<
42 I , std::tuple<TN...>>::type;
44 template<std::size_t I>
45 using C = std::integral_constant<std::size_t, I>;
63 // moved-from object becomes empty
64 variant(variant&& other)
66 i_ = other.move(&buf_, C<0>{});
69 variant(variant const& other)
71 i_ = other.copy(&buf_, C<0>{});
74 // moved-from object becomes empty
75 variant& operator=(variant&& other)
79 i_ = other.move(&buf_, C<0>{});
83 variant& operator=(variant const& other)
87 i_ = other.copy(&buf_, C<0>{});
91 template<std::size_t I, class... Args>
93 emplace(Args&&... args)
98 std::forward<Args>(args)...);
102 template<std::size_t I>
106 BOOST_ASSERT(i_ == I);
107 return *reinterpret_cast<
111 template<std::size_t I>
115 BOOST_ASSERT(i_ == I);
116 return *reinterpret_cast<
117 type<I-1> const*>(&buf_);
130 destroy(C<sizeof...(TN)>)
135 template<std::size_t I>
150 move(void*, C<sizeof...(TN)>)
155 template<std::size_t I>
157 move(void* dest, C<I>)
162 new(dest) T{std::move(get<I+1>())};
167 return move(dest, C<I+1>{});
171 copy(void*, C<sizeof...(TN)>) const
176 template<std::size_t I>
178 copy(void* dest, C<I>) const
183 auto const& t = get<I+1>();
187 return copy(dest, C<I+1>{});