mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
@@ -469,6 +469,7 @@ reflect-cpp supports the following containers from the C++ standard library:
|
||||
In addition, it supports the following custom containers:
|
||||
|
||||
- `rfl::Box`: Similar to `std::unique_ptr`, but (almost) guaranteed to never be null.
|
||||
- `rfl::Bytestring`: An alias for `std::basic_string<std::byte>`. Supported by BSON, CBOR, flexbuffers and msgpack.
|
||||
- `rfl::Generic`: A catch-all type that can represent (almost) anything.
|
||||
- `rfl::Literal`: An explicitly enumerated string.
|
||||
- `rfl::NamedTuple`: Similar to `std::tuple`, but with named fields that can be retrieved via their name at compile time.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2")
|
||||
|
||||
if (MSVC)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++20")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++20 -O2")
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -O2")
|
||||
endif()
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
|
||||
1.14) [C arrays and inheritance](https://github.com/getml/reflect-cpp/blob/main/docs/c_arrays_and_inheritance.md) - Describes how reflect-cpp handles C arrays and inheritance.
|
||||
|
||||
1.15) [rfl::Bytestring](https://github.com/getml/reflect-cpp/blob/main/docs/bytestring.md) - Describes how reflect-cpp handles binary strings for formats that support them.
|
||||
|
||||
## 2) Validation
|
||||
|
||||
2.1) [Regex patterns](https://github.com/getml/reflect-cpp/blob/main/docs/patterns.md) - For requiring that strings follow used-defined regex patterns.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# `rfl::Bytestring`
|
||||
|
||||
`rfl::Bytestring` is a simple alias:
|
||||
|
||||
```cpp
|
||||
namespace rfl {
|
||||
|
||||
using Bytestring = std::basic_string<std::byte>;
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
For more information on how to handle `std::basic_string` please refer to the
|
||||
relevant documentation:
|
||||
|
||||
https://en.cppreference.com/w/cpp/string/basic_string
|
||||
|
||||
Bytestrings are supported by BSON, CBOR, flexbuffers and msgpack. Textual formats
|
||||
do not support them.
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "rfl/AnyOf.hpp"
|
||||
#include "rfl/Attribute.hpp"
|
||||
#include "rfl/Box.hpp"
|
||||
#include "rfl/Bytestring.hpp"
|
||||
#include "rfl/Description.hpp"
|
||||
#include "rfl/ExtraFields.hpp"
|
||||
#include "rfl/Field.hpp"
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef RFL_BYTESTRING_HPP_
|
||||
#define RFL_BYTESTRING_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
|
||||
using Bytestring = std::basic_string<std::byte>;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -18,6 +19,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
@@ -108,6 +110,19 @@ struct Reader {
|
||||
return rfl::Error(
|
||||
"Could not cast to string. The type must be UTF8 or symbol.");
|
||||
}
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (btype != BSON_TYPE_BINARY) {
|
||||
return rfl::Error("Could not cast to bytestring.");
|
||||
}
|
||||
if (value.v_binary.subtype != BSON_SUBTYPE_BINARY) {
|
||||
return rfl::Error(
|
||||
"The BSON subtype must be a binary in order to read into a "
|
||||
"bytestring.");
|
||||
}
|
||||
return rfl::Bytestring(
|
||||
reinterpret_cast<const std::byte*>(value.v_binary.data),
|
||||
value.v_binary.data_len);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (btype != BSON_TYPE_BOOL) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <bson/bson.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
@@ -14,6 +15,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
@@ -125,6 +127,12 @@ class Writer {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
bson_array_builder_append_utf8(_parent->val_, _var.c_str(),
|
||||
static_cast<int>(_var.size()));
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
bson_array_builder_append_binary(
|
||||
_parent->val_, BSON_SUBTYPE_BINARY,
|
||||
reinterpret_cast<const uint8_t*>(_var.c_str()),
|
||||
static_cast<uint32_t>(_var.size()));
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
bson_array_builder_append_bool(_parent->val_, _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
@@ -149,6 +157,12 @@ class Writer {
|
||||
bson_append_utf8(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), _var.c_str(),
|
||||
static_cast<int>(_var.size()));
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
bson_append_binary(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
|
||||
reinterpret_cast<const uint8_t*>(_var.c_str()),
|
||||
static_cast<uint32_t>(_var.size()));
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
bson_append_bool(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), _var);
|
||||
|
||||
+44
-12
@@ -3,12 +3,14 @@
|
||||
|
||||
#include <cbor.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
@@ -65,7 +67,7 @@ struct Reader {
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
InputVarType var;
|
||||
auto buffer = std::vector<char>();
|
||||
auto buffer = std::string();
|
||||
auto err = cbor_value_enter_container(&_obj.val_, &var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
@@ -87,7 +89,7 @@ struct Reader {
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_name == buffer.data()) {
|
||||
if (_name == buffer) {
|
||||
return var;
|
||||
}
|
||||
err = cbor_value_advance(&var.val_);
|
||||
@@ -108,12 +110,23 @@ struct Reader {
|
||||
if (!cbor_value_is_text_string(&_var.val_)) {
|
||||
return Error("Could not cast to string.");
|
||||
}
|
||||
std::vector<char> buffer;
|
||||
const auto err = get_string(&_var.val_, &buffer);
|
||||
std::string str;
|
||||
const auto err = get_string(&_var.val_, &str);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return std::string(buffer.data());
|
||||
return str;
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (!cbor_value_is_byte_string(&_var.val_)) {
|
||||
return Error("Could not cast to bytestring.");
|
||||
}
|
||||
rfl::Bytestring bstr;
|
||||
const auto err = get_bytestring(&_var.val_, &bstr);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
return bstr;
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!cbor_value_is_boolean(&_var.val_)) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
@@ -215,7 +228,7 @@ struct Reader {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
|
||||
auto buffer = std::vector<char>();
|
||||
auto buffer = std::string();
|
||||
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
err = get_string(&var.val_, &buffer);
|
||||
@@ -226,7 +239,7 @@ struct Reader {
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
const auto name = std::string_view(buffer.data(), buffer.size() - 1);
|
||||
const auto name = std::string_view(buffer);
|
||||
_object_reader.read(name, var);
|
||||
cbor_value_advance(&var.val_);
|
||||
}
|
||||
@@ -245,16 +258,35 @@ struct Reader {
|
||||
}
|
||||
|
||||
private:
|
||||
CborError get_string(const CborValue* _ptr,
|
||||
std::vector<char>* _buffer) const noexcept {
|
||||
CborError get_bytestring(const CborValue* _ptr,
|
||||
rfl::Bytestring* _str) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_buffer->resize(length + 1);
|
||||
_buffer->back() = '\0';
|
||||
return cbor_value_copy_text_string(_ptr, _buffer->data(), &length, NULL);
|
||||
_str->resize(length);
|
||||
if (length > 0) {
|
||||
return cbor_value_copy_byte_string(
|
||||
_ptr, reinterpret_cast<uint8_t*>(&((*_str)[0])), &length, NULL);
|
||||
} else {
|
||||
return CborNoError;
|
||||
}
|
||||
}
|
||||
|
||||
CborError get_string(const CborValue* _ptr,
|
||||
std::string* _str) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_str->resize(length);
|
||||
if (length > 0) {
|
||||
return cbor_value_copy_text_string(_ptr, &((*_str)[0]), &length, NULL);
|
||||
} else {
|
||||
return CborNoError;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Box.hpp"
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
@@ -138,6 +139,10 @@ class Writer {
|
||||
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
cbor_encode_byte_string(
|
||||
_parent, reinterpret_cast<const uint8_t*>(_var.c_str()), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
cbor_encode_boolean(_parent, _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
@@ -12,6 +13,7 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
@@ -69,9 +71,17 @@ struct Reader {
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (!_var.IsString()) {
|
||||
return rfl::Error("Could not cast to string.");
|
||||
return rfl::Error("Could not cast to a string.");
|
||||
}
|
||||
return std::string(_var.AsString().c_str());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (!_var.IsBlob()) {
|
||||
return rfl::Error("Could not cast to a bytestring.");
|
||||
}
|
||||
const auto blob = _var.AsBlob();
|
||||
return rfl::Bytestring(reinterpret_cast<const std::byte*>(blob.data()),
|
||||
blob.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.IsBool()) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
@@ -14,6 +15,7 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
@@ -118,6 +120,9 @@ struct Writer {
|
||||
const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
fbb_->String(_name.data(), _var);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
fbb_->Blob(_name.data(), _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
fbb_->Bool(_name.data(), _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
@@ -134,6 +139,9 @@ struct Writer {
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
fbb_->String(_var);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
fbb_->Blob(_var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
fbb_->Bool(_var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
@@ -20,8 +22,9 @@ struct Reader {
|
||||
using InputVarType = msgpack_object;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_msgpack_obj(var); });
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_msgpack_obj(var);
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
@@ -61,6 +64,14 @@ struct Reader {
|
||||
}
|
||||
const auto str = _var.via.str;
|
||||
return std::string(str.ptr, str.size);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>()) {
|
||||
if (type != MSGPACK_OBJECT_BIN) {
|
||||
return Error("Could not cast to a bytestring.");
|
||||
}
|
||||
const auto bin = _var.via.bin;
|
||||
return rfl::Bytestring(reinterpret_cast<const std::byte*>(bin.ptr),
|
||||
bin.size);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (type != MSGPACK_OBJECT_BOOLEAN) {
|
||||
return Error("Could not cast to boolean.");
|
||||
|
||||
@@ -125,18 +125,22 @@ class Writer {
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_same<Type, std::string>()) {
|
||||
msgpack_pack_str(pk_, _var.size());
|
||||
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<Type, rfl::Bytestring>()) {
|
||||
msgpack_pack_bin(pk_, _var.size());
|
||||
msgpack_pack_bin_body(pk_, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<Type, bool>()) {
|
||||
if (_var) {
|
||||
msgpack_pack_true(pk_);
|
||||
} else {
|
||||
msgpack_pack_false(pk_);
|
||||
}
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<Type>()) {
|
||||
msgpack_pack_double(pk_, static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<Type>()) {
|
||||
msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_bytestring {
|
||||
|
||||
struct TestStruct {
|
||||
rfl::Bytestring bytestring;
|
||||
std::vector<rfl::Bytestring> bytestrings;
|
||||
};
|
||||
|
||||
TEST(bson, test_bytestring) {
|
||||
const auto bstr = rfl::Bytestring(
|
||||
{std::byte{13}, std::byte{14}, std::byte{15}, std::byte{16}});
|
||||
const auto test = TestStruct{
|
||||
.bytestring = bstr,
|
||||
.bytestrings = std::vector<rfl::Bytestring>({bstr, bstr, bstr})};
|
||||
write_and_read(test);
|
||||
}
|
||||
} // namespace test_bytestring
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_bytestring {
|
||||
|
||||
struct TestStruct {
|
||||
rfl::Bytestring bytestring;
|
||||
};
|
||||
|
||||
TEST(cbor, test_bytestring) {
|
||||
const auto test =
|
||||
TestStruct{.bytestring = rfl::Bytestring({std::byte{13}, std::byte{14},
|
||||
std::byte{15}, std::byte{16}})};
|
||||
|
||||
write_and_read(test);
|
||||
}
|
||||
} // namespace test_bytestring
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_bytestring {
|
||||
|
||||
struct TestStruct {
|
||||
rfl::Bytestring bytestring;
|
||||
std::vector<rfl::Bytestring> bytestrings;
|
||||
};
|
||||
|
||||
TEST(flexbuf, test_bytestring) {
|
||||
const auto bstr = rfl::Bytestring(
|
||||
{std::byte{13}, std::byte{14}, std::byte{15}, std::byte{16}});
|
||||
const auto test = TestStruct{
|
||||
.bytestring = bstr,
|
||||
.bytestrings = std::vector<rfl::Bytestring>({bstr, bstr, bstr})};
|
||||
write_and_read(test);
|
||||
}
|
||||
} // namespace test_bytestring
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_bytestring {
|
||||
|
||||
struct TestStruct {
|
||||
rfl::Bytestring bytestring;
|
||||
};
|
||||
|
||||
TEST(msgpack, test_bytestring) {
|
||||
const auto test =
|
||||
TestStruct{.bytestring = rfl::Bytestring({std::byte{13}, std::byte{14},
|
||||
std::byte{15}, std::byte{16}})};
|
||||
|
||||
write_and_read(test);
|
||||
}
|
||||
} // namespace test_bytestring
|
||||
Reference in New Issue
Block a user