0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-09 11:31:11 +01:00
construct/include/ircd/json/tuple/get.h

129 lines
2.6 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_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)
noexcept
{
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)
noexcept
{
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)
noexcept
{
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)
noexcept
{
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)
noexcept
{
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)
noexcept
{
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)
noexcept
{
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)
noexcept
{
return get<name_hash(name), tuple>(t, def);
}
} // namespace json
} // namespace ircd