Skip to content

Commit 5917f47

Browse files
authored
Merge pull request #1759 from evoskuil/master
Add is_empty_tuple, is_shared_ptr, tests.
2 parents 7a65bac + b1a3b24 commit 5917f47

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

include/bitcoin/system/typelets.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,30 @@ constexpr bool is_tuple = is_tuple_t<Type>::value;
163163

164164
template <typename Tuple>
165165
struct decay_tuple;
166-
167166
template <typename ...Args>
168167
struct decay_tuple<std::tuple<Args...>>
169168
{
170169
using type = std::tuple<std::decay_t<Args>...>;
171170
};
171+
template <typename Tuple>
172+
using decay_tuple_t = typename decay_tuple<Tuple>::type;
173+
174+
template <typename Left, typename Right>
175+
constexpr bool is_same_tuple = is_same_type<decay_tuple_t<Left>,
176+
decay_tuple_t<Right>>;
177+
178+
template <typename Tuple>
179+
constexpr bool is_empty_tuple = is_zero(std::tuple_size_v<Tuple>);
180+
181+
/// std::shared_ptr<> detection.
182+
/// ---------------------------------------------------------------------------
183+
184+
template <typename Type>
185+
struct is_shared_ptr_t : std::false_type {};
186+
template <typename Type>
187+
struct is_shared_ptr_t<std::shared_ptr<Type>> : std::true_type {};
188+
template <typename Type>
189+
constexpr bool is_shared_ptr = is_shared_ptr_t<std::decay_t<Type>>::value;
172190

173191
/// uintx_t detection.
174192
/// ---------------------------------------------------------------------------

test/typelets.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,36 @@ static_assert(!std::is_same_v<std::tuple_element<0, decayed_test_tuple>::type, i
470470
static_assert(!std::is_same_v<std::tuple_element<1, decayed_test_tuple>::type, bool&&>);
471471
static_assert(!std::is_same_v<std::tuple_element<2, decayed_test_tuple>::type, std::string&&>);
472472

473-
// is_uintx
473+
static_assert( is_same_tuple<std::tuple<char* const&, double&&>, std::tuple<char*, double>>);
474+
static_assert(!is_same_tuple<std::tuple<int, double>, std::tuple<int, float>>);
475+
static_assert(!is_same_tuple<std::tuple<const char*>, std::tuple<int>>);
476+
static_assert(!is_same_tuple<std::tuple<int, double>, std::tuple<int>>);
477+
static_assert(!is_same_tuple<std::tuple<>, std::tuple<int>>);
478+
static_assert( is_same_tuple<std::tuple<>, std::tuple<>>);
479+
static_assert( is_same_tuple<std::tuple<int, double>, std::tuple<int, double>>);
480+
static_assert( is_same_tuple<std::tuple<int[5], void(*)(int)>, std::tuple<int*, void(*)(int)>>);
481+
static_assert(!is_same_tuple<std::tuple<char*, const char* const&>, std::tuple<char*, char*>>);
482+
483+
static_assert( is_empty_tuple<std::tuple<>>);
484+
static_assert(!is_empty_tuple<std::tuple<bool>>);
485+
486+
// std::shared_ptr<>
487+
// ----------------------------------------------------------------------------
488+
489+
static_assert( is_shared_ptr<std::shared_ptr<int>>);
490+
static_assert(!is_shared_ptr<std::unique_ptr<int>>);
491+
static_assert(!is_shared_ptr<std::weak_ptr<int>>);
492+
static_assert(!is_shared_ptr<int*>);
493+
static_assert(!is_shared_ptr<int>);
494+
static_assert( is_shared_ptr<const std::shared_ptr<double>>);
495+
static_assert( is_shared_ptr<const std::shared_ptr<double>&>);
496+
static_assert( is_shared_ptr<std::shared_ptr<double> const&>);
497+
static_assert( is_shared_ptr<std::shared_ptr<std::string>>);
498+
static_assert( is_shared_ptr<std::shared_ptr<const std::string>>);
499+
static_assert( is_shared_ptr<std::shared_ptr<const std::string&>>);
500+
static_assert(!is_shared_ptr<std::vector<int>>);
501+
502+
// uintx_t
474503
// ----------------------------------------------------------------------------
475504

476505
static_assert(is_uintx<uint5_t>);

0 commit comments

Comments
 (0)