Skip to content

Commit f640e4b

Browse files
committed
Implement is_tuple<>/if_tuple<>.
1 parent d4365d3 commit f640e4b

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

include/bitcoin/system/constraints.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ template <typename Integer>
259259
using if_floating_point = bool_if<
260260
is_floating_point<Integer>>;
261261

262-
/// std::array/std::vector
262+
/// std::array/std::vector/std::tuple
263263

264264
template <typename Type>
265265
using if_std_array = bool_if<
@@ -274,6 +274,10 @@ using if_integral_array = bool_if<
274274
is_std_array<Type> &&
275275
is_integral_integer<typename Type::value_type>>;
276276

277+
template <typename Type>
278+
using if_tuple = bool_if<
279+
is_tuple<Type>>;
280+
277281
template <typename Type>
278282
using if_byte_insertable = bool_if<
279283
std::is_base_of<std::string, Type>::value ||

include/bitcoin/system/typelets.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ constexpr size_t bits = to_bits(sizeof(Type));
9797
template <size_t Bits, std::enable_if_t<is_byte_sized(Bits), bool> = true>
9898
constexpr size_t bytes = Bits / byte_bits;
9999

100-
/// std_array.
100+
/// std_array/std_vector.
101101
/// ---------------------------------------------------------------------------
102102

103103
template<typename>
@@ -140,7 +140,7 @@ constexpr size_t size_of()
140140

141141
// Type constraint fails to match as throw is not constexpr.
142142
if (count > (std::numeric_limits<size_t>::max() / size))
143-
throw overflow_exception("type contraint violated");
143+
throw overflow_exception("type constraint violated");
144144

145145
return size * count;
146146
}
@@ -151,9 +151,16 @@ template <typename Larger, typename Smaller, size_t Lanes = one,
151151
////std::enable_if_t<Lanes <= (max_size_t / size_of<Smaller>()), bool> = true>
152152
constexpr size_t capacity = size_of<Larger>() / (Lanes * size_of<Smaller>());
153153

154-
/// std::tuple decay elements.
154+
/// std::tuple.
155155
/// ---------------------------------------------------------------------------
156156

157+
template <typename, typename = void>
158+
struct is_tuple_t : std::false_type {};
159+
template <typename ...Args>
160+
struct is_tuple_t<std::tuple<Args...>> : std::true_type {};
161+
template<typename Type>
162+
constexpr bool is_tuple = is_tuple_t<Type>::value;
163+
157164
template <typename Tuple>
158165
struct decay_tuple;
159166

test/constraints.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,12 @@ static_assert(is_defined<if_integral_array<std_array<uint8_t, 0>>>);
645645
////static_assert(!is_defined<if_integral_array<std_array<base, 0>>>);
646646
////static_assert(!is_defined<if_integral_array<uint8_t>>);
647647

648+
static_assert(is_defined<if_tuple<std::tuple<>>>);
649+
static_assert(is_defined<if_tuple<std::tuple<uint8_t, bool>>>);
650+
////static_assert(!is_defined<if_tuple<std_array<uint8_t, 0>>>);
651+
////static_assert(!is_defined<if_tuple<uint8_t>>);
652+
////static_assert(!is_defined<if_tuple<bool>>);
653+
648654
static_assert(is_defined<if_byte_insertable<std::string>>);
649655
static_assert(is_defined<if_byte_insertable<std::vector<uint8_t>>>);
650656
static_assert(is_defined<if_byte_insertable<std_vector<uint8_t>>>);

test/typelets.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,18 @@ static_assert(size_of<std_array<const volatile std_array<std_array<uint32_t, 42>
417417
static_assert(size_of<std_array<std_array<const volatile std_array<uint32_t, 42>, 24>, 8>>() == sizeof(uint32_t) * 42 * 24 * 8);
418418
static_assert(size_of<const volatile std_array<const volatile std_array<const volatile std_array<uint32_t, 42>, 24>, 8>&>() == sizeof(uint32_t) * 42 * 24 * 8);
419419

420-
421-
// std::tuple decay elements.
420+
// std::tuple.
422421
// ----------------------------------------------------------------------------
423422
// is_same_type decays individual types (but not tuple elements).
424423

424+
static_assert(!is_tuple<uint8_t>);
425+
static_assert(!is_tuple<std::string>);
426+
static_assert(!is_tuple<std::array<uint8_t, 42>>);
427+
static_assert( is_tuple<std::tuple<int, bool>>);
428+
static_assert( is_tuple<std::tuple<>>);
429+
static_assert( is_same_type<decltype(is_tuple<std_array<uint8_t, 0>>), const bool>);
430+
static_assert( is_same_type<decltype(is_tuple<std::tuple<int, bool>&>), const bool>);
431+
425432
using test_tuple = std::tuple<int&, bool&&, const std::string&>;
426433
static_assert( is_same_type<decay_tuple<test_tuple>, decay_tuple<test_tuple>>);
427434
static_assert(!is_same_type<test_tuple, decay_tuple<test_tuple>>);

0 commit comments

Comments
 (0)