cpp-reflect
C++ Reflection and Annotations Library
Loading...
Searching...
No Matches
serializer.cppm
Go to the documentation of this file.
1// Copyright (c) 2024-2025, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
8
9export module reflect.serialize;
10
11import std;
12
13import packtl;
14
15export import reflect;
19
20export namespace refl {
21 template <template <typename> typename Format = formats::default_fmt>
22 struct serializer {
23 template <typename O>
24 using args_t = typename Format<O>::args_t;
25
26 static std::string to_string(const auto& obj, const args_t<std::stringstream>& args = {}) {
27 std::stringstream str{};
28 auto format = Format<std::stringstream>{str, args};
29 format.serialize(obj);
30 return str.str();
31 }
32
33 template <typename O>
34 static void to_stream(O& out, const auto& obj, const args_t<O>& args = {}) {
35 auto format = Format<O>{out, args};
36 format.serialize(obj);
37 }
38 };
39
40 template <template <typename> typename Format = formats::default_fmt>
41 std::string to_string(const auto& obj, const typename Format<std::stringstream>::args_t& args = {}) {
42 return serializer<Format>::to_string(obj, args);
43 }
44} // namespace refl
std::string to_string(const auto &obj, const typename Format< std::stringstream >::args_t &args={})
static void to_stream(O &out, const auto &obj, const args_t< O > &args={})
static std::string to_string(const auto &obj, const args_t< std::stringstream > &args={})
typename Format< O >::args_t args_t