1// Copyright (c) 2016-2024 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
6#ifndef BOOST_PFR_FUNCTIONS_FOR_HPP
7#define BOOST_PFR_FUNCTIONS_FOR_HPP
8#pragma once
9
10#include <boost/pfr/detail/config.hpp>
11
12#include <boost/pfr/ops_fields.hpp>
13#include <boost/pfr/io_fields.hpp>
14
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.
17/// \b Example:
18/// \code
19/// #include <boost/pfr/functions_for.hpp>
20///
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;
24/// };
25/// BOOST_PFR_FUNCTIONS_FOR(my_struct)
26/// }
27/// \endcode
28///
29/// \podops for other ways to define operators and more details.
30///
31/// \b Synopsis:
32
33/// \def BOOST_PFR_FUNCTIONS_FOR(T)
34/// Defines comparison and stream operators for T along with hash_value function.
35///
36/// \b Example:
37/// \code
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;
41/// };
42/// BOOST_PFR_FUNCTIONS_FOR(comparable_struct)
43/// // ...
44///
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};
47/// assert(s1 < s2);
48/// std::cout << s1 << std::endl; // Outputs: {0, 1, H, e, l, l, o, , , 0, 6, 7, 8, 9, 10, 11}
49/// \endcode
50///
51/// \podops for other ways to define operators and more details.
52///
53/// \b Defines \b following \b for \b T:
54/// \code
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);
61///
62/// template <class Char, class Traits>
63/// std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, const T& value);
64///
65/// template <class Char, class Traits>
66/// std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& in, T& value);
67///
68/// // helper function for Boost unordered containers and boost::hash<>.
69/// std::size_t hash_value(const T& value);
70/// \endcode
71
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); \
82 } \
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); \
86 } \
87 BOOST_PFR_MAYBE_UNUSED inline std::size_t hash_value(const T& v) { \
88 return ::boost::pfr::hash_fields(v); \
89 } \
90/**/
91
92#endif // BOOST_PFR_FUNCTIONS_FOR_HPP
93
94

source code of boost/libs/pfr/include/boost/pfr/functions_for.hpp