libDwm-0.9.45
DwmTupleHelpers.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2020
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMTUPLEHELPERS_HH_
42#define _DWMTUPLEHELPERS_HH_
43
44#include <string>
45#include <tuple>
46
47namespace Dwm {
48
49 namespace TupleHelpers {
50
51 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
54 template <class Tuple, class Func, std::size_t... I>
55 constexpr Func ForEachImpl(Tuple && t, Func && f, std::index_sequence<I...>)
56 {
57 return (void)std::initializer_list<int>{(std::forward<Func>(f)(std::get<I>(std::forward<Tuple>(t))),0)...}, f;
58 }
59
60 //------------------------------------------------------------------------
62 //------------------------------------------------------------------------
63 template <class Tuple, class Func>
64 constexpr Func ForEach(Tuple && t, Func && f)
65 {
66 return ForEachImpl(std::forward<Tuple>(t), std::forward<Func>(f),
67 std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{});
68 }
69
70 //------------------------------------------------------------------------
72 //------------------------------------------------------------------------
73 template<typename Tuple, typename Pred>
74 constexpr size_t FindIf(Tuple && tuple, Pred pred)
75 {
76 size_t index = std::tuple_size<std::remove_reference_t<Tuple>>::value;
77 size_t currentIndex = 0;
78 bool found = false;
79 ForEach(tuple, [&](auto&& value)
80 {
81 if (!found && pred(value)) {
82 index = currentIndex;
83 found = true;
84 }
85 ++currentIndex;
86 });
87 return index;
88 }
89
90 //------------------------------------------------------------------------
92 //------------------------------------------------------------------------
93 template<typename Tuple, typename Action>
94 void Perform(Tuple && tpl, size_t index, Action action)
95 {
96 size_t currentIndex = 0;
97 ForEach(tpl,
98 [action = std::move(action), index, &currentIndex]
99 (auto && value)
100 {
101 if (currentIndex == index) {
102 action(std::forward<decltype(value)>(value));
103 }
104 ++currentIndex;
105 });
106 }
107
108 } // namespace TupleHelpers
109
110} // namespace Dwm
111
112#endif // _DWMTUPLEHELPERS_HH_
113
114
115//---------------------------- emacs settings -----------------------------
116// Local Variables:
117// mode: C++
118// tab-width: 2
119// indent-tabs-mode: nil
120// c-basic-offset: 2
121// End:
122//-------------------------------------------------------------------------