]>
Commit | Line | Data |
---|---|---|
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/accessors.hpp> | |
6 | #include <boost/hana/assert.hpp> | |
7 | #include <boost/hana/core/to.hpp> | |
8 | #include <boost/hana/define_struct.hpp> | |
9 | #include <boost/hana/equal.hpp> | |
10 | #include <boost/hana/find.hpp> | |
11 | #include <boost/hana/first.hpp> | |
12 | #include <boost/hana/map.hpp> | |
13 | #include <boost/hana/not_equal.hpp> | |
14 | #include <boost/hana/optional.hpp> | |
15 | #include <boost/hana/pair.hpp> | |
16 | #include <boost/hana/string.hpp> | |
17 | #include <boost/hana/transform.hpp> | |
18 | #include <boost/hana/tuple.hpp> | |
19 | ||
20 | #include <string> | |
21 | namespace hana = boost::hana; | |
22 | ||
23 | ||
24 | struct Person { | |
25 | BOOST_HANA_DEFINE_STRUCT(Person, | |
26 | (std::string, name), | |
27 | (int, age) | |
28 | ); | |
29 | }; | |
30 | ||
31 | // The member names are hana::strings: | |
32 | auto names = hana::transform(hana::accessors<Person>(), hana::first); | |
33 | BOOST_HANA_CONSTANT_CHECK( | |
34 | names == hana::make_tuple(BOOST_HANA_STRING("name"), BOOST_HANA_STRING("age")) | |
35 | ); | |
36 | ||
37 | int main() { | |
38 | Person john{"John", 30}, bob{"Bob", 40}; | |
39 | BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john)); | |
40 | BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob)); | |
41 | ||
42 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("name")) == hana::just("John")); | |
43 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("age")) == hana::just(30)); | |
44 | BOOST_HANA_CONSTANT_CHECK(hana::find(john, BOOST_HANA_STRING("foo")) == hana::nothing); | |
45 | ||
46 | BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple( | |
47 | hana::make_pair(BOOST_HANA_STRING("name"), "John"), | |
48 | hana::make_pair(BOOST_HANA_STRING("age"), 30) | |
49 | )); | |
50 | ||
51 | BOOST_HANA_RUNTIME_CHECK(hana::to_map(john) == hana::make_map( | |
52 | hana::make_pair(BOOST_HANA_STRING("name"), "John"), | |
53 | hana::make_pair(BOOST_HANA_STRING("age"), 30) | |
54 | )); | |
55 | } |