Async 1.8.0
AsyncMsg.h File Reference

A message packing framework. More...

#include <istream>
#include <ostream>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <limits>
#include <endian.h>
#include <stdint.h>

Go to the source code of this file.

Classes

class  Async::MsgPacker< T >
 
class  Async::MsgPacker< char >
 
class  Async::Packer64< T >
 
class  Async::MsgPacker< uint64_t >
 
class  Async::MsgPacker< int64_t >
 
class  Async::MsgPacker< double >
 
class  Async::Packer32< T >
 
class  Async::MsgPacker< uint32_t >
 
class  Async::MsgPacker< int32_t >
 
class  Async::MsgPacker< float >
 
class  Async::Packer16< T >
 
class  Async::MsgPacker< uint16_t >
 
class  Async::MsgPacker< int16_t >
 
class  Async::Packer8< T >
 
class  Async::MsgPacker< uint8_t >
 
class  Async::MsgPacker< int8_t >
 
class  Async::MsgPacker< std::string >
 
class  Async::MsgPacker< std::vector< I > >
 
class  Async::MsgPacker< std::set< I > >
 
class  Async::MsgPacker< std::map< Tag, Value > >
 
class  Async::MsgPacker< std::array< T, N > >
 
class  Async::MsgPacker< T[N]>
 
class  Async::Msg
 Base class for all messages. More...
 

Namespaces

namespace  Async
 Namespace for the asynchronous programming classes.
 

Macros

#define ASYNC_MSG_DERIVED_FROM(BASE_CLASS)
 Define which class is the baseclass.
 
#define ASYNC_MSG_MEMBERS(...)
 Define which members of the class that should be packed.
 
#define ASYNC_MSG_NO_MEMBERS
 Specify that this class have no members to pack.
 

Detailed Description

A message packing framework.

Author
Tobias Blomberg / SM0SVX
Date
2017-02-25

This is a message packing framework for use when for example sending network messages or storing structured binary data to a file. Messages are defined as classes which can be packed (serialized) and unpacked (deserialized) to/from a stream. This is a simple example class:

class MsgBase : public Async::Msg
{
public:
int a;
std::string str;
};
#define ASYNC_MSG_MEMBERS(...)
Define which members of the class that should be packed.
Definition AsyncMsg.h:214
Base class for all messages.
Definition AsyncMsg.h:688

The most common types may be packed, like number types, std::string, std::vector, std::map. Adding new types are rather simple. This is an example implementing support for the std::pair type:

namespace Async
{
template <typename First, typename Second>
class MsgPacker<std::pair<First, Second> >
{
public:
static bool pack(std::ostream& os, const std::pair<First, Second>& p)
{
return MsgPacker<First>::pack(os, p.first) &&
MsgPacker<Second>::pack(os, p.second);
}
static size_t packedSize(const std::pair<First, Second>& p)
{
return MsgPacker<First>::packedSize(p.first) +
}
static bool unpack(std::istream& is, std::pair<First, Second>& p)
{
return MsgPacker<First>::unpack(is, p.first) &&
return true;
}
};
};
static bool unpack(std::istream &is, T &val)
Definition AsyncMsg.h:265
static bool pack(std::ostream &os, const T &val)
Definition AsyncMsg.h:263
static size_t packedSize(const T &val)
Definition AsyncMsg.h:264
Namespace for the asynchronous programming classes.

Inheritance is also possible. If you want the base class to also be packed when the derived class is packed, use the ASYNC_MSG_DERIVED_FROM macro. The use of the std::pair extension is also demonstrated here.

class MsgDerived : public MsgBase
{
public:
float f;
std::vector<int> vec;
std::pair<int, std::string> p;
ASYNC_MSG_MEMBERS(f, vec, p)
};
#define ASYNC_MSG_DERIVED_FROM(BASE_CLASS)
Define which class is the baseclass.
Definition AsyncMsg.h:193

These classes can be used in the usual way but with the extra property that they can be serialized/deserialized to/from a stream.

MsgDerived d1;
d1.a = 42;
d1.str = "Fourtytwo";
d1.f = 3.14;
d1.vec.push_back(4711);
d1.p.first = 100;
d1.p.second = "THE STRING";
std::stringstream ss;
d1.pack(ss);
MsgDerived d2;
d2.unpack(ss);

For a working example, have a look at the demo application, AsyncMsg_demo.cpp.

Async - A library for programming event driven applications
Copyright (C) 2003-2023 Tobias Blomberg / SM0SVX

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Definition in file AsyncMsg.h.

Macro Definition Documentation

◆ ASYNC_MSG_DERIVED_FROM

#define ASYNC_MSG_DERIVED_FROM ( BASE_CLASS)
Value:
bool packParent(std::ostream& os) const \
{ \
return BASE_CLASS::pack(os); \
} \
size_t packedSizeParent(void) const \
{ \
return BASE_CLASS::packedSize(); \
} \
bool unpackParent(std::istream& is) \
{ \
return BASE_CLASS::unpack(is); \
}

Define which class is the baseclass.

Parameters
BASE_CLASSThe name of the baseclass

Use this macro in a class definition to define which class is the base class. Multiple inheritance is not supported.

Definition at line 193 of file AsyncMsg.h.

◆ ASYNC_MSG_MEMBERS

#define ASYNC_MSG_MEMBERS ( ...)
Value:
bool pack(std::ostream& os) const override \
{ \
return packParent(os) && Msg::pack(os, __VA_ARGS__); \
} \
size_t packedSize(void) const override \
{ \
return packedSizeParent() + Msg::packedSize(__VA_ARGS__); \
} \
bool unpack(std::istream& is) override \
{ \
return unpackParent(is) && Msg::unpack(is, __VA_ARGS__); \
}

Define which members of the class that should be packed.

Use this macro to define which members of the class that should be packed. Variables not listed here will not be included in the serialized version of the class.

Examples
AsyncMsg_demo.cpp.

Definition at line 214 of file AsyncMsg.h.

◆ ASYNC_MSG_NO_MEMBERS

#define ASYNC_MSG_NO_MEMBERS
Value:
bool pack(std::ostream& os) const override \
{ \
return packParent(os); \
} \
size_t packedSize(void) const override { return packedSizeParent(); } \
bool unpack(std::istream& is) override \
{ \
return unpackParent(is); \
}

Specify that this class have no members to pack.

If the class have no members to pack, this macro must be used.

Definition at line 233 of file AsyncMsg.h.