Added support for saving and loading

This commit is contained in:
Patrick Urbanke
2023-11-12 20:41:15 +01:00
parent 73ddc22063
commit 8d000e9db2
7 changed files with 177 additions and 0 deletions
+5
View File
@@ -1,7 +1,12 @@
#ifndef FLEXBUF_FLEXBUF_HPP_
#define FLEXBUF_FLEXBUF_HPP_
#include "rfl/flexbuf/Parser.hpp"
#include "rfl/flexbuf/Reader.hpp"
#include "rfl/flexbuf/Writer.hpp"
#include "rfl/flexbuf/load.hpp"
#include "rfl/flexbuf/read.hpp"
#include "rfl/flexbuf/save.hpp"
#include "rfl/flexbuf/write.hpp"
#endif
+20
View File
@@ -0,0 +1,20 @@
#ifndef RFL_FLEXBUF_LOAD_HPP_
#define RFL_FLEXBUF_LOAD_HPP_
#include "rfl/Result.hpp"
#include "rfl/flexbuf/read.hpp"
#include "rfl/io/load_bytes.hpp"
namespace rfl {
namespace flexbuf {
template <class T>
Result<T> load(const std::string& _fname) {
const auto read_bytes = [](const auto& _bytes) { return read<T>(_bytes); };
return rfl::io::load_bytes(_fname).and_then(read_bytes);
}
} // namespace flexbuf
} // namespace rfl
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef RFL_FLEXBUF_SAVE_HPP_
#define RFL_FLEXBUF_SAVE_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "rfl/Result.hpp"
#include "rfl/flexbuf/write.hpp"
#include "rfl/io/save_bytes.hpp"
namespace rfl {
namespace flexbuf {
template <class T>
Result<Nothing> save(const std::string& _fname, const T& _obj) {
return rfl::io::save_bytes(_fname, write(_obj));
}
} // namespace flexbuf
} // namespace rfl
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef RFL_IO_LOAD_BYTES_HPP_
#define RFL_IO_LOAD_BYTES_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "rfl/Result.hpp"
namespace rfl {
namespace io {
Result<std::vector<unsigned char>> load_bytes(const std::string& _fname) {
std::ifstream input(_fname, std::ios::binary);
if (input.is_open()) {
std::istreambuf_iterator<char> begin(input), end;
const auto bytes = std::vector<unsigned char>(begin, end);
input.close();
return bytes;
} else {
return rfl::Error("File '" + _fname + "' not found!");
}
}
} // namespace io
} // namespace rfl
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef RFL_IO_SAVE_BYTES_HPP_
#define RFL_IO_SAVE_BYTES_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "rfl/Result.hpp"
namespace rfl {
namespace io {
Result<Nothing> save_bytes(const std::string& _fname,
const std::vector<unsigned char>& _bytes) {
try {
std::ofstream output(_fname, std::ios::out | std::ios::binary);
output.write(reinterpret_cast<const char*>(_bytes.data()), _bytes.size());
output.close();
} catch (std::exception& e) {
return Error(e.what());
}
return Nothing{};
}
} // namespace io
} // namespace rfl
#endif
+68
View File
@@ -0,0 +1,68 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_save_load() {
std::cout << "test_save_load" << std::endl;
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
rfl::Field<"age", Age> age;
rfl::Field<"email", rfl::Email> email;
rfl::Field<"children", std::vector<Person>> children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 10,
.email = "bart@simpson.com",
.children = std::vector<Person>()};
const auto lisa = Person{
.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 8,
.email = "lisa@simpson.com",
.children = rfl::default_value // same as std::vector<Person>()
};
const auto maggie =
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")),
rfl::make_field<"email">(std::string("maggie@simpson.com")),
rfl::make_field<"age">(0));
const auto homer1 =
Person{.first_name = "Homer",
.last_name = "Simpson",
.birthday = "1987-04-19",
.age = 45,
.email = "homer@simpson.com",
.children = std::vector<Person>({bart, lisa, maggie})};
rfl::flexbuf::save("homer.fb", homer1);
const auto homer2 = rfl::flexbuf::load<Person>("homer.fb").value();
const auto bytes1 = rfl::flexbuf::write(homer1);
const auto bytes2 = rfl::flexbuf::write(homer2);
if (bytes1 != bytes2) {
std::cout << "Test failed. Content was not identical." << std::endl
<< std::endl;
return;
}
std::cout << "OK" << std::endl << std::endl;
}
+3
View File
@@ -18,6 +18,7 @@
#include "test_optional_fields.hpp"
#include "test_readme_example.hpp"
#include "test_ref.hpp"
#include "test_save_load.hpp"
#include "test_set.hpp"
#include "test_string_map.hpp"
#include "test_string_unordered_map.hpp"
@@ -66,5 +67,7 @@ int main() {
test_all_of();
test_save_load();
return 0;
}