Skip to content

Commit ec29833

Browse files
authored
Merge pull request #1746 from evoskuil/master
define FORWARD_VARIANT(type, inner).
2 parents 9fe287a + f8c9b6e commit ec29833

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

include/bitcoin/system/types.hpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define LIBBITCOIN_SYSTEM_TYPES_HPP
2121

2222
#include <tuple>
23+
#include <utility>
2324
#include <variant>
2425
#include <bitcoin/system/allocator.hpp>
2526

@@ -202,7 +203,7 @@ struct text_t
202203
BC_POP_WARNING()
203204
BC_POP_WARNING()
204205

205-
/// Overload pattern.
206+
/// Variant (overload and forwarding).
206207
/// ---------------------------------------------------------------------------
207208
/// For use with std::visit.
208209
template<class... Overload>
@@ -212,6 +213,42 @@ struct overload : Overload... { using Overload::operator()...; };
212213
/// Explicit deduction guide, should not be required in C++20 (namespace scope).
213214
template<class... Overload> overload(Overload...) -> overload<Overload...>;
214215

216+
/// Declare on struct to define constructors that forward to an inner variant.
217+
#define FORWARD_VARIANT_CONSTRUCT(type, inner) \
218+
template <class Type, class... Args> \
219+
constexpr type(std::in_place_type_t<Type >, Args&&... args) NOEXCEPT \
220+
: inner(std::in_place_type<Type>, std::forward<Args>(args)...) \
221+
{ \
222+
} \
223+
template <size_t Index, class... Args> \
224+
constexpr type(std::in_place_index_t<Index>, Args&&... args) NOEXCEPT \
225+
: inner(std::in_place_index<Index>, std::forward<Args>(args)...) \
226+
{ \
227+
}
228+
229+
/// Assign to inner variant by emplacing a new alternative in-place.
230+
#define FORWARD_VARIANT_ASSIGNMENT(type, inner) \
231+
template <class Type, class... Args> \
232+
constexpr type& assign(std::in_place_type_t<Type>, Args&&... args) NOEXCEPT \
233+
{ \
234+
inner.emplace<Type>(std::forward<Args>(args)...); \
235+
return *this; \
236+
} \
237+
template <size_t Index, class... Args> \
238+
constexpr type& assign(std::in_place_index_t<Index>, Args&&... args) NOEXCEPT \
239+
{ \
240+
inner.emplace<Index>(std::forward<Args>(args)...); \
241+
return *this; \
242+
}
243+
244+
/// Generate operator= overloads forwarding assignment for a specific alt type.
245+
#define FORWARD_ALTERNATIVE_VARIANT_ASSIGNMENT(type, Alternative, inner) \
246+
type& operator=(Alternative&& alternative) NOEXCEPT \
247+
{ \
248+
inner.emplace(std::forward<Alternative>(alternative)); \
249+
return *this; \
250+
}
251+
215252
/// Argument placeholders.
216253
/// ---------------------------------------------------------------------------
217254

0 commit comments

Comments
 (0)