Skip to content

Commit

Permalink
OBJECT STREAMED AS RANGE
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 12, 2024
1 parent 876c29e commit ef8ce68
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions nano/lib/object_stream_adapters.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/lib/object_stream.hpp>
#include <nano/lib/utility.hpp>

#include <ostream>
#include <sstream>
Expand All @@ -9,42 +10,64 @@

namespace nano
{
template <class Streamable>
template <class Streamable, class Writer>
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<Streamable> const & self)
friend std::ostream & operator<< (std::ostream & os, object_stream_formatter<Streamable, Writer> 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<Streamable> const & val)
friend auto format_as (object_stream_formatter<Streamable, Writer> const & self)
{
return fmt::streamed (val);
return fmt::streamed (self);
}
};

template <class Streamable>
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 <class Streamable>
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 <class StreamableRange>
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) };
}

/**
Expand Down Expand Up @@ -109,7 +132,7 @@ template <nano::object_or_array_streamable Value>
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 ();
}
}
Expand Down

0 comments on commit ef8ce68

Please sign in to comment.