mirror of
https://github.com/matrix-construct/construct
synced 2024-11-11 04:21:07 +01:00
116 lines
2.3 KiB
C++
116 lines
2.3 KiB
C++
// Matrix Construct
|
|
//
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
#pragma once
|
|
#define HAVE_IRCD_JSON_TUPLE_FOR_EACH_H
|
|
|
|
namespace ircd {
|
|
namespace json {
|
|
|
|
template<size_t i = 0,
|
|
class tuple,
|
|
class function>
|
|
inline enable_if_tuple<tuple, bool>
|
|
for_each(const tuple &t,
|
|
function&& f)
|
|
{
|
|
if constexpr(i < size<tuple>())
|
|
{
|
|
using closure_result = std::invoke_result_t
|
|
<
|
|
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,
|
|
class tuple,
|
|
class function>
|
|
inline enable_if_tuple<tuple, bool>
|
|
for_each(tuple &t,
|
|
function&& f)
|
|
{
|
|
if constexpr(i < size<tuple>())
|
|
{
|
|
using closure_result = std::invoke_result_t
|
|
<
|
|
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,
|
|
class function>
|
|
inline void
|
|
for_each(const tuple &t,
|
|
const vector_view<const string_view> &mask,
|
|
function&& f)
|
|
{
|
|
std::for_each(std::begin(mask), std::end(mask), [&t, &f]
|
|
(const auto &key)
|
|
{
|
|
at(t, key, [&f, &key]
|
|
(auto&& val)
|
|
{
|
|
f(key, val);
|
|
});
|
|
});
|
|
}
|
|
|
|
template<class tuple,
|
|
class function>
|
|
inline void
|
|
for_each(tuple &t,
|
|
const vector_view<const string_view> &mask,
|
|
function&& f)
|
|
{
|
|
std::for_each(std::begin(mask), std::end(mask), [&t, &f]
|
|
(const auto &key)
|
|
{
|
|
at(t, key, [&f, &key]
|
|
(auto&& val)
|
|
{
|
|
f(key, val);
|
|
});
|
|
});
|
|
}
|
|
|
|
} // namespace json
|
|
} // namespace ircd
|