0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-06 02:28:38 +02:00

ircd::json: Add tuple get()/at() non-constexpr accessor suite.

This commit is contained in:
Jason Volk 2020-03-19 15:13:02 -07:00
parent f8b99ad26e
commit 537bdc5e19
3 changed files with 116 additions and 2 deletions

View file

@ -126,5 +126,63 @@ at(const tuple &t,
at<tuple, function, i + 1>(t, name, std::forward<function>(f));
}
template<class R,
class tuple>
enable_if_tuple<tuple, const R &>
at(const tuple &t,
const string_view &name)
{
const R *ret;
const auto closure
{
[&name, &ret](const auto &key, const auto &val)
{
if(key == name)
{
ret = std::addressof(val);
return false;
}
else return true;
}
};
if(unlikely(until(t, closure)))
throw not_found
{
"%s", name
};
return *ret;
}
template<class R,
class tuple>
enable_if_tuple<tuple, R &>
at(tuple &t,
const string_view &name)
{
R *ret;
const auto closure
{
[&name, &ret](const auto &key, auto &val)
{
if(key == name)
{
ret = std::addressof(val);
return false;
}
else return true;
}
};
if(unlikely(until(t, closure)))
throw not_found
{
"%s", name
};
return *ret;
}
} // namespace json
} // namespace ircd

View file

@ -125,5 +125,27 @@ noexcept
return get<name_hash(name), tuple>(t, def);
}
template<class R,
class tuple>
enable_if_tuple<tuple, R>
get(const tuple &t,
const string_view &name,
R ret)
noexcept
{
until(t, [&name, &ret]
(const auto &key, auto&& val)
{
if(key == name)
{
ret = val;
return false;
}
else return true;
});
return ret;
}
} // namespace json
} // namespace ircd

View file

@ -67,6 +67,10 @@ struct tuple
template<class name> constexpr decltype(auto) at(name&&) const;
template<class name> constexpr decltype(auto) at(name&&);
template<class R, class name> R get(name&&, R def = {}) const noexcept;
template<class R, class name> const R &at(name&&) const;
template<class R, class name> R &at(name&&);
template<class... U> explicit tuple(const tuple<U...> &);
template<class U> explicit tuple(const json::object &, const json::keys<U> &);
template<class U> explicit tuple(const tuple &, const json::keys<U> &);
@ -166,10 +170,10 @@ key_exists(const string_view &key)
} // namespace json
} // namespace ircd
#include "get.h"
#include "at.h"
#include "for_each.h"
#include "until.h"
#include "get.h"
#include "at.h"
#include "set.h"
namespace ircd {
@ -297,6 +301,36 @@ const noexcept
return json::get<hash>(*this);
}
template<class... T>
template<class R,
class name>
R
tuple<T...>::get(name&& n,
R ret)
const noexcept
{
return json::get<R>(*this, n, ret);
}
template<class... T>
template<class R,
class name>
const R &
tuple<T...>::at(name&& n)
const
{
return json::at<R>(*this, n);
}
template<class... T>
template<class R,
class name>
R &
tuple<T...>::at(name&& n)
{
return json::at<R>(*this, n);
}
template<class... T>
constexpr size_t
tuple<T...>::size()