2017-09-09 21:20:00 +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
|
|
|
|
#define HAVE_IRCD_JSON_IOV_H
|
|
|
|
|
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
struct iov;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
IRCD_EXCEPTION(json::error, error);
|
|
|
|
IRCD_EXCEPTION(error, exists);
|
|
|
|
|
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;
|
2017-09-09 21:20:00 +02: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;
|
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
|
|
|
|
|
|
|
friend string_view stringify(mutable_buffer &, const iov &);
|
2017-09-12 19:03:06 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const iov &);
|
|
|
|
friend size_t serialized(const iov &);
|
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
|
|
|
{
|
2017-10-16 06:21:16 +02:00
|
|
|
push(iov &iov, json::member m)
|
|
|
|
:node{iov, std::move(m)}
|
2017-09-12 19:03:06 +02:00
|
|
|
{}
|
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
|
|
|
{
|
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
|
|
|
/// iov::add only if the bool argument is true for your condition
|
2017-09-12 19:03:06 +02:00
|
|
|
struct ircd::json::iov::add_if
|
2017-09-14 20:30:06 +02:00
|
|
|
:protected ircd::json::iov::node
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2017-09-12 19:03:06 +02:00
|
|
|
add_if(iov &, const bool &, member);
|
|
|
|
add_if() = 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
|
|
|
{
|
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:30:04 +02:00
|
|
|
/// iov::set only if the bool argument is true for your condition
|
2017-09-12 19:03:06 +02:00
|
|
|
struct ircd::json::iov::set_if
|
2017-09-14 20:30:06 +02:00
|
|
|
:protected ircd::json::iov::node
|
2017-09-09 21:20:00 +02:00
|
|
|
{
|
2017-09-12 19:03:06 +02:00
|
|
|
set_if(iov &, const bool &, member);
|
|
|
|
set_if() = default;
|
|
|
|
};
|
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
|
|
|
|
{
|
|
|
|
defaults(iov &, member);
|
|
|
|
defaults() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// iov::defaults only if the bool argument is true for your condition
|
|
|
|
struct ircd::json::iov::defaults_if
|
|
|
|
:protected ircd::json::iov::node
|
|
|
|
{
|
|
|
|
defaults_if(iov &, const bool &, member);
|
|
|
|
defaults_if() = default;
|
|
|
|
};
|