]>
git.proxmox.com Git - ceph.git/blob - 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)
5 #include <boost/hana.hpp>
9 namespace hana
= boost::hana
;
10 using namespace hana::literals
;
11 using namespace std::literals
;
14 //! [BOOST_HANA_DEFINE_STRUCT]
16 BOOST_HANA_DEFINE_STRUCT(Person
,
21 //! [BOOST_HANA_DEFINE_STRUCT]
26 Person john
{"John", 30};
28 hana::for_each(john
, [](auto pair
) {
29 std::cout
<< hana::to
<char const*>(hana::first(pair
)) << ": "
30 << hana::second(pair
) << std::endl
;
38 hana::for_each(john
, hana::fuse([](auto name
, auto member
) {
39 std::cout
<< hana::to
<char const*>(name
) << ": " << member
<< std::endl
;
43 #ifdef BOOST_HANA_CONFIG_ENABLE_STRING_UDL
48 std::string name
= hana::at_key(john
, "name"_s
);
49 BOOST_HANA_RUNTIME_CHECK(name
== "John");
51 int age
= hana::at_key(john
, "age"_s
);
52 BOOST_HANA_RUNTIME_CHECK(age
== 30);
58 auto map
= hana::insert(hana::to
<hana::map_tag
>(john
), hana::make_pair("last name"_s
, "Doe"s
));
60 std::string name
= map
["name"_s
];
61 BOOST_HANA_RUNTIME_CHECK(name
== "John");
63 std::string last_name
= map
["last name"_s
];
64 BOOST_HANA_RUNTIME_CHECK(last_name
== "Doe");
66 int age
= map
["age"_s
];
67 BOOST_HANA_RUNTIME_CHECK(age
== 30);
76 //! [BOOST_HANA_ADAPT_STRUCT]
77 namespace not_my_namespace
{
84 BOOST_HANA_ADAPT_STRUCT(not_my_namespace::Person
, name
, age
);
85 //! [BOOST_HANA_ADAPT_STRUCT]
88 //! [BOOST_HANA_ADAPT_ADT]
89 namespace also_not_my_namespace
{
91 std::string
get_name();
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(); })
100 //! [BOOST_HANA_ADAPT_ADT]