@@ -142,24 +142,28 @@ namespace msgpack23 {
142
142
data_.emplace_back (static_cast <std::byte>(std::to_underlying (value)));
143
143
}
144
144
145
- template <typename T>
146
- requires std::is_integral_v<T>
145
+ template <std::integral T>
147
146
void emplace_integral (T const &value) {
148
147
auto const serialize_value = to_big_endian (value);
149
148
auto const bytes = std::bit_cast<std::array<std::byte, sizeof (serialize_value)> >(serialize_value);
150
149
data_.insert (data_.end (), bytes.begin (), bytes.end ());
151
150
}
152
151
152
+ template <std::integral T>
153
+ void emplace_combined (FormatConstants const &constant, T const &value) {
154
+ data_.reserve (1 + sizeof (T));
155
+ emplace_constant (constant);
156
+ emplace_integral<T>(value);
157
+ }
158
+
153
159
[[nodiscard]] bool pack_map_header (std::size_t const n) {
154
160
if (n < 16 ) {
155
161
constexpr auto size_mask = static_cast <std::byte>(0b10000000 );
156
162
data_.emplace_back (static_cast <std::byte>(n) | size_mask);
157
163
} else if (n < std::numeric_limits<std::uint16_t >::max ()) {
158
- emplace_constant (FormatConstants::map16);
159
- emplace_integral (static_cast <std::uint16_t >(n));
164
+ emplace_combined (FormatConstants::map16, static_cast <std::uint16_t >(n));
160
165
} else if (n < std::numeric_limits<std::uint32_t >::max ()) {
161
- emplace_constant (FormatConstants::map32);
162
- emplace_integral (static_cast <std::uint32_t >(n));
166
+ emplace_combined (FormatConstants::map32, static_cast <std::uint32_t >(n));
163
167
} else {
164
168
return false ;
165
169
}
@@ -171,11 +175,9 @@ namespace msgpack23 {
171
175
constexpr auto size_mask = static_cast <std::byte>(0b10010000 );
172
176
data_.emplace_back (static_cast <std::byte>(n) | size_mask);
173
177
} else if (n < std::numeric_limits<std::uint16_t >::max ()) {
174
- emplace_constant (FormatConstants::array16);
175
- emplace_integral (static_cast <std::uint16_t >(n));
178
+ emplace_combined (FormatConstants::array16, static_cast <std::uint16_t >(n));
176
179
} else if (n < std::numeric_limits<std::uint32_t >::max ()) {
177
- emplace_constant (FormatConstants::array32);
178
- emplace_integral (static_cast <std::uint32_t >(n));
180
+ emplace_combined (FormatConstants::array32, static_cast <std::uint32_t >(n));
179
181
} else {
180
182
return false ;
181
183
}
@@ -222,6 +224,7 @@ namespace msgpack23 {
222
224
if (index > 127 ) {
223
225
throw std::overflow_error (" Variant index is to large to be serialized." );
224
226
}
227
+
225
228
if (data.size () == 1 ) {
226
229
emplace_constant (FormatConstants::fixext1);
227
230
} else if (data.size () == 2 ) {
@@ -233,14 +236,11 @@ namespace msgpack23 {
233
236
} else if (data.size () == 16 ) {
234
237
emplace_constant (FormatConstants::fixext16);
235
238
} else if (data.size () < std::numeric_limits<std::uint8_t >::max ()) {
236
- emplace_constant (FormatConstants::ext8);
237
- emplace_integral (static_cast <std::uint8_t >(data.size ()));
239
+ emplace_combined (FormatConstants::ext8, static_cast <std::uint8_t >(data.size ()));
238
240
} else if (data.size () < std::numeric_limits<std::uint16_t >::max ()) {
239
- emplace_constant (FormatConstants::ext16);
240
- emplace_integral (static_cast <std::uint16_t >(data.size ()));
241
+ emplace_combined (FormatConstants::ext16, static_cast <std::uint16_t >(data.size ()));
241
242
} else if (data.size () < std::numeric_limits<std::uint32_t >::max ()) {
242
- emplace_constant (FormatConstants::ext32);
243
- emplace_integral (static_cast <std::uint32_t >(data.size ()));
243
+ emplace_combined (FormatConstants::ext32, static_cast <std::uint32_t >(data.size ()));
244
244
} else {
245
245
throw std::length_error (" Variant is too long to be serialized." );
246
246
}
@@ -318,8 +318,7 @@ namespace msgpack23 {
318
318
) {
319
319
pack_type (static_cast <std::int8_t >(value));
320
320
} else {
321
- emplace_constant (FormatConstants::int16);
322
- emplace_integral (value);
321
+ emplace_combined (FormatConstants::int16, value);
323
322
}
324
323
}
325
324
@@ -331,8 +330,7 @@ namespace msgpack23 {
331
330
) {
332
331
pack_type (static_cast <std::int16_t >(value));
333
332
} else {
334
- emplace_constant (FormatConstants::int32);
335
- emplace_integral (value);
333
+ emplace_combined (FormatConstants::int32, value);
336
334
}
337
335
}
338
336
@@ -344,8 +342,7 @@ namespace msgpack23 {
344
342
) {
345
343
pack_type (static_cast <std::int32_t >(value));
346
344
} else {
347
- emplace_constant (FormatConstants::int64);
348
- emplace_integral (value);
345
+ emplace_combined (FormatConstants::int64, value);
349
346
}
350
347
}
351
348
@@ -362,8 +359,7 @@ namespace msgpack23 {
362
359
template <>
363
360
inline void Packer::pack_type (std::uint16_t const &value) {
364
361
if (value > std::numeric_limits<std::uint8_t >::max ()) {
365
- emplace_constant (FormatConstants::uint16);
366
- emplace_integral (value);
362
+ emplace_combined (FormatConstants::uint16, value);
367
363
} else {
368
364
pack_type (static_cast <std::uint8_t >(value));
369
365
}
@@ -372,8 +368,7 @@ namespace msgpack23 {
372
368
template <>
373
369
inline void Packer::pack_type (std::uint32_t const &value) {
374
370
if (value > std::numeric_limits<std::uint16_t >::max ()) {
375
- emplace_constant (FormatConstants::uint32);
376
- emplace_integral (value);
371
+ emplace_combined (FormatConstants::uint32, value);
377
372
} else {
378
373
pack_type (static_cast <std::uint16_t >(value));
379
374
}
@@ -382,8 +377,7 @@ namespace msgpack23 {
382
377
template <>
383
378
inline void Packer::pack_type (std::uint64_t const &value) {
384
379
if (value > std::numeric_limits<std::uint32_t >::max ()) {
385
- emplace_constant (FormatConstants::uint64);
386
- emplace_integral (value);
380
+ emplace_combined (FormatConstants::uint64, value);
387
381
} else {
388
382
pack_type (static_cast <std::uint32_t >(value));
389
383
}
@@ -405,14 +399,12 @@ namespace msgpack23 {
405
399
406
400
template <>
407
401
inline void Packer::pack_type (float const &value) {
408
- emplace_constant (FormatConstants::float32);
409
- emplace_integral (std::bit_cast<std::uint32_t >(value));
402
+ emplace_combined (FormatConstants::float32, std::bit_cast<std::uint32_t >(value));
410
403
}
411
404
412
405
template <>
413
406
inline void Packer::pack_type (double const &value) {
414
- emplace_constant (FormatConstants::float64);
415
- emplace_integral (std::bit_cast<std::uint64_t >(value));
407
+ emplace_combined (FormatConstants::float64, std::bit_cast<std::uint64_t >(value));
416
408
}
417
409
418
410
template <>
@@ -423,11 +415,9 @@ namespace msgpack23 {
423
415
emplace_constant (FormatConstants::str8);
424
416
data_.emplace_back (static_cast <std::byte>(value.size ()));
425
417
} else if (value.size () < std::numeric_limits<std::uint16_t >::max ()) {
426
- emplace_constant (FormatConstants::str16);
427
- emplace_integral (static_cast <std::uint16_t >(value.size ()));
418
+ emplace_combined (FormatConstants::str16, static_cast <std::uint16_t >(value.size ()));
428
419
} else if (value.size () < std::numeric_limits<std::uint32_t >::max ()) {
429
- emplace_constant (FormatConstants::str32);
430
- emplace_integral (static_cast <std::uint32_t >(value.size ()));
420
+ emplace_combined (FormatConstants::str32, static_cast <std::uint32_t >(value.size ()));
431
421
} else {
432
422
throw std::length_error (" String is too long to be serialized." );
433
423
}
@@ -446,11 +436,9 @@ namespace msgpack23 {
446
436
emplace_constant (FormatConstants::bin8);
447
437
data_.emplace_back (static_cast <std::byte>(value.size ()));
448
438
} else if (value.size () < std::numeric_limits<std::uint16_t >::max ()) {
449
- emplace_constant (FormatConstants::bin16);
450
- emplace_integral (static_cast <std::uint16_t >(value.size ()));
439
+ emplace_combined (FormatConstants::bin16, static_cast <std::uint16_t >(value.size ()));
451
440
} else if (value.size () < std::numeric_limits<std::uint32_t >::max ()) {
452
- emplace_constant (FormatConstants::bin32);
453
- emplace_integral (static_cast <std::uint32_t >(value.size ()));
441
+ emplace_combined (FormatConstants::bin32, static_cast <std::uint32_t >(value.size ()));
454
442
} else {
455
443
throw std::length_error (" Vector is too long to be serialized." );
456
444
}
0 commit comments