]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/pfr/example/sample_printing.cpp
1 // Copyright 2016-2020 Antony Polukhin
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See the accompanying file LICENSE_1_0.txt
5 // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
9 The following example shows how to write your own io-manipulator for printing:
11 #include <boost/pfr/ops.hpp>
17 /// struct foo {std::uint8_t a, b;};
19 /// std::cout << my_ns::my_io(foo{42, 22});
23 auto my_io(const T
& value
);
26 // Helpers to print individual values
28 void print_each(std::ostream
& out
, const T
& v
) { out
<< v
; }
29 void print_each(std::ostream
& out
, std::uint8_t v
) { out
<< static_cast<unsigned>(v
); }
30 void print_each(std::ostream
& out
, std::int8_t v
) { out
<< static_cast<int>(v
); }
32 // Structure to keep a reference to value, that will be ostreamed lower
38 // Output each field of io_reference::value
40 std::ostream
& operator<<(std::ostream
& out
, io_reference
<T
>&& x
) {
43 boost::pfr::for_each_field(x
.value
, [&](const auto& v
) {
44 out
<< std::exchange(sep
, ", ");
45 detail::print_each(out
, v
);
53 auto my_io(const T
& value
) {
54 return detail::io_reference
<T
>{value
};
58 //] [/pfr_sample_printing]
65 struct foo
{std::uint8_t a
, b
;};
67 std::ostringstream oss
;
68 oss
<< my_ns::my_io(foo
{42, 22});
70 if (oss
.str() != "42, 22") {
74 struct two_big_strings
{
79 #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
80 const char* huge_string
= "Some huge string that should not fit into std::string SSO."
81 "And by 'huge' I mean really HUGE string with multiple statements and a lot of periods........."
85 oss
<< my_ns::my_io(two_big_strings
{
86 huge_string
, huge_string
89 if (oss
.str() != huge_string
+ std::string(", ") + huge_string
) {