From 8d000e9db233521b96b068b4948bacbc0b33342d Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 12 Nov 2023 20:41:15 +0100 Subject: [PATCH] Added support for saving and loading --- include/rfl/flexbuf.hpp | 5 ++ include/rfl/flexbuf/load.hpp | 20 ++++++++ include/rfl/flexbuf/save.hpp | 23 ++++++++++ include/rfl/io/load_bytes.hpp | 29 ++++++++++++ include/rfl/io/save_bytes.hpp | 29 ++++++++++++ tests/flexbuffers/test_save_load.hpp | 68 ++++++++++++++++++++++++++++ tests/flexbuffers/tests.cpp | 3 ++ 7 files changed, 177 insertions(+) create mode 100644 include/rfl/flexbuf/load.hpp create mode 100644 include/rfl/flexbuf/save.hpp create mode 100644 include/rfl/io/load_bytes.hpp create mode 100644 include/rfl/io/save_bytes.hpp create mode 100644 tests/flexbuffers/test_save_load.hpp diff --git a/include/rfl/flexbuf.hpp b/include/rfl/flexbuf.hpp index 7187eb2f..fe201173 100644 --- a/include/rfl/flexbuf.hpp +++ b/include/rfl/flexbuf.hpp @@ -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 diff --git a/include/rfl/flexbuf/load.hpp b/include/rfl/flexbuf/load.hpp new file mode 100644 index 00000000..bbe1f5ec --- /dev/null +++ b/include/rfl/flexbuf/load.hpp @@ -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 +Result load(const std::string& _fname) { + const auto read_bytes = [](const auto& _bytes) { return read(_bytes); }; + return rfl::io::load_bytes(_fname).and_then(read_bytes); +} + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/flexbuf/save.hpp b/include/rfl/flexbuf/save.hpp new file mode 100644 index 00000000..8d2aa6d6 --- /dev/null +++ b/include/rfl/flexbuf/save.hpp @@ -0,0 +1,23 @@ +#ifndef RFL_FLEXBUF_SAVE_HPP_ +#define RFL_FLEXBUF_SAVE_HPP_ + +#include +#include +#include + +#include "rfl/Result.hpp" +#include "rfl/flexbuf/write.hpp" +#include "rfl/io/save_bytes.hpp" + +namespace rfl { +namespace flexbuf { + +template +Result save(const std::string& _fname, const T& _obj) { + return rfl::io::save_bytes(_fname, write(_obj)); +} + +} // namespace flexbuf +} // namespace rfl + +#endif diff --git a/include/rfl/io/load_bytes.hpp b/include/rfl/io/load_bytes.hpp new file mode 100644 index 00000000..bb945094 --- /dev/null +++ b/include/rfl/io/load_bytes.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_IO_LOAD_BYTES_HPP_ +#define RFL_IO_LOAD_BYTES_HPP_ + +#include +#include +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace io { + +Result> load_bytes(const std::string& _fname) { + std::ifstream input(_fname, std::ios::binary); + if (input.is_open()) { + std::istreambuf_iterator begin(input), end; + const auto bytes = std::vector(begin, end); + input.close(); + return bytes; + } else { + return rfl::Error("File '" + _fname + "' not found!"); + } +} + +} // namespace io +} // namespace rfl + +#endif diff --git a/include/rfl/io/save_bytes.hpp b/include/rfl/io/save_bytes.hpp new file mode 100644 index 00000000..e97af1a3 --- /dev/null +++ b/include/rfl/io/save_bytes.hpp @@ -0,0 +1,29 @@ +#ifndef RFL_IO_SAVE_BYTES_HPP_ +#define RFL_IO_SAVE_BYTES_HPP_ + +#include +#include +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace io { + +Result save_bytes(const std::string& _fname, + const std::vector& _bytes) { + try { + std::ofstream output(_fname, std::ios::out | std::ios::binary); + output.write(reinterpret_cast(_bytes.data()), _bytes.size()); + output.close(); + } catch (std::exception& e) { + return Error(e.what()); + } + return Nothing{}; +} + +} // namespace io +} // namespace rfl + +#endif diff --git a/tests/flexbuffers/test_save_load.hpp b/tests/flexbuffers/test_save_load.hpp new file mode 100644 index 00000000..505c1c55 --- /dev/null +++ b/tests/flexbuffers/test_save_load.hpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_save_load() { + std::cout << "test_save_load" << std::endl; + + using Age = rfl::Validator, 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> children; + }; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + 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() + }; + + 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({bart, lisa, maggie})}; + + rfl::flexbuf::save("homer.fb", homer1); + + const auto homer2 = rfl::flexbuf::load("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; +} diff --git a/tests/flexbuffers/tests.cpp b/tests/flexbuffers/tests.cpp index 24824bb3..0b2bc4a7 100644 --- a/tests/flexbuffers/tests.cpp +++ b/tests/flexbuffers/tests.cpp @@ -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; }