From cbec1e75a88c6cfb44ce1225fb8a41d0112157d9 Mon Sep 17 00:00:00 2001 From: vvish Date: Mon, 7 Sep 2026 20:27:01 +0200 Subject: [PATCH] Remove POC for exception support --- include/cpp_lmdb/environment.hpp | 8 +-- include/cpp_lmdb/error.hpp | 63 +------------------ test/unit/CMakeLists.txt | 2 - test/unit/test_error_handling_exceptions.cpp | 58 ----------------- test/unit/test_error_handling_expected.cpp | 65 -------------------- 5 files changed, 5 insertions(+), 191 deletions(-) delete mode 100644 test/unit/test_error_handling_exceptions.cpp delete mode 100644 test/unit/test_error_handling_expected.cpp diff --git a/include/cpp_lmdb/environment.hpp b/include/cpp_lmdb/environment.hpp index 5d1e2a7..eedd840 100644 --- a/include/cpp_lmdb/environment.hpp +++ b/include/cpp_lmdb/environment.hpp @@ -100,8 +100,8 @@ class environment_base { cmp)); } - if constexpr (key_value_trait_helper< - KeyValueTrait>::has_value_cmp_fun) { + if constexpr ( + key_value_trait_helper::has_value_cmp_fun) { LMDB_CALL_API(api.mdb_set_dupsort( transaction->get(), db_index, @@ -179,8 +179,8 @@ template < auto make_environment( char const *const environment_path, db_file_mode_t const db_file_mode, - LmdbApi &&api = LmdbApi{}) - LMDB_NOEXCEPT->LMDB_RESULT((environment_t)) + LmdbApi &&api = LmdbApi{}) noexcept + -> std::expected, error_t> { MDB_env *env{nullptr}; LMDB_CALL_API(api.mdb_env_create(&env)); diff --git a/include/cpp_lmdb/error.hpp b/include/cpp_lmdb/error.hpp index ca4ce08..6d11623 100644 --- a/include/cpp_lmdb/error.hpp +++ b/include/cpp_lmdb/error.hpp @@ -3,7 +3,6 @@ #include "lmdb.h" // std -#include #include namespace lmdb @@ -32,70 +31,10 @@ enum class error_t : int { bad_dbi = MDB_BAD_DBI, }; -#ifdef CPP_LMDB_EXCEPTIONS_ENABLED - -class lmdb_exception : public std::exception { -public: - lmdb_exception(error_t error) noexcept : _error{error} - {} - - auto what() const noexcept -> char const* override - { - return ""; - } - - auto error() const noexcept -> error_t - { - return _error; - } - -private: - error_t _error; -}; - -#endif // CPP_LMDB_EXCEPTIONS_ENABLED - } // namespace lmdb -template -struct extract_parantesized_arg; -template -struct extract_parantesized_arg { - using arg = A; -}; - -#ifdef CPP_LMDB_EXCEPTIONS_ENABLED - -#define LMDB_RESULT(res_type) \ - typename extract_parantesized_arg::arg -#define LMDB_NOEXCEPT -// #define LMDB_NOEXCEPT_COND(cond) noexcept(cond) - -#define LMDB_REPORT_ERROR(code) \ - throw ::lmdb::lmdb_exception \ - { \ - code \ - } - -#else - -#define LMDB_RESULT(res_type) \ - std::expected< \ - typename extract_parantesized_arg::arg, \ - ::lmdb::error_t> - -#define LMDB_NOEXCEPT noexcept - -#define LMDB_REPORT_ERROR(code) \ - return std::unexpected \ - { \ - code \ - } - -#endif - #define LMDB_CALL_API(expr) \ do { \ if (auto const result = (expr); result != MDB_SUCCESS) \ - LMDB_REPORT_ERROR(::lmdb::error_t{result}); \ + return std::unexpected{::lmdb::error_t{result}}; \ } while (false) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 356e939..df8aa49 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -4,8 +4,6 @@ add_executable( test_environment.cpp test_transaction.cpp test_key_value_traits.cpp - test_error_handling_exceptions.cpp - test_error_handling_expected.cpp ) enable_testing() diff --git a/test/unit/test_error_handling_exceptions.cpp b/test/unit/test_error_handling_exceptions.cpp deleted file mode 100644 index 0846bc5..0000000 --- a/test/unit/test_error_handling_exceptions.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#define CPP_LMDB_EXCEPTIONS_ENABLED -#include "cpp_lmdb/error.hpp" -#include "mocks.hpp" - -// gtest -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -// std -#include - -namespace cpp_lmdb_tests -{ -namespace -{ -struct test_result {}; - -void test_function_noexcept() LMDB_NOEXCEPT{}; -} // namespace - -static_assert(std::is_same_v); -static_assert(!noexcept(test_function_noexcept())); - -TEST(error_handling_exceptions, report_error) -{ - try { - LMDB_REPORT_ERROR(lmdb::error_t::bad_dbi); - FAIL(); - } catch (lmdb::lmdb_exception const &ex) { - EXPECT_EQ(ex.error(), lmdb::error_t::bad_dbi); - } -} - -TEST(error_handling_exceptions, api_call_exception) -{ - MDB_env *env{}; - - StrictMock api; - EXPECT_CALL(api, mdb_env_create(_)).WillOnce(Return(MDB_BAD_DBI)); - - try { - LMDB_CALL_API(api.mdb_env_create(&env)); - FAIL(); - } catch (lmdb::lmdb_exception const &ex) { - EXPECT_EQ(ex.error(), lmdb::error_t::bad_dbi); - } -} - -TEST(error_handling_exceptions, api_call_no_exception) -{ - MDB_env *env{}; - - StrictMock api; - EXPECT_CALL(api, mdb_env_create(_)).WillOnce(Return(MDB_SUCCESS)); - - EXPECT_NO_THROW(LMDB_CALL_API(api.mdb_env_create(&env))); -} -} // namespace cpp_lmdb_tests diff --git a/test/unit/test_error_handling_expected.cpp b/test/unit/test_error_handling_expected.cpp deleted file mode 100644 index 03b77cd..0000000 --- a/test/unit/test_error_handling_expected.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "cpp_lmdb/error.hpp" -#include "mocks.hpp" - -// gtest -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -// std -#include - -namespace cpp_lmdb_tests -{ - -namespace -{ -struct test_result {}; - -void test_function_noexcept() LMDB_NOEXCEPT{}; -} // namespace - -static_assert(std::is_same_v< - LMDB_RESULT(test_result), - std::expected>); -static_assert(noexcept(test_function_noexcept())); - -TEST(error_handling_expected, report_error) -{ - auto const error = []() { LMDB_REPORT_ERROR(lmdb::error_t::bad_dbi); }(); - - EXPECT_TRUE(( - std:: - is_same_v>)); - - EXPECT_EQ(error.error(), lmdb::error_t::bad_dbi); -} - -TEST(error_handling_expected, api_call_error) -{ - StrictMock api; - EXPECT_CALL(api, mdb_env_create(_)).WillOnce(Return(MDB_BAD_DBI)); - - auto const result = [&api]() -> std::expected { - MDB_env *env{}; - LMDB_CALL_API(api.mdb_env_create(&env)); - return {}; - }(); - - ASSERT_FALSE(result); - EXPECT_EQ(result.error(), lmdb::error_t::bad_dbi); -} - -TEST(error_handling_expected, api_call_no_error) -{ - StrictMock api; - EXPECT_CALL(api, mdb_env_create(_)).WillOnce(Return(MDB_SUCCESS)); - - auto const result = [&api]() -> std::expected { - MDB_env *env{}; - LMDB_CALL_API(api.mdb_env_create(&env)); - return {}; - }(); - - ASSERT_TRUE(result); -} -} // namespace cpp_lmdb_tests