]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
b32b8144 1// Copyright Louis Dionne 2013-2017
7c673cae
FG
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>
9namespace hana = boost::hana;
10using namespace hana::literals;
11using namespace std::literals;
12
13
14//! [BOOST_HANA_DEFINE_STRUCT]
15struct Person {
16 BOOST_HANA_DEFINE_STRUCT(Person,
17 (std::string, name),
18 (int, age)
19 );
20};
21//! [BOOST_HANA_DEFINE_STRUCT]
22
23int main() {
24
25//! [for_each]
26Person john{"John", 30};
27
28hana::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]
38hana::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]
48std::string name = hana::at_key(john, "name"_s);
49BOOST_HANA_RUNTIME_CHECK(name == "John");
50
51int age = hana::at_key(john, "age"_s);
52BOOST_HANA_RUNTIME_CHECK(age == 30);
53//! [at_key]
54
55}{
56
57//! [to<map_tag>]
58auto map = hana::insert(hana::to<hana::map_tag>(john), hana::make_pair("last name"_s, "Doe"s));
59
60std::string name = map["name"_s];
61BOOST_HANA_RUNTIME_CHECK(name == "John");
62
63std::string last_name = map["last name"_s];
64BOOST_HANA_RUNTIME_CHECK(last_name == "Doe");
65
66int age = map["age"_s];
67BOOST_HANA_RUNTIME_CHECK(age == 30);
68//! [to<map_tag>]
69
70}
71
72#endif
73
74}
75
76//! [BOOST_HANA_ADAPT_STRUCT]
77namespace not_my_namespace {
78 struct Person {
79 std::string name;
80 int age;
81 };
82}
83
84BOOST_HANA_ADAPT_STRUCT(not_my_namespace::Person, name, age);
85//! [BOOST_HANA_ADAPT_STRUCT]
86
87
88//! [BOOST_HANA_ADAPT_ADT]
89namespace also_not_my_namespace {
90 struct Person {
91 std::string get_name();
92 int get_age();
93 };
94}
95
96BOOST_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]