2016-11-29 16:23:38 +01: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-23 23:32:28 +02:00
|
|
|
#define HAVE_IRCD_JSON_INDEX_H
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace json {
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
struct index
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
struct member;
|
|
|
|
struct const_iterator;
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
using key_type = value;
|
|
|
|
using mapped_type = value;
|
2016-11-29 16:23:38 +01:00
|
|
|
using value_type = const member;
|
|
|
|
using size_type = size_t;
|
2017-03-16 21:32:42 +01:00
|
|
|
using difference_type = size_t;
|
2016-11-29 16:23:38 +01:00
|
|
|
using key_compare = std::less<member>;
|
2017-08-23 23:32:28 +02:00
|
|
|
using index_type = std::vector<member>;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
index_type idx;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
const_iterator begin() const;
|
|
|
|
const_iterator end() const;
|
|
|
|
const_iterator cbegin();
|
|
|
|
const_iterator cend();
|
|
|
|
|
|
|
|
bool empty() const;
|
|
|
|
size_t count() const;
|
|
|
|
size_t size() const;
|
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
const_iterator find(const string_view &name) const;
|
|
|
|
bool has(const string_view &name) const;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
const value &operator[](const string_view &name) const;
|
|
|
|
const value &at(const string_view &name) const;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
const_iterator erase(const const_iterator &s, const const_iterator &e);
|
|
|
|
void erase(const const_iterator &s);
|
|
|
|
bool erase(const string_view &name);
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
operator std::string() const;
|
2017-03-20 12:25:55 +01:00
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
IRCD_OVERLOAD(recursive)
|
2017-03-21 09:54:10 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
index(std::initializer_list<member>);
|
|
|
|
index(const object &d);
|
|
|
|
index(recursive_t, const object &d);
|
|
|
|
index() = default;
|
|
|
|
index(index &&) = default;
|
|
|
|
index(const index &) = delete;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
friend index &operator+=(index &, const object &); // integration
|
|
|
|
friend index operator+(const object &, const object &); // integral
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
friend object serialize(const index &, char *&start, char *const &stop);
|
|
|
|
friend size_t print(char *const &buf, const size_t &max, const index &);
|
|
|
|
friend std::ostream &operator<<(std::ostream &, const index &);
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
struct index::member
|
|
|
|
:std::pair<value, value>
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-20 12:25:55 +01:00
|
|
|
template<class K> member(const K &k, std::initializer_list<member> v);
|
|
|
|
template<class K, class V> member(const K &k, V&& v);
|
|
|
|
explicit member(const string_view &k);
|
2017-08-23 23:32:28 +02:00
|
|
|
explicit member(const object::member &m);
|
2017-03-16 21:32:42 +01:00
|
|
|
member() = default;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
friend bool operator==(const member &a, const member &b);
|
|
|
|
friend bool operator!=(const member &a, const member &b);
|
|
|
|
friend bool operator<(const member &a, const member &b);
|
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
friend bool operator==(const member &a, const string_view &b);
|
2017-03-20 12:25:55 +01:00
|
|
|
friend bool operator!=(const member &a, const string_view &b);
|
|
|
|
friend bool operator<(const member &a, const string_view &b);
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const member &);
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
struct index::const_iterator
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
using value_type = const member;
|
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
2017-03-16 21:32:42 +01:00
|
|
|
using difference_type = size_t;
|
2017-03-31 00:55:17 +02:00
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
protected:
|
2017-08-23 23:32:28 +02:00
|
|
|
friend class index;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
index::index_type::const_iterator it;
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
operator const auto &() const { return it; }
|
|
|
|
|
|
|
|
const_iterator(const decltype(it) &it)
|
2016-11-29 16:23:38 +01:00
|
|
|
:it{it}
|
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
|
|
|
value_type *operator->() const { return it.operator->(); }
|
|
|
|
value_type &operator*() const { return it.operator*(); }
|
|
|
|
|
|
|
|
const_iterator &operator++() { ++it; return *this; }
|
2017-03-31 00:55:17 +02:00
|
|
|
const_iterator &operator--() { --it; return *this; }
|
2017-03-20 12:25:55 +01:00
|
|
|
|
|
|
|
friend bool operator==(const const_iterator &a, const const_iterator &b);
|
|
|
|
friend bool operator!=(const const_iterator &a, const const_iterator &b);
|
|
|
|
friend bool operator<(const const_iterator &a, const const_iterator &b);
|
2016-11-29 16:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace json
|
|
|
|
} // namespace ircd
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline const ircd::json::value &
|
|
|
|
ircd::json::index::operator[](const string_view &name)
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
2017-03-16 21:32:42 +01:00
|
|
|
return at(name);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::has(const string_view &name)
|
2017-03-16 21:32:42 +01:00
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-16 21:32:42 +01:00
|
|
|
return find(name) != end();
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::index::const_iterator
|
|
|
|
ircd::json::index::find(const string_view &name)
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
2017-03-16 21:32:42 +01:00
|
|
|
return std::find(std::begin(idx), std::end(idx), name);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::count()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return idx.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::empty()
|
2016-11-29 16:23:38 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return idx.empty();
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::index::const_iterator
|
|
|
|
ircd::json::index::cbegin()
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
|
|
|
return { std::begin(idx) };
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::index::const_iterator
|
|
|
|
ircd::json::index::cend()
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
|
|
|
return { std::end(idx) };
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::index::const_iterator
|
|
|
|
ircd::json::index::begin()
|
2017-03-16 21:32:42 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return { std::begin(idx) };
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
inline ircd::json::index::const_iterator
|
|
|
|
ircd::json::index::end()
|
2017-03-16 21:32:42 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return { std::end(idx) };
|
|
|
|
}
|
|
|
|
|
2017-03-20 12:25:55 +01:00
|
|
|
template<class K,
|
|
|
|
class V>
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::member::member(const K &k,
|
|
|
|
V&& v)
|
|
|
|
:std::pair<value, value>
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
value { k }, value { std::forward<V>(v) }
|
2017-03-20 12:25:55 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
template<class K>
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::member::member(const K &k,
|
|
|
|
std::initializer_list<member> v)
|
|
|
|
:std::pair<value, value>
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
value { k }, value { std::make_unique<index>(std::move(v)) }
|
2017-03-20 12:25:55 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::member::member(const object::member &m)
|
|
|
|
:std::pair<value, value>
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
2017-08-23 23:32:28 +02:00
|
|
|
m.first, value { m.second, type(m.second) }
|
2017-03-20 12:25:55 +01:00
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::index::member::member(const string_view &k)
|
|
|
|
:std::pair<value, value>
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
k, string_view{}
|
|
|
|
}
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<(const index::member &a, const index::member &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator!=(const index::member &a, const index::member &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.first != b.first;
|
|
|
|
}
|
|
|
|
|
2017-03-16 21:32:42 +01:00
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator==(const index::member &a, const index::member &b)
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
|
|
|
return a.first == b.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<(const index::member &a, const string_view &b)
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
2017-03-20 12:25:55 +01:00
|
|
|
return string_view(a.first.string, a.first.len) < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator!=(const index::member &a, const string_view &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
2017-03-21 03:30:07 +01:00
|
|
|
return string_view(a.first.string, a.first.len) != b;
|
2017-03-16 21:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator==(const index::member &a, const string_view &b)
|
2017-03-16 21:32:42 +01:00
|
|
|
{
|
|
|
|
return string_view(a.first.string, a.first.len) == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<(const index::const_iterator &a, const index::const_iterator &b)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-20 12:25:55 +01:00
|
|
|
return a.it < b.it;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator!=(const index::const_iterator &a, const index::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.it != b.it;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator==(const index::const_iterator &a, const index::const_iterator &b)
|
2017-03-20 12:25:55 +01:00
|
|
|
{
|
|
|
|
return a.it == b.it;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|