struct.mcd.nested.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/concept/struct.hpp>
  6. #include <boost/hana/config.hpp>
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/find.hpp>
  9. #include <boost/hana/functional/id.hpp>
  10. #include <boost/hana/integral_constant.hpp>
  11. #include <boost/hana/not_equal.hpp>
  12. #include <boost/hana/pair.hpp>
  13. #include <boost/hana/string.hpp>
  14. #include <boost/hana/tuple.hpp>
  15. #include <string>
  16. #include <utility>
  17. namespace hana = boost::hana;
  18. //! [main]
  19. struct Person {
  20. std::string name;
  21. int age;
  22. struct hana_accessors_impl {
  23. static BOOST_HANA_CONSTEXPR_LAMBDA auto apply() {
  24. return boost::hana::make_tuple(
  25. boost::hana::make_pair(BOOST_HANA_STRING("name"),
  26. [](auto&& p) -> decltype(auto) {
  27. return boost::hana::id(std::forward<decltype(p)>(p).name);
  28. }),
  29. boost::hana::make_pair(BOOST_HANA_STRING("age"),
  30. [](auto&& p) -> decltype(auto) {
  31. return boost::hana::id(std::forward<decltype(p)>(p).age);
  32. })
  33. );
  34. }
  35. };
  36. };
  37. //! [main]
  38. int main() {
  39. Person john{"John", 30}, bob{"Bob", 40};
  40. BOOST_HANA_RUNTIME_CHECK(hana::equal(john, john));
  41. BOOST_HANA_RUNTIME_CHECK(hana::not_equal(john, bob));
  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. BOOST_HANA_RUNTIME_CHECK(hana::to_tuple(john) == hana::make_tuple(
  46. hana::make_pair(BOOST_HANA_STRING("name"), "John"),
  47. hana::make_pair(BOOST_HANA_STRING("age"), 30)
  48. ));
  49. }