draconisplusplus/include/rfl/msgpack/Writer.hpp

155 lines
4.4 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_MSGPACK_WRITER_HPP_
#define RFL_MSGPACK_WRITER_HPP_
#include <exception>
#include <map>
2024-06-08 14:10:59 -04:00
#include <msgpack.h>
2024-06-05 19:04:53 -04:00
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>
#include <vector>
#include "../Box.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl::msgpack {
2024-06-08 14:10:59 -04:00
class Writer {
public:
struct MsgpackOutputArray {};
struct MsgpackOutputObject {};
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
struct MsgpackOutputVar {};
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
using OutputArrayType = MsgpackOutputArray;
using OutputObjectType = MsgpackOutputObject;
using OutputVarType = MsgpackOutputVar;
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
Writer(msgpack_packer* _pk) : pk_(_pk) {}
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
~Writer() = default;
2024-06-05 19:04:53 -04:00
2024-06-16 00:13:15 -04:00
OutputArrayType array_as_root(const size_t _size) const noexcept { return new_array(_size); }
2024-06-05 19:04:53 -04:00
2024-06-16 00:13:15 -04:00
OutputObjectType object_as_root(const size_t _size) const noexcept { return new_object(_size); }
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
OutputVarType null_as_root() const noexcept {
msgpack_pack_nil(pk_);
return OutputVarType {};
}
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
return new_value(_var);
}
2024-06-05 19:04:53 -04:00
2024-06-16 00:13:15 -04:00
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
2024-06-08 14:10:59 -04:00
return new_array(_size);
}
OutputArrayType add_array_to_object(
2024-06-16 00:13:15 -04:00
const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent
2024-06-08 15:53:06 -04:00
) const noexcept {
2024-06-08 14:10:59 -04:00
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
return new_array(_size);
}
2024-06-16 00:13:15 -04:00
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
2024-06-08 14:10:59 -04:00
return new_object(_size);
}
OutputObjectType add_object_to_object(
2024-06-16 00:13:15 -04:00
const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent
2024-06-08 15:53:06 -04:00
) const noexcept {
2024-06-08 14:10:59 -04:00
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
return new_object(_size);
}
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
template <class T>
2024-06-16 00:13:15 -04:00
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
2024-06-08 14:10:59 -04:00
return new_value(_var);
}
template <class T>
OutputVarType add_value_to_object(
2024-06-16 00:13:15 -04:00
const std::string_view& _name,
const T& _var,
OutputObjectType* _parent
2024-06-08 15:53:06 -04:00
) const noexcept {
2024-06-08 14:10:59 -04:00
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
return new_value(_var);
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
msgpack_pack_nil(pk_);
return OutputVarType {};
}
2024-06-16 00:13:15 -04:00
OutputVarType add_null_to_object(const std::string_view& _name, OutputObjectType* _parent)
const noexcept {
2024-06-08 14:10:59 -04:00
msgpack_pack_str(pk_, _name.size());
msgpack_pack_str_body(pk_, _name.data(), _name.size());
msgpack_pack_nil(pk_);
return OutputVarType {};
}
void end_array(OutputArrayType* _arr) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept {}
private:
OutputArrayType new_array(const size_t _size) const noexcept {
msgpack_pack_array(pk_, _size);
return OutputArrayType {};
}
OutputObjectType new_object(const size_t _size) const noexcept {
msgpack_pack_map(pk_, _size);
return OutputObjectType {};
}
template <class T>
OutputVarType new_value(const T& _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
msgpack_pack_str(pk_, _var.size());
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (_var) {
msgpack_pack_true(pk_);
} else {
msgpack_pack_false(pk_);
}
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
msgpack_pack_double(pk_, static_cast<double>(_var));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
2024-06-05 19:04:53 -04:00
} else {
2024-06-08 14:10:59 -04:00
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
return OutputVarType {};
2024-06-05 19:04:53 -04:00
}
2024-06-08 14:10:59 -04:00
private:
/// The underlying packer.
msgpack_packer* pk_;
};
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl::msgpack
2024-06-05 19:04:53 -04:00
2024-06-08 14:10:59 -04:00
#endif // MSGPACK_PARSER_HPP_