From ef8ce6817516f1977cf68dd6b96160eeb3dd11b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 12 Mar 2024 17:20:09 +0100 Subject: [PATCH] OBJECT STREAMED AS RANGE --- nano/lib/object_stream_adapters.hpp | 49 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/nano/lib/object_stream_adapters.hpp b/nano/lib/object_stream_adapters.hpp index 8be5b445f4..29e372c86d 100644 --- a/nano/lib/object_stream_adapters.hpp +++ b/nano/lib/object_stream_adapters.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -9,42 +10,64 @@ namespace nano { -template +template struct object_stream_formatter { nano::object_stream_config const & config; Streamable const & value; + Writer writer; - explicit object_stream_formatter (Streamable const & value, nano::object_stream_config const & config) : + explicit object_stream_formatter (Streamable const & value, Writer writer, nano::object_stream_config const & config) : config{ config }, - value{ value } + value{ value }, + writer{ writer } { } - friend std::ostream & operator<< (std::ostream & os, object_stream_formatter const & self) + friend std::ostream & operator<< (std::ostream & os, object_stream_formatter const & self) { nano::root_object_stream obs{ os, self.config }; - obs.write (self.value); + self.writer (self.value, obs); return os; } // Needed for fmt formatting, uses the ostream operator under the hood - friend auto format_as (object_stream_formatter const & val) + friend auto format_as (object_stream_formatter const & self) { - return fmt::streamed (val); + return fmt::streamed (self); } }; -template -auto streamed (Streamable const & value) +enum class streamed_format +{ + basic, + json +}; + +inline nano::object_stream_config const & to_object_stream_config (streamed_format format) { - return object_stream_formatter{ value, nano::object_stream_config::default_config () }; + switch (format) + { + case streamed_format::basic: + return nano::object_stream_config::default_config (); + case streamed_format::json: + return nano::object_stream_config::json_config (); + default: + debug_assert (false); + return nano::object_stream_config::default_config (); + } } template -auto streamed_as_json (Streamable const & value) +auto streamed (Streamable const & value, streamed_format format = streamed_format::basic) +{ + return object_stream_formatter{ value, [] (auto const & value, nano::root_object_stream & obs) { obs.write (value); }, to_object_stream_config (format) }; +} + +template +auto streamed_range (StreamableRange const & value, streamed_format format = streamed_format::basic) { - return object_stream_formatter{ value, nano::object_stream_config::json_config () }; + return object_stream_formatter{ value, [] (auto const & value, nano::root_object_stream & obs) { obs.write_range (value); }, to_object_stream_config (format) }; } /** @@ -109,7 +132,7 @@ template std::string to_json (Value const & value) { std::stringstream ss; - ss << nano::streamed_as_json (value); + ss << nano::streamed (value, nano::streamed_format::json); return ss.str (); } }