mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Added flexbuffers reader and writer as well as first test (but does not pass)
This commit is contained in:
+7
-6
@@ -26,10 +26,9 @@ class Box {
|
||||
|
||||
Box(Box<T>&& _other) = default;
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
template <class U>
|
||||
Box(Box<U>&& _other) noexcept
|
||||
: ptr_(std::make_unique<T>(std::forward<U>(*_other))) {}
|
||||
: ptr_(std::forward<std::unique_ptr<U>>(_other.ptr())) {}
|
||||
|
||||
~Box() = default;
|
||||
|
||||
@@ -43,10 +42,9 @@ class Box {
|
||||
Box<T>& operator=(Box<T>&& _other) noexcept = default;
|
||||
|
||||
/// Move assignment operator
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
template <class U>
|
||||
Box<T>& operator=(Box<U>&& _other) noexcept {
|
||||
ptr_ = std::make_unique<T>(std::forward<U>(*_other));
|
||||
ptr_ = std::forward<std::unique_ptr<U>>(_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<T>& ptr() { return ptr_; }
|
||||
|
||||
/// Returns the underlying unique_ptr
|
||||
const std::unique_ptr<T>& ptr() const { return ptr_; }
|
||||
|
||||
|
||||
+9
-10
@@ -26,14 +26,12 @@ class Ref {
|
||||
|
||||
Ref(Ref<T>&& _other) = default;
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
template <class U>
|
||||
Ref(const Ref<U>& _other) : ptr_(_other.ptr()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
template <class U>
|
||||
Ref(Ref<U>&& _other) noexcept
|
||||
: ptr_(std::make_shared<T>(std::forward<U>(*_other))) {}
|
||||
: ptr_(std::forward<std::shared_ptr<U>>(_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<T>& ptr() { return ptr_; }
|
||||
|
||||
/// Returns the underlying shared_ptr
|
||||
const std::shared_ptr<T>& ptr() const { return ptr_; }
|
||||
|
||||
/// Copy assignment operator.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
template <class U>
|
||||
Ref<T>& operator=(const Ref<U>& _other) {
|
||||
ptr_ = _other.ptr();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Move assignment operator
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
template <class U>
|
||||
Ref<T>& operator=(Ref<U>&& _other) noexcept {
|
||||
ptr_ = std::make_shared<T>(std::forward<U>(*_other));
|
||||
ptr_ = std::forward<std::shared_ptr<U>>(_other.ptr());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef FLEXBUF_FLEXBUF_HPP_
|
||||
#define FLEXBUF_FLEXBUF_HPP_
|
||||
|
||||
#include "rfl/flexbuf/read.hpp"
|
||||
#include "rfl/flexbuf/write.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef FLEXBUF_OUTPUTARRAY_HPP_
|
||||
#define FLEXBUF_OUTPUTARRAY_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#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<std::string>& _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<OutputVar>& _var) { vars_.push_back(_var); }
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<Ref<OutputVar>> vars_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef FLEXBUF_OUTPUTNULL_HPP_
|
||||
#define FLEXBUF_OUTPUTNULL_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#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<std::string>& _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
|
||||
@@ -0,0 +1,61 @@
|
||||
#ifndef FLEXBUF_OUTPUTOBJECT_HPP_
|
||||
#define FLEXBUF_OUTPUTOBJECT_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#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<std::string>& _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<OutputVar>& _var) {
|
||||
vars_.push_back(std::make_pair(_name, _var));
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<std::pair<std::string, Ref<OutputVar>>> vars_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
#ifndef FLEXBUF_OUTPUTVALUE_HPP_
|
||||
#define FLEXBUF_OUTPUTVALUE_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/flexbuf/OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class T>
|
||||
class OutputValue : public OutputVar {
|
||||
public:
|
||||
OutputValue(T _val) : val_(_val) {}
|
||||
|
||||
~OutputValue() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if (_key) {
|
||||
_fbb->String(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->String(val_);
|
||||
}
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
if (_key) {
|
||||
_fbb->Bool(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Bool(val_);
|
||||
}
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
if (_key) {
|
||||
_fbb->Double(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Double(val_);
|
||||
}
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
if (_key) {
|
||||
_fbb->Int(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Int(val_);
|
||||
}
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
T val_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef FLEXBUF_OUTPUTVAR_HPP_
|
||||
#define FLEXBUF_OUTPUTVAR_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputVar {
|
||||
public:
|
||||
/// Inserts the value into the buffer.
|
||||
virtual void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) = 0;
|
||||
|
||||
/// Whether this is null.
|
||||
virtual bool is_null() const = 0;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -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 <class T>
|
||||
using Parser = rfl::parsing::Parser<Reader, Writer, T>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,169 @@
|
||||
#ifndef FLEXBUF_READER_HPP_
|
||||
#define FLEXBUF_READER_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#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 <class T, class = void>
|
||||
struct has_from_flexbuf : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_flexbuf<
|
||||
T, std::enable_if_t<std::is_invocable_r<T, decltype(T::from_flexbuf),
|
||||
InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_flexbuf<
|
||||
T, std::enable_if_t<std::is_invocable_r<
|
||||
rfl::Result<T>, decltype(T::from_flexbuf), InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = has_from_flexbuf<T>::value;
|
||||
|
||||
rfl::Result<InputVarType> 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 <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, 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<std::decay_t<T>, bool>()) {
|
||||
if (!_var.IsBool()) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.AsBool();
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
if (!_var.IsNumeric()) {
|
||||
return rfl::Error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(_var.AsDouble());
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
if (!_var.IsNumeric()) {
|
||||
return rfl::Error("Could not cast to int.");
|
||||
}
|
||||
return static_cast<T>(_var.AsInt64());
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t size, class FunctionType>
|
||||
std::array<InputVarType, size> to_fields_array(
|
||||
const FunctionType& _fct, const InputObjectType& _obj) const noexcept {
|
||||
std::array<InputVarType, size> 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<InputArrayType> 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<std::pair<std::string, InputVarType>> to_map(
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
std::vector<std::pair<std::string, InputVarType>> 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<InputObjectType> 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<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
|
||||
const auto size = _arr.size();
|
||||
std::vector<InputVarType> vec;
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
vec.push_back(_arr[i]);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> 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
|
||||
@@ -0,0 +1,69 @@
|
||||
#ifndef FLEXBUF_WRITER_HPP_
|
||||
#define FLEXBUF_WRITER_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#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<OutputArray>;
|
||||
using OutputObjectType = Ref<OutputObject>;
|
||||
using OutputVarType = Ref<OutputVar>;
|
||||
|
||||
Writer() {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
(*_arr)->push_back(_var);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept { return Ref<OutputNull>::make(); }
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T _var) const noexcept {
|
||||
return Ref<OutputValue<T>>::make(_var);
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return Ref<OutputArray>::make();
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return Ref<OutputObject>::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
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef FLEXBUF_READ_HPP_
|
||||
#define FLEXBUF_READ_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#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 <class T>
|
||||
Result<T> read(const unsigned char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, root);
|
||||
}
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::vector<unsigned char>& _bytes) {
|
||||
return read<T>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef FLEXBUF_WRITE_HPP_
|
||||
#define FLEXBUF_WRITE_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/flexbuf/Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
/// Writes an object to flexbuf.
|
||||
template <class T>
|
||||
std::vector<unsigned char> write(const T& _obj) {
|
||||
auto w = Writer();
|
||||
const auto flexbuf_obj = Parser<T>::write(w, _obj);
|
||||
flexbuffers::Builder fbb;
|
||||
flexbuf_obj->insert(std::nullopt, &fbb);
|
||||
fbb.Finish();
|
||||
const auto vec = fbb.GetBuffer();
|
||||
const auto data = reinterpret_cast<const unsigned char*>(vec.data());
|
||||
return std::vector<unsigned char>(data, data + vec.size());
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
)
|
||||
@@ -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
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
docker build --tag=reflectcpp_test_flexbuffers .
|
||||
Executable
+3
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_readme_example() {
|
||||
std::cout << "test_readme_example" << std::endl;
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, 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<Person>> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com",
|
||||
.children = std::vector<Person>()};
|
||||
|
||||
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<Person>()
|
||||
};
|
||||
|
||||
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<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "test_readme_example.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef WRITE_AND_READ_
|
||||
#define WRITE_AND_READ_
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
|
||||
template <class T>
|
||||
void write_and_read(const T& _struct) {
|
||||
const auto bytes1 = rfl::flexbuf::write(_struct);
|
||||
|
||||
const auto res = rfl::flexbuf::read<T>(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
|
||||
@@ -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")
|
||||
Reference in New Issue
Block a user