TinyRobotics
Loading...
Searching...
No Matches
SerializeSTL.h
1#pragma once
2// STL-based serialization for Serializable objects
3#include <iostream>
4#include <string>
5
6namespace tinyrobotics {
7
8/**
9 * @class SerializeSTL
10 * @ingroup serialize
11 * @brief STL-based serialization utility for Serializable objects.
12 *
13 * Provides methods to serialize and deserialize objects using standard C++ streams.
14 * Useful for saving/loading object state in desktop or simulation environments.
15 */
16class SerializeSTL {
17 public:
18 SerializeSTL(std::istream* in, std::ostream* out) : p_in(in), p_out(out) {}
19 SerializeSTL(std::ostream* out) : p_in(nullptr), p_out(out) {}
20 SerializeSTL(std::istream* in) : p_in(in), p_out(nullptr) {}
21
22 size_t print(Serializable& obj) {
23 if (!p_out) return 0;
24 std::string str = obj.toString();
25 (*p_out) << str << std::endl;
26 return str.size();
27 }
28
29 bool read(Serializable& obj) {
30 if (!p_in) return false;
31 std::string str;
32 if (!std::getline(*p_in, str)) return false;
33 return obj.fromString(str);
34 }
35
36 protected:
37 std::istream* p_in = nullptr;
38 std::ostream* p_out = nullptr;
39};
40
41} // namespace tinyrobotics
STL-based serialization utility for Serializable objects.
Definition: SerializeSTL.h:16