functions_for.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2016-2025 Antony Polukhin
  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_FUNCTIONS_FOR_HPP
  6. #define BOOST_PFR_FUNCTIONS_FOR_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #if !defined(BOOST_USE_MODULES) || defined(BOOST_PFR_INTERFACE_UNIT)
  10. #include <boost/pfr/ops_fields.hpp>
  11. #include <boost/pfr/io_fields.hpp>
  12. #endif
  13. /// \file boost/pfr/functions_for.hpp
  14. /// Contains BOOST_PFR_FUNCTIONS_FOR macro that defined comparison and stream operators for T along with hash_value function.
  15. /// \b Example:
  16. /// \code
  17. /// #include <boost/pfr/functions_for.hpp>
  18. ///
  19. /// namespace my_namespace {
  20. /// struct my_struct { // No operators defined for that structure
  21. /// int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
  22. /// };
  23. /// BOOST_PFR_FUNCTIONS_FOR(my_struct)
  24. /// }
  25. /// \endcode
  26. ///
  27. /// \podops for other ways to define operators and more details.
  28. ///
  29. /// \b Synopsis:
  30. /// \def BOOST_PFR_FUNCTIONS_FOR(T)
  31. /// Defines comparison and stream operators for T along with hash_value function.
  32. ///
  33. /// \b Example:
  34. /// \code
  35. /// #include <boost/pfr/functions_for.hpp>
  36. /// struct comparable_struct { // No operators defined for that structure
  37. /// int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
  38. /// };
  39. /// BOOST_PFR_FUNCTIONS_FOR(comparable_struct)
  40. /// // ...
  41. ///
  42. /// comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
  43. /// comparable_struct s2 {0, 1, "Hello", false, 6,7,8,9,10,11111};
  44. /// assert(s1 < s2);
  45. /// std::cout << s1 << std::endl; // Outputs: {0, 1, H, e, l, l, o, , , 0, 6, 7, 8, 9, 10, 11}
  46. /// \endcode
  47. ///
  48. /// \podops for other ways to define operators and more details.
  49. ///
  50. /// \b Defines \b following \b for \b T:
  51. /// \code
  52. /// bool operator==(const T& lhs, const T& rhs);
  53. /// bool operator!=(const T& lhs, const T& rhs);
  54. /// bool operator< (const T& lhs, const T& rhs);
  55. /// bool operator> (const T& lhs, const T& rhs);
  56. /// bool operator<=(const T& lhs, const T& rhs);
  57. /// bool operator>=(const T& lhs, const T& rhs);
  58. ///
  59. /// template <class Char, class Traits>
  60. /// std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const T& value);
  61. ///
  62. /// template <class Char, class Traits>
  63. /// std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& in, T& value);
  64. ///
  65. /// // helper function for Boost unordered containers and boost::hash<>.
  66. /// std::size_t hash_value(const T& value);
  67. /// \endcode
  68. #define BOOST_PFR_FUNCTIONS_FOR(T) \
  69. BOOST_PFR_MAYBE_UNUSED inline bool operator==(const T& lhs, const T& rhs) { return ::boost::pfr::eq_fields(lhs, rhs); } \
  70. BOOST_PFR_MAYBE_UNUSED inline bool operator!=(const T& lhs, const T& rhs) { return ::boost::pfr::ne_fields(lhs, rhs); } \
  71. BOOST_PFR_MAYBE_UNUSED inline bool operator< (const T& lhs, const T& rhs) { return ::boost::pfr::lt_fields(lhs, rhs); } \
  72. BOOST_PFR_MAYBE_UNUSED inline bool operator> (const T& lhs, const T& rhs) { return ::boost::pfr::gt_fields(lhs, rhs); } \
  73. BOOST_PFR_MAYBE_UNUSED inline bool operator<=(const T& lhs, const T& rhs) { return ::boost::pfr::le_fields(lhs, rhs); } \
  74. BOOST_PFR_MAYBE_UNUSED inline bool operator>=(const T& lhs, const T& rhs) { return ::boost::pfr::ge_fields(lhs, rhs); } \
  75. template <class Char, class Traits> \
  76. BOOST_PFR_MAYBE_UNUSED inline ::std::basic_ostream<Char, Traits>& operator<<(::std::basic_ostream<Char, Traits>& out, const T& value) { \
  77. return out << ::boost::pfr::io_fields(value); \
  78. } \
  79. template <class Char, class Traits> \
  80. BOOST_PFR_MAYBE_UNUSED inline ::std::basic_istream<Char, Traits>& operator>>(::std::basic_istream<Char, Traits>& in, T& value) { \
  81. return in >> ::boost::pfr::io_fields(value); \
  82. } \
  83. BOOST_PFR_MAYBE_UNUSED inline std::size_t hash_value(const T& v) { \
  84. return ::boost::pfr::hash_fields(v); \
  85. } \
  86. /**/
  87. #endif // BOOST_PFR_FUNCTIONS_FOR_HPP