members.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*!
  2. @file
  3. Defines `boost::hana::members`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_MEMBERS_HPP
  9. #define BOOST_HANA_MEMBERS_HPP
  10. #include <boost/hana/fwd/members.hpp>
  11. #include <boost/hana/accessors.hpp>
  12. #include <boost/hana/concept/struct.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/second.hpp>
  16. #include <boost/hana/transform.hpp>
  17. BOOST_HANA_NAMESPACE_BEGIN
  18. //! @cond
  19. template <typename Object>
  20. constexpr auto members_t::operator()(Object&& object) const {
  21. using S = typename hana::tag_of<Object>::type;
  22. using Members = BOOST_HANA_DISPATCH_IF(members_impl<S>,
  23. hana::Struct<S>::value
  24. );
  25. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  26. static_assert(hana::Struct<S>::value,
  27. "hana::members(object) requires 'object' to be a Struct");
  28. #endif
  29. return Members::apply(static_cast<Object&&>(object));
  30. }
  31. //! @endcond
  32. namespace struct_detail {
  33. template <typename Holder, typename Forward>
  34. struct members_helper {
  35. Holder object;
  36. template <typename Accessor>
  37. constexpr decltype(auto) operator()(Accessor&& accessor) const {
  38. return hana::second(static_cast<Accessor&&>(accessor))(
  39. static_cast<Forward>(object)
  40. );
  41. }
  42. };
  43. }
  44. template <typename S, bool condition>
  45. struct members_impl<S, when<condition>> : default_ {
  46. template <typename Object>
  47. static constexpr auto apply(Object&& object) {
  48. return hana::transform(hana::accessors<S>(),
  49. struct_detail::members_helper<Object&, Object&&>{object}
  50. );
  51. }
  52. };
  53. BOOST_HANA_NAMESPACE_END
  54. #endif // !BOOST_HANA_MEMBERS_HPP