diff --git a/.github/workflows/linux.yaml b/.github/workflows/linux.yaml index b00d570..8d38252 100644 --- a/.github/workflows/linux.yaml +++ b/.github/workflows/linux.yaml @@ -16,11 +16,15 @@ jobs: cxx: 20 - compiler: llvm compiler-version: 18 - cxx: 20 + cxx: 20 + - compiler: llvm + compiler-version: 19 + deps: "clang-19" + cxx: 23 - compiler: gcc compiler-version: 11 - additional-dep: "g++-11" - cxx: 20 + cxx: 20 + deps: "g++-11" - compiler: gcc compiler-version: 12 cxx: 20 @@ -29,7 +33,7 @@ jobs: cxx: 20 - compiler: gcc compiler-version: 14 - cxx: 20 + cxx: 23 name: "${{ github.job }} (C++${{ matrix.cxx }}-${{ matrix.compiler }}-${{ matrix.compiler-version }})" runs-on: ubuntu-24.04 steps: @@ -52,7 +56,7 @@ jobs: - name: Install dependencies run: | sudo apt update - sudo apt install -y ninja-build ${{ matrix.additional-dep }} + sudo apt install -y ninja-build ${{ matrix.deps }} - name: Compile run: | if [[ "${{ matrix.compiler }}" == "llvm" ]]; then diff --git a/README.md b/README.md index 76bc23e..7cd0c2d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # ![C++](https://img.shields.io/badge/c++-%2300599C.svg?style=for-the-badge&logo=c%2B%2B&logoColor=white) kvdb-cpp +[![linux](https://github.com/BestITUserEUW/kvdb-cpp/actions/workflows/linux.yaml/badge.svg)](https://github.com/BestITUserEUW/kvdb-cpp/actions/workflows/linux.yaml) + Orm Key Value Database **kvdb-cpp** is a simple orm key value database using leveldb as db engine and reflect-cpp for providing orm support. diff --git a/include/oryx/key_value_database.hpp b/include/oryx/key_value_database.hpp index 9a4b0d3..dc828bc 100644 --- a/include/oryx/key_value_database.hpp +++ b/include/oryx/key_value_database.hpp @@ -14,6 +14,12 @@ namespace oryx { namespace detail { +template +struct is_same_r : std::bool_constant<(std::is_same_v || ...)> {}; + +template +inline constexpr bool is_same_r_v = is_same_r::value; + template constexpr auto FromChars(std::string_view s) -> std::optional { T val; @@ -33,18 +39,18 @@ constexpr auto FromChars(std::string_view s) -> std::optional { } template -auto Read(const std::string& val) -> std::optional { +constexpr auto Read(const std::string& val) -> std::optional { using _T = std::remove_cvref_t; - if constexpr (std::is_same<_T, std::string>()) { + if constexpr (std::is_same_v<_T, std::string>) return val; - } else if constexpr (std::is_same<_T, bool>()) { + else if constexpr (std::is_same_v<_T, bool>) return FromChars(val); - } else if constexpr (std::is_floating_point<_T>()) { + else if constexpr (std::is_floating_point_v<_T>) return FromChars(val); - } else if constexpr (std::is_integral<_T>()) { + else if constexpr (std::is_integral_v<_T>) return FromChars(val); - } else { + else { if (auto result = rfl::json::read(val); result) { return result.value(); } else { @@ -54,20 +60,19 @@ auto Read(const std::string& val) -> std::optional { } template -auto Write(const T& obj) -> std::string { +constexpr auto Write(const T& obj) { using _T = std::remove_cvref_t; - if constexpr (std::is_same<_T, std::string>()) { + if constexpr (is_same_r_v<_T, std::string, std::string_view>) return obj; - } else if constexpr (std::is_same<_T, bool>()) { + else if constexpr (std::is_same_v<_T, bool>) return std::to_string(static_cast(obj)); - } else if constexpr (std::is_floating_point<_T>()) { + else if constexpr (std::is_floating_point_v<_T>) return std::to_string(obj); - } else if constexpr (std::is_integral<_T>()) { + else if constexpr (std::is_integral_v<_T>) return std::to_string(obj); - } else { + else return rfl::json::write(obj); - } } } // namespace detail @@ -79,11 +84,15 @@ class KeyValueDatabase { auto Open(const std::string& name, const leveldb::Options& opts = DefaultOptions()) -> leveldb::Status { Close(); +#ifdef __cpp_lib_out_ptr + const auto status = leveldb::DB::Open(opts, name, std::out_ptr(handle_)); +#else leveldb::DB* db; const auto status = leveldb::DB::Open(opts, name, &db); if (status.ok()) { handle_ = std::unique_ptr(db); } +#endif return status; } diff --git a/tests/read_write.cpp b/tests/read_write.cpp index 02f75fb..7a39125 100644 --- a/tests/read_write.cpp +++ b/tests/read_write.cpp @@ -62,6 +62,7 @@ TEST_CASE("Reading supported types") { TEST_CASE("Writing supported types") { CHECK_EQ(detail::Write("hello world1232!*2`-."), "hello world1232!*2`-."); + CHECK_EQ(detail::Write("hello world1232!*2`-."), "hello world1232!*2`-."); CHECK_EQ(detail::Write(false), "0"); CHECK_EQ(detail::Write(true), "1"); CHECK_EQ(detail::Write(1.256), "1.256000"); // std::to_string appends 0 at the end for some reason