2017-08-23 23:32:28 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Charybdis Development Team
|
|
|
|
* Copyright (C) 2017 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2017-08-26 06:39:36 +02:00
|
|
|
#define HAVE_IRCD_JSON_TUPLE_H
|
2017-08-23 23:32:28 +02:00
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// All tuple templates inherit from this non-template type for tagging.
|
2017-09-08 21:29:21 +02:00
|
|
|
struct tuple_base
|
|
|
|
{
|
|
|
|
// EBO tag
|
|
|
|
};
|
2017-09-08 12:15:58 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// A compile-time construct to describe a JSON object's members and types.
|
|
|
|
///
|
2017-09-14 20:30:06 +02:00
|
|
|
/// Member access by name is O(1) because of recursive constexpr function
|
|
|
|
/// inlining when translating a name to the index number which is then used
|
|
|
|
/// as the template argument to std::get() for the value.
|
|
|
|
///
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Here we represent a JSON object with a named tuple, allowing the programmer
|
|
|
|
/// to create a structure specifying all of the potentially valid members of the
|
|
|
|
/// object. Thus at runtime, the tuple only carries around its values like a
|
|
|
|
/// `struct`. Unlike a `struct`, the tuple is abstractly iterable and we have
|
|
|
|
/// implemented logic operating on all JSON tuples regardless of their makeup
|
2017-09-14 20:30:06 +02:00
|
|
|
/// without any effort from a developer when creating a new tuple.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
|
|
|
/// The member structure for the tuple is called `property` because json::member
|
2017-09-14 20:30:06 +02:00
|
|
|
/// is already used to pair together runtime oriented json::values.
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
|
|
|
/// Create and use a tuple to efficiently extract members from a json::object.
|
|
|
|
/// The tuple will populate its own members during a single-pass iteration of
|
|
|
|
/// the JSON input. If the JSON does not contain a member specified in the
|
|
|
|
/// tuple, the value will be default initialized. If the JSON contains a member
|
|
|
|
/// not specified in the tuple, it is ignored. If you need to know all of the
|
|
|
|
/// members specified in the JSON dynamically, use a json::index or iterate
|
|
|
|
/// manually.
|
|
|
|
///
|
2017-08-23 23:32:28 +02:00
|
|
|
template<class... T>
|
2017-08-26 06:39:36 +02:00
|
|
|
struct tuple
|
2017-09-08 12:15:58 +02:00
|
|
|
:std::tuple<T...>
|
2017-09-08 21:29:21 +02:00
|
|
|
,tuple_base
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
|
|
|
using tuple_type = std::tuple<T...>;
|
2017-08-27 01:30:49 +02:00
|
|
|
using super_type = tuple<T...>;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
static constexpr size_t size();
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
tuple(const json::object &);
|
2017-09-09 21:20:00 +02:00
|
|
|
tuple(const json::iov &);
|
2017-09-08 12:08:29 +02:00
|
|
|
tuple(const std::initializer_list<member> &);
|
2017-08-27 01:30:49 +02:00
|
|
|
tuple() = default;
|
2017-08-23 23:32:28 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
template<class tuple>
|
|
|
|
constexpr bool
|
|
|
|
is_tuple()
|
|
|
|
{
|
|
|
|
return std::is_base_of<tuple_base, tuple>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple, class R>
|
|
|
|
using enable_if_tuple = typename std::enable_if<is_tuple<tuple>(), R>::type;
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
|
|
|
using tuple_type = typename tuple::tuple_type;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
|
|
|
using tuple_size = std::tuple_size<tuple_type<tuple>>;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
2017-08-26 06:39:36 +02:00
|
|
|
using tuple_element = typename std::tuple_element<i, tuple_type<tuple>>::type;
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
|
|
|
using tuple_value_type = typename tuple_element<tuple, i>::value_type;
|
|
|
|
|
2017-09-08 12:15:58 +02:00
|
|
|
template<class... T>
|
|
|
|
constexpr size_t
|
|
|
|
tuple<T...>::size()
|
|
|
|
{
|
|
|
|
return std::tuple_size<tuple_type>();
|
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
2017-08-27 01:30:49 +02:00
|
|
|
auto &
|
2017-08-26 06:39:36 +02:00
|
|
|
stdcast(const tuple &o)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-08-26 06:39:36 +02:00
|
|
|
return static_cast<const typename tuple::tuple_type &>(o);
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple>
|
2017-08-27 01:30:49 +02:00
|
|
|
auto &
|
2017-08-26 06:39:36 +02:00
|
|
|
stdcast(tuple &o)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-08-26 06:39:36 +02:00
|
|
|
return static_cast<typename tuple::tuple_type &>(o);
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class tuple>
|
2017-09-08 21:29:21 +02:00
|
|
|
constexpr enable_if_tuple<tuple, size_t>
|
2017-08-27 01:30:49 +02:00
|
|
|
size()
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
return tuple_size<tuple>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
2017-09-08 21:29:21 +02:00
|
|
|
constexpr enable_if_tuple<tuple, const char *const &>
|
2017-08-27 01:30:49 +02:00
|
|
|
key()
|
|
|
|
{
|
|
|
|
return tuple_element<tuple, i>::key;
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const char *const &>
|
|
|
|
key(const tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
return std::get<i>(t).key;
|
2017-08-27 01:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
|
|
|
constexpr typename std::enable_if<i == size<tuple>(), size_t>::type
|
|
|
|
indexof(const char *const &name)
|
|
|
|
{
|
|
|
|
return size<tuple>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
size_t i = 0>
|
|
|
|
constexpr typename std::enable_if<i < size<tuple>(), size_t>::type
|
|
|
|
indexof(const char *const &name)
|
|
|
|
{
|
|
|
|
const auto equal
|
|
|
|
{
|
|
|
|
_constexpr_equal(key<tuple, i>(), name)
|
|
|
|
};
|
|
|
|
|
|
|
|
return equal? i : indexof<tuple, i + 1>(name);
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class tuple,
|
|
|
|
size_t i>
|
|
|
|
constexpr typename std::enable_if<i == size<tuple>(), size_t>::type
|
|
|
|
indexof(const string_view &name)
|
|
|
|
{
|
|
|
|
return size<tuple>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
size_t i = 0>
|
|
|
|
constexpr typename std::enable_if<i < size<tuple>(), size_t>::type
|
|
|
|
indexof(const string_view &name)
|
|
|
|
{
|
|
|
|
const auto equal
|
|
|
|
{
|
|
|
|
name == key<tuple, i>()
|
|
|
|
};
|
|
|
|
|
|
|
|
return equal? i : indexof<tuple, i + 1>(name);
|
|
|
|
}
|
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
|
|
|
|
get(tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
|
|
|
return std::get<i>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, i> &>
|
|
|
|
get(const tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
|
|
|
return std::get<i>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, i> &>
|
|
|
|
val(const tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
using value_type = tuple_value_type<tuple, i>;
|
2017-08-27 01:30:49 +02:00
|
|
|
return static_cast<const value_type &>(get<i>(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, i> &>
|
|
|
|
val(tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
using value_type = tuple_value_type<tuple, i>;
|
2017-08-27 01:30:49 +02:00
|
|
|
return static_cast<value_type &>(get<i>(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple>(name)> &>
|
|
|
|
val(const tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
return val<indexof<tuple>(name)>(t);
|
2017-08-27 01:30:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple>(name)> &>
|
|
|
|
val(tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
return val<indexof<tuple>(name)>(t);
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<const char *const &name,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple>(name)> &>
|
|
|
|
at(const tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
indexof<tuple>(name)
|
2017-08-27 01:30:49 +02:00
|
|
|
};
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
auto &ret
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
using value_type = tuple_value_type<tuple, idx>;
|
2017-08-27 01:30:49 +02:00
|
|
|
|
|
|
|
//TODO: does tcmalloc zero this or huh?
|
|
|
|
if(ret == value_type{})
|
|
|
|
throw not_found("%s", name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple>(name)> &>
|
|
|
|
at(tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
indexof<tuple>(name)
|
2017-08-27 01:30:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
using value_type = tuple_value_type<tuple, idx>;
|
2017-08-27 01:30:49 +02:00
|
|
|
|
|
|
|
//TODO: does tcmalloc zero this or huh?
|
|
|
|
if(ret == value_type{})
|
|
|
|
throw not_found("%s", name);
|
|
|
|
|
|
|
|
return ret;
|
2017-08-26 06:39:36 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<const char *const &name,
|
|
|
|
class... T>
|
|
|
|
tuple_value_type<tuple<T...>, indexof<tuple<T...>>(name)>
|
|
|
|
get(const tuple<T...> &t,
|
|
|
|
const tuple_value_type<tuple<T...>, indexof<tuple<T...>>(name)> &def = {})
|
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple<T...>>(name)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
using value_type = tuple_value_type<tuple<T...>, idx>;
|
|
|
|
|
|
|
|
//TODO: does tcmalloc zero this or huh?
|
|
|
|
return ret != value_type{}? ret : def;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class... T>
|
|
|
|
tuple_value_type<tuple<T...>, indexof<tuple<T...>>(name)> &
|
|
|
|
get(tuple<T...> &t,
|
|
|
|
tuple_value_type<tuple<T...>, indexof<tuple<T...>>(name)> &def)
|
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
|
|
|
indexof<tuple<T...>>(name)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
using value_type = tuple_value_type<tuple<T...>, idx>;
|
|
|
|
|
|
|
|
//TODO: does tcmalloc zero this or huh?
|
|
|
|
return ret != value_type{}? ret : def;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
template<size_t i,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i == size<tuple>(), void>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
for_each(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i == size<tuple>(), void>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
for_each(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i < size<tuple>(), void>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
for_each(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
f(key<i>(t), val<i>(t));
|
2017-08-23 23:32:28 +02:00
|
|
|
for_each<i + 1>(t, std::forward<function>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i < size<tuple>(), void>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
for_each(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
f(key<i>(t), val<i>(t));
|
2017-08-23 23:32:28 +02:00
|
|
|
for_each<i + 1>(t, std::forward<function>(f));
|
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
|
|
|
ssize_t i>
|
|
|
|
typename std::enable_if<(i < 0), void>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
rfor_each(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
|
|
|
ssize_t i>
|
|
|
|
typename std::enable_if<(i < 0), void>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
rfor_each(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
2017-08-27 01:30:49 +02:00
|
|
|
ssize_t i = size<tuple>() - 1>
|
2017-08-26 06:39:36 +02:00
|
|
|
typename std::enable_if<i < tuple_size<tuple>(), void>::type
|
|
|
|
rfor_each(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
f(key<i>(t), val<i>(t));
|
2017-08-26 06:39:36 +02:00
|
|
|
rfor_each<tuple, function, i - 1>(t, std::forward<function>(f));
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
2017-08-27 01:30:49 +02:00
|
|
|
ssize_t i = size<tuple>() - 1>
|
2017-08-26 06:39:36 +02:00
|
|
|
typename std::enable_if<i < tuple_size<tuple>(), void>::type
|
|
|
|
rfor_each(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
f(key<i>(t), val<i>(t));
|
2017-08-26 06:39:36 +02:00
|
|
|
rfor_each<tuple, function, i - 1>(t, std::forward<function>(f));
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i == size<tuple>(), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
until(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i == size<tuple>(), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
until(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i < size<tuple>(), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
until(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
return f(key<i>(t), val<i>(t))?
|
2017-08-23 23:32:28 +02:00
|
|
|
until<i + 1>(t, std::forward<function>(f)):
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
2017-08-26 06:39:36 +02:00
|
|
|
class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function>
|
2017-08-27 01:30:49 +02:00
|
|
|
typename std::enable_if<i < size<tuple>(), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
until(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
return f(key<i>(t), val<i>(t))?
|
2017-08-23 23:32:28 +02:00
|
|
|
until<i + 1>(t, std::forward<function>(f)):
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
|
|
|
ssize_t i>
|
|
|
|
typename std::enable_if<(i < 0), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
runtil(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
|
|
|
ssize_t i>
|
|
|
|
typename std::enable_if<(i < 0), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
runtil(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
2017-08-27 01:30:49 +02:00
|
|
|
ssize_t i = size<tuple>() - 1>
|
|
|
|
typename std::enable_if<i < size<tuple>(), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
runtil(const tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
return f(key<i>(t), val<i>(t))?
|
2017-08-26 06:39:36 +02:00
|
|
|
runtil<tuple, function, i - 1>(t, std::forward<function>(f)):
|
2017-08-23 23:32:28 +02:00
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2017-08-26 06:39:36 +02:00
|
|
|
template<class tuple,
|
2017-08-23 23:32:28 +02:00
|
|
|
class function,
|
2017-08-27 01:30:49 +02:00
|
|
|
ssize_t i = size<tuple>() - 1>
|
|
|
|
typename std::enable_if<i < size<tuple>(), bool>::type
|
2017-08-26 06:39:36 +02:00
|
|
|
runtil(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
function&& f)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
return f(key<i>(t), val<i>(t))?
|
2017-08-26 06:39:36 +02:00
|
|
|
runtil<tuple, function, i - 1>(t, std::forward<function>(f)):
|
2017-08-23 23:32:28 +02:00
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i>
|
|
|
|
typename std::enable_if<i == size<tuple>(), void>::type
|
|
|
|
at(tuple &t,
|
2017-08-23 23:32:28 +02:00
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
|
|
|
{
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i = 0>
|
|
|
|
typename std::enable_if<i < size<tuple>(), void>::type
|
|
|
|
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));
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i>
|
|
|
|
typename std::enable_if<i == size<tuple>(), void>::type
|
|
|
|
at(const tuple &t,
|
2017-08-27 01:30:49 +02:00
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i = 0>
|
|
|
|
typename std::enable_if<i < size<tuple>(), void>::type
|
|
|
|
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));
|
|
|
|
}
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class V,
|
|
|
|
class... T>
|
|
|
|
tuple<T...> &
|
|
|
|
set(tuple<T...> &t,
|
|
|
|
const string_view &key,
|
|
|
|
const V &val)
|
|
|
|
{
|
|
|
|
at(t, key, [&key, &val]
|
|
|
|
(auto &target)
|
|
|
|
{
|
|
|
|
using target_type = decltype(target);
|
|
|
|
using cast_type = typename std::remove_reference<target_type>::type;
|
|
|
|
target = byte_view<cast_type>(val);
|
2017-08-23 23:32:28 +02:00
|
|
|
});
|
2017-09-08 10:41:02 +02:00
|
|
|
|
|
|
|
return t;
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 01:30:49 +02:00
|
|
|
template<class... T>
|
|
|
|
tuple<T...>::tuple(const json::object &object)
|
2017-08-26 06:39:36 +02:00
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
//TODO: is tcmalloc zero-initializing all tuple elements, or is something else doing that?
|
|
|
|
std::for_each(std::begin(object), std::end(object), [this]
|
|
|
|
(const auto &member)
|
2017-08-26 06:39:36 +02:00
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
at(*this, member.first, [&member]
|
|
|
|
(auto &target)
|
2017-08-26 06:39:36 +02:00
|
|
|
{
|
|
|
|
using target_type = decltype(target);
|
|
|
|
using cast_type = typename std::remove_reference<target_type>::type; try
|
|
|
|
{
|
|
|
|
target = lex_cast<cast_type>(member.second);
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
2017-08-27 01:30:49 +02:00
|
|
|
throw parse_error("member '%s' must convert to '%s'",
|
2017-08-26 06:39:36 +02:00
|
|
|
member.first,
|
|
|
|
typeid(target_type).name());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
template<class... T>
|
2017-09-09 21:20:00 +02:00
|
|
|
tuple<T...>::tuple(const json::iov &iov)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
|
|
|
//TODO: is tcmalloc zero-initializing all tuple elements, or is something else doing that?
|
2017-09-12 19:03:06 +02:00
|
|
|
std::for_each(std::begin(iov), std::end(iov), [this]
|
2017-09-08 21:29:21 +02:00
|
|
|
(const auto &member)
|
|
|
|
{
|
2017-09-09 21:20:00 +02:00
|
|
|
switch(type(member.second))
|
|
|
|
{
|
|
|
|
case type::OBJECT:
|
|
|
|
case type::ARRAY:
|
|
|
|
if(unlikely(!member.second.serial))
|
|
|
|
throw print_error("iov member '%s' must be JSON to be used by the tuple",
|
|
|
|
string_view{member.first});
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
at(*this, member.first, [&member]
|
|
|
|
(auto &target)
|
|
|
|
{
|
|
|
|
using target_type = decltype(target);
|
|
|
|
using cast_type = typename std::remove_reference<target_type>::type; try
|
|
|
|
{
|
|
|
|
target = static_cast<cast_type>(member.second);
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
|
|
|
throw parse_error("member '%s' must convert to '%s'",
|
|
|
|
member.first,
|
|
|
|
typeid(target_type).name());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class... T>
|
2017-09-08 12:08:29 +02:00
|
|
|
tuple<T...>::tuple(const std::initializer_list<member> &members)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
|
|
|
std::for_each(std::begin(members), std::end(members), [this]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
switch(type(member.second))
|
|
|
|
{
|
|
|
|
case type::STRING:
|
|
|
|
case type::LITERAL:
|
|
|
|
set(*this, member.first, string_view{member.second});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type::NUMBER:
|
|
|
|
if(member.second.floats)
|
|
|
|
set(*this, member.first, member.second.floating);
|
|
|
|
else
|
|
|
|
set(*this, member.first, member.second.integer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type::ARRAY:
|
|
|
|
case type::OBJECT:
|
|
|
|
if(!member.second.serial)
|
|
|
|
throw parse_error("Unserialized value not supported yet");
|
|
|
|
|
|
|
|
set(*this, member.first, string_view{member.second});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
template<class tuple,
|
|
|
|
class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure>
|
|
|
|
constexpr void
|
|
|
|
_key_transform(it_a it,
|
|
|
|
const it_b end,
|
|
|
|
closure&& lambda)
|
|
|
|
{
|
|
|
|
for(size_t i(0); i < tuple::size() && it != end; ++i)
|
|
|
|
{
|
|
|
|
*it = lambda(key<tuple, i>());
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class tuple,
|
|
|
|
class it_a,
|
|
|
|
class it_b>
|
|
|
|
constexpr void
|
|
|
|
_key_transform(it_a it,
|
|
|
|
const it_b end)
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
for(size_t i(0); i < tuple::size() && it != end; ++i)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
|
|
|
*it = key<tuple, i>();
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class... T>
|
|
|
|
void
|
|
|
|
_key_transform(const tuple<T...> &tuple,
|
|
|
|
it_a it,
|
|
|
|
const it_b end)
|
|
|
|
{
|
|
|
|
for_each(tuple, [&it, &end]
|
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
|
|
|
if(it != end)
|
|
|
|
{
|
|
|
|
*it = key;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure,
|
|
|
|
class... T>
|
|
|
|
void
|
|
|
|
_member_transform(const tuple<T...> &tuple,
|
|
|
|
it_a it,
|
|
|
|
const it_b end,
|
|
|
|
closure&& lambda)
|
|
|
|
{
|
|
|
|
until(tuple, [&it, &end, &lambda]
|
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
|
|
|
if(it == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*it = lambda(key, val);
|
|
|
|
++it;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class... T>
|
|
|
|
void
|
|
|
|
_member_transform(const tuple<T...> &tuple,
|
|
|
|
it_a it,
|
|
|
|
const it_b end)
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
until(tuple, [&it, &end]
|
2017-09-08 10:41:02 +02:00
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
if(it == end)
|
|
|
|
return false;
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
*it = member { key, val };
|
2017-09-08 16:47:07 +02:00
|
|
|
++it;
|
|
|
|
return true;
|
2017-09-08 10:41:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
template<class T>
|
|
|
|
constexpr bool
|
|
|
|
serialized_lex_cast()
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
using type = typename std::remove_reference<T>::type;
|
|
|
|
return std::is_arithmetic<type>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename std::enable_if<serialized_lex_cast<T>(), size_t>::type
|
|
|
|
serialized(T&& t)
|
|
|
|
{
|
|
|
|
return lex_cast(t).size();
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
size_t
|
2017-09-08 16:47:07 +02:00
|
|
|
serialized(const tuple<T...> &t)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
constexpr const size_t member_count
|
|
|
|
{
|
|
|
|
tuple<T...>::size()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Number of commas for this object is one less than the member count, or 0
|
|
|
|
const size_t commas
|
|
|
|
{
|
|
|
|
member_count? member_count - 1 : 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// 2 for the {} and the comma count
|
|
|
|
const size_t overhead
|
|
|
|
{
|
|
|
|
2 + commas
|
|
|
|
};
|
|
|
|
|
|
|
|
std::array<size_t, member_count> sizes;
|
|
|
|
_member_transform(t, begin(sizes), end(sizes), []
|
|
|
|
(const string_view &key, auto&& val)
|
|
|
|
{
|
|
|
|
// " " :
|
|
|
|
return 1 + key.size() + 1 + 1 + serialized(val);
|
|
|
|
});
|
|
|
|
|
|
|
|
return std::accumulate(begin(sizes), end(sizes), overhead);
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class... T>
|
2017-09-08 16:47:07 +02:00
|
|
|
string_view
|
|
|
|
stringify(mutable_buffer &buf,
|
|
|
|
const tuple<T...> &tuple)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-08 12:08:29 +02:00
|
|
|
std::array<member, tuple.size()> members;
|
2017-09-08 10:41:02 +02:00
|
|
|
_member_transform(tuple, begin(members), end(members));
|
2017-09-08 16:47:07 +02:00
|
|
|
return stringify(buf, begin(members), end(members));
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
std::ostream &
|
|
|
|
operator<<(std::ostream &s, const tuple<T...> &t)
|
|
|
|
{
|
2017-09-08 21:29:21 +02:00
|
|
|
s << json::string(t);
|
2017-09-08 10:41:02 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|