]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/introspection.adapt.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / example / tutorial / introspection.adapt.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4
5 #include <boost/hana.hpp>
6
7 #include <iostream>
8 #include <string>
9 namespace hana = boost::hana;
10 using namespace hana::literals;
11 using namespace std::literals;
12
13
14 //! [BOOST_HANA_DEFINE_STRUCT]
15 struct Person {
16 BOOST_HANA_DEFINE_STRUCT(Person,
17 (std::string, name),
18 (int, age)
19 );
20 };
21 //! [BOOST_HANA_DEFINE_STRUCT]
22
23 int main() {
24
25 //! [for_each]
26 Person john{"John", 30};
27
28 hana::for_each(john, [](auto pair) {
29 std::cout << hana::to<char const*>(hana::first(pair)) << ": "
30 << hana::second(pair) << std::endl;
31 });
32
33 // name: John
34 // age: 30
35 //! [for_each]
36
37 //! [for_each.fuse]
38 hana::for_each(john, hana::fuse([](auto name, auto member) {
39 std::cout << hana::to<char const*>(name) << ": " << member << std::endl;
40 }));
41 //! [for_each.fuse]
42
43 #ifdef BOOST_HANA_CONFIG_ENABLE_STRING_UDL
44
45 {
46
47 //! [at_key]
48 std::string name = hana::at_key(john, "name"_s);
49 BOOST_HANA_RUNTIME_CHECK(name == "John");
50
51 int age = hana::at_key(john, "age"_s);
52 BOOST_HANA_RUNTIME_CHECK(age == 30);
53 //! [at_key]
54
55 }{
56
57 //! [to<map_tag>]
58 auto map = hana::insert(hana::to<hana::map_tag>(john), hana::make_pair("last name"_s, "Doe"s));
59
60 std::string name = map["name"_s];
61 BOOST_HANA_RUNTIME_CHECK(name == "John");
62
63 std::string last_name = map["last name"_s];
64 BOOST_HANA_RUNTIME_CHECK(last_name == "Doe");
65
66 int age = map["age"_s];
67 BOOST_HANA_RUNTIME_CHECK(age == 30);
68 //! [to<map_tag>]
69
70 }
71
72 #endif
73
74 }
75
76 //! [BOOST_HANA_ADAPT_STRUCT]
77 namespace not_my_namespace {
78 struct Person {
79 std::string name;
80 int age;
81 };
82 }
83
84 BOOST_HANA_ADAPT_STRUCT(not_my_namespace::Person, name, age);
85 //! [BOOST_HANA_ADAPT_STRUCT]
86
87
88 //! [BOOST_HANA_ADAPT_ADT]
89 namespace also_not_my_namespace {
90 struct Person {
91 std::string get_name();
92 int get_age();
93 };
94 }
95
96 BOOST_HANA_ADAPT_ADT(also_not_my_namespace::Person,
97 (name, [](auto const& p) { return p.get_name(); }),
98 (age, [](auto const& p) { return p.get_age(); })
99 );
100 //! [BOOST_HANA_ADAPT_ADT]