]>
Commit | Line | Data |
---|---|---|
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/assert.hpp> | |
6 | #include <boost/hana/config.hpp> | |
7 | #include <boost/hana/core/to.hpp> | |
8 | #include <boost/hana/equal.hpp> | |
9 | #include <boost/hana/find.hpp> | |
10 | #include <boost/hana/functional/id.hpp> | |
11 | #include <boost/hana/fwd/accessors.hpp> | |
12 | #include <boost/hana/integral_constant.hpp> | |
13 | #include <boost/hana/not_equal.hpp> | |
14 | #include <boost/hana/pair.hpp> | |
15 | #include <boost/hana/string.hpp> | |
16 | #include <boost/hana/tuple.hpp> | |
17 | ||
18 | #include <string> | |
19 | #include <utility> | |
20 | namespace hana = boost::hana; | |
21 | ||
22 | ||
23 | //! [main] | |
24 | struct Person { | |
25 | std::string name; | |
26 | int age; | |
27 | }; | |
28 | ||
29 | // The keys can be anything as long as they are compile-time comparable. | |
30 | constexpr auto name = hana::integral_c<std::string Person::*, &Person::name>; | |
31 | constexpr auto age = hana::string_c<'a', 'g', 'e'>; | |
32 | ||
33 | namespace boost { namespace hana { | |
34 | template <> | |
35 | struct accessors_impl<Person> { | |
36 | static BOOST_HANA_CONSTEXPR_LAMBDA auto apply() { | |
37 | return make_tuple( | |
38 | make_pair(name, [](auto&& p) -> decltype(auto) { | |
39 | return id(std::forward<decltype(p)>(p).name); | |
40 | }), | |
41 | make_pair(age, [](auto&& p) -> decltype(auto) { | |
42 | return id(std::forward<decltype(p)>(p).age); | |
43 | }) | |
44 | ); | |
45 | } | |
46 | }; | |
47 | }} | |
48 | //! [main] | |
49 | ||
50 | int main() { | |
51 | Person john{"John", 30}, bob{"Bob", 40}; | |
52 | BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john)); | |
53 | BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob)); | |
54 | ||
55 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, name) == hana::just("John")); | |
56 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, age) == hana::just(30)); | |
57 | BOOST_HANA_CONSTANT_CHECK(hana::find(john, BOOST_HANA_STRING("foo")) == hana::nothing); | |
58 | ||
59 | BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple( | |
60 | hana::make_pair(name, "John"), | |
61 | hana::make_pair(age, 30) | |
62 | )); | |
63 | } |