blob: dafd4f99e51f862fcfdedaebbc777c6a0324057a [file] [log] [blame]
Brad Bishop186aa672017-05-15 22:27:57 -04001#pragma once
2
Andrew Geisslerf3367682020-05-16 14:02:45 -05003#include <cstddef>
Brad Bishop186aa672017-05-15 22:27:57 -04004#include <tuple>
5#include <utility>
6
7namespace phosphor
8{
9namespace dbus
10{
11namespace monitoring
12{
13
14/** @brief A tuple of references. */
15template <typename... T>
16using TupleOfRefs = std::tuple<std::reference_wrapper<T>...>;
17
18namespace detail
19{
20/** @brief Less than implementation for tuples of references. */
Patrick Venture3d6d3182018-08-31 09:33:09 -070021template <size_t size, size_t i, typename T, typename U>
22struct TupleOfRefsLess
Brad Bishop186aa672017-05-15 22:27:57 -040023{
24 static constexpr bool compare(const T& l, const U& r)
25 {
26 if (std::get<i>(l).get() < std::get<i>(r).get())
27 {
28 return true;
29 }
30 if (std::get<i>(r).get() < std::get<i>(l).get())
31 {
32 return false;
33 }
Brad Bishopd1eac882018-03-29 10:34:05 -040034 return TupleOfRefsLess<size, i + 1, T, U>::compare(l, r);
Brad Bishop186aa672017-05-15 22:27:57 -040035 }
36};
37
38/** @brief Less than specialization for tuple element sizeof...(tuple) +1. */
39template <size_t size, typename T, typename U>
40struct TupleOfRefsLess<size, size, T, U>
41{
George Liu5e6b51d2022-06-21 16:59:39 +080042 static constexpr bool compare(const T& /* l */, const U& /* r */)
Brad Bishop186aa672017-05-15 22:27:57 -040043 {
44 return false;
45 }
46};
47} // namespace detail
48
49/** @brief Less than comparison for tuples of references. */
50struct TupleOfRefsLess
51{
52 template <typename... T, typename... U>
Brad Bishopd1eac882018-03-29 10:34:05 -040053 constexpr bool operator()(const TupleOfRefs<T...>& l,
54 const TupleOfRefs<U...>& r) const
Brad Bishop186aa672017-05-15 22:27:57 -040055 {
56 static_assert(sizeof...(T) == sizeof...(U),
57 "Cannot compare tuples of different lengths.");
Brad Bishopd1eac882018-03-29 10:34:05 -040058 return detail::TupleOfRefsLess<sizeof...(T), 0, TupleOfRefs<T...>,
59 TupleOfRefs<U...>>::compare(l, r);
Brad Bishop186aa672017-05-15 22:27:57 -040060 }
61};
62
63} // namespace monitoring
64} // namespace dbus
65} // namespace phosphor