| 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/accessors.hpp> |
| 6 | #include <boost/hana/adapt_struct.hpp> |
| 7 | #include <boost/hana/assert.hpp> |
| 8 | #include <boost/hana/core/to.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 | namespace ns { |
| 25 | struct Person { |
| 26 | std::string name; |
| 27 | int age; |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | BOOST_HANA_ADAPT_STRUCT(ns::Person, |
| 32 | name, |
| 33 | age |
| 34 | ); |
| 35 | |
| 36 | // The member names are hana::strings: |
| 37 | auto names = hana::transform(hana::accessors<ns::Person>(), hana::first); |
| 38 | BOOST_HANA_CONSTANT_CHECK( |
| 39 | names == hana::make_tuple(BOOST_HANA_STRING("name" ), BOOST_HANA_STRING("age" )) |
| 40 | ); |
| 41 | |
| 42 | int main() { |
| 43 | ns::Person john{.name: "John" , .age: 30}, bob{.name: "Bob" , .age: 40}; |
| 44 | BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john)); |
| 45 | BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob)); |
| 46 | |
| 47 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("name" )) == hana::just("John" )); |
| 48 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, BOOST_HANA_STRING("age" )) == hana::just(30)); |
| 49 | BOOST_HANA_CONSTANT_CHECK(hana::find(john, BOOST_HANA_STRING("foo" )) == hana::nothing); |
| 50 | |
| 51 | BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple( |
| 52 | hana::make_pair(BOOST_HANA_STRING("name" ), "John" ), |
| 53 | hana::make_pair(BOOST_HANA_STRING("age" ), 30) |
| 54 | )); |
| 55 | |
| 56 | BOOST_HANA_RUNTIME_CHECK(hana::to_map(john) == hana::make_map( |
| 57 | hana::make_pair(BOOST_HANA_STRING("name" ), "John" ), |
| 58 | hana::make_pair(BOOST_HANA_STRING("age" ), 30) |
| 59 | )); |
| 60 | } |
| 61 | |