diff options
| -rw-r--r-- | libnvxx/const_nv_list.cc | 6 | ||||
| -rw-r--r-- | libnvxx/nvxx.3 | 8 | ||||
| -rw-r--r-- | libnvxx/nvxx_base.h | 5 | ||||
| -rw-r--r-- | libnvxx/tests/nvxx_basic.cc | 32 |
4 files changed, 51 insertions, 0 deletions
diff --git a/libnvxx/const_nv_list.cc b/libnvxx/const_nv_list.cc index 883eebb..0ed83b2 100644 --- a/libnvxx/const_nv_list.cc +++ b/libnvxx/const_nv_list.cc @@ -74,6 +74,12 @@ __const_nv_list::exists(std::string_view key) const } bool +__const_nv_list::exists_type(std::string_view key, int type) const +{ + return ::nvlist_exists_type(__m_nv, std::string(key).c_str(), type); +} + +bool __const_nv_list::empty() const noexcept { return ::nvlist_empty(__m_nv); diff --git a/libnvxx/nvxx.3 b/libnvxx/nvxx.3 index 26fa5eb..8a43649 100644 --- a/libnvxx/nvxx.3 +++ b/libnvxx/nvxx.3 @@ -66,6 +66,7 @@ struct const_nv_list : <unspecified> { void send(int fd) const; bool exists(std::string_view key) const; + bool exists_type(std::string_view key, int) const; bool exists_null(std::string_view key) const; bool exists_bool(std::string_view key) const; bool exists_number(std::string_view key) const; @@ -361,6 +362,13 @@ member function returns if a key by the given name exists, otherwise .Dv false . .Pp +The +.Fn exists_type +member function returns +.Dv true +if a key by the given name exists with the specified type, which should be one +of the type constants defined in <sys/nv.h>. +.Pp The .Fn exists_null , .Fn exists_bool , diff --git a/libnvxx/nvxx_base.h b/libnvxx/nvxx_base.h index df97893..aa1f176 100644 --- a/libnvxx/nvxx_base.h +++ b/libnvxx/nvxx_base.h @@ -161,6 +161,11 @@ struct __const_nv_list : virtual __nv_list_base { */ [[nodiscard]] bool exists(std::string_view __key) const; + /* + * 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; + // TODO: exists_type() /* exists */ diff --git a/libnvxx/tests/nvxx_basic.cc b/libnvxx/tests/nvxx_basic.cc index d8faea3..d73a54b 100644 --- a/libnvxx/tests/nvxx_basic.cc +++ b/libnvxx/tests/nvxx_basic.cc @@ -47,6 +47,35 @@ TEST_CASE(nvxx_const_ctor_error) } /* + * exists(_type) + */ + +TEST_CASE(nvxx_exists) +{ + 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)); + ATF_REQUIRE_EQ(false, nvl.exists("nonesuch")); +} + +TEST_CASE(nvxx_exists_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_type(key, NV_TYPE_NUMBER)); + ATF_REQUIRE_EQ(false, nvl.exists_type(key, NV_TYPE_STRING)); + ATF_REQUIRE_EQ(false, nvl.exists_type("nonesuch", NV_TYPE_NUMBER)); +} + +/* * test the NV_FLAG_IGNORE_CASE flag. */ @@ -975,6 +1004,9 @@ ATF_INIT_TEST_CASES(tcs) ATF_ADD_TEST_CASE(tcs, nvxx_const_ctor_error); ATF_ADD_TEST_CASE(tcs, nvxx_ignore_case); + ATF_ADD_TEST_CASE(tcs, nvxx_exists); + ATF_ADD_TEST_CASE(tcs, nvxx_exists_type); + 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); |
