0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 11:48:54 +02:00

ircd::json: Reuse ircd::util abstract tuple tools.

This commit is contained in:
Jason Volk 2022-06-30 12:22:14 -07:00
parent 642165a8bd
commit f7e76cc9f1
2 changed files with 18 additions and 88 deletions

View file

@ -14,66 +14,30 @@
namespace ircd { namespace ircd {
namespace json { namespace json {
template<size_t i = 0, template<class tuple,
class tuple,
class function> class function>
inline enable_if_tuple<tuple, bool> inline enable_if_tuple<tuple, bool>
for_each(const tuple &t, for_each(const tuple &t,
function&& f) function&& f)
{ {
if constexpr(i < size<tuple>()) return util::for_each(t, [&f]
(const auto &prop)
{ {
using closure_result = std::invoke_result_t return f(prop.key, prop.value);
< });
decltype(f), decltype(key<i>(t)), decltype(val<i>(t))
>;
constexpr bool terminable
{
std::is_same<closure_result, bool>()
};
if constexpr(terminable)
{
if(!f(key<i>(t), val<i>(t)))
return false;
}
else f(key<i>(t), val<i>(t));
return for_each<i + 1>(t, std::forward<function>(f));
}
else return true;
} }
template<size_t i = 0, template<class tuple,
class tuple,
class function> class function>
inline enable_if_tuple<tuple, bool> inline enable_if_tuple<tuple, bool>
for_each(tuple &t, for_each(tuple &t,
function&& f) function&& f)
{ {
if constexpr(i < size<tuple>()) return util::for_each(t, [&f]
(auto &prop)
{ {
using closure_result = std::invoke_result_t return f(prop.key, prop.value);
< });
decltype(f), decltype(key<i>(t)), decltype(val<i>(t))
>;
constexpr bool terminable
{
std::is_same<closure_result, bool>()
};
if constexpr(terminable)
{
if(!f(key<i>(t), val<i>(t)))
return false;
}
else f(key<i>(t), val<i>(t));
return for_each<i + 1>(t, std::forward<function>(f));
}
else return true;
} }
template<class tuple, template<class tuple,

View file

@ -21,28 +21,11 @@ inline enable_if_tuple<tuple, bool>
rfor_each(const tuple &t, rfor_each(const tuple &t,
function&& f) function&& f)
{ {
if constexpr(i >= 0) return util::rfor_each(t, [&f]
(const auto &prop)
{ {
using closure_result = std::invoke_result_t return f(prop.key, prop.value);
< });
decltype(f), decltype(key<i>(t)), decltype(val<i>(t))
>;
constexpr bool terminable
{
std::is_same<closure_result, bool>()
};
if constexpr(terminable)
{
if(!f(key<i>(t), val<i>(t)))
return false;
}
else f(key<i>(t), val<i>(t));
return rfor_each<tuple, function, i - 1>(t, std::forward<function>(f));
}
else return true;
} }
template<class tuple, template<class tuple,
@ -52,28 +35,11 @@ inline enable_if_tuple<tuple, bool>
rfor_each(tuple &t, rfor_each(tuple &t,
function&& f) function&& f)
{ {
if constexpr(i >= 0) return util::rfor_each(t, [&f]
(auto &prop)
{ {
using closure_result = std::invoke_result_t return f(prop.key, prop.value);
< });
decltype(f), decltype(key<i>(t)), decltype(val<i>(t))
>;
constexpr bool terminable
{
std::is_same<closure_result, bool>()
};
if constexpr(terminable)
{
if(!f(key<i>(t), val<i>(t)))
return false;
}
else f(key<i>(t), val<i>(t));
return rfor_each<tuple, function, i - 1>(t, std::forward<function>(f));
}
else return true;
} }
} // namespace json } // namespace json