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_GET_H
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
|
|
|
template<size_t hash,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
|
|
|
get(const tuple &t)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple, hash>()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t hash,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()>>
|
|
|
|
get(const tuple &t,
|
|
|
|
const tuple_value_type<tuple, indexof<tuple, hash>()> &def)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple, hash>()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return defined(json::value(ret))? ret : def;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t hash,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
|
|
|
get(tuple &t)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple, hash>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t hash,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
|
|
|
get(tuple &t,
|
|
|
|
tuple_value_type<tuple, indexof<tuple, hash>()> &def)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
auto &ret
|
|
|
|
{
|
|
|
|
get<hash, tuple>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return defined(json::value(ret))? ret : def;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, name>()> &>
|
|
|
|
get(const tuple &t)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
return get<name_hash(name), tuple>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, name>()>>
|
|
|
|
get(const tuple &t,
|
|
|
|
const tuple_value_type<tuple, indexof<tuple, name>()> &def)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
return get<name_hash(name), tuple>(t, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, name>()>>
|
|
|
|
get(tuple &t)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
return get<name_hash(name), tuple>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, name>()>>
|
|
|
|
get(tuple &t,
|
|
|
|
tuple_value_type<tuple, indexof<tuple, hash>()> &def)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
return get<name_hash(name), tuple>(t, def);
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:13:02 +01:00
|
|
|
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)
|
|
|
|
{
|
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 = val;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else return true;
|
2020-03-19 23:13:02 +01:00
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:31:32 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|