blob: c6a6dcd34e2e2cf3ab575d4d2a2fbbcb4411cda9 [file] [log] [blame]
Brad Bishop186aa672017-05-15 22:27:57 -04001#pragma once
2
3#include <tuple>
4#include <utility>
5
6namespace phosphor
7{
8namespace dbus
9{
10namespace monitoring
11{
12
13/** @brief A tuple of references. */
14template <typename... T>
15using TupleOfRefs = std::tuple<std::reference_wrapper<T>...>;
16
17namespace detail
18{
19/** @brief Less than implementation for tuples of references. */
20template <size_t size, size_t i, typename T, typename U>
21struct TupleOfRefsLess
22{
23 static constexpr bool compare(const T& l, const U& r)
24 {
25 if (std::get<i>(l).get() < std::get<i>(r).get())
26 {
27 return true;
28 }
29 if (std::get<i>(r).get() < std::get<i>(l).get())
30 {
31 return false;
32 }
33 return TupleOfRefsLess < size, i + 1, T, U >::compare(l, r);
34 }
35};
36
37/** @brief Less than specialization for tuple element sizeof...(tuple) +1. */
38template <size_t size, typename T, typename U>
39struct TupleOfRefsLess<size, size, T, U>
40{
41 static constexpr bool compare(const T& l, const U& r)
42 {
43 return false;
44 }
45};
46} // namespace detail
47
48/** @brief Less than comparison for tuples of references. */
49struct TupleOfRefsLess
50{
51 template <typename... T, typename... U>
52 constexpr bool operator()(
53 const TupleOfRefs<T...>& l,
54 const TupleOfRefs<U...>& r) const
55 {
56 static_assert(sizeof...(T) == sizeof...(U),
57 "Cannot compare tuples of different lengths.");
58 return detail::TupleOfRefsLess <
59 sizeof...(T),
60 0,
61 TupleOfRefs<T...>,
62 TupleOfRefs<U... >>::compare(l, r);
63 }
64};
65
66} // namespace monitoring
67} // namespace dbus
68} // namespace phosphor