Simple-Log  alpha-v0.7
TupleAlgorithms.hpp
Go to the documentation of this file.
1 // Copyright Dominic Koepke 2021 - 2021.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // https://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef SL_LOG_TUPLE_ALGORITHMS_HPP
7 #define SL_LOG_TUPLE_ALGORITHMS_HPP
8 
9 #pragma once
10 
11 #include <functional>
12 #include <tuple>
13 
14 namespace sl::log::detail
15 {
16  class TupleAllOf
17  {
18  public:
19  template <class TTuple, class... TArgs>
20  constexpr bool operator ()(TTuple& tuple, TArgs&&... args) const
21  {
22  return invoke<0>(tuple, std::forward<TArgs>(args)...);
23  }
24 
25  private:
26  template <std::size_t index, class TTuple, class... TArgs>
27  constexpr bool invoke(TTuple& tuple, TArgs&&... args) const
28  {
29  if constexpr (index < std::tuple_size_v<TTuple>)
30  {
31  if (!std::invoke(std::get<index>(tuple), std::forward<TArgs>(args)...))
32  {
33  return false;
34  }
35  return invoke<index + 1>(tuple, std::forward<TArgs>(args)...);
36  }
37  return true;
38  }
39  };
40 
41  class TupleAnyOf
42  {
43  public:
44  template <class TTuple, class... TArgs>
45  constexpr bool operator ()(TTuple& tuple, TArgs&&... args) const
46  {
47  return invoke<0>(tuple, std::forward<TArgs>(args)...);
48  }
49 
50  private:
51  template <std::size_t index, class TTuple, class... TArgs>
52  constexpr bool invoke(TTuple& tuple, TArgs&&... args) const
53  {
54  if constexpr (index < std::tuple_size_v<TTuple>)
55  {
56  if (std::invoke(std::get<index>(tuple), std::forward<TArgs>(args)...))
57  {
58  return true;
59  }
60  return invoke<index + 1>(tuple, std::forward<TArgs>(args)...);
61  }
62  return false;
63  }
64  };
65 
66  class TupleNoneOf :
67  private TupleAnyOf
68  {
69  using Super = TupleAnyOf;
70 
71  public:
72  template <class TTuple, class... TArgs>
73  constexpr bool operator ()(TTuple& tuple, TArgs&&... args) const
74  {
75  return !Super::operator()(tuple, std::forward<TArgs>(args)...);
76  }
77  };
78 
79  class TupleForEach
80  {
81  public:
82  template <class TTuple, class TFunc>
83  constexpr void operator ()(TTuple& tuple, TFunc func) const
84  {
85  invoke<0>(tuple, func);
86  }
87 
88  private:
89  template <std::size_t index, class TTuple, class TFunc>
90  constexpr void invoke(TTuple& tuple, TFunc func) const
91  {
92  if constexpr (index < std::tuple_size_v<TTuple>)
93  {
94  std::invoke(func, std::get<index>(tuple));
95  invoke<index + 1>(tuple, func);
96  }
97  }
98  };
99 }
100 
101 #endif