members.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef BOOST_DESCRIBE_DETAIL_MEMBERS_HPP_INCLUDED
  2. #define BOOST_DESCRIBE_DETAIL_MEMBERS_HPP_INCLUDED
  3. // Copyright 2020 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/describe/modifiers.hpp>
  7. #include <boost/describe/detail/pp_for_each.hpp>
  8. #include <boost/describe/detail/pp_utilities.hpp>
  9. #include <boost/describe/detail/list.hpp>
  10. #include <type_traits>
  11. namespace boost
  12. {
  13. namespace describe
  14. {
  15. namespace detail
  16. {
  17. template<class Pm> constexpr unsigned add_static_modifier( Pm )
  18. {
  19. return std::is_member_pointer<Pm>::value? 0: mod_static;
  20. }
  21. template<class Pm> constexpr unsigned add_function_modifier( Pm )
  22. {
  23. return std::is_member_function_pointer<Pm>::value || std::is_function< std::remove_pointer_t<Pm> >::value? mod_function: 0;
  24. }
  25. template<class D, unsigned M> struct member_descriptor
  26. {
  27. static constexpr decltype(D::pointer()) pointer = D::pointer();
  28. static constexpr decltype(D::name()) name = D::name();
  29. static constexpr unsigned modifiers = M | add_static_modifier( D::pointer() ) | add_function_modifier( D::pointer() );
  30. };
  31. #ifndef __cpp_inline_variables
  32. template<class D, unsigned M> constexpr decltype(D::pointer()) member_descriptor<D, M>::pointer;
  33. template<class D, unsigned M> constexpr decltype(D::name()) member_descriptor<D, M>::name;
  34. template<class D, unsigned M> constexpr unsigned member_descriptor<D, M>::modifiers;
  35. #endif
  36. template<unsigned M, class... T> auto member_descriptor_fn_impl( int, T... )
  37. {
  38. return list<member_descriptor<T, M>...>();
  39. }
  40. template<class C, class F> constexpr auto mfn( F C::* p ) { return p; }
  41. template<class C, class F> constexpr auto mfn( F * p ) { return p; }
  42. #define BOOST_DESCRIBE_MEMBER_IMPL(C, m) , []{ struct _boost_desc { \
  43. static constexpr auto pointer() noexcept { return BOOST_DESCRIBE_PP_POINTER(C, m); } \
  44. static constexpr auto name() noexcept { return BOOST_DESCRIBE_PP_NAME(m); } }; return _boost_desc(); }()
  45. #if defined(_MSC_VER) && !defined(__clang__)
  46. #define BOOST_DESCRIBE_PUBLIC_MEMBERS(C, ...) inline auto boost_public_member_descriptor_fn( C** ) \
  47. { return boost::describe::detail::member_descriptor_fn_impl<boost::describe::mod_public>( 0 BOOST_DESCRIBE_PP_FOR_EACH(BOOST_DESCRIBE_MEMBER_IMPL, C, __VA_ARGS__) ); }
  48. #define BOOST_DESCRIBE_PROTECTED_MEMBERS(C, ...) inline auto boost_protected_member_descriptor_fn( C** ) \
  49. { return boost::describe::detail::member_descriptor_fn_impl<boost::describe::mod_protected>( 0 BOOST_DESCRIBE_PP_FOR_EACH(BOOST_DESCRIBE_MEMBER_IMPL, C, __VA_ARGS__) ); }
  50. #define BOOST_DESCRIBE_PRIVATE_MEMBERS(C, ...) inline auto boost_private_member_descriptor_fn( C** ) \
  51. { return boost::describe::detail::member_descriptor_fn_impl<boost::describe::mod_private>( 0 BOOST_DESCRIBE_PP_FOR_EACH(BOOST_DESCRIBE_MEMBER_IMPL, C, __VA_ARGS__) ); }
  52. #else
  53. #define BOOST_DESCRIBE_PUBLIC_MEMBERS(C, ...) inline auto boost_public_member_descriptor_fn( C** ) \
  54. { return boost::describe::detail::member_descriptor_fn_impl<boost::describe::mod_public>( 0 BOOST_DESCRIBE_PP_FOR_EACH(BOOST_DESCRIBE_MEMBER_IMPL, C, ##__VA_ARGS__) ); }
  55. #define BOOST_DESCRIBE_PROTECTED_MEMBERS(C, ...) inline auto boost_protected_member_descriptor_fn( C** ) \
  56. { return boost::describe::detail::member_descriptor_fn_impl<boost::describe::mod_protected>( 0 BOOST_DESCRIBE_PP_FOR_EACH(BOOST_DESCRIBE_MEMBER_IMPL, C, ##__VA_ARGS__) ); }
  57. #define BOOST_DESCRIBE_PRIVATE_MEMBERS(C, ...) inline auto boost_private_member_descriptor_fn( C** ) \
  58. { return boost::describe::detail::member_descriptor_fn_impl<boost::describe::mod_private>( 0 BOOST_DESCRIBE_PP_FOR_EACH(BOOST_DESCRIBE_MEMBER_IMPL, C, ##__VA_ARGS__) ); }
  59. #endif
  60. } // namespace detail
  61. } // namespace describe
  62. } // namespace boost
  63. #endif // #ifndef BOOST_DESCRIBE_DETAIL_MEMBERS_HPP_INCLUDED