WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
pack.h
Go to the documentation of this file.
1#pragma once
2#ifdef MSGPACK_DEFINE_MAP
3#error "webcface/common/internal/pack.h must be included first"
4#endif
5
6#include <msgpack.hpp>
7#include <string>
8#include <cstdint>
9#include <utf8.h>
11#include "./base.h"
12
13#ifdef WEBCFACE_COMPILER_IS_GCC
14#pragma GCC diagnostic push
15#pragma GCC diagnostic ignored "-Wabi"
16#endif
17#include <spdlog/logger.h>
18#ifdef WEBCFACE_COMPILER_IS_GCC
19#pragma GCC diagnostic pop
20#endif
21
22MSGPACK_ADD_ENUM(webcface::ValType)
23
25namespace message {
26struct Ping;
27struct PingStatusReq;
28
33std::string messageTrace(const std::string &message);
34
39std::vector<std::pair<int, std::shared_ptr<void>>>
40unpack(const std::string &message,
41 const std::shared_ptr<spdlog::logger> &logger);
42
47template <typename T>
48std::string packSingle(const T &obj) {
49 msgpack::type::tuple<int, T> src(static_cast<int>(T::kind), obj);
50 std::stringstream buffer;
51 msgpack::pack(buffer, src);
52 return buffer.str();
53}
54
59template <typename T>
60void pack(std::stringstream &buffer, int &len, const T &obj) {
61 msgpack::pack(buffer, static_cast<int>(T::kind));
62 msgpack::pack(buffer, obj);
63 len += 2;
64}
65
66inline std::string packDone(std::stringstream &buffer, int len) {
67 std::stringstream buffer2;
68 msgpack::packer packer(buffer2);
69 packer.pack_array(len);
70 buffer2 << buffer.rdbuf();
71 return buffer2.str();
72}
73
74} // namespace message
76
77namespace msgpack {
78MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
79 namespace adaptor {
80
81 template <>
82 struct convert<webcface::SharedString> {
83 msgpack::object const &operator()(msgpack::object const &o,
84 webcface::SharedString &v) const {
85 v = webcface::SharedString::fromU8String(utf8::replace_invalid(
86 std::string(o.via.bin.ptr, o.via.bin.size)));
87 return o;
88 }
89 };
90 template <>
91 struct pack<webcface::SharedString> {
92 template <typename Stream>
93 msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
94 const webcface::SharedString &v) {
95 o.pack(v.u8String());
96 return o;
97 }
98 };
99
100 template <>
101 struct convert<webcface::ValAdaptor> {
102 msgpack::object const &operator()(msgpack::object const &o,
103 webcface::ValAdaptor &v) const {
104 switch (o.type) {
105 case msgpack::type::FLOAT32:
106 case msgpack::type::FLOAT64:
107 v = o.via.f64;
108 break;
109 case msgpack::type::POSITIVE_INTEGER:
110 v = o.via.u64;
111 break;
112 case msgpack::type::NEGATIVE_INTEGER:
113 v = o.via.i64;
114 break;
115 case msgpack::type::BOOLEAN:
116 v = o.via.boolean;
117 break;
118 case msgpack::type::BIN:
119 case msgpack::type::STR:
120 v = webcface::SharedString::fromU8String(utf8::replace_invalid(
121 std::string(o.via.bin.ptr, o.via.bin.size)));
122 break;
123 default:
124 throw msgpack::type_error();
125 }
126 return o;
127 }
128 };
129 template <>
130 struct pack<webcface::ValAdaptor> {
131 template <typename Stream>
132 msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
133 const webcface::ValAdaptor &v) {
134 switch (v.valType()) {
136 o.pack(static_cast<bool>(v));
137 break;
139 o.pack(static_cast<std::int64_t>(v));
140 break;
142 o.pack(static_cast<double>(v));
143 break;
145 default:
146 o.pack(v.asU8StringRef());
147 break;
148 }
149 return o;
150 }
151 };
152
153 template <typename T>
154 struct EmptyConvert {
155 msgpack::object const &operator()(msgpack::object const &o, T &) const {
156 return o;
157 }
158 };
159 template <typename T>
160 struct EmptyPack {
161 template <typename Stream>
162 msgpack::packer<Stream> &operator()(msgpack::packer<Stream> &o,
163 const T &) {
164 o.pack_map(0);
165 return o;
166 }
167 };
168 template <>
169 struct convert<webcface::message::Ping>
170 : public EmptyConvert<webcface::message::Ping> {};
171 template <>
172 struct convert<webcface::message::PingStatusReq>
173 : public EmptyConvert<webcface::message::PingStatusReq> {};
174 template <>
175 struct pack<webcface::message::Ping>
176 : public EmptyPack<webcface::message::Ping> {};
177 template <>
178 struct pack<webcface::message::PingStatusReq>
179 : public EmptyPack<webcface::message::PingStatusReq> {};
180
181 } // namespace adaptor
182}
183} // namespace msgpack
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:67
const std::string & u8String() const
Definition encoding.cc:104
static SharedString fromU8String(std::string_view u8s)
Definition encoding.cc:63
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
const std::string & asU8StringRef() const
Definition val_adaptor.cc:94
ValType valType() const
Definition val_adaptor.h:166
webcface::ValAdaptor ValAdaptor
Definition val_adaptor.h:350
webcface::SharedString SharedString
Definition encoding.h:121
std::string packDone(std::stringstream &buffer, int len)
Definition pack.h:66
void pack(std::stringstream &buffer, int &len, const T &obj)
メッセージをシリアル化しbufferに追加
Definition pack.h:60
std::vector< std::pair< int, std::shared_ptr< void > > > unpack(const std::string &message, const std::shared_ptr< spdlog::logger > &logger)
msgpackのメッセージをパースし返す
Definition message.cc:53
std::string packSingle(const T &obj)
メッセージ1つを要素数2の配列としてシリアル化
Definition pack.h:48
std::string messageTrace(const std::string &message)
バイナリを16進数の文字列に変換
Definition message.cc:16
Definition arg.h:14
ValType
引数や戻り値の型を表すenum
Definition val_adaptor.h:21
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117