0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-28 16:34:13 +01:00

ircd::util: Add non-const overloads to tuple iterations.

This commit is contained in:
Jason Volk 2017-03-23 15:03:15 -07:00
parent 5a7852a0d4
commit c8953c038d

View file

@ -125,6 +125,14 @@ using custom_ptr = std::unique_ptr<T, std::function<void (T *) noexcept>>;
//
// for_each(tuple, [](auto&& elem) { ... });
template<size_t i,
class func,
class... args>
typename std::enable_if<i == std::tuple_size<std::tuple<args...>>::value, void>::type
for_each(std::tuple<args...> &t,
func&& f)
{}
template<size_t i,
class func,
class... args>
@ -144,11 +152,32 @@ for_each(const std::tuple<args...> &t,
for_each<i+1>(t, std::forward<func>(f));
}
template<size_t i = 0,
class func,
class... args>
typename std::enable_if<i < std::tuple_size<std::tuple<args...>>::value, void>::type
for_each(std::tuple<args...> &t,
func&& f)
{
f(std::get<i>(t));
for_each<i+1>(t, std::forward<func>(f));
}
//
// Iteration of a tuple until() style: your closure returns true to continue, false
// to break. until() then remains true to the end, or returns false if not.
template<size_t i,
class func,
class... args>
typename std::enable_if<i == std::tuple_size<std::tuple<args...>>::value, bool>::type
until(std::tuple<args...> &t,
func&& f)
{
return true;
}
template<size_t i,
class func,
class... args>
@ -159,6 +188,16 @@ until(const std::tuple<args...> &t,
return true;
}
template<size_t i = 0,
class func,
class... args>
typename std::enable_if<i < std::tuple_size<std::tuple<args...>>::value, bool>::type
until(std::tuple<args...> &t,
func&& f)
{
return f(std::get<i>(t))? until<i+1>(t, std::forward<func>(f)) : false;
}
template<size_t i = 0,
class func,
class... args>