From bd1f6709d2ef5430ad27520bda1e9581e8d98a27 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Thu, 27 Mar 2025 11:20:39 +0000 Subject: implement nv_list::free_type --- libnvxx/nv_list.cc | 13 +++++++++++++ libnvxx/nvxx.3 | 19 +++++++++++++++++++ libnvxx/nvxx_base.h | 6 +++--- libnvxx/tests/nvxx_basic.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/libnvxx/nv_list.cc b/libnvxx/nv_list.cc index 852aa76..1ab382e 100644 --- a/libnvxx/nv_list.cc +++ b/libnvxx/nv_list.cc @@ -158,6 +158,19 @@ __nv_list::free(std::string_view key) ::nvlist_free(__m_nv, std::string(key).c_str()); } +void +__nv_list::free_type(std::string_view key, int type) +{ + __throw_if_error(); + + auto skey = std::string(key); + + if (!::nvlist_exists_type(__m_nv, skey.c_str(), type)) + throw nv_key_not_found(skey); + + ::nvlist_free_type(__m_nv, skey.c_str(), type); +} + /* * null operations */ diff --git a/libnvxx/nvxx.3 b/libnvxx/nvxx.3 index 8a43649..19b572d 100644 --- a/libnvxx/nvxx.3 +++ b/libnvxx/nvxx.3 @@ -133,6 +133,8 @@ struct nv_list : { void move_nvlist(std::string_view key, nv_list &&); void move_nvlist(std::string_view key, ::nvlist_t *); + void free(std::string_view key); + void free_type(std::string_view key, int type); void free_null(std::string_view key); void free_bool(std::string_view key); void free_number(std::string_view key); @@ -481,6 +483,23 @@ described for the member functions. .Pp The +.Fn free +member function removes the first key of the given name. +If no such key exists, an exception of type +.Vt nv_key_not_found +is thrown. +.Pp +The +.Fn free_type +member function removes the first key of the given name and type. +The +.Fa type +arguments should be one of the type constants defined in . +If no such key exists, an exception of type +.Vt nv_key_not_found +is thrown. +.Pp +The .Fn free_null , .Fn free_bool , .Fn free_number , diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index aa1f176..de62d62 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -164,7 +164,8 @@ struct __const_nv_list : virtual __nv_list_base { /* * If a key of the given type with the given name exists, return true. */ - [[nodiscard]] bool exists_type(std::string_view __key, int type) const; + [[nodiscard]] bool exists_type(std::string_view __key, + int __type) const; // TODO: exists_type() @@ -205,7 +206,6 @@ struct __const_nv_list : virtual __nv_list_base { [[nodiscard]] auto get_binary(std::string_view key) const -> std::span; - [[nodiscard]] auto get_bool_array(std::string_view __key) const -> std::span; @@ -267,7 +267,7 @@ struct __nv_list : virtual __nv_list_base { /* free */ void free(std::string_view __key); - // TODO: free_type() + void free_type(std::string_view __key, int __type); void free_null(std::string_view __key); void free_bool(std::string_view __key); void free_number(std::string_view __key); diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index d73a54b..b701f71 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -75,6 +75,47 @@ TEST_CASE(nvxx_exists_type) ATF_REQUIRE_EQ(false, nvl.exists_type("nonesuch", NV_TYPE_NUMBER)); } +/* + * free(_type) + */ + +TEST_CASE(nvxx_free) +{ + using namespace std::literals; + auto key = "test number"sv; + auto value = 42u; + + auto nvl = bsd::nv_list{}; + nvl.add_number(key, value); + ATF_REQUIRE_EQ(true, nvl.exists(key)); + nvl.free(key); + ATF_REQUIRE_EQ(false, nvl.exists(key)); +} + +TEST_CASE(nvxx_free_type) +{ + using namespace std::literals; + auto key = "test number"sv; + auto value = 42u; + + auto nvl = bsd::nv_list{}; + nvl.add_number(key, value); + ATF_REQUIRE_EQ(true, nvl.exists(key)); + nvl.free_type(key, NV_TYPE_NUMBER); + ATF_REQUIRE_EQ(false, nvl.exists(key)); +} + +TEST_CASE(nvxx_free_type_nonexistent) +{ + using namespace std::literals; + auto key = "test number"sv; + + auto nvl = bsd::nv_list{}; + ATF_REQUIRE_EQ(false, nvl.exists(key)); + ATF_REQUIRE_THROW(bsd::nv_key_not_found, + nvl.free_type(key, NV_TYPE_NUMBER)); +} + /* * test the NV_FLAG_IGNORE_CASE flag. */ @@ -1007,6 +1048,10 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, nvxx_exists); ATF_ADD_TEST_CASE(tcs, nvxx_exists_type); + ATF_ADD_TEST_CASE(tcs, nvxx_free); + ATF_ADD_TEST_CASE(tcs, nvxx_free_type); + ATF_ADD_TEST_CASE(tcs, nvxx_free_type_nonexistent); + ATF_ADD_TEST_CASE(tcs, nvxx_add_null); ATF_ADD_TEST_CASE(tcs, nvxx_add_null_error); ATF_ADD_TEST_CASE(tcs, nvxx_add_duplicate_null); -- cgit v1.2.3