44#include < map>
55#include < memory>
66#include < optional>
7+ #include < string>
78
89#include " rtbot/Operator.h"
910
@@ -13,41 +14,46 @@ using namespace std;
1314
1415struct Program {
1516 private:
17+ string program_json;
1618 map<string, Op_ptr<uint64_t , double >> all_op; // from id to operator
1719 string entryOperator; // id of the entry operator
1820 map<string, vector<string>> outputFilter;
1921
2022 public:
2123 explicit Program (string const & json_string);
24+ explicit Program (Bytes const & bytes) {
25+ // create an iterator
26+ Bytes::const_iterator bytes_it = bytes.begin ();
27+ // read the size of the program json
28+ uint64_t size = *reinterpret_cast <const uint64_t *>(&(*bytes_it));
29+ bytes_it += sizeof (size);
30+ // read the program json
31+ program_json = string (bytes_it, bytes_it + size);
32+ bytes_it += size;
33+
34+ // init the program
35+ init ();
36+
37+ // load the state of the operators
38+ while (bytes_it != bytes.end ()) {
39+ // read the size of the operator id
40+ size = *reinterpret_cast <const uint64_t *>(&(*bytes_it));
41+ bytes_it += sizeof (size);
42+ string opId (bytes_it, bytes_it + size);
43+ bytes_it += size;
44+
45+ // read the operator state
46+ all_op.at (opId)->restore (bytes_it);
47+ }
48+ }
2249
2350 Program (Program const &) = delete ;
2451 void operator =(Program const &) = delete ;
2552 Program (Program&& other) = default ;
2653
27- Bytes collect () {
28- Bytes bytes;
29- for (auto it = this ->all_op .begin (); it != this ->all_op .end (); ++it) {
30- Bytes opBytes = it->second ->collect ();
31- bytes.insert (bytes.end (), opBytes.begin (), opBytes.end ());
32- }
33-
34- return bytes;
35- }
36-
37- void restore (Bytes const & bytes) {
38- Bytes::const_iterator bytes_it = bytes.begin ();
39- for (auto it = this ->all_op .begin (); it != this ->all_op .end (); ++it) {
40- it->second ->restore (bytes_it);
41- }
42- }
54+ void init ();
4355
44- string debug () {
45- string toReturn;
46- for (auto it = this ->all_op .begin (); it != this ->all_op .end (); ++it) {
47- toReturn += " \n " + it->second ->debug (" " );
48- }
49- return toReturn;
50- }
56+ Bytes serialize ();
5157
5258 string getProgramEntryOperatorId () { return entryOperator; }
5359
0 commit comments