2018-05-07 06:31:32 +02:00
|
|
|
// 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_AT_H
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
|
|
|
template<size_t hash,
|
|
|
|
class tuple>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
2018-05-07 06:31:32 +02:00
|
|
|
at(const tuple &t)
|
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple, hash>()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
2020-08-02 16:19:59 +02:00
|
|
|
if(unlikely(!defined(json::value(ret))))
|
2018-05-07 06:31:32 +02:00
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"%s", key<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t hash,
|
|
|
|
class tuple>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
2018-05-07 06:31:32 +02:00
|
|
|
at(tuple &t)
|
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple, hash>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
2020-08-02 16:19:59 +02:00
|
|
|
if(unlikely(!defined(json::value(ret))))
|
2018-05-07 06:31:32 +02:00
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"%s", key<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, name>()> &>
|
2018-05-07 06:31:32 +02:00
|
|
|
at(const tuple &t)
|
|
|
|
{
|
|
|
|
return at<name_hash(name), tuple>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, name>()> &>
|
2018-05-07 06:31:32 +02:00
|
|
|
at(tuple &t)
|
|
|
|
{
|
|
|
|
return at<name_hash(name), tuple>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i>
|
2020-04-03 21:07:45 +02:00
|
|
|
constexpr typename std::enable_if<i == size<tuple>(), void>::type
|
2018-05-07 06:31:32 +02:00
|
|
|
at(tuple &t,
|
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
2020-04-03 21:07:45 +02:00
|
|
|
noexcept
|
|
|
|
{}
|
2018-05-07 06:31:32 +02:00
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i = 0>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline typename std::enable_if<i < size<tuple>(), void>::type
|
2018-05-07 06:31:32 +02:00
|
|
|
at(tuple &t,
|
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
if(indexof<tuple>(name) == i)
|
|
|
|
f(val<i>(t));
|
|
|
|
else
|
|
|
|
at<tuple, function, i + 1>(t, name, std::forward<function>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i>
|
2020-04-03 21:07:45 +02:00
|
|
|
constexpr typename std::enable_if<i == size<tuple>(), void>::type
|
2018-05-07 06:31:32 +02:00
|
|
|
at(const tuple &t,
|
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
2020-04-03 21:07:45 +02:00
|
|
|
noexcept
|
|
|
|
{}
|
2018-05-07 06:31:32 +02:00
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i = 0>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline typename std::enable_if<i < size<tuple>(), void>::type
|
2018-05-07 06:31:32 +02:00
|
|
|
at(const tuple &t,
|
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
if(indexof<tuple>(name) == i)
|
|
|
|
f(val<i>(t));
|
|
|
|
else
|
|
|
|
at<tuple, function, i + 1>(t, name, std::forward<function>(f));
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:13:02 +01:00
|
|
|
template<class R,
|
|
|
|
class tuple>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline enable_if_tuple<tuple, const R &>
|
2020-03-19 23:13:02 +01:00
|
|
|
at(const tuple &t,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
const R *ret;
|
|
|
|
const auto closure
|
|
|
|
{
|
|
|
|
[&name, &ret](const auto &key, const auto &val)
|
|
|
|
{
|
2020-03-22 23:54:35 +01:00
|
|
|
if constexpr(std::is_assignable<R, decltype(val)>())
|
2020-03-19 23:13:02 +01:00
|
|
|
{
|
2020-03-22 23:54:35 +01:00
|
|
|
if(key == name)
|
|
|
|
{
|
|
|
|
ret = std::addressof(val);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
2020-03-19 23:13:02 +01:00
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(until(t, closure)))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"%s", name
|
|
|
|
};
|
|
|
|
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class R,
|
|
|
|
class tuple>
|
2020-04-03 21:07:45 +02:00
|
|
|
inline enable_if_tuple<tuple, R &>
|
2020-03-19 23:13:02 +01:00
|
|
|
at(tuple &t,
|
|
|
|
const string_view &name)
|
|
|
|
{
|
|
|
|
R *ret;
|
|
|
|
const auto closure
|
|
|
|
{
|
|
|
|
[&name, &ret](const auto &key, auto &val)
|
|
|
|
{
|
2020-03-22 23:54:35 +01:00
|
|
|
if constexpr(std::is_assignable<R, decltype(val)>())
|
2020-03-19 23:13:02 +01:00
|
|
|
{
|
2020-03-22 23:54:35 +01:00
|
|
|
if(key == name)
|
|
|
|
{
|
|
|
|
ret = std::addressof(val);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
2020-03-19 23:13:02 +01:00
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(until(t, closure)))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"%s", name
|
|
|
|
};
|
|
|
|
|
|
|
|
return *ret;
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|