RIT VEXU Core API
Loading...
Searching...
No Matches
serializer.h
1#pragma once
2#include <algorithm>
3#include <map>
4#include <stdio.h>
5#include <string>
6#include <vector>
7#include <vex.h>
8
10const char serialization_separator = '$';
12const std::size_t MAX_FILE_SIZE = 4096;
13
16private:
17 bool flush_always;
18 std::string filename;
19 std::map<std::string, int> ints;
20 std::map<std::string, bool> bools;
21 std::map<std::string, double> doubles;
22 std::map<std::string, std::string> strings;
23
25 bool read_from_disk();
26
27public:
32 printf("Saving %s\n", filename.c_str());
33 fflush(stdout);
34 }
35
40 explicit Serializer(const std::string &filename, bool flush_always = true)
41 : flush_always(flush_always), filename(filename), ints({}), bools({}), doubles({}), strings({})
42
43 {
44 read_from_disk();
45 }
46
48 void save_to_disk() const;
49
51
55 void set_int(const std::string &name, int i);
56
60 void set_bool(const std::string &name, bool b);
61
65 void set_double(const std::string &name, double d);
66
70 void set_string(const std::string &name, std::string str);
71
74
79 int int_or(const std::string &name, int otherwise);
80
85 bool bool_or(const std::string &name, bool otherwise);
86
91 double double_or(const std::string &name, double otherwise);
92
97 std::string string_or(const std::string &name, std::string otherwise);
98};
Serializes Arbitrary data to a file on the SD Card.
Definition serializer.h:15
double double_or(const std::string &name, double otherwise)
gets a value stored in the serializer. If not, sets the value to otherwise
Definition serializer.cpp:147
void set_bool(const std::string &name, bool b)
sets a bool by the name of name to b. If flush_always == true, this will save to the sd card
Definition serializer.cpp:114
Serializer(const std::string &filename, bool flush_always=true)
create a Serializer
Definition serializer.h:40
bool bool_or(const std::string &name, bool otherwise)
gets a value stored in the serializer. If not, sets the value to otherwise
Definition serializer.cpp:140
void set_double(const std::string &name, double d)
sets a double by the name of name to d. If flush_always == true, this will save to the sd card
Definition serializer.cpp:120
~Serializer()
Save and close upon destruction (bc of vex, this doesnt always get called when the program ends....
Definition serializer.h:30
std::string string_or(const std::string &name, std::string otherwise)
gets a value stored in the serializer. If not, sets the value to otherwise
Definition serializer.cpp:154
int int_or(const std::string &name, int otherwise)
gets a value stored in the serializer. If not found, sets the value to otherwise
Definition serializer.cpp:133
void save_to_disk() const
saves current Serializer state to disk
Definition serializer.cpp:163
void set_int(const std::string &name, int i)
Setters - not saved until save_to_disk is called.
Definition serializer.cpp:108
void set_string(const std::string &name, std::string str)
sets a string by the name of name to s. If flush_always == true, this will save to the sd card
Definition serializer.cpp:126