| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* @file set_data.cpp |
| 3 |
|
|
* @author Vincent Berenz |
| 4 |
|
|
* @license License BSD-3-Clause |
| 5 |
|
|
* @copyright Copyright (c) 2019, New York University and Max Planck |
| 6 |
|
|
* Gesellschaft. |
| 7 |
|
|
* @date 2019-05-22 |
| 8 |
|
|
* |
| 9 |
|
|
* @brief shows how to serialize an instance of a class with an eigen matrix |
| 10 |
|
|
* as attribute |
| 11 |
|
|
*/ |
| 12 |
|
|
|
| 13 |
|
|
#include <iostream> |
| 14 |
|
|
#include "shared_memory/serializer.hpp" |
| 15 |
|
|
#include "shared_memory/shared_memory.hpp" |
| 16 |
|
|
|
| 17 |
|
|
template <int SIZE> |
| 18 |
|
|
class Serializable |
| 19 |
|
|
{ |
| 20 |
|
|
typedef Eigen::Matrix<double, SIZE, 1> Vector; |
| 21 |
|
|
|
| 22 |
|
|
public: |
| 23 |
|
✗ |
Serializable() : map_(serialized_v_, SIZE, 1) |
| 24 |
|
|
{ |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
✗ |
void set(int index, double v) |
| 28 |
|
|
{ |
| 29 |
|
✗ |
map_[index] = v; |
| 30 |
|
|
} |
| 31 |
|
|
|
| 32 |
|
✗ |
double get(int index) |
| 33 |
|
|
{ |
| 34 |
|
✗ |
return map_[index]; |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
|
template <class Archive> |
| 38 |
|
✗ |
void serialize(Archive& archive) |
| 39 |
|
|
{ |
| 40 |
|
✗ |
archive(serialized_v_); |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
|
private: |
| 44 |
|
|
friend shared_memory::private_serialization; |
| 45 |
|
|
|
| 46 |
|
|
double serialized_v_[SIZE]; |
| 47 |
|
|
Eigen::Map<Vector> map_; |
| 48 |
|
|
}; |
| 49 |
|
|
|
| 50 |
|
✗ |
int main() |
| 51 |
|
|
{ |
| 52 |
|
|
#define SIZE 100 |
| 53 |
|
|
|
| 54 |
|
✗ |
shared_memory::Serializer<Serializable<SIZE>> serializer; |
| 55 |
|
|
|
| 56 |
|
✗ |
Serializable<SIZE> s1; |
| 57 |
|
✗ |
for (int i = 0; i < SIZE; i++) |
| 58 |
|
|
{ |
| 59 |
|
✗ |
s1.set(i, static_cast<double>(i)); |
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
|
✗ |
std::string serialized_s1 = serializer.serialize(s1); |
| 63 |
|
|
|
| 64 |
|
✗ |
Serializable<SIZE> s2; |
| 65 |
|
✗ |
serializer.deserialize(serialized_s1, s2); |
| 66 |
|
|
|
| 67 |
|
✗ |
for (int i = 0; i < SIZE; i++) |
| 68 |
|
|
{ |
| 69 |
|
✗ |
double d = s2.get(i); |
| 70 |
|
✗ |
std::cout << d << " "; |
| 71 |
|
|
} |
| 72 |
|
✗ |
std::cout << std::endl; |
| 73 |
|
|
} |
| 74 |
|
|
|