From 249a5b6421098ccaa6d269afac2b0ee67f389860 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sat, 29 Mar 2025 04:42:19 +0000 Subject: change set_error() to take an std::errc --- libnvxx/nv_list.cc | 4 ++-- libnvxx/nvxx.3 | 7 ++++++- libnvxx/nvxx_base.h | 2 +- libnvxx/tests/nvxx_basic.cc | 30 ++++++++++++++++-------------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/libnvxx/nv_list.cc b/libnvxx/nv_list.cc index 8f868d9..3d8c0a1 100644 --- a/libnvxx/nv_list.cc +++ b/libnvxx/nv_list.cc @@ -142,12 +142,12 @@ namespace __detail { */ void -__nv_list::set_error(int error) +__nv_list::set_error(std::errc error) { __throw_if_null(); // nvlist does not allow changing an existing error state __throw_if_error(); - ::nvlist_set_error(__m_nv, error); + ::nvlist_set_error(__m_nv, static_cast(error)); } __nv_list::operator const_nv_list() const diff --git a/libnvxx/nvxx.3 b/libnvxx/nvxx.3 index 4cdfea4..ed3dfae 100644 --- a/libnvxx/nvxx.3 +++ b/libnvxx/nvxx.3 @@ -128,7 +128,7 @@ struct nv_list : { ::nvlist_t *release() &&; - void set_error(int error); + void set_error(std::errc error); void free(std::string_view name); @@ -595,6 +595,11 @@ is equal to .Vt T . .Sh NV_LIST OPERATIONS The +.Fn set_error +member function sets the given error on the nvlist, placing it in the error +state. +.Pp +The .Fn add_null , .Fn add_bool , .Fn add_number , diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index 0f6b254..257c217 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -205,7 +205,7 @@ struct __nv_list : virtual __nv_list_base { /* * Set the error code on this nvlist to the given value. */ - void set_error(int); + void set_error(std::errc); /* * Convert this nv_list into a const_nv_list. This is a shallow copy diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index 7638a0b..a552ad1 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -37,10 +37,11 @@ TEST_CASE(nvxx_set_error) auto nvl = bsd::nv_list(); ATF_REQUIRE_EQ(true, !nvl.error()); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_EQ(true, (nvl.error() == std::errc::invalid_argument)); - ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.set_error(EEXIST)); + ATF_REQUIRE_THROW(bsd::nv_error_state, + nvl.set_error(std::errc::invalid_argument)); } TEST_CASE(nvxx_error_null) @@ -56,7 +57,8 @@ TEST_CASE(nvxx_set_error_null) auto nvl = bsd::nv_list(); auto nvl2 = std::move(nvl); - ATF_REQUIRE_THROW(std::logic_error, nvl.set_error(EINVAL)); + ATF_REQUIRE_THROW(std::logic_error, + nvl.set_error(std::errc::invalid_argument)); } /* @@ -203,7 +205,7 @@ TEST_CASE(nvxx_add_null_error) auto constexpr key = "test_null"sv; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_null(key)); } @@ -296,7 +298,7 @@ TEST_CASE(nvxx_add_bool_error) auto constexpr value = true; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_bool(key, value)); } @@ -421,7 +423,7 @@ TEST_CASE(nvxx_add_bool_array_error) auto value = std::vector{true, false}; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_bool_range(key, value)); } @@ -558,7 +560,7 @@ TEST_CASE(nvxx_add_number_error) auto constexpr value = 42_u64; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_number(key, value)); } @@ -684,7 +686,7 @@ TEST_CASE(nvxx_add_number_array_error) auto value = std::vector{42, 666}; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_number_range(key, value)); } @@ -946,7 +948,7 @@ TEST_CASE(nvxx_add_string_error) auto constexpr value = "test"sv; auto nvl = bsd::nv_list{}; - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_string(key, value)); } @@ -957,7 +959,7 @@ TEST_CASE(nvxx_add_string_array_error) auto values = std::vector{"one"sv, "two"sv, "three"sv}; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_string_range(key, values)); } @@ -1095,7 +1097,7 @@ TEST_CASE(nvxx_add_nvlist_error) auto value = bsd::nv_list{}; auto nvl = bsd::nv_list{}; - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_nvlist(key, value)); } @@ -1227,7 +1229,7 @@ TEST_CASE(nvxx_add_nvlist_array_error) auto value = std::vector{bsd::nv_list{}, bsd::nv_list{}}; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_nvlist_array(key, value)); } @@ -1340,7 +1342,7 @@ TEST_CASE(nvxx_add_descriptor_error) auto key = "test_descriptor"sv; auto nvl = bsd::nv_list{}; - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_descriptor(key, 0)); } @@ -1454,7 +1456,7 @@ TEST_CASE(nvxx_add_binary_error) auto value = std::array{}; auto nvl = bsd::nv_list(); - nvl.set_error(EINVAL); + nvl.set_error(std::errc::invalid_argument); ATF_REQUIRE_THROW(bsd::nv_error_state, nvl.add_binary(key, value)); } -- cgit v1.2.3