]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/pfr/example/quick_examples.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 #include <unordered_set>
12 #include <boost/pfr.hpp>
13 #include <boost/type_index.hpp>
15 void test_examples() {
17 #if BOOST_PFR_USE_CPP17
19 //[pfr_quick_examples_ops
21 // Note that the equality operator for structure is not defined.
29 boost::pfr::eq(test
{"aaa", "zomg"}, test
{"aaa", "zomg"})
36 //[pfr_quick_examples_for_each
37 // Increment each field of the variable on 1 and
38 // output the content of the variable.
47 boost::pfr::for_each_field(var
, [](auto& field
) {
52 std::cout
<< boost::pfr::io(var
);
57 //[pfr_quick_examples_for_each_idx
58 // Iterate over fields of a varible and output index and
59 // type of a variable.
71 boost::pfr::for_each_field(sample
{}, [](const auto& field
, std::size_t idx
) {
72 std::cout
<< '\n' << idx
<< ": "
73 << boost::typeindex::type_id_runtime(field
);
80 //[pfr_quick_examples_tuple_size
81 // Getting fields count of some structure
83 struct some
{ int a
,b
,c
,d
,e
; };
85 std::cout
<< "Fields count in structure: "
86 << boost::pfr::tuple_size
<some
>::value
// Outputs: 5
92 //[pfr_quick_examples_get
93 // Get field by index and assign new value to that field
101 boost::pfr::get
<1>(var
) = 42.01f
;
103 std::cout
<< var
.f
; // Outputs: 42.01
107 #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
109 //[pfr_quick_examples_structure_to_tuple
110 // Getting a std::tuple of values from structures fields
112 struct foo
{ int a
, b
; };
118 other var
{'A', {3, 4}};
119 std::tuple
<char, foo
> t
= boost::pfr::structure_to_tuple(var
);
120 assert(std::get
<0>(t
) == 'A');
122 boost::pfr::eq(std::get
<1>(t
), foo
{3, 4})
128 #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
130 //[pfr_quick_examples_structure_tie
131 // Getting a std::tuple of references to structure fields
133 struct foo
{ int a
, b
; };
139 other var
{'A', {14, 15}};
140 std::tuple
<char&, foo
&> t
= boost::pfr::structure_tie(var
);
141 std::get
<1>(t
) = foo
{1, 2};
143 std::cout
<< boost::pfr::io(var
.f
); // Outputs: {1, 2}
148 } // void test_examples()
150 //[pfr_quick_examples_functions_for
151 // Define all the comparison and IO operators for my_structure type along
152 // with hash_value function.
154 #include <boost/pfr/functions_for.hpp>
156 namespace my_namespace
{
157 struct my_structure
{
161 BOOST_PFR_FUNCTIONS_FOR(my_structure
)
165 //[pfr_quick_examples_eq_fields
166 // Define only the equality and inequality operators for my_eq_ne_structure.
168 #include <boost/pfr/functions_for.hpp>
170 namespace my_namespace
{
171 struct my_eq_ne_structure
{
176 inline bool operator==(const my_eq_ne_structure
& x
, const my_eq_ne_structure
& y
) {
177 return boost::pfr::eq_fields(x
, y
);
180 inline bool operator!=(const my_eq_ne_structure
& x
, const my_eq_ne_structure
& y
) {
181 return boost::pfr::ne_fields(x
, y
);