functions_for.hpp 5.0 KB

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