Skip to content

Commit fd126e4

Browse files
committed
Add DEFINE_HAS_METHOD() and tests.
1 parent 06cc797 commit fd126e4

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

include/bitcoin/system/constraints.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ using if_byte_insertable = bool_if<
280280
std::is_base_of<std_vector<uint8_t>, Type>::value ||
281281
std::is_base_of<std::vector<uint8_t>, Type>::value>;
282282

283+
/// define: DEFINE_HAS_METHOD(flush)
284+
/// usage: bool_if<has_flush_v<Type>>
285+
#define DEFINE_HAS_METHOD(method) \
286+
template <typename T, typename = void> \
287+
struct has_##method : std::false_type {}; \
288+
template <typename T> \
289+
struct has_##method<T, std::void_t<decltype(&T::method)>> : std::true_type {}; \
290+
template <typename Type> \
291+
static constexpr bool has_##method##_v = has_##method<Type>::value
292+
283293
} // namespace libbitcoin
284294

285295
#endif

include/bitcoin/system/types.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ BC_PUSH_WARNING(NO_DYNAMIC_ARRAY_INDEXING)
183183
BC_PUSH_WARNING(NO_ARRAY_INDEXING)
184184

185185
template <size_t Size>
186-
struct text_t
186+
class text_t
187187
{
188+
public:
188189
std::array<uint8_t, Size - 1> data;
189190
CONSTEVAL text_t(const char (&string)[Size]) noexcept
190191
: data(to_array(string)) {}

test/constraints.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,4 +652,21 @@ static_assert(is_defined<if_byte_insertable<std_vector<uint8_t>>>);
652652
////static_assert(!is_defined<if_byte_insertable<std::array<uint8_t, 42>>>);
653653
////static_assert(!is_defined<if_byte_insertable<std::u32string>>);
654654
////static_assert(!is_defined<if_byte_insertable<std_vector<uint32_t>>>);
655-
////static_assert(!is_defined<if_byte_insertable<uint32_t>>);
655+
////static_assert(!is_defined<if_byte_insertable<uint32_t>>);
656+
657+
// HAS_METHOD
658+
659+
struct not_has { void foobar_() {} };
660+
struct has_args { void foobar(bool, int) {} };
661+
struct has_base { void foobar() {} };
662+
struct has_publics_derived : public has_base {};
663+
struct has_protect_derived : protected has_base {};
664+
struct has_private_derived : private has_base {};
665+
666+
DEFINE_HAS_METHOD(foobar);
667+
static_assert(!has_foobar_v<not_has>);
668+
static_assert(has_foobar_v<has_args>);
669+
static_assert(has_foobar_v<has_base>);
670+
static_assert(has_foobar_v<has_publics_derived>);
671+
static_assert(!has_foobar_v<has_protect_derived>);
672+
static_assert(!has_foobar_v<has_private_derived>);

0 commit comments

Comments
 (0)