From fae5e5ff586be8f2dd40029ab50f2619dd001239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Sun, 30 Jun 2024 20:41:53 +0200 Subject: [PATCH] Added support for bytestrings; #69 (#132) --- README.md | 1 + benchmarks/CMakeLists.txt | 2 +- docs/README.md | 2 + docs/bytestring.md | 19 +++++++++ include/rfl.hpp | 1 + include/rfl/Bytestring.hpp | 13 +++++++ include/rfl/bson/Reader.hpp | 15 +++++++ include/rfl/bson/Writer.hpp | 14 +++++++ include/rfl/cbor/Reader.hpp | 56 +++++++++++++++++++++------ include/rfl/cbor/Writer.hpp | 5 +++ include/rfl/flexbuf/Reader.hpp | 12 +++++- include/rfl/flexbuf/Writer.hpp | 8 ++++ include/rfl/msgpack/Reader.hpp | 15 ++++++- include/rfl/msgpack/Writer.hpp | 12 ++++-- tests/bson/test_bytestring.cpp | 23 +++++++++++ tests/cbor/test_bytestring.cpp | 21 ++++++++++ tests/flexbuffers/test_bytestring.cpp | 23 +++++++++++ tests/msgpack/test_bytestring.cpp | 21 ++++++++++ 18 files changed, 243 insertions(+), 20 deletions(-) create mode 100644 docs/bytestring.md create mode 100644 include/rfl/Bytestring.hpp create mode 100644 tests/bson/test_bytestring.cpp create mode 100644 tests/cbor/test_bytestring.cpp create mode 100644 tests/flexbuffers/test_bytestring.cpp create mode 100644 tests/msgpack/test_bytestring.cpp diff --git a/README.md b/README.md index 93af1ce2..a738ea9e 100644 --- a/README.md +++ b/README.md @@ -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`. 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. diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index ae37617f..838fde93 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -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() diff --git a/docs/README.md b/docs/README.md index 5d47460b..1f03f77e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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. diff --git a/docs/bytestring.md b/docs/bytestring.md new file mode 100644 index 00000000..92ad231e --- /dev/null +++ b/docs/bytestring.md @@ -0,0 +1,19 @@ +# `rfl::Bytestring` + +`rfl::Bytestring` is a simple alias: + +```cpp +namespace rfl { + +using Bytestring = std::basic_string; + +} +``` + +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. diff --git a/include/rfl.hpp b/include/rfl.hpp index 9dc36c7b..c2897dae 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -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" diff --git a/include/rfl/Bytestring.hpp b/include/rfl/Bytestring.hpp new file mode 100644 index 00000000..896be499 --- /dev/null +++ b/include/rfl/Bytestring.hpp @@ -0,0 +1,13 @@ +#ifndef RFL_BYTESTRING_HPP_ +#define RFL_BYTESTRING_HPP_ + +#include +#include + +namespace rfl { + +using Bytestring = std::basic_string; + +} // namespace rfl + +#endif diff --git a/include/rfl/bson/Reader.hpp b/include/rfl/bson/Reader.hpp index 0203300c..0cff0554 100644 --- a/include/rfl/bson/Reader.hpp +++ b/include/rfl/bson/Reader.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include #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, + 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(value.v_binary.data), + value.v_binary.data_len); } else if constexpr (std::is_same, bool>()) { if (btype != BSON_TYPE_BOOL) { return rfl::Error("Could not cast to boolean."); diff --git a/include/rfl/bson/Writer.hpp b/include/rfl/bson/Writer.hpp index aac627a1..80a7d730 100644 --- a/include/rfl/bson/Writer.hpp +++ b/include/rfl/bson/Writer.hpp @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #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::string>()) { bson_array_builder_append_utf8(_parent->val_, _var.c_str(), static_cast(_var.size())); + } else if constexpr (std::is_same, + rfl::Bytestring>()) { + bson_array_builder_append_binary( + _parent->val_, BSON_SUBTYPE_BINARY, + reinterpret_cast(_var.c_str()), + static_cast(_var.size())); } else if constexpr (std::is_same, bool>()) { bson_array_builder_append_bool(_parent->val_, _var); } else if constexpr (std::is_floating_point>()) { @@ -149,6 +157,12 @@ class Writer { bson_append_utf8(_parent->val_, _name.data(), static_cast(_name.size()), _var.c_str(), static_cast(_var.size())); + } else if constexpr (std::is_same, + rfl::Bytestring>()) { + bson_append_binary(_parent->val_, _name.data(), + static_cast(_name.size()), BSON_SUBTYPE_BINARY, + reinterpret_cast(_var.c_str()), + static_cast(_var.size())); } else if constexpr (std::is_same, bool>()) { bson_append_bool(_parent->val_, _name.data(), static_cast(_name.size()), _var); diff --git a/include/rfl/cbor/Reader.hpp b/include/rfl/cbor/Reader.hpp index ffdc365b..5b6ebddc 100644 --- a/include/rfl/cbor/Reader.hpp +++ b/include/rfl/cbor/Reader.hpp @@ -3,12 +3,14 @@ #include +#include #include #include #include #include #include +#include "../Bytestring.hpp" #include "../Result.hpp" #include "../always_false.hpp" @@ -65,7 +67,7 @@ struct Reader { rfl::Result get_field_from_object( const std::string& _name, const InputObjectType& _obj) const noexcept { InputVarType var; - auto buffer = std::vector(); + 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 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, + 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, 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(); + 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* _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(&((*_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; + } } }; diff --git a/include/rfl/cbor/Writer.hpp b/include/rfl/cbor/Writer.hpp index d2cb7d16..87f06bb6 100644 --- a/include/rfl/cbor/Writer.hpp +++ b/include/rfl/cbor/Writer.hpp @@ -14,6 +14,7 @@ #include #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::string>()) { cbor_encode_text_string(_parent, _var.c_str(), _var.size()); + } else if constexpr (std::is_same, + rfl::Bytestring>()) { + cbor_encode_byte_string( + _parent, reinterpret_cast(_var.c_str()), _var.size()); } else if constexpr (std::is_same, bool>()) { cbor_encode_boolean(_parent, _var); } else if constexpr (std::is_floating_point>()) { diff --git a/include/rfl/flexbuf/Reader.hpp b/include/rfl/flexbuf/Reader.hpp index 8c601403..a1f8cfe5 100644 --- a/include/rfl/flexbuf/Reader.hpp +++ b/include/rfl/flexbuf/Reader.hpp @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -12,6 +13,7 @@ #include #include +#include "../Bytestring.hpp" #include "../Result.hpp" #include "../always_false.hpp" @@ -69,9 +71,17 @@ struct Reader { rfl::Result to_basic_type(const InputVarType& _var) const noexcept { if constexpr (std::is_same, 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, + rfl::Bytestring>()) { + if (!_var.IsBlob()) { + return rfl::Error("Could not cast to a bytestring."); + } + const auto blob = _var.AsBlob(); + return rfl::Bytestring(reinterpret_cast(blob.data()), + blob.size()); } else if constexpr (std::is_same, bool>()) { if (!_var.IsBool()) { return rfl::Error("Could not cast to boolean."); diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp index 3659a6e9..16e66725 100644 --- a/include/rfl/flexbuf/Writer.hpp +++ b/include/rfl/flexbuf/Writer.hpp @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #include +#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::string>()) { fbb_->String(_name.data(), _var); + } else if constexpr (std::is_same, + rfl::Bytestring>()) { + fbb_->Blob(_name.data(), _var.c_str(), _var.size()); } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_name.data(), _var); } else if constexpr (std::is_floating_point>()) { @@ -134,6 +139,9 @@ struct Writer { OutputVarType insert_value(const T& _var) const noexcept { if constexpr (std::is_same, std::string>()) { fbb_->String(_var); + } else if constexpr (std::is_same, + rfl::Bytestring>()) { + fbb_->Blob(_var.c_str(), _var.size()); } else if constexpr (std::is_same, bool>()) { fbb_->Bool(_var); } else if constexpr (std::is_floating_point>()) { diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp index 69f50c50..1c670449 100644 --- a/include/rfl/msgpack/Reader.hpp +++ b/include/rfl/msgpack/Reader.hpp @@ -3,11 +3,13 @@ #include +#include #include #include #include #include +#include "../Bytestring.hpp" #include "../Result.hpp" #include "../always_false.hpp" @@ -20,8 +22,9 @@ struct Reader { using InputVarType = msgpack_object; template - 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 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, + 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(bin.ptr), + bin.size); } else if constexpr (std::is_same, bool>()) { if (type != MSGPACK_OBJECT_BOOLEAN) { return Error("Could not cast to boolean."); diff --git a/include/rfl/msgpack/Writer.hpp b/include/rfl/msgpack/Writer.hpp index 0ee2796d..8cdfa7b2 100644 --- a/include/rfl/msgpack/Writer.hpp +++ b/include/rfl/msgpack/Writer.hpp @@ -125,18 +125,22 @@ class Writer { template OutputVarType new_value(const T& _var) const noexcept { - if constexpr (std::is_same, std::string>()) { + using Type = std::remove_cvref_t; + if constexpr (std::is_same()) { msgpack_pack_str(pk_, _var.size()); msgpack_pack_str_body(pk_, _var.c_str(), _var.size()); - } else if constexpr (std::is_same, bool>()) { + } else if constexpr (std::is_same()) { + msgpack_pack_bin(pk_, _var.size()); + msgpack_pack_bin_body(pk_, _var.c_str(), _var.size()); + } else if constexpr (std::is_same()) { if (_var) { msgpack_pack_true(pk_); } else { msgpack_pack_false(pk_); } - } else if constexpr (std::is_floating_point>()) { + } else if constexpr (std::is_floating_point()) { msgpack_pack_double(pk_, static_cast(_var)); - } else if constexpr (std::is_integral>()) { + } else if constexpr (std::is_integral()) { msgpack_pack_int64(pk_, static_cast(_var)); } else { static_assert(rfl::always_false_v, "Unsupported type."); diff --git a/tests/bson/test_bytestring.cpp b/tests/bson/test_bytestring.cpp new file mode 100644 index 00000000..c7ca18cf --- /dev/null +++ b/tests/bson/test_bytestring.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_bytestring { + +struct TestStruct { + rfl::Bytestring bytestring; + std::vector 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({bstr, bstr, bstr})}; + write_and_read(test); +} +} // namespace test_bytestring diff --git a/tests/cbor/test_bytestring.cpp b/tests/cbor/test_bytestring.cpp new file mode 100644 index 00000000..e433cc69 --- /dev/null +++ b/tests/cbor/test_bytestring.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +#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 diff --git a/tests/flexbuffers/test_bytestring.cpp b/tests/flexbuffers/test_bytestring.cpp new file mode 100644 index 00000000..5220ee7b --- /dev/null +++ b/tests/flexbuffers/test_bytestring.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_bytestring { + +struct TestStruct { + rfl::Bytestring bytestring; + std::vector 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({bstr, bstr, bstr})}; + write_and_read(test); +} +} // namespace test_bytestring diff --git a/tests/msgpack/test_bytestring.cpp b/tests/msgpack/test_bytestring.cpp new file mode 100644 index 00000000..33d7ec17 --- /dev/null +++ b/tests/msgpack/test_bytestring.cpp @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +#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