1 // Copyright (c) 2016-2020 Antony Polukhin
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)
6 #ifndef BOOST_PFR_FUNCTIONS_FOR_HPP
7 #define BOOST_PFR_FUNCTIONS_FOR_HPP
10 #include <boost/pfr/detail/config.hpp>
12 #include <boost/pfr/ops_fields.hpp>
13 #include <boost/pfr/io_fields.hpp>
15 /// \file boost/pfr/functions_for.hpp
16 /// Contains BOOST_PFR_FUNCTIONS_FOR macro that defined comparison and stream operators for T along with hash_value function.
19 /// #include <boost/pfr/functions_for.hpp>
21 /// namespace my_namespace {
22 /// struct my_struct { // No operators defined for that structure
23 /// int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
25 /// BOOST_PFR_FUNCTIONS_FOR(my_struct)
29 /// \podops for other ways to define operators and more details.
33 /// \def BOOST_PFR_FUNCTIONS_FOR(T)
34 /// Defines comparison and stream operators for T along with hash_value function.
38 /// #include <boost/pfr/functions_for.hpp>
39 /// struct comparable_struct { // No operators defined for that structure
40 /// int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
42 /// BOOST_PFR_FUNCTIONS_FOR(comparable_struct)
45 /// comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
46 /// comparable_struct s2 {0, 1, "Hello", false, 6,7,8,9,10,11111};
48 /// std::cout << s1 << std::endl; // Outputs: {0, 1, H, e, l, l, o, , , 0, 6, 7, 8, 9, 10, 11}
51 /// \podops for other ways to define operators and more details.
53 /// \b Defines \b following \b for \b T:
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 /// bool operator> (const T& lhs, const T& rhs);
59 /// bool operator<=(const T& lhs, const T& rhs);
60 /// bool operator>=(const T& lhs, const T& rhs);
62 /// template <class Char, class Traits>
63 /// std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const T& value);
65 /// template <class Char, class Traits>
66 /// std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& in, T& value);
68 /// // helper function for Boost unordered containers and boost::hash<>.
69 /// std::size_t hash_value(const T& value);
72 #define BOOST_PFR_FUNCTIONS_FOR(T) \
73 BOOST_PFR_MAYBE_UNUSED inline bool operator==(const T& lhs, const T& rhs) { return ::boost::pfr::eq_fields(lhs, rhs); } \
74 BOOST_PFR_MAYBE_UNUSED inline bool operator!=(const T& lhs, const T& rhs) { return ::boost::pfr::ne_fields(lhs, rhs); } \
75 BOOST_PFR_MAYBE_UNUSED inline bool operator< (const T& lhs, const T& rhs) { return ::boost::pfr::lt_fields(lhs, rhs); } \
76 BOOST_PFR_MAYBE_UNUSED inline bool operator> (const T& lhs, const T& rhs) { return ::boost::pfr::gt_fields(lhs, rhs); } \
77 BOOST_PFR_MAYBE_UNUSED inline bool operator<=(const T& lhs, const T& rhs) { return ::boost::pfr::le_fields(lhs, rhs); } \
78 BOOST_PFR_MAYBE_UNUSED inline bool operator>=(const T& lhs, const T& rhs) { return ::boost::pfr::ge_fields(lhs, rhs); } \
79 template <class Char, class Traits> \
80 BOOST_PFR_MAYBE_UNUSED inline ::std::basic_ostream<Char, Traits>& operator<<(::std::basic_ostream<Char, Traits>& out, const T& value) { \
81 return out << ::boost::pfr::io_fields(value); \
83 template <class Char, class Traits> \
84 BOOST_PFR_MAYBE_UNUSED inline ::std::basic_istream<Char, Traits>& operator>>(::std::basic_istream<Char, Traits>& in, T& value) { \
85 return in >> ::boost::pfr::io_fields(value); \
87 BOOST_PFR_MAYBE_UNUSED inline std::size_t hash_value(const T& v) { \
88 return ::boost::pfr::hash_fields(v); \
92 #endif // BOOST_PFR_FUNCTIONS_FOR_HPP