2018-02-04 03:22:01 +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-09-09 21:20:00 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JSON_IOV_H
|
|
|
|
|
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct iov;
|
2018-01-24 17:07:36 +01:00
|
|
|
|
2019-02-16 01:47:00 +01:00
|
|
|
size_t serialized(const iov &);
|
|
|
|
string_view stringify(mutable_buffer &, const iov &);
|
|
|
|
std::ostream &operator<<(std::ostream &, const iov &);
|
|
|
|
|
2019-06-21 12:26:46 +02:00
|
|
|
template<class node, size_t SIZE, class T> iov &make_iov(iov &, node (&)[SIZE], T&& t);
|
|
|
|
template<class node, class T> iov &make_iov(iov &, node *const, const size_t &, T&& t);
|
2017-09-09 21:20:00 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 19:03:06 +02:00
|
|
|
/// A forward list to compose JSON efficiently on the stack.
|
|
|
|
///
|
|
|
|
/// The IOV gathers members for a JSON object being assembled from various
|
|
|
|
/// sources and presents an iteration to a generator. This prevents the need
|
|
|
|
/// for multiple generations and copying to occur before the final JSON is
|
|
|
|
/// realized, if ever.
|
|
|
|
///
|
|
|
|
/// Add and remove items on the IOV by construction and destruction one of
|
|
|
|
/// the node objects. The IOV has a standard forward list interface, only use
|
|
|
|
/// that to observe and sort/rearrange the IOV. Do not add or remove things
|
|
|
|
/// that way.
|
|
|
|
///
|
|
|
|
/// Nodes support a single member each. To support initializer_list syntax
|
|
|
|
/// the iov allocates and internally manages the iov node that should have
|
|
|
|
/// been on your stack.
|
|
|
|
///
|
2017-09-09 21:20:00 +02:00
|
|
|
struct ircd::json::iov
|
2017-09-12 19:03:06 +02:00
|
|
|
:ircd::iov<ircd::json::member>
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2017-09-12 19:03:06 +02:00
|
|
|
struct push;
|
|
|
|
struct add;
|
|
|
|
struct add_if;
|
|
|
|
struct set;
|
|
|
|
struct set_if;
|
2017-10-25 18:29:34 +02:00
|
|
|
struct defaults;
|
|
|
|
struct defaults_if;
|
2018-06-05 20:16:52 +02:00
|
|
|
using conditional_member = std::pair<string_view, std::function<json::value ()>>;
|
2017-09-09 21:20:00 +02:00
|
|
|
|
2018-03-17 19:52:28 +01:00
|
|
|
IRCD_EXCEPTION(json::error, error);
|
|
|
|
IRCD_EXCEPTION(error, exists);
|
|
|
|
IRCD_EXCEPTION(error, oversize);
|
|
|
|
|
2022-06-11 21:41:30 +02:00
|
|
|
static constexpr const uint &max_size {1024};
|
2018-03-17 19:52:28 +01:00
|
|
|
|
2017-09-12 19:03:06 +02:00
|
|
|
public:
|
2017-09-09 21:20:00 +02:00
|
|
|
bool has(const string_view &key) const;
|
2017-09-12 19:03:06 +02:00
|
|
|
const value &at(const string_view &key) const;
|
2018-05-26 02:45:49 +02:00
|
|
|
value &at(const string_view &key);
|
2017-09-09 21:20:00 +02:00
|
|
|
|
2017-09-12 19:03:06 +02:00
|
|
|
iov() = default;
|
2017-09-09 21:20:00 +02:00
|
|
|
};
|
|
|
|
|
2017-10-25 18:30:04 +02:00
|
|
|
/// Unconditionally append a member to the object vector
|
2017-09-09 21:20:00 +02:00
|
|
|
struct ircd::json::iov::push
|
2017-09-12 19:03:06 +02:00
|
|
|
:protected ircd::json::iov::node
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2018-02-19 06:14:45 +01:00
|
|
|
operator const member &() const;
|
|
|
|
operator member &();
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
push(iov &, const bool &, const conditional_member &);
|
|
|
|
push(iov &iov, member m);
|
2017-09-14 20:30:06 +02:00
|
|
|
push() = default;
|
2017-09-09 21:20:00 +02:00
|
|
|
};
|
|
|
|
|
2017-10-25 18:30:04 +02:00
|
|
|
/// Add a new member to the object vector; throws if exists
|
2017-09-09 21:20:00 +02:00
|
|
|
struct ircd::json::iov::add
|
2017-09-12 19:03:06 +02:00
|
|
|
:protected ircd::json::iov::node
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
add(iov &, const bool &, const conditional_member &);
|
2017-09-12 19:03:06 +02:00
|
|
|
add(iov &, member);
|
|
|
|
add() = default;
|
2017-09-09 21:20:00 +02:00
|
|
|
};
|
|
|
|
|
2017-10-25 18:30:04 +02:00
|
|
|
/// Add or overwrite a member in the object vector.
|
2017-09-12 19:03:06 +02:00
|
|
|
struct ircd::json::iov::set
|
|
|
|
:protected ircd::json::iov::node
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
set(iov &, const bool &, const conditional_member &);
|
2017-09-12 19:03:06 +02:00
|
|
|
set(iov &, member);
|
|
|
|
set() = default;
|
|
|
|
};
|
2017-09-09 21:20:00 +02:00
|
|
|
|
2017-10-25 18:29:34 +02:00
|
|
|
/// Add member to the object vector if doesn't exist; otherwise ignored
|
|
|
|
struct ircd::json::iov::defaults
|
|
|
|
:protected ircd::json::iov::node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
defaults(iov &, bool, const conditional_member &);
|
2017-10-25 18:29:34 +02:00
|
|
|
defaults(iov &, member);
|
|
|
|
defaults() = default;
|
|
|
|
};
|
|
|
|
|
2018-02-19 06:14:45 +01:00
|
|
|
inline ircd::json::iov::push::operator
|
|
|
|
ircd::json::member &()
|
|
|
|
{
|
|
|
|
auto &node(static_cast<iov::node &>(*this));
|
|
|
|
return static_cast<member &>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::json::iov::push::operator
|
|
|
|
const ircd::json::member &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &node(static_cast<const iov::node &>(*this));
|
|
|
|
return static_cast<const member &>(node);
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:07:36 +01:00
|
|
|
/// Conversion/Generator template. This reduces boilerplate when converting
|
|
|
|
/// some iterable collection of members to an iov. You have to pre-place the
|
|
|
|
/// nodes for the iov ahead of this function and they will be filled in.
|
|
|
|
template<class node,
|
|
|
|
size_t size,
|
|
|
|
class T>
|
2018-02-19 06:14:45 +01:00
|
|
|
ircd::json::iov &
|
|
|
|
ircd::json::make_iov(iov &ret,
|
|
|
|
node (&nodes)[size],
|
2018-01-24 17:07:36 +01:00
|
|
|
T&& members)
|
|
|
|
{
|
2018-02-19 06:14:45 +01:00
|
|
|
return make_iov<node, T>(ret, nodes, size, std::forward<T>(members));
|
2018-01-24 17:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversion/Generator template. This reduces boilerplate when converting
|
|
|
|
/// some iterable collection of members to an iov. You have to pre-place the
|
|
|
|
/// nodes for the iov ahead of this function. This overload takes a dynamic
|
|
|
|
/// sized array, you have to pass the size.
|
|
|
|
template<class node,
|
|
|
|
class T>
|
2018-02-19 06:14:45 +01:00
|
|
|
ircd::json::iov &
|
|
|
|
ircd::json::make_iov(iov &ret,
|
|
|
|
node *const nodes,
|
2018-01-24 17:07:36 +01:00
|
|
|
const size_t &size,
|
|
|
|
T&& members)
|
|
|
|
{
|
|
|
|
size_t i{0};
|
|
|
|
for(auto&& member : members)
|
|
|
|
if(likely(i < size))
|
2018-02-19 01:48:18 +01:00
|
|
|
new (nodes + i++) node(ret, json::member(member));
|
2018-01-24 17:07:36 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|