2018-01-22 10:07:09 +01: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.
|
2017-08-23 23:32:28 +02:00
|
|
|
|
|
|
|
#pragma once
|
2017-08-26 06:39:36 +02:00
|
|
|
#define HAVE_IRCD_JSON_TUPLE_H
|
2017-08-23 23:32:28 +02:00
|
|
|
|
2018-02-01 04:06:31 +01:00
|
|
|
#include "property.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
|
2017-09-19 11:09:19 +02:00
|
|
|
/// the JSON input.
|
|
|
|
///
|
|
|
|
/// But remember, the tuple carries very little information for you at runtime
|
|
|
|
/// which may make it difficult to represent all JS phenomena like "undefined"
|
|
|
|
/// and "null".
|
2017-09-12 18:37:44 +02:00
|
|
|
///
|
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
|
|
|
{
|
2017-12-12 21:22:41 +01:00
|
|
|
struct keys;
|
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-12-12 21:23:20 +01:00
|
|
|
static constexpr size_t size();
|
|
|
|
|
2017-09-19 11:09:19 +02:00
|
|
|
operator json::value() const;
|
2017-10-16 06:22:52 +02:00
|
|
|
operator crh::sha256::buf() const;
|
2017-09-19 11:09:19 +02:00
|
|
|
|
2017-10-27 21:25:39 +02:00
|
|
|
template<class... U> tuple(const tuple<U...> &);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:07:10 +01:00
|
|
|
template<class tuple,
|
|
|
|
class R>
|
2017-09-08 21:29:21 +02:00
|
|
|
using enable_if_tuple = typename std::enable_if<is_tuple<tuple>(), R>::type;
|
|
|
|
|
2017-11-25 22:07:10 +01:00
|
|
|
template<class tuple,
|
|
|
|
class test,
|
|
|
|
class R>
|
|
|
|
using enable_if_tuple_and = typename std::enable_if<is_tuple<tuple>() && test(), 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-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
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<class tuple,
|
|
|
|
size_t hash,
|
|
|
|
size_t i>
|
|
|
|
constexpr typename std::enable_if<i == size<tuple>(), size_t>::type
|
|
|
|
indexof()
|
|
|
|
{
|
|
|
|
return size<tuple>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
size_t hash,
|
|
|
|
size_t i = 0>
|
|
|
|
constexpr typename std::enable_if<i < size<tuple>(), size_t>::type
|
|
|
|
indexof()
|
|
|
|
{
|
2017-10-12 03:13:30 +02:00
|
|
|
constexpr auto equal
|
2017-10-05 01:25:15 +02:00
|
|
|
{
|
|
|
|
ircd::hash(key<tuple, i>()) == hash
|
|
|
|
};
|
|
|
|
|
|
|
|
return equal? i : indexof<tuple, hash, i + 1>();
|
|
|
|
}
|
|
|
|
|
2018-02-25 04:45:57 +01:00
|
|
|
template<class tuple,
|
|
|
|
const char *const &name,
|
|
|
|
size_t i>
|
|
|
|
constexpr typename std::enable_if<i == size<tuple>(), size_t>::type
|
|
|
|
indexof()
|
|
|
|
{
|
|
|
|
return size<tuple>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
const char *const &name,
|
|
|
|
size_t i = 0>
|
|
|
|
constexpr typename std::enable_if<i < size<tuple>(), size_t>::type
|
|
|
|
indexof()
|
|
|
|
{
|
|
|
|
return indexof<tuple, name_hash(name)>();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2017-10-12 03:13:30 +02:00
|
|
|
constexpr auto equal
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
|
|
|
_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-09-19 11:09:19 +02:00
|
|
|
template<class tuple>
|
|
|
|
constexpr bool
|
|
|
|
key_exists(const string_view &key)
|
|
|
|
{
|
|
|
|
return indexof<tuple>(key) < size<tuple>();
|
|
|
|
}
|
|
|
|
|
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> &>
|
2017-10-05 01:25:15 +02:00
|
|
|
val(tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
return static_cast<tuple_value_type<tuple, i> &>(std::get<i>(t));
|
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 tuple_value_type<tuple, i> &>
|
2017-10-05 01:25:15 +02:00
|
|
|
val(const tuple &t)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
return static_cast<const tuple_value_type<tuple, i> &>(std::get<i>(t));
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 22:07:10 +01:00
|
|
|
template<class T>
|
|
|
|
typename std::enable_if<is_number<T>(), size_t>::type
|
|
|
|
serialized(T&& t)
|
|
|
|
{
|
|
|
|
return lex_cast(t).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
typename std::enable_if<is_number<T>(), bool>::type
|
|
|
|
defined(T&& t)
|
|
|
|
{
|
2018-02-08 23:01:53 +01:00
|
|
|
// :-(
|
|
|
|
using type = typename std::decay<T>::type;
|
|
|
|
return t != std::numeric_limits<type>::max();
|
2017-11-25 22:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
std::is_base_of<json::string, dst>() &&
|
|
|
|
std::is_convertible<src, ircd::string_view>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
{
|
|
|
|
d = unquote(string_view{std::forward<src>(s)});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
!std::is_base_of<json::string, dst>() &&
|
|
|
|
std::is_convertible<src, dst>() &&
|
|
|
|
!ircd::json::is_tuple<dst>() &&
|
|
|
|
!std::is_same<bool, dst>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
{
|
|
|
|
d = std::forward<src>(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
!std::is_base_of<json::string, dst>() &&
|
|
|
|
std::is_convertible<src, dst>() &&
|
|
|
|
!ircd::json::is_tuple<dst>() &&
|
|
|
|
std::is_same<bool, dst>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
{
|
|
|
|
static const is_zero test{};
|
|
|
|
d = test(std::forward<src>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
std::is_arithmetic<dst>() &&
|
|
|
|
std::is_base_of<std::string_view, typename std::remove_reference<src>::type>() &&
|
|
|
|
!std::is_base_of<ircd::byte_view<ircd::string_view>, typename std::remove_reference<src>::type>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
d = lex_cast<dst>(std::forward<src>(s));
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
|
|
|
throw parse_error("cannot convert '%s' to '%s'",
|
|
|
|
demangle<src>(),
|
|
|
|
demangle<dst>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
std::is_arithmetic<dst>() &&
|
|
|
|
std::is_base_of<ircd::byte_view<ircd::string_view>, typename std::remove_reference<src>::type>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
{
|
|
|
|
assert(!s.empty());
|
|
|
|
d = byte_view<dst>(std::forward<src>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
std::is_base_of<std::string_view, dst>() &&
|
|
|
|
std::is_pod<typename std::remove_reference<src>::type>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
{
|
|
|
|
d = byte_view<string_view>(std::forward<src>(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class dst,
|
|
|
|
class src>
|
|
|
|
typename std::enable_if
|
|
|
|
<
|
|
|
|
ircd::json::is_tuple<dst>(),
|
|
|
|
void>::type
|
|
|
|
_assign(dst &d,
|
|
|
|
src&& s)
|
|
|
|
{
|
|
|
|
d = dst{std::forward<src>(s)};
|
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<size_t hash,
|
2017-10-17 09:52:17 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
|
|
|
get(const tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-10-17 09:52:17 +02:00
|
|
|
indexof<tuple, hash>()
|
2017-10-05 01:25:15 +02:00
|
|
|
};
|
|
|
|
|
2018-02-21 23:24:01 +01:00
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2017-08-27 01:30:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<size_t hash,
|
2017-10-17 09:52:17 +02:00
|
|
|
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)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-10-17 09:52:17 +02:00
|
|
|
indexof<tuple, hash>()
|
2017-10-05 01:25:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
2017-10-17 09:52:17 +02:00
|
|
|
using value_type = tuple_value_type<tuple, idx>;
|
2017-10-05 01:25:15 +02:00
|
|
|
|
|
|
|
//TODO: undefined
|
2017-12-12 21:23:20 +01:00
|
|
|
return defined(ret)? ret : def;
|
2017-08-27 01:30:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<size_t hash,
|
2017-10-17 09:52:17 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
|
|
|
get(tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-10-17 09:52:17 +02:00
|
|
|
indexof<tuple, hash>()
|
2017-10-05 01:25:15 +02:00
|
|
|
};
|
|
|
|
|
2018-02-21 23:24:01 +01:00
|
|
|
auto &ret
|
|
|
|
{
|
|
|
|
val<idx>(t)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
2017-08-27 01:30:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<size_t hash,
|
2017-10-17 09:52:17 +02:00
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
|
|
|
get(tuple &t,
|
|
|
|
tuple_value_type<tuple, indexof<tuple, hash>()> &def)
|
2017-08-23 23:32:28 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
//TODO: undefined
|
2017-10-17 09:52:17 +02:00
|
|
|
auto &ret{get<hash, tuple>(t)};
|
2017-10-05 01:25:15 +02:00
|
|
|
using value_type = decltype(ret);
|
2017-12-12 21:23:20 +01:00
|
|
|
return defined(ret)? ret : def;
|
2017-08-23 23:32:28 +02:00
|
|
|
}
|
|
|
|
|
2018-02-25 04:45:57 +01:00
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, name>()> &>
|
|
|
|
get(const tuple &t)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return get<name_hash(name), tuple>(t, def);
|
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<size_t hash,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
2017-10-05 01:25:15 +02:00
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
2017-09-08 21:29:21 +02:00
|
|
|
at(const tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
indexof<tuple, hash>()
|
2017-08-27 01:30:49 +02:00
|
|
|
};
|
|
|
|
|
2018-02-21 23:24:01 +01:00
|
|
|
const 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
|
|
|
|
2017-12-12 21:23:20 +01:00
|
|
|
//TODO: XXX
|
|
|
|
if(!defined(ret))
|
2017-10-05 01:25:15 +02:00
|
|
|
throw not_found("%s", key<idx>(t));
|
2017-08-27 01:30:49 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<size_t hash,
|
2017-09-08 21:29:21 +02:00
|
|
|
class tuple>
|
2017-10-05 01:25:15 +02:00
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, hash>()> &>
|
2017-09-08 21:29:21 +02:00
|
|
|
at(tuple &t)
|
2017-08-27 01:30:49 +02:00
|
|
|
{
|
|
|
|
constexpr size_t idx
|
|
|
|
{
|
2017-10-05 01:25:15 +02:00
|
|
|
indexof<tuple, hash>()
|
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
|
|
|
|
2017-12-12 21:23:20 +01:00
|
|
|
//TODO: XXX
|
|
|
|
if(!defined(ret))
|
2017-10-05 01:25:15 +02:00
|
|
|
throw not_found("%s", key<idx>(t));
|
2017-08-27 01:30:49 +02:00
|
|
|
|
|
|
|
return ret;
|
2017-08-26 06:39:36 +02:00
|
|
|
}
|
|
|
|
|
2018-02-25 04:45:57 +01:00
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, const tuple_value_type<tuple, indexof<tuple, name>()> &>
|
|
|
|
at(const tuple &t)
|
|
|
|
{
|
|
|
|
return at<name_hash(name), tuple>(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<const char *const &name,
|
|
|
|
class tuple>
|
|
|
|
enable_if_tuple<tuple, tuple_value_type<tuple, indexof<tuple, name>()> &>
|
|
|
|
at(tuple &t)
|
|
|
|
{
|
|
|
|
return at<name_hash(name), tuple>(t);
|
|
|
|
}
|
|
|
|
|
2017-10-05 01:25:15 +02:00
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i>
|
|
|
|
typename std::enable_if<i == size<tuple>(), void>::type
|
|
|
|
at(tuple &t,
|
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
2017-09-21 04:33:50 +02:00
|
|
|
{
|
2017-10-05 01:25:15 +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));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function,
|
|
|
|
size_t i>
|
|
|
|
typename std::enable_if<i == size<tuple>(), void>::type
|
|
|
|
at(const tuple &t,
|
|
|
|
const string_view &name,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
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-10-28 21:29:28 +02:00
|
|
|
template<class tuple,
|
|
|
|
class function>
|
|
|
|
void
|
|
|
|
for_each(const tuple &t,
|
|
|
|
const vector_view<const string_view> &mask,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
std::for_each(std::begin(mask), std::end(mask), [&t, &f]
|
|
|
|
(const auto &key)
|
|
|
|
{
|
|
|
|
at(t, key, [&f, &key]
|
|
|
|
(auto&& val)
|
|
|
|
{
|
|
|
|
f(key, val);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple,
|
|
|
|
class function>
|
|
|
|
void
|
|
|
|
for_each(tuple &t,
|
|
|
|
const vector_view<const string_view> &mask,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
std::for_each(std::begin(mask), std::end(mask), [&t, &f]
|
|
|
|
(const auto &key)
|
|
|
|
{
|
|
|
|
at(t, key, [&f, &key]
|
|
|
|
(auto&& val)
|
|
|
|
{
|
|
|
|
f(key, val);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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)):
|
2017-09-22 05:05:30 +02:00
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
|
|
|
class tuple,
|
|
|
|
class function>
|
|
|
|
typename std::enable_if<i == size<tuple>(), bool>::type
|
|
|
|
until(const tuple &a,
|
|
|
|
const tuple &b,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
|
|
|
class tuple,
|
|
|
|
class function>
|
|
|
|
typename std::enable_if<i < size<tuple>(), bool>::type
|
|
|
|
until(const tuple &a,
|
|
|
|
const tuple &b,
|
|
|
|
function&& f)
|
|
|
|
{
|
|
|
|
return f(key<i>(a), val<i>(a), val<i>(b))?
|
|
|
|
until<i + 1>(a, b, 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,
|
|
|
|
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 V,
|
|
|
|
class... T>
|
|
|
|
tuple<T...> &
|
|
|
|
set(tuple<T...> &t,
|
|
|
|
const string_view &key,
|
2017-09-21 04:33:50 +02:00
|
|
|
V&& val)
|
|
|
|
try
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
|
|
|
at(t, key, [&key, &val]
|
|
|
|
(auto &target)
|
|
|
|
{
|
2017-09-21 04:33:50 +02:00
|
|
|
_assign(target, std::forward<V>(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-09-21 04:33:50 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2017-10-03 12:59:14 +02:00
|
|
|
throw parse_error("failed to set member '%s' (from %s): %s",
|
2017-09-21 04:33:50 +02:00
|
|
|
key,
|
2017-10-03 12:59:14 +02:00
|
|
|
demangle<V>(),
|
2017-09-21 04:33:50 +02:00
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
tuple<T...> &
|
|
|
|
set(tuple<T...> &t,
|
|
|
|
const string_view &key,
|
|
|
|
const json::value &value)
|
|
|
|
{
|
|
|
|
switch(type(value))
|
|
|
|
{
|
|
|
|
case type::STRING:
|
|
|
|
case type::LITERAL:
|
|
|
|
set(t, key, string_view{value});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type::NUMBER:
|
|
|
|
if(value.floats)
|
|
|
|
set(t, key, value.floating);
|
|
|
|
else
|
|
|
|
set(t, key, value.integer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case type::OBJECT:
|
|
|
|
case type::ARRAY:
|
|
|
|
if(unlikely(!value.serial))
|
|
|
|
throw print_error("Type %s must be JSON to be used by tuple member '%s'",
|
|
|
|
reflect(type(value)),
|
|
|
|
key);
|
|
|
|
|
|
|
|
set(t, key, string_view{value});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
std::for_each(std::begin(object), std::end(object), [this]
|
|
|
|
(const auto &member)
|
2017-08-26 06:39:36 +02:00
|
|
|
{
|
2017-09-21 04:33:50 +02:00
|
|
|
set(*this, member.first, member.second);
|
2017-08-26 06:39:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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-21 04:33:50 +02:00
|
|
|
set(*this, member.first, member.second);
|
2017-09-08 21:29:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2017-09-21 04:33:50 +02:00
|
|
|
set(*this, member.first, member.second);
|
2017-09-08 10:41:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:25:39 +02:00
|
|
|
template<class... T>
|
|
|
|
template<class... U>
|
|
|
|
tuple<T...>::tuple(const tuple<U...> &t)
|
|
|
|
{
|
|
|
|
for_each(t, [this]
|
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
|
|
|
set(*this, key, val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:32:35 +01:00
|
|
|
template<class... T>
|
|
|
|
constexpr size_t
|
|
|
|
tuple<T...>::size()
|
|
|
|
{
|
|
|
|
return std::tuple_size<tuple_type>();
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
template<class tuple,
|
|
|
|
class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure>
|
2017-09-28 03:27:25 +02:00
|
|
|
constexpr auto
|
2017-09-08 16:47:07 +02:00
|
|
|
_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-28 03:27:25 +02:00
|
|
|
|
|
|
|
return it;
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class tuple,
|
|
|
|
class it_a,
|
|
|
|
class it_b>
|
2017-09-28 03:27:25 +02:00
|
|
|
constexpr auto
|
2017-09-08 10:41:02 +02:00
|
|
|
_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;
|
|
|
|
}
|
2017-09-28 03:27:25 +02:00
|
|
|
|
|
|
|
return it;
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class... T>
|
2017-09-28 03:27:25 +02:00
|
|
|
auto
|
2017-09-08 10:41:02 +02:00
|
|
|
_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-28 03:27:25 +02:00
|
|
|
|
|
|
|
return it;
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:22:41 +01:00
|
|
|
template<class... T>
|
|
|
|
struct tuple<T...>::keys
|
|
|
|
:std::array<string_view, tuple<T...>::size()>
|
|
|
|
{
|
|
|
|
struct selection;
|
|
|
|
struct include;
|
|
|
|
struct exclude;
|
|
|
|
|
|
|
|
constexpr keys()
|
|
|
|
{
|
|
|
|
_key_transform<tuple<T...>>(this->begin(), this->end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
struct tuple<T...>::keys::selection
|
|
|
|
:std::bitset<tuple<T...>::size()>
|
|
|
|
{
|
|
|
|
template<class closure>
|
|
|
|
constexpr bool until(closure &&function) const
|
|
|
|
{
|
|
|
|
for(size_t i(0); i < this->size(); ++i)
|
|
|
|
if(this->test(i))
|
|
|
|
if(!function(key<tuple<T...>, i>()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class closure>
|
|
|
|
constexpr void for_each(closure &&function) const
|
|
|
|
{
|
|
|
|
this->until([&function](auto&& key)
|
|
|
|
{
|
|
|
|
function(key);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it_a,
|
|
|
|
class it_b>
|
|
|
|
constexpr auto transform(it_a it, const it_b end) const
|
|
|
|
{
|
|
|
|
this->until([&it, &end](auto&& key)
|
|
|
|
{
|
|
|
|
if(it == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*it = key;
|
|
|
|
++it;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
struct tuple<T...>::keys::include
|
|
|
|
:selection
|
|
|
|
{
|
|
|
|
constexpr include(const std::initializer_list<string_view> &list)
|
|
|
|
{
|
|
|
|
for(const auto &key : list)
|
|
|
|
this->set(indexof<tuple<T...>>(key), true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
struct tuple<T...>::keys::exclude
|
|
|
|
:selection
|
|
|
|
{
|
|
|
|
constexpr exclude(const std::initializer_list<string_view> &list)
|
|
|
|
{
|
|
|
|
this->set();
|
|
|
|
for(const auto &key : list)
|
|
|
|
this->set(indexof<tuple<T...>>(key), false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure,
|
|
|
|
class... T>
|
2017-09-28 03:27:25 +02:00
|
|
|
auto
|
|
|
|
_member_transform_if(const tuple<T...> &tuple,
|
|
|
|
it_a it,
|
|
|
|
const it_b end,
|
|
|
|
closure&& lambda)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
|
|
|
until(tuple, [&it, &end, &lambda]
|
|
|
|
(const auto &key, const auto &val)
|
|
|
|
{
|
|
|
|
if(it == end)
|
|
|
|
return false;
|
|
|
|
|
2017-09-28 03:27:25 +02:00
|
|
|
if(lambda(*it, key, val))
|
|
|
|
++it;
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
return true;
|
|
|
|
});
|
2017-09-28 03:27:25 +02:00
|
|
|
|
|
|
|
return it;
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
2017-09-28 03:27:25 +02:00
|
|
|
class closure,
|
2017-09-08 10:41:02 +02:00
|
|
|
class... T>
|
2017-09-28 03:27:25 +02:00
|
|
|
auto
|
2017-09-08 10:41:02 +02:00
|
|
|
_member_transform(const tuple<T...> &tuple,
|
|
|
|
it_a it,
|
2017-09-28 03:27:25 +02:00
|
|
|
const it_b end,
|
|
|
|
closure&& lambda)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-28 03:27:25 +02:00
|
|
|
return _member_transform_if(tuple, it, end, [&lambda]
|
|
|
|
(auto&& ret, const auto &key, const auto &val)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-28 03:27:25 +02:00
|
|
|
ret = lambda(key, val);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2017-09-08 16:47:07 +02:00
|
|
|
|
2017-09-28 03:27:25 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class... T>
|
|
|
|
auto
|
|
|
|
_member_transform(const tuple<T...> &tuple,
|
|
|
|
it_a it,
|
|
|
|
const it_b end)
|
|
|
|
{
|
2018-04-04 23:00:57 +02:00
|
|
|
return _member_transform(tuple, it, end, []
|
|
|
|
(auto&& ret, const auto &key, const auto &val) -> member
|
2017-09-28 03:27:25 +02:00
|
|
|
{
|
2018-04-04 23:00:57 +02:00
|
|
|
return { key, val };
|
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()
|
|
|
|
};
|
|
|
|
|
2017-10-12 03:15:09 +02:00
|
|
|
std::array<size_t, member_count> sizes {0};
|
2017-09-28 03:27:25 +02:00
|
|
|
const auto e{_member_transform_if(t, begin(sizes), end(sizes), []
|
2017-10-12 03:15:09 +02:00
|
|
|
(auto &ret, const string_view &key, auto&& val)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-09-28 03:27:25 +02:00
|
|
|
if(!defined(val))
|
|
|
|
return false;
|
|
|
|
|
2017-10-12 03:15:09 +02:00
|
|
|
// " " : ,
|
2017-09-28 03:27:25 +02:00
|
|
|
ret = 1 + key.size() + 1 + 1 + serialized(val) + 1;
|
|
|
|
return true;
|
|
|
|
})};
|
2017-09-08 16:47:07 +02:00
|
|
|
|
2017-09-28 03:27:25 +02:00
|
|
|
// Subtract one to get the final size when an extra comma is
|
|
|
|
// accumulated on non-empty objects.
|
2017-10-12 03:15:09 +02:00
|
|
|
const auto overhead
|
|
|
|
{
|
|
|
|
1 + std::all_of(begin(sizes), e, is_zero{})
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::accumulate(begin(sizes), e, size_t(overhead));
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 11:23:32 +01:00
|
|
|
template<class... T>
|
|
|
|
size_t
|
|
|
|
serialized(const tuple<T...> *const &b,
|
|
|
|
const tuple<T...> *const &e)
|
|
|
|
{
|
|
|
|
size_t ret(1 + (b == e));
|
|
|
|
return std::accumulate(b, e, ret, []
|
|
|
|
(size_t ret, const tuple<T...> &t)
|
|
|
|
{
|
|
|
|
return ret += serialized(t) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-28 03:27:25 +02:00
|
|
|
const auto e{_member_transform_if(tuple, begin(members), end(members), []
|
2017-10-12 03:15:09 +02:00
|
|
|
(auto &ret, const string_view &key, auto&& val)
|
2017-09-28 03:27:25 +02:00
|
|
|
{
|
|
|
|
if(!defined(val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ret = member { key, val };
|
|
|
|
return true;
|
|
|
|
})};
|
|
|
|
|
|
|
|
return stringify(buf, begin(members), e);
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 11:23:32 +01:00
|
|
|
template<class... T>
|
|
|
|
string_view
|
|
|
|
stringify(mutable_buffer &buf,
|
|
|
|
const tuple<T...> *b,
|
|
|
|
const tuple<T...> *e)
|
|
|
|
{
|
|
|
|
const auto start(begin(buf));
|
|
|
|
consume(buf, copy(buf, "["_sv));
|
|
|
|
if(b != e)
|
|
|
|
{
|
|
|
|
stringify(buf, *b);
|
|
|
|
for(++b; b != e; ++b)
|
|
|
|
{
|
|
|
|
consume(buf, copy(buf, ","_sv));
|
|
|
|
stringify(buf, *b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consume(buf, copy(buf, "]"_sv));
|
|
|
|
return { start, begin(buf) };
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
template<class... T>
|
|
|
|
std::ostream &
|
|
|
|
operator<<(std::ostream &s, const tuple<T...> &t)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(t);
|
2017-09-08 10:41:02 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-10-16 06:22:52 +02:00
|
|
|
template<class... T>
|
|
|
|
tuple<T...>::operator
|
|
|
|
crh::sha256::buf()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: XXX
|
|
|
|
const auto preimage
|
|
|
|
{
|
|
|
|
json::strung(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
return crh::sha256::buf
|
|
|
|
{
|
|
|
|
[&preimage](auto &buf)
|
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
sha256{buf, const_buffer{preimage}};
|
2017-10-16 06:22:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-19 11:09:19 +02:00
|
|
|
template<class... T>
|
|
|
|
tuple<T...>::operator
|
|
|
|
json::value()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
json::value ret;
|
|
|
|
ret.type = OBJECT;
|
|
|
|
ret.create_string(serialized(*this), [this]
|
|
|
|
(mutable_buffer buffer)
|
|
|
|
{
|
|
|
|
stringify(buffer, *this);
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|