From 6b857e1104ff49e2873accb18741498fcfe4621e Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 19 Nov 2023 12:34:06 +0100 Subject: [PATCH] Added support for reading and writing into streams --- README.md | 4 +++- docs/flexbuffers.md | 19 +++++++++++++++++++ docs/json.md | 17 +++++++++++++++++ include/rfl/flexbuf/read.hpp | 13 +++++++++++-- include/rfl/flexbuf/save.hpp | 5 ++++- include/rfl/flexbuf/write.hpp | 25 ++++++++++++++++++++----- include/rfl/io/load_bytes.hpp | 4 ++-- include/rfl/io/load_string.hpp | 20 ++++++++++---------- include/rfl/io/save_bytes.hpp | 7 ++++--- include/rfl/io/save_string.hpp | 23 ++++++++++++----------- include/rfl/json/read.hpp | 21 +++++++++++++++------ include/rfl/json/save.hpp | 5 ++++- include/rfl/json/write.hpp | 32 +++++++++++++++++++++++--------- 13 files changed, 144 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index e5bb7b53..b16b297d 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,10 @@ const auto homer = // We can now transform this into a JSON string. const std::string json_string = rfl::json::write(homer); - std::cout << json_string << std::endl; + +// We can also directly write into the stream. +rfl::json::write(homer, std::cout) << std::endl; ``` This results in the following JSON string: diff --git a/docs/flexbuffers.md b/docs/flexbuffers.md index f1ec40fe..c9386f2c 100644 --- a/docs/flexbuffers.md +++ b/docs/flexbuffers.md @@ -244,6 +244,25 @@ const auto person = Person{...}; rfl::flexbuf::save("/path/to/file.fb", person); ``` +## Reading from and writing into streams + +You can also read from and write into any `std::istream` and `std::ostream` respectively. + +```cpp +const rfl::Result result = rfl::flexbuf::read(my_istream); + +const auto person = Person{...}; +rfl::flexbuf::write(person, my_ostream); +``` + +Note that `std::cout` is also an ostream, so this works as well: + +```cpp +rfl::flexbuf::write(person, std::cout) << std::endl; +``` + +(Since flexbuffers is a binary format, the readability of this will be limited, but it might be useful for debugging). + ## Custom constructors One of the great things about C++ is that it gives you control over diff --git a/docs/json.md b/docs/json.md index cc24e5f0..3b21930e 100644 --- a/docs/json.md +++ b/docs/json.md @@ -39,6 +39,23 @@ const auto person = Person{...}; rfl::json::save("/path/to/file.json", person); ``` +## Reading from and writing into streams + +You can also read from and write into any `std::istream` and `std::ostream` respectively. + +```cpp +const rfl::Result result = rfl::json::read(my_istream); + +const auto person = Person{...}; +rfl::json::write(person, my_ostream); +``` + +Note that `std::cout` is also an ostream, so this works as well: + +```cpp +rfl::json::write(person, std::cout) << std::endl; +``` + ## Custom constructors One of the great things about C++ is that it gives you control over diff --git a/include/rfl/flexbuf/read.hpp b/include/rfl/flexbuf/read.hpp index d84500d3..8b64e2fa 100644 --- a/include/rfl/flexbuf/read.hpp +++ b/include/rfl/flexbuf/read.hpp @@ -3,6 +3,7 @@ #include +#include #include #include "rfl/Result.hpp" @@ -22,7 +23,7 @@ Result read(const InputVarType& _obj) { /// Parses an object from flexbuf using reflection. template -Result read(const unsigned char* _bytes, const size_t _size) { +Result read(const char* _bytes, const size_t _size) { const InputVarType root = flexbuffers::GetRoot(reinterpret_cast(_bytes), _size); return read(root); @@ -30,10 +31,18 @@ Result read(const unsigned char* _bytes, const size_t _size) { /// Parses an object from flexbuf using reflection. template -Result read(const std::vector& _bytes) { +Result read(const std::vector& _bytes) { return read(_bytes.data(), _bytes.size()); } +/// Parses an object directly from a stream. +template +Result read(std::istream& _stream) { + std::istreambuf_iterator begin(_stream), end; + const auto bytes = std::vector(begin, end); + return read(bytes.data(), bytes.size()); +} + } // namespace flexbuf } // namespace rfl diff --git a/include/rfl/flexbuf/save.hpp b/include/rfl/flexbuf/save.hpp index 8d2aa6d6..ccce4d2a 100644 --- a/include/rfl/flexbuf/save.hpp +++ b/include/rfl/flexbuf/save.hpp @@ -14,7 +14,10 @@ namespace flexbuf { template Result save(const std::string& _fname, const T& _obj) { - return rfl::io::save_bytes(_fname, write(_obj)); + const auto write_func = [](const auto& _obj, auto& _stream) -> auto& { + return write(_obj, _stream); + }; + return rfl::io::save_bytes(_fname, _obj, write_func); } } // namespace flexbuf diff --git a/include/rfl/flexbuf/write.hpp b/include/rfl/flexbuf/write.hpp index e23f9244..01b15c21 100644 --- a/include/rfl/flexbuf/write.hpp +++ b/include/rfl/flexbuf/write.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -12,17 +13,31 @@ namespace rfl { namespace flexbuf { -/// Writes an object to flexbuf. template -std::vector write(const T& _obj) { +std::vector to_buffer(const T& _obj) { auto w = Writer(); const auto flexbuf_obj = Parser::write(w, _obj); flexbuffers::Builder fbb; flexbuf_obj->insert(std::nullopt, &fbb); fbb.Finish(); - const auto vec = fbb.GetBuffer(); - const auto data = reinterpret_cast(vec.data()); - return std::vector(data, data + vec.size()); + return fbb.GetBuffer(); +} + +/// Writes an object to flexbuf. +template +std::vector write(const T& _obj) { + const auto buffer = to_buffer(_obj); + const auto data = reinterpret_cast(buffer.data()); + return std::vector(data, data + buffer.size()); +} + +/// Writes an object to an ostream. +template +std::ostream& write(const T& _obj, std::ostream& _stream) { + const auto buffer = to_buffer(_obj); + const auto data = reinterpret_cast(buffer.data()); + _stream.write(data, buffer.size()); + return _stream; } } // namespace flexbuf diff --git a/include/rfl/io/load_bytes.hpp b/include/rfl/io/load_bytes.hpp index bb945094..7a3415b8 100644 --- a/include/rfl/io/load_bytes.hpp +++ b/include/rfl/io/load_bytes.hpp @@ -11,11 +11,11 @@ namespace rfl { namespace io { -Result> load_bytes(const std::string& _fname) { +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); + const auto bytes = std::vector(begin, end); input.close(); return bytes; } else { diff --git a/include/rfl/io/load_string.hpp b/include/rfl/io/load_string.hpp index 4a8c28d0..ecb2c0c0 100644 --- a/include/rfl/io/load_string.hpp +++ b/include/rfl/io/load_string.hpp @@ -11,16 +11,16 @@ namespace rfl { namespace io { Result load_string(const std::string& _fname) { - std::ifstream infile(_fname); - if (infile.is_open()) { - auto r = std::string(std::istreambuf_iterator(infile), - std::istreambuf_iterator()); - infile.close(); - return r; - } else { - return Error("Unable to open file '" + _fname + - "' or file could not be found."); - } + std::ifstream infile(_fname); + if (infile.is_open()) { + auto r = std::string(std::istreambuf_iterator(infile), + std::istreambuf_iterator()); + infile.close(); + return r; + } else { + return Error("Unable to open file '" + _fname + + "' or file could not be found."); + } } } // namespace io diff --git a/include/rfl/io/save_bytes.hpp b/include/rfl/io/save_bytes.hpp index e97af1a3..2b1926c6 100644 --- a/include/rfl/io/save_bytes.hpp +++ b/include/rfl/io/save_bytes.hpp @@ -11,11 +11,12 @@ namespace rfl { namespace io { -Result save_bytes(const std::string& _fname, - const std::vector& _bytes) { +template +Result save_bytes(const std::string& _fname, const T& _obj, + const WriteFunction& _write) { try { std::ofstream output(_fname, std::ios::out | std::ios::binary); - output.write(reinterpret_cast(_bytes.data()), _bytes.size()); + _write(_obj, output); output.close(); } catch (std::exception& e) { return Error(e.what()); diff --git a/include/rfl/io/save_string.hpp b/include/rfl/io/save_string.hpp index dcef60ed..5e51f6e4 100644 --- a/include/rfl/io/save_string.hpp +++ b/include/rfl/io/save_string.hpp @@ -10,17 +10,18 @@ namespace rfl { namespace io { -Result save_string(const std::string& _fname, - const std::string& _str) { - try { - std::ofstream outfile; - outfile.open(_fname); - outfile << _str; - outfile.close(); - } catch (std::exception& e) { - return Error(e.what()); - } - return Nothing{}; +template +Result save_string(const std::string& _fname, const T& _obj, + const WriteFunction& _write) { + try { + std::ofstream outfile; + outfile.open(_fname); + _write(_obj, outfile); + outfile.close(); + } catch (std::exception& e) { + return Error(e.what()); + } + return Nothing{}; } } // namespace io diff --git a/include/rfl/json/read.hpp b/include/rfl/json/read.hpp index 10f67666..06ec8211 100644 --- a/include/rfl/json/read.hpp +++ b/include/rfl/json/read.hpp @@ -3,6 +3,7 @@ #include +#include #include #include "rfl/json/Parser.hpp" @@ -17,17 +18,25 @@ using InputVarType = typename Reader::InputVarType; /// Parses an object from a JSON var. template Result read(const InputVarType& _obj) { - const auto r = Reader(); - return Parser::read(r, _obj); + const auto r = Reader(); + return Parser::read(r, _obj); } /// Parses an object from JSON using reflection. template Result read(const std::string& _json_str) { - using PtrType = std::unique_ptr; - yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0); - const auto ptr = PtrType(doc, yyjson_doc_free); - return read(InputVarType(yyjson_doc_get_root(doc))); + using PtrType = std::unique_ptr; + yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0); + const auto ptr = PtrType(doc, yyjson_doc_free); + return read(InputVarType(yyjson_doc_get_root(doc))); +} + +/// Parses an object from a stringstream. +template +Result read(std::istream& _stream) { + const auto json_str = std::string(std::istreambuf_iterator(_stream), + std::istreambuf_iterator()); + return read(json_str); } } // namespace json diff --git a/include/rfl/json/save.hpp b/include/rfl/json/save.hpp index 433e159e..77501468 100644 --- a/include/rfl/json/save.hpp +++ b/include/rfl/json/save.hpp @@ -14,7 +14,10 @@ namespace json { template Result save(const std::string& _fname, const T& _obj) { - return rfl::io::save_string(_fname, write(_obj)); + const auto write_func = [](const auto& _obj, auto& _stream) -> auto& { + return write(_obj, _stream); + }; + return rfl::io::save_string(_fname, _obj, write_func); } } // namespace json diff --git a/include/rfl/json/write.hpp b/include/rfl/json/write.hpp index 91d70d81..a056e571 100644 --- a/include/rfl/json/write.hpp +++ b/include/rfl/json/write.hpp @@ -3,6 +3,7 @@ #include +#include #include #include @@ -11,17 +12,30 @@ namespace rfl { namespace json { -/// Parses an object to JSON. +/// Returns a JSON string. template std::string write(const T& _obj) { - auto w = Writer(yyjson_mut_doc_new(NULL)); - const auto json_obj = Parser::write(w, _obj); - yyjson_mut_doc_set_root(w.doc_, json_obj.val_); - const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL); - const auto json_str = std::string(json_c_str); - free((void*)json_c_str); - yyjson_mut_doc_free(w.doc_); - return json_str; + auto w = Writer(yyjson_mut_doc_new(NULL)); + const auto json_obj = Parser::write(w, _obj); + yyjson_mut_doc_set_root(w.doc_, json_obj.val_); + const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL); + const auto json_str = std::string(json_c_str); + free((void*)json_c_str); + yyjson_mut_doc_free(w.doc_); + return json_str; +} + +/// Writes a JSON into an ostream. +template +std::ostream& write(const T& _obj, std::ostream& _stream) { + auto w = Writer(yyjson_mut_doc_new(NULL)); + const auto json_obj = Parser::write(w, _obj); + yyjson_mut_doc_set_root(w.doc_, json_obj.val_); + const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL); + _stream << json_c_str; + free((void*)json_c_str); + yyjson_mut_doc_free(w.doc_); + return _stream; } } // namespace json