draconisplusplus/include/rfl/flexbuf/Reader.hpp

148 lines
4.5 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef FLEXBUF_READER_HPP_
#define FLEXBUF_READER_HPP_
#include <exception>
2024-06-08 14:10:59 -04:00
#include <flatbuffers/flexbuffers.h>
2024-06-05 19:04:53 -04:00
#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 {
2024-06-08 14:10:59 -04:00
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,
2024-06-08 15:53:06 -04:00
std::enable_if_t<
std::is_invocable_r<T, decltype(T::from_flexbuf), InputVarType>::
value>> : std::true_type {};
2024-06-08 14:10:59 -04:00
template <class T>
struct has_from_flexbuf<
T,
2024-06-08 15:53:06 -04:00
std::enable_if_t<std::is_invocable_r<
rfl::Result<T>,
decltype(T::from_flexbuf),
InputVarType>::value>> : std::true_type {};
2024-06-08 14:10:59 -04:00
template <class T>
static constexpr bool has_custom_constructor = has_from_flexbuf<T>::value;
rfl::Result<InputVarType> get_field(
2024-06-08 15:53:06 -04:00
const std::string& _name,
const InputObjectType& _obj
) const noexcept {
2024-06-08 14:10:59 -04:00
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]; }
}
2024-06-08 15:53:06 -04:00
return rfl::Error(
"Map does not contain any element called '" + _name + "'."
);
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
bool is_empty(const InputVarType& _var) const noexcept {
return _var.IsNull();
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
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.");
}
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
template <class ArrayReader>
std::optional<Error> read_array(
2024-06-08 15:53:06 -04:00
const ArrayReader& _array_reader,
const InputArrayType& _arr
) const noexcept {
2024-06-08 14:10:59 -04:00
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;
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
template <class ObjectReader>
std::optional<Error> read_object(
2024-06-08 15:53:06 -04:00
const ObjectReader& _object_reader,
const InputObjectType& _obj
) const noexcept {
2024-06-08 14:10:59 -04:00
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) {
2024-06-08 15:53:06 -04:00
_object_reader.read(
std::string_view(keys[i].AsString().c_str()), values[i]
);
2024-06-08 14:10:59 -04:00
}
return std::nullopt;
}
2024-06-08 15:53:06 -04:00
rfl::Result<InputArrayType> to_array(const InputVarType& _var
) const noexcept {
2024-06-08 14:10:59 -04:00
if (!_var.IsVector()) {
return rfl::Error("Could not cast to Vector.");
}
return _var.AsVector();
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
2024-06-08 15:53:06 -04:00
rfl::Result<InputObjectType> to_object(const InputVarType& _var
) const noexcept {
2024-06-08 14:10:59 -04:00
if (!_var.IsMap()) { return rfl::Error("Could not cast to Map!"); }
return _var.AsMap();
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
template <class T>
2024-06-08 15:53:06 -04:00
rfl::Result<T> use_custom_constructor(const InputVarType& _var
) const noexcept {
2024-06-08 14:10:59 -04:00
try {
return T::from_flexbuf(_var);
} catch (std::exception& e) { return rfl::Error(e.what()); }
}
};
} // namespace flexbuf
} // namespace rfl
2024-06-05 19:04:53 -04:00
#endif