Zakero's C++ Header Libraries
A collection of reusable C++ libraries
Classes | Macros
Zakero_MessagePack.h File Reference

Zakero MessagePack. More...

Include dependency graph for Zakero_MessagePack.h:

Go to the source code of this file.

Classes

class  zakero::MessagePack
 Object and Data Serialization. More...
 

Macros

#define ZAKERO_DISABLE_IMPLICIT_CASTS(func_name_)
 Don't allow implicit parameter conversion. More...
 
#define ZAKERO_MESSAGEPACK_IMPLEMENTATION
 Activate the implementation code. More...
 

Format Value Masks

#define X(type_, id_, mask_, text_)
 Convert ZAKERO_MESSAGEPACK__FORMAT_DATA into code.
 
#define X(type_, id_, mask_, text_)
 Convert ZAKERO_MESSAGEPACK__FORMAT_DATA into code.
 
#define X(type_, id_, mask_, text_)
 Convert ZAKERO_MESSAGEPACK__FORMAT_DATA into code.
 

Detailed Description

API Dependencies TL;DR What Is It? Why Use It? How To Use It? Version

The Zakero_MessagePack will serialize and deserialize data using the MessagePack specification.

Dependencies
  • None

TL;DR:

This library makes it very easy to use the MessagePack data format.

To use:

  1. Add the implementation to a source code file:
    #define ZAKERO_MESSAGEPACK_IMPLEMENTATION
    #include "Zakero_MessagePack.h"
  2. Add the library to where it is used:
    Zakero MessagePack.

What Is It?
The Zakero_MessagePack library provides a way to serialize data for storage or transport over a network. Deserialization is also available so that the data may be accessed. The MessagePack specification provides a format that allows many different types of data to be packed with very little overhead.

Why Use It?

There are many libraries available to do the same thing, however the Zakero_MessagePack library offers the following:

Benefits

  • Single Header Library
  • Small compile size
  • Easy to add data
  • Access to existing data
  • Modify existing data

Draw Backs

  • Memory Usage: Serialization makes a copy of the contents
  • No C++ iterators, must use traditional C-Style loops

Instead of attempting to interface with other libraries or provide a dynamic interface, basic C++ types are used. It is expected that this approach will make using the library with other code bases easier and less volatile.

How To Use It?

Step 0

Your compiler must support at least the C++20 standard. The location of the Zakero_*.h header files must be in your compiler's include path.

Step 1

The first step is to select which C++ source code file will contain the Zakero MessagePack implementation. Once the location has been determined, add the following to that file:

#define ZAKERO_MESSAGEPACK_IMPLEMENTATION
#include "Zakero_MessagePack.h"

The macro ZAKERO_MESSAGEPACK_IMPLEMENTATION tells the header file to include the implementation of the MessagePack.

In all other files that will use the MessagePack, they need to include the header.

Step 2

After creating an instance of MessagePack, it needs to be populated with data. There are two options to do this:

Add data manually

zakero::MessagePack message_pack;
size_t text;
message_pack.append(uint64_t(42));
message_pack.append(true);
message_pack.append("Hello, World!", &text);
Object and Data Serialization.
Definition: Zakero_MessagePack.h:221
void append(const bool, size_t *const =nullptr) noexcept
Append a boolean value.
Definition: Zakero_MessagePack.h:511

Deserialize data

std::vector<uint8_t> data = load_data();
zakero::MessagePack message_pack;
message_pack.deserialize(data);
void deserialize(const std::vector< uint8_t > &) noexcept
Deserialize MessagePack data.
Definition: Zakero_MessagePack.h:2148

The data in the MessagePack can be modified:

message_pack.object(text).string = "Good Bye!";
Object & object(const size_t index) noexcept
Access a data object.
Definition: Zakero_MessagePack.h:2081

Then the MessagePack can be (re)serialized:

data = message_pack.serialize();
save_data(data);
std::vector< uint8_t > serialize() noexcept
Serialize MessagePack data.
Definition: Zakero_MessagePack.h:2387

Version
v0.1.0
  • The initial implementation
Author
Andrew "Zakero" Moore
  • Original Author

Macro Definition Documentation

◆ ZAKERO_DISABLE_IMPLICIT_CASTS

#define ZAKERO_DISABLE_IMPLICIT_CASTS (   func_name_)

When passing a value to a function's parameter which does not have a matching type, the compiler will try to inject code to convert the value into the function's expected type. Usually, this is fine. But in some instances can lead to very subtle bugs. Placing the function name in this macro will prevent the compiler from doing this automatic type conversion.

Parameters
func_name_The name of the function.
Todo:
Move to Zakero_Base

◆ ZAKERO_MESSAGEPACK_IMPLEMENTATION

#define ZAKERO_MESSAGEPACK_IMPLEMENTATION

Defining this macro will cause the zakero::MessagePack implementation to be included. This should only be done once, since compiler and/or linker errors will typically be generated if more than a single implementation is found.

Note
It does not matter if the macro is given a value or not, only its existence is checked.