Skip to content

Commit 9c33595

Browse files
author
me
committed
Added serialization of nullptr_t
adding more format enums
1 parent 9091525 commit 9c33595

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

src/msgpack.h

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ namespace msgpackcpp
142142

143143
//----------------------------------------------------------------------------------------------------------------
144144

145+
template<class Stream>
146+
void serialize(Stream& out, std::nullptr_t);
147+
145148
template<class Stream>
146149
void serialize(Stream& out, bool v);
147150

@@ -355,15 +358,31 @@ namespace msgpackcpp
355358

356359
enum msgpack_identifier : uint8_t
357360
{
361+
MSGPACK_NIL = 0xc0,
358362
MSGPACK_FALSE = 0xc2,
359363
MSGPACK_TRUE = 0xc3,
360364
MSGPACK_FIXINT_POS = 0x7f,
365+
MSGPACK_F32 = 0xca,
366+
MSGPACK_F64 = 0xcb,
361367
MSGPACK_U8 = 0xcc,
362-
MSGPACK_U16 = 0xcd
368+
MSGPACK_U16 = 0xcd,
369+
MSGPACK_U32 = 0xce,
370+
MSGPACK_U64 = 0xcf,
371+
MSGPACK_I8 = 0xd0,
372+
MSGPACK_I16 = 0xd1,
373+
MSGPACK_I32 = 0xd2,
374+
MSGPACK_I64 = 0xd3
363375
};
364-
376+
365377
//----------------------------------------------------------------------------------------------------------------
366378

379+
template<class Stream>
380+
inline void serialize(Stream& out, std::nullptr_t)
381+
{
382+
constexpr uint8_t format = MSGPACK_NIL;
383+
out((const char*)&format, 1);
384+
}
385+
367386
template<class Stream>
368387
inline void serialize(Stream& out, bool v)
369388
{
@@ -411,15 +430,15 @@ namespace msgpackcpp
411430
else if (v <= std::numeric_limits<uint32_t>::max())
412431
{
413432
// unsigned 32
414-
constexpr uint8_t format = 0xce;
433+
constexpr uint8_t format = MSGPACK_U32;
415434
const uint32_t v32 = host_to_b32(static_cast<uint32_t>(v));
416435
out((const char*)&format, 1);
417436
out((const char*)&v32, 4);
418437
}
419438
else
420439
{
421440
// unsigned 64
422-
constexpr uint8_t format = 0xcf;
441+
constexpr uint8_t format = MSGPACK_U64;
423442
const uint64_t v64 = host_to_b64(static_cast<uint64_t>(v));
424443
out((const char*)&format, 1);
425444
out((const char*)&v64, 8);
@@ -444,31 +463,31 @@ namespace msgpackcpp
444463
else if (v >= std::numeric_limits<int8_t>::min())
445464
{
446465
// negative - int8
447-
constexpr uint8_t format = 0xd0;
466+
constexpr uint8_t format = MSGPACK_I8;
448467
const int8_t v8 = static_cast<int8_t>(v);
449468
out((const char*)&format, 1);
450469
out((const char*)&v8, 1);
451470
}
452471
else if (v >= std::numeric_limits<int16_t>::min())
453472
{
454473
// negative - int16
455-
constexpr uint8_t format = 0xd1;
474+
constexpr uint8_t format = MSGPACK_I16;
456475
const uint16_t v16 = host_to_b16(bit_cast<uint16_t>(static_cast<int16_t>(v)));
457476
out((const char*)&format, 1);
458477
out((const char*)&v16, 2);
459478
}
460479
else if (v >= std::numeric_limits<int32_t>::min())
461480
{
462481
// negative - int32_t
463-
constexpr uint8_t format = 0xd2;
482+
constexpr uint8_t format = MSGPACK_I32;
464483
const uint32_t v32 = host_to_b32(bit_cast<uint32_t>(static_cast<int32_t>(v)));
465484
out((const char*)&format, 1);
466485
out((const char*)&v32, 4);
467486
}
468487
else
469488
{
470489
// negative - int64_T
471-
constexpr uint8_t format = 0xd3;
490+
constexpr uint8_t format = MSGPACK_I64;
472491
const uint64_t v64 = host_to_b64(bit_cast<uint64_t>(static_cast<int64_t>(v)));
473492
out((const char*)&format, 1);
474493
out((const char*)&v64, 8);
@@ -481,7 +500,7 @@ namespace msgpackcpp
481500
uint8_t format{};
482501
in((char*)&format, 1);
483502

484-
if (format <= 0x7f)
503+
if (format <= MSGPACK_FIXINT_POS)
485504
{
486505
// positive fixint (7-bit positive integer)
487506
v = format;
@@ -491,56 +510,56 @@ namespace msgpackcpp
491510
// negative fixing (5-bit negative integer)
492511
v = bit_cast<int8_t>(format);
493512
}
494-
else if (format == 0xcc)
513+
else if (format == MSGPACK_U8)
495514
{
496515
// unsigned 8
497516
uint8_t tmp{};
498517
in((char*)&tmp, 1);
499518
v = tmp;
500519
}
501-
else if (format == 0xcd)
520+
else if (format == MSGPACK_U16)
502521
{
503522
// unsigned 16
504523
uint16_t tmp{};
505524
in((char*)&tmp, 2);
506525
v = host_to_b16(tmp);
507526
}
508-
else if (format == 0xce)
527+
else if (format == MSGPACK_U32)
509528
{
510529
// unsigned 32
511530
uint32_t tmp{};
512531
in((char*)&tmp, 4);
513532
v = host_to_b32(tmp);
514533
}
515-
else if (format == 0xcf)
534+
else if (format == MSGPACK_U64)
516535
{
517536
// unsigned 64
518537
uint64_t tmp{};
519538
in((char*)&tmp, 8);
520539
v = host_to_b64(tmp);
521540
}
522-
else if (format == 0xd0)
541+
else if (format == MSGPACK_I8)
523542
{
524543
// signed 8
525544
int8_t tmp{};
526545
in((char*)&tmp, 1);
527546
v = tmp;
528547
}
529-
else if (format == 0xd1)
548+
else if (format == MSGPACK_I16)
530549
{
531550
// signed 16
532551
uint16_t tmp{};
533552
in((char*)&tmp, 2);
534553
v = bit_cast<int16_t>(host_to_b16(tmp));
535554
}
536-
else if (format == 0xd2)
555+
else if (format == MSGPACK_I32)
537556
{
538557
// signed 32
539558
uint32_t tmp{};
540559
in((char*)&tmp, 4);
541560
v = bit_cast<int32_t>(host_to_b32(tmp));
542561
}
543-
else if (format == 0xd3)
562+
else if (format == MSGPACK_I64)
544563
{
545564
// signed 64
546565
uint64_t tmp{};
@@ -556,7 +575,7 @@ namespace msgpackcpp
556575
template<class Stream>
557576
inline void serialize(Stream& out, float v)
558577
{
559-
constexpr uint8_t format = 0xca;
578+
constexpr uint8_t format = MSGPACK_F32;
560579
const uint32_t tmp = host_to_b32(bit_cast<uint32_t>(v));
561580
out((const char*)&format, 1);
562581
out((const char*)&tmp, 4);
@@ -565,7 +584,7 @@ namespace msgpackcpp
565584
template<class Stream>
566585
inline void serialize(Stream& out, double v)
567586
{
568-
constexpr uint8_t format = 0xcb;
587+
constexpr uint8_t format = MSGPACK_F64;
569588
const uint64_t tmp = host_to_b64(bit_cast<uint64_t>(v));
570589
out((const char*)&format, 1);
571590
out((const char*)&tmp, 8);
@@ -577,13 +596,13 @@ namespace msgpackcpp
577596
uint8_t format{};
578597
in((char*)&format, 1);
579598

580-
if (format == 0xca)
599+
if (format == MSGPACK_F32)
581600
{
582601
uint32_t tmp{};
583602
in((char*)&tmp, 4);
584603
v = bit_cast<float>(host_to_b32(tmp));
585604
}
586-
else if (format == 0xcb)
605+
else if (format == MSGPACK_F64)
587606
{
588607
uint64_t tmp{};
589608
in((char*)&tmp, 8);

0 commit comments

Comments
 (0)