From 5efa9df16e7561d338d56a44fba841c96230463d Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sat, 11 Nov 2023 23:16:49 +0100 Subject: [PATCH] Added flexbuffers reader and writer as well as first test (but does not pass) --- include/rfl/Box.hpp | 13 +- include/rfl/Ref.hpp | 19 +- include/rfl/flexbuf.hpp | 7 + include/rfl/flexbuf/OutputArray.hpp | 59 ++++++ include/rfl/flexbuf/OutputNull.hpp | 38 ++++ include/rfl/flexbuf/OutputObject.hpp | 61 +++++++ include/rfl/flexbuf/OutputValue.hpp | 67 +++++++ include/rfl/flexbuf/OutputVar.hpp | 25 +++ include/rfl/flexbuf/Parser.hpp | 17 ++ include/rfl/flexbuf/Reader.hpp | 169 ++++++++++++++++++ include/rfl/flexbuf/Writer.hpp | 69 +++++++ include/rfl/flexbuf/read.hpp | 34 ++++ include/rfl/flexbuf/write.hpp | 31 ++++ tests/flexbuffers/CMakeLists.txt | 23 +++ tests/flexbuffers/Dockerfile | 25 +++ tests/flexbuffers/build_docker.sh | 3 + tests/flexbuffers/compile.sh | 3 + tests/flexbuffers/compile_on_docker.sh | 5 + tests/flexbuffers/test_readme_example.hpp | 54 ++++++ tests/flexbuffers/tests.cpp | 7 + tests/flexbuffers/write_and_read.hpp | 39 ++++ tests/{ => json}/CMakeLists.txt | 4 +- tests/{ => json}/test_all_of.hpp | 0 tests/{ => json}/test_anonymous_fields.hpp | 0 tests/{ => json}/test_any_of.hpp | 0 tests/{ => json}/test_array.hpp | 0 tests/{ => json}/test_as.hpp | 0 tests/{ => json}/test_as_flatten.hpp | 0 tests/{ => json}/test_box.hpp | 0 tests/{ => json}/test_custom_class1.hpp | 0 tests/{ => json}/test_custom_class2.hpp | 0 tests/{ => json}/test_custom_class3.hpp | 0 tests/{ => json}/test_custom_class4.hpp | 0 tests/{ => json}/test_custom_constructor.hpp | 0 tests/{ => json}/test_default_values.hpp | 0 tests/{ => json}/test_deque.hpp | 0 tests/{ => json}/test_email.hpp | 0 tests/{ => json}/test_error_messages.hpp | 0 tests/{ => json}/test_field_variant.hpp | 0 tests/{ => json}/test_flatten.hpp | 0 tests/{ => json}/test_forward_list.hpp | 0 tests/{ => json}/test_list.hpp | 0 tests/{ => json}/test_literal.hpp | 0 tests/{ => json}/test_map.hpp | 0 tests/{ => json}/test_move_replace.hpp | 0 tests/{ => json}/test_multimap.hpp | 0 tests/{ => json}/test_multiset.hpp | 0 tests/{ => json}/test_one_of.hpp | 0 tests/{ => json}/test_optional_fields.hpp | 0 tests/{ => json}/test_readme_example.hpp | 0 tests/{ => json}/test_ref.hpp | 0 tests/{ => json}/test_replace.hpp | 0 tests/{ => json}/test_replace_flatten.hpp | 0 .../test_replace_with_other_struct.hpp | 0 tests/{ => json}/test_result.hpp | 0 tests/{ => json}/test_set.hpp | 0 tests/{ => json}/test_size.hpp | 0 tests/{ => json}/test_string_map.hpp | 0 .../{ => json}/test_string_unordered_map.hpp | 0 tests/{ => json}/test_tagged_union.hpp | 0 tests/{ => json}/test_timestamp.hpp | 0 tests/{ => json}/test_unique_ptr.hpp | 0 tests/{ => json}/test_unique_ptr2.hpp | 0 tests/{ => json}/test_unordered_map.hpp | 0 tests/{ => json}/test_unordered_multimap.hpp | 0 tests/{ => json}/test_unordered_multiset.hpp | 0 tests/{ => json}/test_unordered_set.hpp | 0 tests/{ => json}/test_variant.hpp | 0 tests/{ => json}/tests.cpp | 0 tests/{ => json}/write_and_read.hpp | 0 70 files changed, 754 insertions(+), 18 deletions(-) create mode 100644 include/rfl/flexbuf.hpp create mode 100644 include/rfl/flexbuf/OutputArray.hpp create mode 100644 include/rfl/flexbuf/OutputNull.hpp create mode 100644 include/rfl/flexbuf/OutputObject.hpp create mode 100644 include/rfl/flexbuf/OutputValue.hpp create mode 100644 include/rfl/flexbuf/OutputVar.hpp create mode 100644 include/rfl/flexbuf/Parser.hpp create mode 100644 include/rfl/flexbuf/Reader.hpp create mode 100644 include/rfl/flexbuf/Writer.hpp create mode 100644 include/rfl/flexbuf/read.hpp create mode 100644 include/rfl/flexbuf/write.hpp create mode 100644 tests/flexbuffers/CMakeLists.txt create mode 100644 tests/flexbuffers/Dockerfile create mode 100755 tests/flexbuffers/build_docker.sh create mode 100755 tests/flexbuffers/compile.sh create mode 100644 tests/flexbuffers/compile_on_docker.sh create mode 100644 tests/flexbuffers/test_readme_example.hpp create mode 100644 tests/flexbuffers/tests.cpp create mode 100644 tests/flexbuffers/write_and_read.hpp rename tests/{ => json}/CMakeLists.txt (89%) rename tests/{ => json}/test_all_of.hpp (100%) rename tests/{ => json}/test_anonymous_fields.hpp (100%) rename tests/{ => json}/test_any_of.hpp (100%) rename tests/{ => json}/test_array.hpp (100%) rename tests/{ => json}/test_as.hpp (100%) rename tests/{ => json}/test_as_flatten.hpp (100%) rename tests/{ => json}/test_box.hpp (100%) rename tests/{ => json}/test_custom_class1.hpp (100%) rename tests/{ => json}/test_custom_class2.hpp (100%) rename tests/{ => json}/test_custom_class3.hpp (100%) rename tests/{ => json}/test_custom_class4.hpp (100%) rename tests/{ => json}/test_custom_constructor.hpp (100%) rename tests/{ => json}/test_default_values.hpp (100%) rename tests/{ => json}/test_deque.hpp (100%) rename tests/{ => json}/test_email.hpp (100%) rename tests/{ => json}/test_error_messages.hpp (100%) rename tests/{ => json}/test_field_variant.hpp (100%) rename tests/{ => json}/test_flatten.hpp (100%) rename tests/{ => json}/test_forward_list.hpp (100%) rename tests/{ => json}/test_list.hpp (100%) rename tests/{ => json}/test_literal.hpp (100%) rename tests/{ => json}/test_map.hpp (100%) rename tests/{ => json}/test_move_replace.hpp (100%) rename tests/{ => json}/test_multimap.hpp (100%) rename tests/{ => json}/test_multiset.hpp (100%) rename tests/{ => json}/test_one_of.hpp (100%) rename tests/{ => json}/test_optional_fields.hpp (100%) rename tests/{ => json}/test_readme_example.hpp (100%) rename tests/{ => json}/test_ref.hpp (100%) rename tests/{ => json}/test_replace.hpp (100%) rename tests/{ => json}/test_replace_flatten.hpp (100%) rename tests/{ => json}/test_replace_with_other_struct.hpp (100%) rename tests/{ => json}/test_result.hpp (100%) rename tests/{ => json}/test_set.hpp (100%) rename tests/{ => json}/test_size.hpp (100%) rename tests/{ => json}/test_string_map.hpp (100%) rename tests/{ => json}/test_string_unordered_map.hpp (100%) rename tests/{ => json}/test_tagged_union.hpp (100%) rename tests/{ => json}/test_timestamp.hpp (100%) rename tests/{ => json}/test_unique_ptr.hpp (100%) rename tests/{ => json}/test_unique_ptr2.hpp (100%) rename tests/{ => json}/test_unordered_map.hpp (100%) rename tests/{ => json}/test_unordered_multimap.hpp (100%) rename tests/{ => json}/test_unordered_multiset.hpp (100%) rename tests/{ => json}/test_unordered_set.hpp (100%) rename tests/{ => json}/test_variant.hpp (100%) rename tests/{ => json}/tests.cpp (100%) rename tests/{ => json}/write_and_read.hpp (100%) diff --git a/include/rfl/Box.hpp b/include/rfl/Box.hpp index 372b1f28..c6eb806d 100644 --- a/include/rfl/Box.hpp +++ b/include/rfl/Box.hpp @@ -26,10 +26,9 @@ class Box { Box(Box&& _other) = default; - template , - bool>::type = true> + template Box(Box&& _other) noexcept - : ptr_(std::make_unique(std::forward(*_other))) {} + : ptr_(std::forward>(_other.ptr())) {} ~Box() = default; @@ -43,10 +42,9 @@ class Box { Box& operator=(Box&& _other) noexcept = default; /// Move assignment operator - template , - bool>::type = true> + template Box& operator=(Box&& _other) noexcept { - ptr_ = std::make_unique(std::forward(*_other)); + ptr_ = std::forward>(_other.ptr()); return *this; } @@ -62,6 +60,9 @@ class Box { /// Returns the underlying object. T* operator->() const { return ptr_.get(); } + /// Returns the underlying unique_ptr + std::unique_ptr& ptr() { return ptr_; } + /// Returns the underlying unique_ptr const std::unique_ptr& ptr() const { return ptr_; } diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index 6bda2e02..e65217a7 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -26,14 +26,12 @@ class Ref { Ref(Ref&& _other) = default; - template , - bool>::type = true> + template Ref(const Ref& _other) : ptr_(_other.ptr()) {} - template , - bool>::type = true> + template Ref(Ref&& _other) noexcept - : ptr_(std::make_shared(std::forward(*_other))) {} + : ptr_(std::forward>(_other.ptr())) {} ~Ref() = default; @@ -52,22 +50,23 @@ class Ref { /// Returns the underlying object. T* operator->() const { return ptr_.get(); } + /// Returns the underlying shared_ptr + std::shared_ptr& ptr() { return ptr_; } + /// Returns the underlying shared_ptr const std::shared_ptr& ptr() const { return ptr_; } /// Copy assignment operator. - template , - bool>::type = true> + template Ref& operator=(const Ref& _other) { ptr_ = _other.ptr(); return *this; } /// Move assignment operator - template , - bool>::type = true> + template Ref& operator=(Ref&& _other) noexcept { - ptr_ = std::make_shared(std::forward(*_other)); + ptr_ = std::forward>(_other.ptr()); return *this; } diff --git a/include/rfl/flexbuf.hpp b/include/rfl/flexbuf.hpp new file mode 100644 index 00000000..7187eb2f --- /dev/null +++ b/include/rfl/flexbuf.hpp @@ -0,0 +1,7 @@ +#ifndef FLEXBUF_FLEXBUF_HPP_ +#define FLEXBUF_FLEXBUF_HPP_ + +#include "rfl/flexbuf/read.hpp" +#include "rfl/flexbuf/write.hpp" + +#endif diff --git a/include/rfl/flexbuf/OutputArray.hpp b/include/rfl/flexbuf/OutputArray.hpp new file mode 100644 index 00000000..617de58d --- /dev/null +++ b/include/rfl/flexbuf/OutputArray.hpp @@ -0,0 +1,59 @@ +#ifndef FLEXBUF_OUTPUTARRAY_HPP_ +#define FLEXBUF_OUTPUTARRAY_HPP_ + +#include + +#include +#include + +#include "rfl/Ref.hpp" +#include "rfl/flexbuf/OutputVar.hpp" + +namespace rfl { +namespace flexbuf { + +class OutputArray : public OutputVar { + public: + OutputArray() {} + + ~OutputArray() = default; + + /// Inserts all elements into the builder. + void insert(const std::optional& _key, + flexbuffers::Builder* _fbb) final { + /// We have to catch the edge case of an empty vector, + /// because flexbuf ignores empty vectors/maps. + if (vars_.size() == 0) { + if (_key) { + _fbb->Null(_key->c_str()); + } else { + _fbb->Null(); + } + return; + } + + const auto start = + _key ? _fbb->StartVector(_key->c_str()) : _fbb->StartVector(); + + for (const auto& var : vars_) { + var->insert(std::nullopt, _fbb); + } + + _fbb->EndVector(start, false, false); + }; + + /// Whether this is null. + bool is_null() const final { return false; } + + /// Adds a new element to the vector. + void push_back(const Ref& _var) { vars_.push_back(_var); } + + private: + /// The underlying variables. + std::vector> vars_; +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/OutputNull.hpp b/include/rfl/flexbuf/OutputNull.hpp new file mode 100644 index 00000000..c81b9ade --- /dev/null +++ b/include/rfl/flexbuf/OutputNull.hpp @@ -0,0 +1,38 @@ +#ifndef FLEXBUF_OUTPUTNULL_HPP_ +#define FLEXBUF_OUTPUTNULL_HPP_ + +#include + +#include +#include + +#include "rfl/Ref.hpp" +#include "rfl/flexbuf/OutputVar.hpp" + +namespace rfl { +namespace flexbuf { + +class OutputNull : public OutputVar { + public: + OutputNull() {} + + ~OutputNull() = default; + + /// Inserts all elements into the builder. + void insert(const std::optional& _key, + flexbuffers::Builder* _fbb) final { + if (_key) { + _fbb->Null(_key->c_str()); + } else { + _fbb->Null(); + } + } + + /// Whether this is null. + bool is_null() const final { return true; } +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/OutputObject.hpp b/include/rfl/flexbuf/OutputObject.hpp new file mode 100644 index 00000000..54c7c5ff --- /dev/null +++ b/include/rfl/flexbuf/OutputObject.hpp @@ -0,0 +1,61 @@ +#ifndef FLEXBUF_OUTPUTOBJECT_HPP_ +#define FLEXBUF_OUTPUTOBJECT_HPP_ + +#include + +#include +#include +#include + +#include "rfl/Ref.hpp" +#include "rfl/flexbuf/OutputVar.hpp" + +namespace rfl { +namespace flexbuf { + +class OutputObject : public OutputVar { + public: + OutputObject() {} + + ~OutputObject() = default; + + /// Inserts all elements into the builder. + void insert(const std::optional& _key, + flexbuffers::Builder* _fbb) final { + /// We have to catch the edge case of an empty map, + /// because flexbuf ignores empty vectors/maps. + if (vars_.size() == 0) { + if (_key) { + _fbb->Null(_key->c_str()); + } else { + _fbb->Null(); + } + return; + } + + const auto start = _key ? _fbb->StartMap(_key->c_str()) : _fbb->StartMap(); + + for (const auto& [name, elem] : vars_) { + elem->insert(name, _fbb); + } + + _fbb->EndMap(start); + }; + + /// Whether this is null. + bool is_null() const final { return false; } + + /// Adds a new element to the vector. + void push_back(const std::string& _name, const Ref& _var) { + vars_.push_back(std::make_pair(_name, _var)); + } + + private: + /// The underlying variables. + std::vector>> vars_; +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/OutputValue.hpp b/include/rfl/flexbuf/OutputValue.hpp new file mode 100644 index 00000000..7aa476bb --- /dev/null +++ b/include/rfl/flexbuf/OutputValue.hpp @@ -0,0 +1,67 @@ +#ifndef FLEXBUF_OUTPUTVALUE_HPP_ +#define FLEXBUF_OUTPUTVALUE_HPP_ + +#include + +#include +#include +#include + +#include "rfl/Ref.hpp" +#include "rfl/always_false.hpp" +#include "rfl/flexbuf/OutputVar.hpp" + +namespace rfl { +namespace flexbuf { + +template +class OutputValue : public OutputVar { + public: + OutputValue(T _val) : val_(_val) {} + + ~OutputValue() = default; + + /// Inserts all elements into the builder. + void insert(const std::optional& _key, + flexbuffers::Builder* _fbb) final { + if constexpr (std::is_same, std::string>()) { + if (_key) { + _fbb->String(_key->c_str(), val_); + } else { + _fbb->String(val_); + } + } else if constexpr (std::is_same, bool>()) { + if (_key) { + _fbb->Bool(_key->c_str(), val_); + } else { + _fbb->Bool(val_); + } + } else if constexpr (std::is_floating_point>()) { + if (_key) { + _fbb->Double(_key->c_str(), val_); + } else { + _fbb->Double(val_); + } + } else if constexpr (std::is_integral>()) { + if (_key) { + _fbb->Int(_key->c_str(), val_); + } else { + _fbb->Int(val_); + } + } else { + static_assert(always_false_v, "Unsupported type"); + } + }; + + /// Whether this is null. + bool is_null() const final { return false; } + + private: + /// The underlying value. + T val_; +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/OutputVar.hpp b/include/rfl/flexbuf/OutputVar.hpp new file mode 100644 index 00000000..9ec6d133 --- /dev/null +++ b/include/rfl/flexbuf/OutputVar.hpp @@ -0,0 +1,25 @@ +#ifndef FLEXBUF_OUTPUTVAR_HPP_ +#define FLEXBUF_OUTPUTVAR_HPP_ + +#include + +#include +#include + +namespace rfl { +namespace flexbuf { + +class OutputVar { + public: + /// Inserts the value into the buffer. + virtual void insert(const std::optional& _key, + flexbuffers::Builder* _fbb) = 0; + + /// Whether this is null. + virtual bool is_null() const = 0; +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/Parser.hpp b/include/rfl/flexbuf/Parser.hpp new file mode 100644 index 00000000..cd74a78b --- /dev/null +++ b/include/rfl/flexbuf/Parser.hpp @@ -0,0 +1,17 @@ +#ifndef FLEXBUF_PARSER_HPP_ +#define FLEXBUF_PARSER_HPP_ + +#include "rfl/flexbuf/Reader.hpp" +#include "rfl/flexbuf/Writer.hpp" +#include "rfl/parsing/Parser.hpp" + +namespace rfl { +namespace flexbuf { + +template +using Parser = rfl::parsing::Parser; + +} +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/Reader.hpp b/include/rfl/flexbuf/Reader.hpp new file mode 100644 index 00000000..373b3aba --- /dev/null +++ b/include/rfl/flexbuf/Reader.hpp @@ -0,0 +1,169 @@ +#ifndef FLEXBUF_READER_HPP_ +#define FLEXBUF_READER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rfl/Result.hpp" +#include "rfl/always_false.hpp" + +namespace rfl { +namespace flexbuf { + +struct Reader { + using InputArrayType = flexbuffers::Vector; + using InputObjectType = flexbuffers::Map; + using InputVarType = flexbuffers::Reference; + + template + struct has_from_flexbuf : std::false_type {}; + + template + struct has_from_flexbuf< + T, std::enable_if_t::value>> + : std::true_type {}; + + template + struct has_from_flexbuf< + T, std::enable_if_t, decltype(T::from_flexbuf), InputVarType>::value>> + : std::true_type {}; + + template + static constexpr bool has_custom_constructor = has_from_flexbuf::value; + + rfl::Result get_field( + const std::string& _name, const InputObjectType& _obj) const noexcept { + const auto keys = _obj.Keys(); + for (size_t i = 0; i < keys.size(); ++i) { + if (_name == keys[i].AsString().c_str()) { + return _obj.Values()[i]; + } + } + return rfl::Error("Map does not contain any element called '" + _name + + "'."); + } + + bool is_empty(const InputVarType& _var) const noexcept { + return _var.IsNull(); + } + + template + 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 std::string(_var.AsString().c_str()); + } else if constexpr (std::is_same, bool>()) { + if (!_var.IsBool()) { + return rfl::Error("Could not cast to boolean."); + } + return _var.AsBool(); + } else if constexpr (std::is_floating_point>()) { + if (!_var.IsNumeric()) { + return rfl::Error("Could not cast to double."); + } + return static_cast(_var.AsDouble()); + } else if constexpr (std::is_integral>()) { + if (!_var.IsNumeric()) { + return rfl::Error("Could not cast to int."); + } + return static_cast(_var.AsInt64()); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + } + + template + std::array to_fields_array( + const FunctionType& _fct, const InputObjectType& _obj) const noexcept { + std::array f_arr; + + const auto keys = _obj.Keys(); + const auto values = _obj.Values(); + const auto num_values = std::min(keys.size(), values.size()); + + for (size_t i = 0; i < num_values; ++i) { + const auto ix = _fct(std::string_view(keys[i].AsString().c_str())); + if (ix != -1) { + f_arr[ix] = values[i]; + } + } + + return f_arr; + } + + rfl::Result to_array( + const InputVarType& _var) const noexcept { + // Necessary, because we write empty vectors as null. + if (_var.IsNull()) { + return flexbuffers::Vector::EmptyVector(); + } + if (!_var.IsVector()) { + return rfl::Error("Could not cast to Vector."); + } + return _var.AsVector(); + } + + std::vector> to_map( + const InputObjectType& _obj) const noexcept { + std::vector> m; + + const auto keys = _obj.Keys(); + const auto values = _obj.Values(); + const auto size = std::min(keys.size(), values.size()); + + for (size_t i = 0; i < size; ++i) { + m.emplace_back( + std::make_pair(std::string(keys[i].AsString().c_str()), values[i])); + } + + return m; + } + + rfl::Result to_object( + const InputVarType& _var) const noexcept { + // Necessary, because we write empty maps as null. + if (_var.IsNull()) { + return flexbuffers::Map::EmptyMap(); + } + if (!_var.IsMap()) { + return rfl::Error("Could not cast to Map!"); + } + return _var.AsMap(); + } + + std::vector to_vec(const InputArrayType& _arr) const noexcept { + const auto size = _arr.size(); + std::vector vec; + for (size_t i = 0; i < size; ++i) { + vec.push_back(_arr[i]); + } + return vec; + } + + template + rfl::Result use_custom_constructor( + const InputVarType& _var) const noexcept { + try { + return T::from_flexbuf(_var); + } catch (std::exception& e) { + return rfl::Error(e.what()); + } + } +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp new file mode 100644 index 00000000..d6bc430f --- /dev/null +++ b/include/rfl/flexbuf/Writer.hpp @@ -0,0 +1,69 @@ +#ifndef FLEXBUF_WRITER_HPP_ +#define FLEXBUF_WRITER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rfl/Ref.hpp" +#include "rfl/Result.hpp" +#include "rfl/flexbuf/OutputArray.hpp" +#include "rfl/flexbuf/OutputNull.hpp" +#include "rfl/flexbuf/OutputObject.hpp" +#include "rfl/flexbuf/OutputValue.hpp" +#include "rfl/flexbuf/OutputVar.hpp" + +namespace rfl { +namespace flexbuf { + +struct Writer { + using OutputArrayType = Ref; + using OutputObjectType = Ref; + using OutputVarType = Ref; + + Writer() {} + + ~Writer() = default; + + void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept { + (*_arr)->push_back(_var); + } + + OutputVarType empty_var() const noexcept { return Ref::make(); } + + template + OutputVarType from_basic_type(const T _var) const noexcept { + return Ref>::make(_var); + } + + OutputArrayType new_array() const noexcept { + return Ref::make(); + } + + OutputObjectType new_object() const noexcept { + return Ref::make(); + } + + bool is_empty(const OutputVarType& _var) const noexcept { + return _var->is_null(); + } + + void set_field(const std::string& _name, const OutputVarType& _var, + OutputObjectType* _obj) const noexcept { + (*_obj)->push_back(_name, _var); + } +}; + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/read.hpp b/include/rfl/flexbuf/read.hpp new file mode 100644 index 00000000..da8c99b6 --- /dev/null +++ b/include/rfl/flexbuf/read.hpp @@ -0,0 +1,34 @@ +#ifndef FLEXBUF_READ_HPP_ +#define FLEXBUF_READ_HPP_ + +#include + +#include + +#include "rfl/Result.hpp" +#include "rfl/flexbuf/Parser.hpp" + +namespace rfl { +namespace flexbuf { + +using InputVarType = typename Reader::InputVarType; + +/// Parses an object from flexbuf using reflection. +template +Result read(const unsigned char* _bytes, const size_t _size) { + const InputVarType root = + flexbuffers::GetRoot(reinterpret_cast(_bytes), _size); + const auto r = Reader(); + return Parser::read(r, root); +} + +/// Parses an object from flexbuf using reflection. +template +Result read(const std::vector& _bytes) { + return read(_bytes.data(), _bytes.size()); +} + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/write.hpp b/include/rfl/flexbuf/write.hpp new file mode 100644 index 00000000..e23f9244 --- /dev/null +++ b/include/rfl/flexbuf/write.hpp @@ -0,0 +1,31 @@ +#ifndef FLEXBUF_WRITE_HPP_ +#define FLEXBUF_WRITE_HPP_ + +#include + +#include +#include +#include + +#include "rfl/flexbuf/Parser.hpp" + +namespace rfl { +namespace flexbuf { + +/// Writes an object to flexbuf. +template +std::vector write(const T& _obj) { + auto w = Writer(); + const auto flexbuf_obj = Parser::write(w, _obj); + flexbuffers::Builder fbb; + flexbuf_obj->insert(std::nullopt, &fbb); + fbb.Finish(); + const auto vec = fbb.GetBuffer(); + const auto data = reinterpret_cast(vec.data()); + return std::vector(data, data + vec.size()); +} + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/tests/flexbuffers/CMakeLists.txt b/tests/flexbuffers/CMakeLists.txt new file mode 100644 index 00000000..6c65cd8a --- /dev/null +++ b/tests/flexbuffers/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.4) + +project(reflect-cpp-tests) + +include_directories( + "../../include" + "../../../flatbuffers/include" +) + +add_executable( + reflect-cpp-tests + "tests.cpp" + "../../src/yyjson.c" +) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0") + +target_link_libraries( + reflect-cpp-tests + /home/reflectcpp/flatbuffers/build/libflatbuffers.a +) diff --git a/tests/flexbuffers/Dockerfile b/tests/flexbuffers/Dockerfile new file mode 100644 index 00000000..97f250af --- /dev/null +++ b/tests/flexbuffers/Dockerfile @@ -0,0 +1,25 @@ +FROM ubuntu:22.04 + +ENV TZ=Europe/Berlin +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +RUN useradd reflectcpp + +RUN apt-get update +RUN apt-get install -y git cmake +RUN apt install gcc-11 g++-11 + +WORKDIR /home/reflectcpp + +RUN git clone https://github.com/google/flatbuffers.git +WORKDIR /home/reflectcpp/flatbuffers +RUN git checkout v23.5.26 +RUN mkdir build +WORKDIR /home/reflectcpp/flatbuffers/build +RUN cmake .. -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 +RUN make + +WORKDIR /home/reflectcpp +COPY compile_on_docker.sh . + +USER reflectcpp diff --git a/tests/flexbuffers/build_docker.sh b/tests/flexbuffers/build_docker.sh new file mode 100755 index 00000000..3db3c555 --- /dev/null +++ b/tests/flexbuffers/build_docker.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker build --tag=reflectcpp_test_flexbuffers . diff --git a/tests/flexbuffers/compile.sh b/tests/flexbuffers/compile.sh new file mode 100755 index 00000000..e182e01f --- /dev/null +++ b/tests/flexbuffers/compile.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +docker run -v $PWD/../../:"/home/reflectcpp/reflect-cpp" --rm -it -t reflectcpp_test_flexbuffers bash compile_on_docker.sh diff --git a/tests/flexbuffers/compile_on_docker.sh b/tests/flexbuffers/compile_on_docker.sh new file mode 100644 index 00000000..6a6277af --- /dev/null +++ b/tests/flexbuffers/compile_on_docker.sh @@ -0,0 +1,5 @@ +cd reflect-cpp/tests/flexbuffers +mkdir build +cd build +cmake .. -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 +make diff --git a/tests/flexbuffers/test_readme_example.hpp b/tests/flexbuffers/test_readme_example.hpp new file mode 100644 index 00000000..df31a5a8 --- /dev/null +++ b/tests/flexbuffers/test_readme_example.hpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_readme_example() { + std::cout << "test_readme_example" << std::endl; + + using Age = rfl::Validator, rfl::Maximum<130>>>; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + rfl::Field<"age", Age> age; + rfl::Field<"email", rfl::Email> email; + rfl::Field<"children", std::vector> children; + }; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{ + .first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com", + .children = rfl::default_value // same as std::vector() + }; + + const auto maggie = + rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), + rfl::make_field<"email">(std::string("maggie@simpson.com")), + rfl::make_field<"age">(0)); + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} diff --git a/tests/flexbuffers/tests.cpp b/tests/flexbuffers/tests.cpp new file mode 100644 index 00000000..fa4a3115 --- /dev/null +++ b/tests/flexbuffers/tests.cpp @@ -0,0 +1,7 @@ +#include "test_readme_example.hpp" + +int main() { + test_readme_example(); + + return 0; +} diff --git a/tests/flexbuffers/write_and_read.hpp b/tests/flexbuffers/write_and_read.hpp new file mode 100644 index 00000000..8bf426f3 --- /dev/null +++ b/tests/flexbuffers/write_and_read.hpp @@ -0,0 +1,39 @@ +#ifndef WRITE_AND_READ_ +#define WRITE_AND_READ_ + +#include +#include +#include + +template +void write_and_read(const T& _struct) { + const auto bytes1 = rfl::flexbuf::write(_struct); + + const auto res = rfl::flexbuf::read(bytes1); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl + << std::endl; + return; + } + + const auto bytes2 = rfl::flexbuf::write(res.value()); + + if (bytes1.size() != bytes2.size()) { + std::cout << "Test failed on write. Number of bytes was different." + << std::endl + << std::endl; + return; + } + + if (bytes1 != bytes2) { + std::cout << "Test failed on write. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} + +#endif diff --git a/tests/CMakeLists.txt b/tests/json/CMakeLists.txt similarity index 89% rename from tests/CMakeLists.txt rename to tests/json/CMakeLists.txt index de1f3658..c99b0b84 100644 --- a/tests/CMakeLists.txt +++ b/tests/json/CMakeLists.txt @@ -3,13 +3,13 @@ cmake_minimum_required(VERSION 3.4) project(reflect-cpp-tests) include_directories( - "../include" + "../../include" ) add_executable( reflect-cpp-tests "tests.cpp" - "../src/yyjson.c" + "../../src/yyjson.c" ) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") diff --git a/tests/test_all_of.hpp b/tests/json/test_all_of.hpp similarity index 100% rename from tests/test_all_of.hpp rename to tests/json/test_all_of.hpp diff --git a/tests/test_anonymous_fields.hpp b/tests/json/test_anonymous_fields.hpp similarity index 100% rename from tests/test_anonymous_fields.hpp rename to tests/json/test_anonymous_fields.hpp diff --git a/tests/test_any_of.hpp b/tests/json/test_any_of.hpp similarity index 100% rename from tests/test_any_of.hpp rename to tests/json/test_any_of.hpp diff --git a/tests/test_array.hpp b/tests/json/test_array.hpp similarity index 100% rename from tests/test_array.hpp rename to tests/json/test_array.hpp diff --git a/tests/test_as.hpp b/tests/json/test_as.hpp similarity index 100% rename from tests/test_as.hpp rename to tests/json/test_as.hpp diff --git a/tests/test_as_flatten.hpp b/tests/json/test_as_flatten.hpp similarity index 100% rename from tests/test_as_flatten.hpp rename to tests/json/test_as_flatten.hpp diff --git a/tests/test_box.hpp b/tests/json/test_box.hpp similarity index 100% rename from tests/test_box.hpp rename to tests/json/test_box.hpp diff --git a/tests/test_custom_class1.hpp b/tests/json/test_custom_class1.hpp similarity index 100% rename from tests/test_custom_class1.hpp rename to tests/json/test_custom_class1.hpp diff --git a/tests/test_custom_class2.hpp b/tests/json/test_custom_class2.hpp similarity index 100% rename from tests/test_custom_class2.hpp rename to tests/json/test_custom_class2.hpp diff --git a/tests/test_custom_class3.hpp b/tests/json/test_custom_class3.hpp similarity index 100% rename from tests/test_custom_class3.hpp rename to tests/json/test_custom_class3.hpp diff --git a/tests/test_custom_class4.hpp b/tests/json/test_custom_class4.hpp similarity index 100% rename from tests/test_custom_class4.hpp rename to tests/json/test_custom_class4.hpp diff --git a/tests/test_custom_constructor.hpp b/tests/json/test_custom_constructor.hpp similarity index 100% rename from tests/test_custom_constructor.hpp rename to tests/json/test_custom_constructor.hpp diff --git a/tests/test_default_values.hpp b/tests/json/test_default_values.hpp similarity index 100% rename from tests/test_default_values.hpp rename to tests/json/test_default_values.hpp diff --git a/tests/test_deque.hpp b/tests/json/test_deque.hpp similarity index 100% rename from tests/test_deque.hpp rename to tests/json/test_deque.hpp diff --git a/tests/test_email.hpp b/tests/json/test_email.hpp similarity index 100% rename from tests/test_email.hpp rename to tests/json/test_email.hpp diff --git a/tests/test_error_messages.hpp b/tests/json/test_error_messages.hpp similarity index 100% rename from tests/test_error_messages.hpp rename to tests/json/test_error_messages.hpp diff --git a/tests/test_field_variant.hpp b/tests/json/test_field_variant.hpp similarity index 100% rename from tests/test_field_variant.hpp rename to tests/json/test_field_variant.hpp diff --git a/tests/test_flatten.hpp b/tests/json/test_flatten.hpp similarity index 100% rename from tests/test_flatten.hpp rename to tests/json/test_flatten.hpp diff --git a/tests/test_forward_list.hpp b/tests/json/test_forward_list.hpp similarity index 100% rename from tests/test_forward_list.hpp rename to tests/json/test_forward_list.hpp diff --git a/tests/test_list.hpp b/tests/json/test_list.hpp similarity index 100% rename from tests/test_list.hpp rename to tests/json/test_list.hpp diff --git a/tests/test_literal.hpp b/tests/json/test_literal.hpp similarity index 100% rename from tests/test_literal.hpp rename to tests/json/test_literal.hpp diff --git a/tests/test_map.hpp b/tests/json/test_map.hpp similarity index 100% rename from tests/test_map.hpp rename to tests/json/test_map.hpp diff --git a/tests/test_move_replace.hpp b/tests/json/test_move_replace.hpp similarity index 100% rename from tests/test_move_replace.hpp rename to tests/json/test_move_replace.hpp diff --git a/tests/test_multimap.hpp b/tests/json/test_multimap.hpp similarity index 100% rename from tests/test_multimap.hpp rename to tests/json/test_multimap.hpp diff --git a/tests/test_multiset.hpp b/tests/json/test_multiset.hpp similarity index 100% rename from tests/test_multiset.hpp rename to tests/json/test_multiset.hpp diff --git a/tests/test_one_of.hpp b/tests/json/test_one_of.hpp similarity index 100% rename from tests/test_one_of.hpp rename to tests/json/test_one_of.hpp diff --git a/tests/test_optional_fields.hpp b/tests/json/test_optional_fields.hpp similarity index 100% rename from tests/test_optional_fields.hpp rename to tests/json/test_optional_fields.hpp diff --git a/tests/test_readme_example.hpp b/tests/json/test_readme_example.hpp similarity index 100% rename from tests/test_readme_example.hpp rename to tests/json/test_readme_example.hpp diff --git a/tests/test_ref.hpp b/tests/json/test_ref.hpp similarity index 100% rename from tests/test_ref.hpp rename to tests/json/test_ref.hpp diff --git a/tests/test_replace.hpp b/tests/json/test_replace.hpp similarity index 100% rename from tests/test_replace.hpp rename to tests/json/test_replace.hpp diff --git a/tests/test_replace_flatten.hpp b/tests/json/test_replace_flatten.hpp similarity index 100% rename from tests/test_replace_flatten.hpp rename to tests/json/test_replace_flatten.hpp diff --git a/tests/test_replace_with_other_struct.hpp b/tests/json/test_replace_with_other_struct.hpp similarity index 100% rename from tests/test_replace_with_other_struct.hpp rename to tests/json/test_replace_with_other_struct.hpp diff --git a/tests/test_result.hpp b/tests/json/test_result.hpp similarity index 100% rename from tests/test_result.hpp rename to tests/json/test_result.hpp diff --git a/tests/test_set.hpp b/tests/json/test_set.hpp similarity index 100% rename from tests/test_set.hpp rename to tests/json/test_set.hpp diff --git a/tests/test_size.hpp b/tests/json/test_size.hpp similarity index 100% rename from tests/test_size.hpp rename to tests/json/test_size.hpp diff --git a/tests/test_string_map.hpp b/tests/json/test_string_map.hpp similarity index 100% rename from tests/test_string_map.hpp rename to tests/json/test_string_map.hpp diff --git a/tests/test_string_unordered_map.hpp b/tests/json/test_string_unordered_map.hpp similarity index 100% rename from tests/test_string_unordered_map.hpp rename to tests/json/test_string_unordered_map.hpp diff --git a/tests/test_tagged_union.hpp b/tests/json/test_tagged_union.hpp similarity index 100% rename from tests/test_tagged_union.hpp rename to tests/json/test_tagged_union.hpp diff --git a/tests/test_timestamp.hpp b/tests/json/test_timestamp.hpp similarity index 100% rename from tests/test_timestamp.hpp rename to tests/json/test_timestamp.hpp diff --git a/tests/test_unique_ptr.hpp b/tests/json/test_unique_ptr.hpp similarity index 100% rename from tests/test_unique_ptr.hpp rename to tests/json/test_unique_ptr.hpp diff --git a/tests/test_unique_ptr2.hpp b/tests/json/test_unique_ptr2.hpp similarity index 100% rename from tests/test_unique_ptr2.hpp rename to tests/json/test_unique_ptr2.hpp diff --git a/tests/test_unordered_map.hpp b/tests/json/test_unordered_map.hpp similarity index 100% rename from tests/test_unordered_map.hpp rename to tests/json/test_unordered_map.hpp diff --git a/tests/test_unordered_multimap.hpp b/tests/json/test_unordered_multimap.hpp similarity index 100% rename from tests/test_unordered_multimap.hpp rename to tests/json/test_unordered_multimap.hpp diff --git a/tests/test_unordered_multiset.hpp b/tests/json/test_unordered_multiset.hpp similarity index 100% rename from tests/test_unordered_multiset.hpp rename to tests/json/test_unordered_multiset.hpp diff --git a/tests/test_unordered_set.hpp b/tests/json/test_unordered_set.hpp similarity index 100% rename from tests/test_unordered_set.hpp rename to tests/json/test_unordered_set.hpp diff --git a/tests/test_variant.hpp b/tests/json/test_variant.hpp similarity index 100% rename from tests/test_variant.hpp rename to tests/json/test_variant.hpp diff --git a/tests/tests.cpp b/tests/json/tests.cpp similarity index 100% rename from tests/tests.cpp rename to tests/json/tests.cpp diff --git a/tests/write_and_read.hpp b/tests/json/write_and_read.hpp similarity index 100% rename from tests/write_and_read.hpp rename to tests/json/write_and_read.hpp