]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/pfr/example/quick_examples.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / pfr / example / quick_examples.cpp
1 // Copyright 2016-2020 Antony Polukhin
2
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>.)
6
7 #include <cassert>
8 #include <iostream>
9 #include <unordered_set>
10 #include <set>
11
12 #include <boost/pfr.hpp>
13 #include <boost/type_index.hpp>
14
15 void test_examples() {
16
17 #if BOOST_PFR_USE_CPP17
18 {
19 //[pfr_quick_examples_ops
20 // Assert equality.
21 // Note that the equality operator for structure is not defined.
22
23 struct test {
24 std::string f1;
25 std::string_view f2;
26 };
27
28 assert(
29 boost::pfr::eq(test{"aaa", "zomg"}, test{"aaa", "zomg"})
30 );
31 //]
32 }
33 #endif
34
35 {
36 //[pfr_quick_examples_for_each
37 // Increment each field of the variable on 1 and
38 // output the content of the variable.
39
40 struct test {
41 int f1;
42 long f2;
43 };
44
45 test var{42, 43};
46
47 boost::pfr::for_each_field(var, [](auto& field) {
48 field += 1;
49 });
50
51 // Outputs: {43, 44}
52 std::cout << boost::pfr::io(var);
53 //]
54 }
55
56 {
57 //[pfr_quick_examples_for_each_idx
58 // Iterate over fields of a varible and output index and
59 // type of a variable.
60
61 struct tag0{};
62 struct tag1{};
63 struct sample {
64 tag0 a;
65 tag1 b;
66 };
67
68 // Outputs:
69 // 0: tag0
70 // 1: tag1
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);
74 });
75 //]
76 }
77
78
79 {
80 //[pfr_quick_examples_tuple_size
81 // Getting fields count of some structure
82
83 struct some { int a,b,c,d,e; };
84
85 std::cout << "Fields count in structure: "
86 << boost::pfr::tuple_size<some>::value // Outputs: 5
87 << '\n';
88 //]
89 }
90
91 {
92 //[pfr_quick_examples_get
93 // Get field by index and assign new value to that field
94
95 struct sample {
96 char c;
97 float f;
98 };
99
100 sample var{};
101 boost::pfr::get<1>(var) = 42.01f;
102
103 std::cout << var.f; // Outputs: 42.01
104 //]
105 }
106
107 #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
108 {
109 //[pfr_quick_examples_structure_to_tuple
110 // Getting a std::tuple of values from structures fields
111
112 struct foo { int a, b; };
113 struct other {
114 char c;
115 foo nested;
116 };
117
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');
121 assert(
122 boost::pfr::eq(std::get<1>(t), foo{3, 4})
123 );
124 //]
125 }
126 #endif
127
128 #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
129 {
130 //[pfr_quick_examples_structure_tie
131 // Getting a std::tuple of references to structure fields
132
133 struct foo { int a, b; };
134 struct other {
135 char c;
136 foo f;
137 };
138
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};
142
143 std::cout << boost::pfr::io(var.f); // Outputs: {1, 2}
144 //]
145 }
146 #endif
147
148 } // void test_examples()
149
150 //[pfr_quick_examples_functions_for
151 // Define all the comparison and IO operators for my_structure type along
152 // with hash_value function.
153
154 #include <boost/pfr/functions_for.hpp>
155
156 namespace my_namespace {
157 struct my_structure {
158 int a,b,c,d,e,f,g;
159 // ...
160 };
161 BOOST_PFR_FUNCTIONS_FOR(my_structure)
162 }
163 //]
164
165 //[pfr_quick_examples_eq_fields
166 // Define only the equality and inequality operators for my_eq_ne_structure.
167
168 #include <boost/pfr/functions_for.hpp>
169
170 namespace my_namespace {
171 struct my_eq_ne_structure {
172 float a,b,c,d,e,f,g;
173 // ...
174 };
175
176 inline bool operator==(const my_eq_ne_structure& x, const my_eq_ne_structure& y) {
177 return boost::pfr::eq_fields(x, y);
178 }
179
180 inline bool operator!=(const my_eq_ne_structure& x, const my_eq_ne_structure& y) {
181 return boost::pfr::ne_fields(x, y);
182 }
183 }
184 //]
185
186 int main() {
187 test_examples();
188 }