]>
Commit | Line | Data |
---|---|---|
1 | // Copyright Louis Dionne 2013-2022 | |
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/assert.hpp> | |
6 | #include <boost/hana/concept/struct.hpp> | |
7 | #include <boost/hana/config.hpp> | |
8 | #include <boost/hana/equal.hpp> | |
9 | #include <boost/hana/find.hpp> | |
10 | #include <boost/hana/functional/id.hpp> | |
11 | #include <boost/hana/integral_constant.hpp> | |
12 | #include <boost/hana/not_equal.hpp> | |
13 | #include <boost/hana/pair.hpp> | |
14 | #include <boost/hana/string.hpp> | |
15 | #include <boost/hana/tuple.hpp> | |
16 | ||
17 | #include <string> | |
18 | #include <utility> | |
19 | namespace hana = boost::hana; | |
20 | ||
21 | ||
22 | //! [main] | |
23 | struct Person { | |
24 | std::string name; | |
25 | int age; | |
26 | ||
27 | struct hana_accessors_impl { | |
28 | static BOOST_HANA_CONSTEXPR_LAMBDA auto apply() { | |
29 | return boost::hana::make_tuple( | |
30 | boost::hana::make_pair(BOOST_HANA_STRING("name"), | |
31 | [](auto&& p) -> decltype(auto) { | |
32 | return boost::hana::id(std::forward<decltype(p)>(p).name); | |
33 | }), | |
34 | boost::hana::make_pair(BOOST_HANA_STRING("age"), | |
35 | [](auto&& p) -> decltype(auto) { | |
36 | return boost::hana::id(std::forward<decltype(p)>(p).age); | |
37 | }) | |
38 | ); | |
39 | } | |
40 | }; | |
41 | }; | |
42 | //! [main] | |
43 | ||
44 | int main() { | |
45 | Person john{"John", 30}, bob{"Bob", 40}; | |
46 | BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john)); | |
47 | BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob)); | |
48 | ||
49 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("name")) == hana::just("John")); | |
50 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("age")) == hana::just(30)); | |
51 | BOOST_HANA_CONSTANT_CHECK(hana::find(john, BOOST_HANA_STRING("foo")) == hana::nothing); | |
52 | ||
53 | BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple( | |
54 | hana::make_pair(BOOST_HANA_STRING("name"), "John"), | |
55 | hana::make_pair(BOOST_HANA_STRING("age"), 30) | |
56 | )); | |
57 | } |