Circus 0.0.1-alpha
C++ Serialization Framework
Loading...
Searching...
No Matches
serializer.hpp
1#pragma once
2#include <filesystem>
3#include <variant>
4
5#include "circus_traits.hpp"
6#include "token.hpp"
7#include "utils/to_chars.hpp"
8
9namespace circus {
14#define CIRCUS_ENTRY(VAR) std::make_pair(#VAR, VAR)
15
25
26template <typename OStreamT>
27 requires(std::is_base_of_v<std::ostream, OStreamT>)
29 OStreamT &stream;
30 using TK = tokens__::TYPE;
31
36 template <std::size_t I, std::size_t Max, traits::OutStreamableLiteral Arg>
37 void handle_value_type(Arg &&arg) {
38 if constexpr (traits::StringLike<Arg>) {
39 stream << tokens__::to_literal(TK::TK_QUOTE_DOUBLE) << arg << tokens__::to_literal(TK::TK_QUOTE_DOUBLE);
40 } else {
41 stream << arg;
42 }
43 stream << tokens__::to_literal(TK::TK_SPACE);
44 }
48 template <std::size_t I, std::size_t Max, traits::Serializable Arg>
49 void handle_value_type(Arg &&arg) {
50 stream << tokens__::to_literal(TK::TK_CURL_L);
51 arg.serialize(*this);
52 stream << tokens__::to_literal(TK::TK_CURL_R);
53 }
57 template <std::size_t I, std::size_t Max, traits::StreamableVector Arg>
58 void handle_value_type(Arg &&arg) {
59 stream << tokens__::to_literal(TK::TK_BRACE_L);
60 for (std::size_t i = 0; i < arg.size(); i++) {
61 stream << arg[i];
62 if (i < arg.size() - 1) {
63 stream << tokens__::to_literal(TK::TK_COMMA);
64 }
65 };
66 stream << tokens__::to_literal(TK::TK_BRACE_R);
67 }
71 template <std::size_t I, std::size_t Max, traits::Container Arg>
72 void handle_value_type(Arg &&arg) {
73 for (auto &a : arg) {
74 handle_pair(a);
75 }
76 }
80 template <std::size_t I, std::size_t Max, traits::PairSerializable Arg>
81 void handle_pair(Arg &&arg) {
82 stream << tokens__::to_literal(TK::TK_DOLLA) << arg.first << tokens__::to_literal(TK::TK_COLON);
83 handle_value_type<I - 1, Max>(arg.second);
84 if constexpr (I < Max - 1) {
85 stream << tokens__::to_literal(TK::TK_COMMA);
86 }
87 }
91 template <std::size_t I, typename Arg>
92 constexpr static auto make_pair_serializable(Arg &&arg) {
93 if constexpr (!traits::PairSerializable<Arg>) {
94 return std::make_pair(circus::utils::num_to_string<I>::value, std::forward<Arg>(arg));
95 } else {
96 return std::forward<Arg>(arg);
97 }
98 }
102 template <std::size_t... Is, typename... Args>
103 void handler(std::index_sequence<Is...>, Args &&...args) {
104 constexpr std::size_t MAXARG = sizeof...(Args);
105 (handle_pair<Is, MAXARG>(make_pair_serializable<Is>(std::forward<Args>(args))), ...);
106 }
107 bool init = false;
108
109 public:
114 serializer(OStreamT &s) : stream(s) {
115 stream << "$root : {";
116 };
117
126 template <typename... Args>
127 void operator()(Args &&...args) & {
128 handler(std::index_sequence_for<Args...>{}, std::forward<Args>(args)...);
129 }
130
141 template <typename... Args>
142 serializer &operator<<(Args &&...args) {
143 handler(std::index_sequence_for<Args...>{}, std::forward<Args>(args)...);
144 return *this;
145 }
146
147 ~serializer() { stream << '}'; };
148};
149} // namespace circus
Provides compile-time traits and C++20 concepts for type introspection used throughout the Circus lib...
A flexible serializer for C++ objects, primitives, STL containers, and user-defined types.
Definition serializer.hpp:28
serializer(OStreamT &s)
Constructs a serializer with the given output stream.
Definition serializer.hpp:114
serializer & operator<<(Args &&...args)
Serializes the given arguments using stream-like syntax.
Definition serializer.hpp:142
void operator()(Args &&...args) &
Serializes the given arguments using function-call syntax.
Definition serializer.hpp:127
Concept for types that behave like key-value pairs.
Definition circus_traits.hpp:100
Concept for types that can be converted to a string-like view.
Definition circus_traits.hpp:105
static constexpr std::string to_literal(TYPE type) noexcept
Convert the token literal to a std::string without surrounding quotes.
Definition token.hpp:100
Converts a compile-time unsigned integer to a string literal.
Definition to_chars.hpp:58
Compile-time conversion of unsigned integers to string literals.