Skip to content

Commit

Permalink
MOVE CODE
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 24, 2024
1 parent c512830 commit 242a3fd
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 209 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ add_executable(
network.cpp
network_filter.cpp
node.cpp
object_stream.cpp
object_stream.cpp
optimistic_scheduler.cpp
processing_queue.cpp
processor_service.cpp
Expand Down
5 changes: 2 additions & 3 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ add_library(
memory.cpp
numbers.hpp
numbers.cpp
object_stream.hpp
object_stream_adapters.hpp
object_stream_writers.hpp
object_stream.hpp
object_stream_ostream.hpp
observer_set.hpp
optional_ptr.hpp
processing_queue.hpp
Expand Down
205 changes: 203 additions & 2 deletions nano/lib/object_stream.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <nano/lib/object_stream_writers.hpp>

#include <boost/type_index.hpp>

#include <cstdint>
Expand All @@ -16,6 +14,110 @@

namespace nano
{
struct object_stream_config
{
std::string field_begin{ "" };
std::string field_end{ "" };
std::string field_assignment{ ": " };
std::string field_separator{ ", " };

std::string object_begin{ "{ " };
std::string object_end{ " }" };

std::string array_begin{ "[ " };
std::string array_end{ " ]" };

std::string array_element_begin{ "" };
std::string array_element_end{ "" };
std::string array_element_separator{ ", " };

std::string string_begin{ "\"" };
std::string string_end{ "\"" };

std::string true_value{ "true" };
std::string false_value{ "false" };
std::string null_value{ "null" };

/** Number of decimal places to show for `float` and `double` */
int precision{ 2 };

static object_stream_config const & default_config ()
{
static object_stream_config const config{};
return config;
}
};

struct object_stream_context
{
object_stream_config const & config;
std::ostream & os;

explicit object_stream_context (std::ostream & os, object_stream_config const & config = object_stream_config::default_config ()) :
os{ os },
config{ config }
{
}

void begin_field (std::string_view name, bool first)
{
if (!first)
{
os << config.field_separator;
}
os << config.field_begin << name << config.field_assignment;
}

void end_field ()
{
os << config.field_end;
}

void begin_object ()
{
os << config.object_begin;
}

void end_object ()
{
os << config.object_end;
}

void begin_array ()
{
os << config.array_begin;
}

void end_array ()
{
os << config.array_end;
}

void begin_array_element (bool first)
{
if (!first)
{
os << config.array_element_separator;
}
os << config.array_element_begin;
}

void end_array_element ()
{
os << config.array_element_end;
}

void begin_string ()
{
os << config.string_begin;
}

void end_string ()
{
os << config.string_end;
}
};

class object_stream;
class array_stream;

Expand Down Expand Up @@ -370,4 +472,103 @@ inline void stream_as (Value const & value, array_stream & ars)
{
value (ars);
}
}

/*
* Specializations for primitive types
*/

namespace nano
{
inline void stream_as (bool const & value, object_stream_context & ctx)
{
ctx.os << (value ? ctx.config.true_value : ctx.config.false_value);
}

inline void stream_as (const int8_t & value, object_stream_context & ctx)
{
ctx.os << static_cast<uint32_t> (value); // Avoid printing as char
}

inline void stream_as (const uint8_t & value, object_stream_context & ctx)
{
ctx.os << static_cast<uint32_t> (value); // Avoid printing as char
}

inline void stream_as (const int16_t & value, object_stream_context & ctx)
{
ctx.os << value;
}

inline void stream_as (const uint16_t & value, object_stream_context & ctx)
{
ctx.os << value;
}

inline void stream_as (const int32_t & value, object_stream_context & ctx)
{
ctx.os << value;
}

inline void stream_as (const uint32_t & value, object_stream_context & ctx)
{
ctx.os << value;
}

inline void stream_as (const int64_t & value, object_stream_context & ctx)
{
ctx.os << value;
}

inline void stream_as (const uint64_t & value, object_stream_context & ctx)
{
ctx.os << value;
}

inline void stream_as (const float & value, object_stream_context & ctx)
{
ctx.os << std::fixed << std::setprecision (ctx.config.precision) << value;
}

inline void stream_as (const double & value, object_stream_context & ctx)
{
ctx.os << std::fixed << std::setprecision (ctx.config.precision) << value;
}

template <class Opt>
inline void stream_as_optional (const Opt & opt, object_stream_context & ctx)
{
if (opt)
{
stream_as (*opt, ctx);
}
else
{
ctx.os << ctx.config.null_value;
}
}

template <class Value>
inline void stream_as (std::shared_ptr<Value> const & value, object_stream_context & ctx)
{
stream_as_optional (value, ctx);
}

template <class Value>
inline void stream_as (std::unique_ptr<Value> const & value, object_stream_context & ctx)
{
stream_as_optional (value, ctx);
}

template <class Value>
inline void stream_as (std::weak_ptr<Value> const & value, object_stream_context & ctx)
{
stream_as_optional (value.lock (), ctx);
}

template <class Value>
inline void stream_as (std::optional<Value> const & value, object_stream_context & ctx)
{
stream_as_optional (value, ctx);
}
}
File renamed without changes.
Loading

0 comments on commit 242a3fd

Please sign in to comment.