weh
This commit is contained in:
parent
693fa17d10
commit
500138ce67
331 changed files with 12348 additions and 60593 deletions
17
include/rfl/flexbuf/Parser.hpp
Normal file
17
include/rfl/flexbuf/Parser.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef FLEXBUF_PARSER_HPP_
|
||||
#define FLEXBUF_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
145
include/rfl/flexbuf/Reader.hpp
Normal file
145
include/rfl/flexbuf/Reader.hpp
Normal file
|
@ -0,0 +1,145 @@
|
|||
#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 "../Result.hpp"
|
||||
#include "../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::remove_cvref_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::remove_cvref_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::remove_cvref_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::remove_cvref_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 <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
const auto size = _arr.size();
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
const auto err = _array_reader.read(InputVarType(_arr[i]));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
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) {
|
||||
_object_reader.read(std::string_view(keys[i].AsString().c_str()),
|
||||
values[i]);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.IsVector()) {
|
||||
return rfl::Error("Could not cast to Vector.");
|
||||
}
|
||||
return _var.AsVector();
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.IsMap()) {
|
||||
return rfl::Error("Could not cast to Map!");
|
||||
}
|
||||
return _var.AsMap();
|
||||
}
|
||||
|
||||
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
|
176
include/rfl/flexbuf/Writer.hpp
Normal file
176
include/rfl/flexbuf/Writer.hpp
Normal file
|
@ -0,0 +1,176 @@
|
|||
#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 "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
struct Writer {
|
||||
struct OutputArray {
|
||||
size_t start_;
|
||||
};
|
||||
|
||||
struct OutputObject {
|
||||
size_t start_;
|
||||
};
|
||||
|
||||
struct OutputVar {};
|
||||
|
||||
using OutputArrayType = OutputArray;
|
||||
using OutputObjectType = OutputObject;
|
||||
using OutputVarType = OutputVar;
|
||||
|
||||
Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
fbb_->Null(_name.data());
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
fbb_->EndVector(_arr->start_, false, false);
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
fbb_->EndMap(_obj->start_);
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string_view& _name,
|
||||
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>, bool>()) {
|
||||
fbb_->Bool(_name.data(), _var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Double(_name.data(), _var);
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Int(_name.data(), _var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
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>, bool>()) {
|
||||
fbb_->Bool(_var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Double(_var);
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Int(_var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept {
|
||||
const auto start = fbb_->StartVector(_name.data());
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
const auto start = fbb_->StartVector();
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const std::string_view& _name) const noexcept {
|
||||
const auto start = fbb_->StartMap(_name.data());
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
const auto start = fbb_->StartMap();
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
private:
|
||||
Ref<flexbuffers::Builder> fbb_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
22
include/rfl/flexbuf/load.hpp
Normal file
22
include/rfl/flexbuf/load.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef RFL_FLEXBUF_LOAD_HPP_
|
||||
#define RFL_FLEXBUF_LOAD_HPP_
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/load_bytes.hpp"
|
||||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) {
|
||||
return read<T, Ps...>(_bytes);
|
||||
};
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
50
include/rfl/flexbuf/read.hpp
Normal file
50
include/rfl/flexbuf/read.hpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#ifndef FLEXBUF_READ_HPP_
|
||||
#define FLEXBUF_READ_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from flexbuf var.
|
||||
template <class T, class... Ps>
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T, class... Ps>
|
||||
auto read(const char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T, Ps...>(root);
|
||||
}
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T, class... Ps>
|
||||
auto read(const std::vector<char>& _bytes) {
|
||||
return read<T, Ps...>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object directly from a stream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
const auto bytes = std::vector<char>(begin, end);
|
||||
return read<T, Ps...>(bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
26
include/rfl/flexbuf/save.hpp
Normal file
26
include/rfl/flexbuf/save.hpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef RFL_FLEXBUF_SAVE_HPP_
|
||||
#define RFL_FLEXBUF_SAVE_HPP_
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/save_bytes.hpp"
|
||||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
50
include/rfl/flexbuf/write.hpp
Normal file
50
include/rfl/flexbuf/write.hpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#ifndef FLEXBUF_WRITE_HPP_
|
||||
#define FLEXBUF_WRITE_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../Ref.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class... Ps>
|
||||
std::vector<uint8_t> to_buffer(const auto& _obj) {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto fbb = Ref<flexbuffers::Builder>::make();
|
||||
auto w = Writer(fbb);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
fbb->Finish();
|
||||
return fbb->GetBuffer();
|
||||
}
|
||||
|
||||
/// Writes an object to flexbuf.
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) {
|
||||
const auto buffer = to_buffer<Ps...>(_obj);
|
||||
const auto data = reinterpret_cast<const char*>(buffer.data());
|
||||
return std::vector<char>(data, data + buffer.size());
|
||||
}
|
||||
|
||||
/// Writes an object to an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) {
|
||||
const auto buffer = to_buffer<Ps...>(_obj);
|
||||
const auto data = reinterpret_cast<const char*>(buffer.data());
|
||||
_stream.write(data, buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue