// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2018 Jason Volk // // 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 inline enable_if_tuple()> &> at(const tuple &t) { constexpr size_t idx { indexof() }; const auto &ret { val(t) }; if(unlikely(!defined(json::value(ret)))) throw not_found { "%s", key(t) }; return ret; } template inline enable_if_tuple()> &> at(tuple &t) { constexpr size_t idx { indexof() }; auto &ret { val(t) }; if(unlikely(!defined(json::value(ret)))) throw not_found { "%s", key(t) }; return ret; } template inline enable_if_tuple()> &> at(const tuple &t) { return at(t); } template inline enable_if_tuple()> &> at(tuple &t) { return at(t); } template constexpr typename std::enable_if(), void>::type at(tuple &t, const string_view &name, function&& f) noexcept {} template inline typename std::enable_if(), void>::type at(tuple &t, const string_view &name, function&& f) { if(indexof(name) == i) f(val(t)); else at(t, name, std::forward(f)); } template constexpr typename std::enable_if(), void>::type at(const tuple &t, const string_view &name, function&& f) noexcept {} template inline typename std::enable_if(), void>::type at(const tuple &t, const string_view &name, function&& f) { if(indexof(name) == i) f(val(t)); else at(t, name, std::forward(f)); } template inline enable_if_tuple at(const tuple &t, const string_view &name) { const R *ret; const auto closure { [&name, &ret](const auto &key, const auto &val) noexcept { if constexpr(std::is_assignable()) { if(key == name) { ret = std::addressof(val); return false; } else return true; } else return true; } }; if(unlikely(until(t, closure))) throw not_found { "%s", name }; return *ret; } template inline enable_if_tuple at(tuple &t, const string_view &name) { R *ret; const auto closure { [&name, &ret](const auto &key, auto &val) noexcept { if constexpr(std::is_assignable()) { if(key == name) { ret = std::addressof(val); return false; } else return true; } else return true; } }; if(unlikely(until(t, closure))) throw not_found { "%s", name }; return *ret; } } // namespace json } // namespace ircd