Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .github/workflows/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
35 changes: 22 additions & 13 deletions include/oryx/key_value_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
namespace oryx {
namespace detail {

template <typename T, typename... Us>
struct is_same_r : std::bool_constant<(std::is_same_v<T, Us> || ...)> {};

template <typename T, typename... Us>
inline constexpr bool is_same_r_v = is_same_r<T, Us...>::value;

template <typename T>
constexpr auto FromChars(std::string_view s) -> std::optional<T> {
T val;
Expand All @@ -33,18 +39,18 @@ constexpr auto FromChars<bool>(std::string_view s) -> std::optional<bool> {
}

template <typename T>
auto Read(const std::string& val) -> std::optional<T> {
constexpr auto Read(const std::string& val) -> std::optional<T> {
using _T = std::remove_cvref_t<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<bool>(val);
} else if constexpr (std::is_floating_point<_T>()) {
else if constexpr (std::is_floating_point_v<_T>)
return FromChars<T>(val);
} else if constexpr (std::is_integral<_T>()) {
else if constexpr (std::is_integral_v<_T>)
return FromChars<T>(val);
} else {
else {
if (auto result = rfl::json::read<T>(val); result) {
return result.value();
} else {
Expand All @@ -54,20 +60,19 @@ auto Read(const std::string& val) -> std::optional<T> {
}

template <typename T>
auto Write(const T& obj) -> std::string {
constexpr auto Write(const T& obj) {
using _T = std::remove_cvref_t<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<uint8_t>(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
Expand All @@ -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<leveldb::DB>(db);
}
#endif
return status;
}

Expand Down
1 change: 1 addition & 0 deletions tests/read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ TEST_CASE("Reading supported types") {

TEST_CASE("Writing supported types") {
CHECK_EQ(detail::Write<std::string>("hello world1232!*2`-."), "hello world1232!*2`-.");
CHECK_EQ(detail::Write<std::string_view>("hello world1232!*2`-."), "hello world1232!*2`-.");
CHECK_EQ(detail::Write<bool>(false), "0");
CHECK_EQ(detail::Write<bool>(true), "1");
CHECK_EQ(detail::Write<double>(1.256), "1.256000"); // std::to_string appends 0 at the end for some reason
Expand Down