traits.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2022 Denis Mikhailov
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PFR_TRAITS_HPP
  6. #define BOOST_PFR_TRAITS_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #include <boost/pfr/detail/possible_reflectable.hpp>
  10. #include <type_traits>
  11. /// \file boost/pfr/traits.hpp
  12. /// Contains traits \forcedlink{is_reflectable} and \forcedlink{is_implicitly_reflectable} for detecting an ability to reflect type.
  13. ///
  14. /// \b Synopsis:
  15. namespace boost { namespace pfr {
  16. /// Has a static const member variable `value` when it is known that type T can or can't be reflected using Boost.PFR; otherwise, there is no member variable.
  17. /// Every user may (and in some difficult cases - should) specialize is_reflectable on his own.
  18. ///
  19. /// \b Example:
  20. /// \code
  21. /// namespace boost { namespace pfr {
  22. /// template<class All> struct is_reflectable<A, All> : std::false_type {}; // 'A' won't be interpreted as reflectable everywhere
  23. /// template<> struct is_reflectable<B, boost_fusion_tag> : std::false_type {}; // 'B' won't be interpreted as reflectable in only Boost Fusion
  24. /// }}
  25. /// \endcode
  26. /// \note is_reflectable affects is_implicitly_reflectable, the decision made by is_reflectable is used by is_implicitly_reflectable.
  27. template<class T, class WhatFor>
  28. struct is_reflectable { /* does not have 'value' because value is unknown */ };
  29. // these specs can't be inherited from 'std::integral_constant< bool, boost::pfr::is_reflectable<T, WhatFor>::value >',
  30. // because it will break the sfinae-friendliness
  31. template<class T, class WhatFor>
  32. struct is_reflectable<const T, WhatFor> : boost::pfr::is_reflectable<T, WhatFor> {};
  33. template<class T, class WhatFor>
  34. struct is_reflectable<volatile T, WhatFor> : boost::pfr::is_reflectable<T, WhatFor> {};
  35. template<class T, class WhatFor>
  36. struct is_reflectable<const volatile T, WhatFor> : boost::pfr::is_reflectable<T, WhatFor> {};
  37. /// Checks the input type for the potential to be reflected.
  38. /// Specialize is_reflectable if you disagree with is_implicitly_reflectable's default decision.
  39. template<class T, class WhatFor>
  40. using is_implicitly_reflectable = std::integral_constant< bool, boost::pfr::detail::possible_reflectable<T, WhatFor>(1L) >;
  41. /// Checks the input type for the potential to be reflected.
  42. /// Specialize is_reflectable if you disagree with is_implicitly_reflectable_v's default decision.
  43. template<class T, class WhatFor>
  44. constexpr bool is_implicitly_reflectable_v = is_implicitly_reflectable<T, WhatFor>::value;
  45. }} // namespace boost::pfr
  46. #endif // BOOST_PFR_TRAITS_HPP