| 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/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 | Person(std::string const& name, int age) : name_(name), age_(age) { } |
| 26 | |
| 27 | std::string const& get_name() const { return name_; } |
| 28 | int get_age() const { return age_; } |
| 29 | |
| 30 | private: |
| 31 | std::string name_; |
| 32 | int age_; |
| 33 | }; |
| 34 | |
| 35 | namespace boost { namespace hana { |
| 36 | template <> |
| 37 | struct accessors_impl<Person> { |
| 38 | static BOOST_HANA_CONSTEXPR_LAMBDA auto apply() { |
| 39 | return make_tuple( |
| 40 | make_pair(BOOST_HANA_STRING("name" ), [](auto&& p) -> std::string const& { |
| 41 | return p.get_name(); |
| 42 | }), |
| 43 | make_pair(BOOST_HANA_STRING("age" ), [](auto&& p) { |
| 44 | return p.get_age(); |
| 45 | }) |
| 46 | ); |
| 47 | } |
| 48 | }; |
| 49 | }} |
| 50 | //! [main] |
| 51 | |
| 52 | int main() { |
| 53 | auto name = BOOST_HANA_STRING("name" ); |
| 54 | auto age = BOOST_HANA_STRING("age" ); |
| 55 | |
| 56 | Person john{"John" , 30}, bob{"Bob" , 40}; |
| 57 | BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john)); |
| 58 | BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob)); |
| 59 | |
| 60 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, name) == hana::just("John" )); |
| 61 | BOOST_HANA_RUNTIME_CHECK(hana::find(john, age) == hana::just(30)); |
| 62 | BOOST_HANA_CONSTANT_CHECK(hana::find(john, BOOST_HANA_STRING("foo" )) == hana::nothing); |
| 63 | |
| 64 | BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple( |
| 65 | hana::make_pair(name, "John" ), |
| 66 | hana::make_pair(age, 30) |
| 67 | )); |
| 68 | } |
| 69 | |