Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2025 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #pragma once
23 :
24 : #include <optional>
25 :
26 : #include "msgpack_forward.h"
27 : #include "../types/bytearray.h"
28 : #include "../types/string.h"
29 : #include "../types/number.h"
30 : #include "../types/flags.h"
31 : #include "../types/retval.h"
32 : #include "../types/uri.h"
33 : #include "../types/bytearray.h"
34 : #include "../io/path.h"
35 :
36 : void pack_custom(std::vector<uint8_t>& data, const size_t& value);
37 : bool unpack_custom(muse::msgpack::Cursor& cursor, size_t& value);
38 :
39 : template<typename T>
40 : void pack_custom(std::vector<uint8_t>& data, const std::optional<T>& value);
41 : template<typename T>
42 : bool unpack_custom(muse::msgpack::Cursor& cursor, std::optional<T>& value);
43 :
44 : void pack_custom(std::vector<uint8_t>& data, const muse::String& value);
45 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::String& value);
46 :
47 : template<typename T>
48 : void pack_custom(std::vector<uint8_t>& data, const muse::number_t<T>& value);
49 : template<typename T>
50 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::number_t<T>& value);
51 :
52 : template<typename T>
53 : void pack_custom(std::vector<uint8_t>& data, const muse::Flags<T>& value);
54 : template<typename T>
55 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::Flags<T>& value);
56 :
57 : // Ret[Val]
58 : void pack_custom(std::vector<uint8_t>& data, const muse::Ret& value);
59 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::Ret& value);
60 : template<typename T>
61 : void pack_custom(std::vector<uint8_t>& data, const muse::RetVal<T>& value);
62 : template<typename T>
63 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::RetVal<T>& value);
64 : template<typename T1, typename T2>
65 : void pack_custom(std::vector<uint8_t>& data, const muse::RetVal2<T1, T2>& value);
66 : template<typename T1, typename T2>
67 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::RetVal2<T1, T2>& value);
68 :
69 : // Uri
70 : void pack_custom(std::vector<uint8_t>& data, const muse::Uri& value);
71 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::Uri& value);
72 :
73 : // ByteArray
74 : void pack_custom(std::vector<uint8_t>& data, const muse::ByteArray& value);
75 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::ByteArray& value);
76 :
77 : // path_t
78 : void pack_custom(std::vector<uint8_t>& data, const muse::io::path_t& value);
79 : bool unpack_custom(muse::msgpack::Cursor& cursor, muse::io::path_t& value);
80 :
81 : #include "../thirdparty/kors_msgpack/msgpack/msgpack.h"
82 :
83 : // std standart
84 : inline void pack_custom(std::vector<uint8_t>& data, const size_t& value)
85 : {
86 : muse::msgpack::Packer::pack(data, static_cast<uint64_t>(value));
87 : }
88 :
89 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, size_t& value)
90 : {
91 : uint64_t v = 0;
92 : bool ok = muse::msgpack::UnPacker::unpack(cursor, v);
93 : value = static_cast<size_t>(v);
94 : return ok;
95 : }
96 :
97 : template<typename T>
98 6 : inline void pack_custom(std::vector<uint8_t>& data, const std::optional<T>& value)
99 : {
100 6 : bool has_value = value.has_value();
101 6 : muse::msgpack::Packer::pack(data, has_value);
102 6 : if (has_value) {
103 4 : muse::msgpack::Packer::pack(data, value.value());
104 : }
105 6 : }
106 :
107 : template<typename T>
108 6 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, std::optional<T>& value)
109 : {
110 6 : bool has_value = false;
111 6 : bool ok = muse::msgpack::UnPacker::unpack(cursor, has_value);
112 6 : if (!ok) {
113 0 : return false;
114 : }
115 :
116 6 : if (!has_value) {
117 2 : value = std::nullopt;
118 2 : return true;
119 : }
120 :
121 4 : T val = {};
122 4 : ok = muse::msgpack::UnPacker::unpack(cursor, val);
123 4 : if (!ok) {
124 0 : return false;
125 : }
126 :
127 4 : value = std::make_optional<T>(val);
128 4 : return true;
129 2 : }
130 :
131 : // muse standart types
132 14016 : inline void pack_custom(std::vector<uint8_t>& data, const muse::String& value)
133 : {
134 14016 : muse::msgpack::Packer::pack(data, value.toStdString());
135 14016 : }
136 :
137 16 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::String& value)
138 : {
139 16 : std::string str;
140 16 : bool ok = muse::msgpack::UnPacker::unpack(cursor, str);
141 16 : value = muse::String::fromStdString(str);
142 16 : return ok;
143 16 : }
144 :
145 : template<typename T>
146 33 : inline void pack_custom(std::vector<uint8_t>& data, const muse::number_t<T>& value)
147 : {
148 33 : muse::msgpack::Packer::pack(data, value.raw());
149 33 : }
150 :
151 : template<typename T>
152 32 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::number_t<T>& value)
153 : {
154 32 : T val = {};
155 32 : bool ok = muse::msgpack::UnPacker::unpack(cursor, val);
156 32 : value = muse::number_t<T>(val);
157 32 : return ok;
158 : }
159 :
160 : template<typename T>
161 2 : inline void pack_custom(std::vector<uint8_t>& data, const muse::Flags<T>& value)
162 : {
163 2 : typename muse::Flags<T>::Int val = value;
164 2 : muse::msgpack::Packer::pack(data, val);
165 2 : }
166 :
167 : template<typename T>
168 2 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::Flags<T>& value)
169 : {
170 2 : typename muse::Flags<T>::Int val = 0;
171 2 : bool ok = muse::msgpack::UnPacker::unpack(cursor, val);
172 2 : value = muse::Flags<T>(val);
173 2 : return ok;
174 : }
175 :
176 : // Ret[Val]
177 26 : inline void pack_custom(std::vector<uint8_t>& data, const muse::Ret& value)
178 : {
179 26 : muse::msgpack::Packer::pack(data, value.code(), value.text());
180 26 : }
181 :
182 12 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::Ret& value)
183 : {
184 12 : int code = 0;
185 12 : std::string text;
186 12 : bool ok = muse::msgpack::UnPacker::unpack(cursor, code, text);
187 12 : value = muse::Ret(code, text);
188 12 : return ok;
189 12 : }
190 :
191 : template<typename T>
192 6 : inline void pack_custom(std::vector<uint8_t>& data, const muse::RetVal<T>& value)
193 : {
194 6 : muse::msgpack::Packer::pack(data, value.ret, value.val);
195 6 : }
196 :
197 : template<typename T>
198 2 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::RetVal<T>& value)
199 : {
200 2 : return muse::msgpack::UnPacker::unpack(cursor, value.ret, value.val);
201 : }
202 :
203 : template<typename T1, typename T2>
204 0 : inline void pack_custom(std::vector<uint8_t>& data, const muse::RetVal2<T1, T2>& value)
205 : {
206 0 : muse::msgpack::Packer::pack(data, value.ret, value.val1, value.val2);
207 0 : }
208 :
209 : template<typename T1, typename T2>
210 0 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::RetVal2<T1, T2>& value)
211 : {
212 0 : return muse::msgpack::UnPacker::unpack(cursor, value.ret, value.val1, value.val2);
213 : }
214 :
215 : // Uri
216 0 : inline void pack_custom(std::vector<uint8_t>& data, const muse::Uri& value)
217 : {
218 0 : muse::msgpack::Packer::pack(data, value.toString());
219 0 : }
220 :
221 0 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::Uri& value)
222 : {
223 0 : std::string str;
224 0 : bool ok = muse::msgpack::UnPacker::unpack(cursor, str);
225 0 : value = muse::Uri(str);
226 0 : return ok;
227 0 : }
228 :
229 : // ByteArray
230 : inline void pack_custom(std::vector<uint8_t>& data, const muse::ByteArray& value)
231 : {
232 : muse::msgpack::Packer::pack(data, value.constVData());
233 : }
234 :
235 0 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::ByteArray& value)
236 : {
237 0 : return muse::msgpack::UnPacker::unpack(cursor, value.vdata());
238 : }
239 :
240 : // path_t
241 : inline void pack_custom(std::vector<uint8_t>& data, const muse::io::path_t& value)
242 : {
243 : muse::msgpack::Packer::pack(data, value.toStdString());
244 : }
245 :
246 : inline bool unpack_custom(muse::msgpack::Cursor& cursor, muse::io::path_t& value)
247 : {
248 : std::string str;
249 : bool ok = muse::msgpack::UnPacker::unpack(cursor, str);
250 : value = muse::io::path_t(str);
251 : return ok;
252 : }
253 :
254 : // pack / unpack
255 : namespace muse::msgpack {
256 : struct Options {
257 : size_t rezerveSize = 100;
258 : };
259 :
260 : template<class ... Types>
261 123 : static inline void pack(std::vector<uint8_t>& data, const Types&... args)
262 : {
263 123 : Packer::pack(data, args ...);
264 123 : }
265 :
266 : template<class ... Types>
267 4 : static inline ByteArray pack(const Options& opt, const Types&... args)
268 : {
269 4 : ByteArray ba;
270 4 : std::vector<uint8_t>& vdata = ba.vdata();
271 4 : vdata.clear();
272 4 : vdata.reserve(opt.rezerveSize);
273 4 : Packer::pack(vdata, args ...);
274 4 : vdata.emplace_back(0);
275 4 : return ba;
276 0 : }
277 :
278 : template<class ... Types>
279 4 : static inline ByteArray pack(const Types&... args)
280 : {
281 4 : return pack(Options {}, args ...);
282 : }
283 :
284 : template<class ... Types>
285 1 : static inline bool unpack(muse::msgpack::Cursor& cursor, Types&... args)
286 : {
287 1 : return UnPacker::unpack(cursor, args ...);
288 : }
289 :
290 : template<class ... Types>
291 158 : static inline bool unpack(const ByteArray& data, Types&... args)
292 : {
293 158 : return UnPacker::unpack(data.constVData(), args ...);
294 : }
295 : }
|