3#include "schema_name.hpp"
12template <
typename T>
inline void _msgpack_schema_pack(
MsgpackSchemaPacker& packer,
const T& obj);
20 : packer<msgpack::sbuffer>(stream)
23 std::set<std::string> emitted_types;
25 bool set_emitted(
const std::string& type)
27 if (emitted_types.find(type) == emitted_types.end()) {
28 emitted_types.insert(type);
40 void pack_alias(
const std::string& schema_name,
const std::string& msgpack_name)
56 template <
typename T>
void pack_schema(
const T& obj) { _msgpack_schema_pack(*
this, obj); }
60 template <
typename... Args>
void pack_template_type(
const std::string& schema_name)
65 pack_array(
sizeof...(Args));
69 (_msgpack_schema_pack(*
this, *std::make_unique<Args>()), ...);
78 template <msgpack_concepts::HasMsgPack T>
void pack_with_name(
const std::string& type, T
const&
object)
80 if (set_emitted(type)) {
84 msgpack::check_msgpack_usage(
object);
86 const_cast<T&
>(object).msgpack([&](
auto&... args) {
87 size_t kv_size =
sizeof...(args);
89 pack_map(uint32_t(1 + kv_size / 2));
93 _schema_pack_map_content(*
this, args...);
104namespace msgpack_concepts {
110template <
typename Value,
typename... Rest>
118 "see the first type argument in the error trace, it might require a specialization of msgpack_schema_pack");
120 msgpack_schema_pack(packer, value);
121 _schema_pack_map_content(packer, rest...);
128 packer.pack(msgpack_schema_name(obj));
136template <msgpack_concepts::HasMsgPackSchema T>
139 obj.msgpack_schema(packer);
149template <msgpack_concepts::HasMsgPack T>
153 std::string type = msgpack_schema_name(
object);
160template <
typename T>
inline void _msgpack_schema_pack(
MsgpackSchemaPacker& packer,
const T& obj)
163 "see the first type argument in the error trace, it might need a msgpack_schema method!");
164 msgpack_schema_pack(packer, obj);
167template <
typename... Args>
inline void msgpack_schema_pack(
MsgpackSchemaPacker& packer, std::tuple<Args...>
const&)
169 packer.pack_template_type<Args...>(
"tuple");
172template <
typename K,
typename V>
inline void msgpack_schema_pack(
MsgpackSchemaPacker& packer, std::map<K, V>
const&)
174 packer.pack_template_type<K, V>(
"map");
177template <
typename T>
inline void msgpack_schema_pack(
MsgpackSchemaPacker& packer, std::optional<T>
const&)
179 packer.pack_template_type<T>(
"optional");
182template <
typename T>
inline void msgpack_schema_pack(
MsgpackSchemaPacker& packer, std::vector<T>
const&)
184 packer.pack_template_type<T>(
"vector");
187template <
typename... Args>
inline void msgpack_schema_pack(
MsgpackSchemaPacker& packer, std::variant<Args...>
const&)
189 packer.pack_template_type<Args...>(
"variant");
192template <
typename T>
inline void msgpack_schema_pack(
MsgpackSchemaPacker& packer, std::shared_ptr<T>
const&)
194 packer.pack_template_type<T>(
"shared_ptr");
198template <
typename T, std::
size_t N>
202 packer.pack_array(2);
203 packer.pack(
"array");
205 packer.pack_array(2);
208 _msgpack_schema_pack(packer, *std::make_unique<T>());
218inline std::string msgpack_schema_to_string(
const auto& obj)
220 msgpack::sbuffer output;
222 _msgpack_schema_pack(printer, obj);
223 msgpack::object_handle oh = msgpack::unpack(output.data(), output.size());
224 std::stringstream pretty_output;
225 pretty_output << oh.get() << std::endl;
226 return pretty_output.str();
Definition: concepts.hpp:11
Definition: concepts.hpp:8
Definition: schema_impl.hpp:106
Definition: schema_impl.hpp:18
void pack_alias(const std::string &schema_name, const std::string &msgpack_name)
Definition: schema_impl.hpp:40
void pack_with_name(const std::string &type, T const &object)
Encode a type that defines msgpack based on its key value pairs.
Definition: schema_impl.hpp:78
void pack_schema(const T &obj)
Definition: schema_impl.hpp:56