| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#pragma once |
| 2 |
|
|
|
| 3 |
|
|
// ensuring cereal is thread safe. |
| 4 |
|
|
// https://uscilab.github.io/cereal/thread_safety.html |
| 5 |
|
|
#define CEREAL_THREAD_SAFE 1 |
| 6 |
|
|
|
| 7 |
|
|
#include <cereal/archives/binary.hpp> |
| 8 |
|
|
#include <cereal/types/array.hpp> |
| 9 |
|
|
#include <cereal/types/vector.hpp> |
| 10 |
|
|
#include <sstream> |
| 11 |
|
|
#include <utility> |
| 12 |
|
|
|
| 13 |
|
|
namespace shared_memory |
| 14 |
|
|
{ |
| 15 |
|
|
typedef cereal::access private_serialization; |
| 16 |
|
|
|
| 17 |
|
|
template <class Serializable> |
| 18 |
|
240 |
class Serializer |
| 19 |
|
|
{ |
| 20 |
|
|
public: |
| 21 |
|
|
/** |
| 22 |
|
|
* @brief serialize an (almost) arbitrary instance to a |
| 23 |
|
|
* string. The method uses cereal internally and the instance |
| 24 |
|
|
* must implement a serialize function. |
| 25 |
|
|
* See for details: |
| 26 |
|
|
* https://uscilab.github.io/cereal/ |
| 27 |
|
|
* Supplementary requirements: |
| 28 |
|
|
* - Serializable must also have a default constructor. |
| 29 |
|
|
* - All instances of Serializable must be of the same size. |
| 30 |
|
|
* (e.g. vectors must be of fixed size) |
| 31 |
|
|
* The generated and returned string is a private member of |
| 32 |
|
|
* the Serializer instance. Successive calls to serialize |
| 33 |
|
|
* overwrite this string. |
| 34 |
|
|
* @param instance to serialize to a string |
| 35 |
|
|
*/ |
| 36 |
|
|
const std::string& serialize(const Serializable& serializable); |
| 37 |
|
|
|
| 38 |
|
|
/** |
| 39 |
|
|
* @brief Restore the instance of serializable based on |
| 40 |
|
|
* the string data, which should have been generated via |
| 41 |
|
|
* the serialize function. |
| 42 |
|
|
* @param the serialized instance |
| 43 |
|
|
* @param instance of Serializable to be restored |
| 44 |
|
|
*/ |
| 45 |
|
|
void deserialize(const std::string& data, Serializable& serializable); |
| 46 |
|
|
|
| 47 |
|
|
public: |
| 48 |
|
|
/** |
| 49 |
|
|
* Returns the serialized size (i.e. the size of the string) |
| 50 |
|
|
* of an instance of Serializable |
| 51 |
|
|
*/ |
| 52 |
|
|
static int serializable_size(); |
| 53 |
|
|
|
| 54 |
|
|
private: |
| 55 |
|
|
std::string data_; |
| 56 |
|
|
}; |
| 57 |
|
|
|
| 58 |
|
|
} // namespace shared_memory |
| 59 |
|
|
|
| 60 |
|
|
#include "shared_memory/serializer.hxx" |
| 61 |
|
|
|