Skip to content

Commit b2d690e

Browse files
committed
Add error category contains(ec) method.
1 parent 489dae9 commit b2d690e

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

include/bitcoin/system/error/macros.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class BC_API cat##_category \
5757
static const message_map<cat##_t> messages; \
5858
public: \
5959
static const cat##_category singleton; \
60+
static bool contains(const std::error_code& ec) noexcept; \
6061
virtual const char* name() const noexcept; \
6162
virtual std::string message(int condition) const noexcept; \
6263
virtual std::error_condition default_error_condition(int value) const noexcept; \
@@ -74,6 +75,7 @@ class BC_API cat##_category \
7475
static const message_map<cat##_t> messages; \
7576
public: \
7677
static const cat##_category singleton; \
78+
static bool contains(const std::error_code& ec) noexcept; \
7779
virtual const char* name() const noexcept; \
7880
virtual std::string message(int condition) const noexcept; \
7981
virtual std::error_condition default_error_condition(int value) const noexcept; \
@@ -84,6 +86,10 @@ std::error_condition make_error_condition(cat##_t value) noexcept
8486

8587
#define DEFINE_ERROR_T_CATEGORY(cat, category_name, unmapped) \
8688
const cat##_category cat##_category::singleton; \
89+
bool cat##_category::contains(const std::error_code& ec) noexcept \
90+
{ \
91+
return is_zero(std::strcmp(ec.category().name(), category_name)); \
92+
} \
8793
const char* cat##_category::name() const noexcept \
8894
{ \
8995
return category_name; \

test/error/error.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ BOOST_AUTO_TEST_CASE(error_t__code__message_overflow__default_message)
4040
// Overflows can be cast in, and value retained, and yield default message.
4141
BOOST_AUTO_TEST_CASE(error_t__code__value_overflow__default_message)
4242
{
43-
const auto overflow = static_cast<int>(error::error_last);
43+
constexpr auto overflow = static_cast<int>(error::error_last);
4444
const auto ec = code(static_cast<error::error_t>(overflow));
4545
BOOST_REQUIRE(ec);
4646
BOOST_REQUIRE_EQUAL(ec.message(), default_message);
@@ -108,6 +108,14 @@ BOOST_AUTO_TEST_CASE(error_t__code__category_name__expected)
108108
{
109109
BOOST_REQUIRE_EQUAL(code().category().name(), system_category_name);
110110
BOOST_REQUIRE_EQUAL(code(error::success).category().name(), bitcoin_category_name);
111+
BOOST_REQUIRE_NE(code(error::script_success).category().name(), code(error::success).category().name());
112+
}
113+
114+
BOOST_AUTO_TEST_CASE(error_t__code__category_contains__expected)
115+
{
116+
BOOST_REQUIRE(system::error::error_category::contains(system::error::success));
117+
BOOST_REQUIRE(!system::error::script_error_category::contains(system::error::success));
118+
BOOST_REQUIRE(system::error::script_error_category::contains(system::error::script_success));
111119
}
112120

113121
BOOST_AUTO_TEST_CASE(error_t__code__default_error_condition_category_name__expected)

0 commit comments

Comments
 (0)