/* * Copyright (C) 2017 Charybdis Development Team * Copyright (C) 2017 Jason Volk * * 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_TUPLE_H namespace ircd { namespace json { /// All tuple templates inherit from this non-template type for tagging. struct tuple_base { // EBO tag }; /// A compile-time construct to describe a JSON object's members and types. /// /// 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. /// /// 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 /// without any effort from a developer when creating a new tuple. /// /// The member structure for the tuple is called `property` because json::member /// is already used to pair together runtime oriented json::values. /// /// 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. /// /// 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". /// template struct tuple :std::tuple ,tuple_base { using tuple_type = std::tuple; using super_type = tuple; operator json::value() const; static constexpr size_t size(); tuple(const json::object &); tuple(const json::iov &); tuple(const std::initializer_list &); tuple() = default; }; template constexpr bool is_tuple() { return std::is_base_of::value; } template using enable_if_tuple = typename std::enable_if(), R>::type; template using tuple_type = typename tuple::tuple_type; template using tuple_size = std::tuple_size>; template using tuple_element = typename std::tuple_element>::type; template using tuple_value_type = typename tuple_element::value_type; template constexpr size_t tuple::size() { return std::tuple_size(); } template auto & stdcast(const tuple &o) { return static_cast(o); } template auto & stdcast(tuple &o) { return static_cast(o); } template constexpr enable_if_tuple size() { return tuple_size::value; } template constexpr enable_if_tuple key() { return tuple_element::key; } template enable_if_tuple key(const tuple &t) { return std::get(t).key; } template constexpr typename std::enable_if(), size_t>::type indexof() { return size(); } template constexpr typename std::enable_if(), size_t>::type indexof() { constexpr auto equal { ircd::hash(key()) == hash }; return equal? i : indexof(); } template constexpr typename std::enable_if(), size_t>::type indexof(const char *const &name) { return size(); } template constexpr typename std::enable_if(), size_t>::type indexof(const char *const &name) { constexpr auto equal { _constexpr_equal(key(), name) }; return equal? i : indexof(name); } template constexpr typename std::enable_if(), size_t>::type indexof(const string_view &name) { return size(); } template constexpr typename std::enable_if(), size_t>::type indexof(const string_view &name) { const auto equal { name == key() }; return equal? i : indexof(name); } template constexpr bool key_exists(const string_view &key) { return indexof(key) < size(); } template enable_if_tuple &> val(tuple &t) { return static_cast &>(std::get(t)); } template enable_if_tuple &> val(const tuple &t) { return static_cast &>(std::get(t)); } template const tuple_value_type, indexof, hash>()> & get(const tuple &t) { constexpr size_t idx { indexof, hash>() }; return val(t); } template tuple_value_type, indexof, hash>()> get(const tuple &t, const tuple_value_type, indexof, hash>()> &def) { constexpr size_t idx { indexof, hash>() }; const auto &ret { val(t) }; using value_type = tuple_value_type, idx>; //TODO: undefined return ret != value_type{}? ret : def; } template tuple_value_type, indexof, hash>()> & get(tuple &t) { constexpr size_t idx { indexof, hash>() }; return val(t); } template tuple_value_type, indexof, hash>()> & get(tuple &t, tuple_value_type, indexof, hash>()> &def) { //TODO: undefined auto &ret{get(t)}; using value_type = decltype(ret); return ret != value_type{}? ret : def; } template enable_if_tuple()> &> at(const tuple &t) { constexpr size_t idx { indexof() }; auto &ret { val(t) }; using value_type = tuple_value_type; //TODO: undefined if(ret == value_type{}) throw not_found("%s", key(t)); return ret; } template enable_if_tuple()> &> at(tuple &t) { constexpr size_t idx { indexof() }; auto &ret { val(t) }; using value_type = tuple_value_type; //TODO: undefined if(ret == value_type{}) throw not_found("%s", key(t)); return ret; } template enable_if_tuple(name)> &> at(const tuple &t) { constexpr size_t idx { indexof(name) }; auto &ret { val(t) }; using value_type = tuple_value_type; //TODO: undefined if(ret == value_type{}) throw not_found("%s", name); return ret; } template enable_if_tuple(name)> &> at(tuple &t) { constexpr size_t idx { indexof(name) }; auto &ret { val(t) }; using value_type = tuple_value_type; //TODO: undefined if(ret == value_type{}) throw not_found("%s", name); return ret; } template typename std::enable_if(), void>::type at(tuple &t, const string_view &name, function&& f) { } template typename std::enable_if(), void>::type at(tuple &t, const string_view &name, function&& f) { if(indexof(name) == i) f(val(t)); else at(t, name, std::forward(f)); } template typename std::enable_if(), void>::type at(const tuple &t, const string_view &name, function&& f) { } template typename std::enable_if(), void>::type at(const tuple &t, const string_view &name, function&& f) { if(indexof(name) == i) f(val(t)); else at(t, name, std::forward(f)); } template typename std::enable_if(), void>::type for_each(const tuple &t, function&& f) {} template typename std::enable_if(), void>::type for_each(tuple &t, function&& f) {} template typename std::enable_if(), void>::type for_each(const tuple &t, function&& f) { f(key(t), val(t)); for_each(t, std::forward(f)); } template typename std::enable_if(), void>::type for_each(tuple &t, function&& f) { f(key(t), val(t)); for_each(t, std::forward(f)); } template typename std::enable_if<(i < 0), void>::type rfor_each(const tuple &t, function&& f) {} template typename std::enable_if<(i < 0), void>::type rfor_each(tuple &t, function&& f) {} template() - 1> typename std::enable_if(), void>::type rfor_each(const tuple &t, function&& f) { f(key(t), val(t)); rfor_each(t, std::forward(f)); } template() - 1> typename std::enable_if(), void>::type rfor_each(tuple &t, function&& f) { f(key(t), val(t)); rfor_each(t, std::forward(f)); } template typename std::enable_if(), bool>::type until(const tuple &t, function&& f) { return true; } template typename std::enable_if(), bool>::type until(tuple &t, function&& f) { return true; } template typename std::enable_if(), bool>::type until(const tuple &t, function&& f) { return f(key(t), val(t))? until(t, std::forward(f)): false; } template typename std::enable_if(), bool>::type until(tuple &t, function&& f) { return f(key(t), val(t))? until(t, std::forward(f)): false; } template typename std::enable_if(), bool>::type until(const tuple &a, const tuple &b, function&& f) { return true; } template typename std::enable_if(), bool>::type until(const tuple &a, const tuple &b, function&& f) { return f(key(a), val(a), val(b))? until(a, b, std::forward(f)): false; } template typename std::enable_if<(i < 0), bool>::type runtil(const tuple &t, function&& f) { return true; } template typename std::enable_if<(i < 0), bool>::type runtil(tuple &t, function&& f) { return true; } template() - 1> typename std::enable_if(), bool>::type runtil(const tuple &t, function&& f) { return f(key(t), val(t))? runtil(t, std::forward(f)): false; } template() - 1> typename std::enable_if(), bool>::type runtil(tuple &t, function&& f) { return f(key(t), val(t))? runtil(t, std::forward(f)): false; } template typename std::enable_if < std::is_convertible::value, void>::type _assign(dst &d, src&& s) { d = std::forward(s); } template typename std::enable_if < std::is_arithmetic() && std::is_base_of::type>() && !std::is_base_of, typename std::remove_reference::type>(), void>::type _assign(dst &d, src&& s) try { d = lex_cast(std::forward(s)); } catch(const bad_lex_cast &e) { throw parse_error("cannot convert '%s' to '%s'", demangle(), demangle()); } template typename std::enable_if < std::is_arithmetic() && std::is_base_of, typename std::remove_reference::type>(), void>::type _assign(dst &d, src&& s) { assert(!s.empty()); d = byte_view(std::forward(s)); } template typename std::enable_if < std::is_base_of() && std::is_pod::type>(), void>::type _assign(dst &d, src&& s) { d = byte_view(std::forward(s)); } template typename std::enable_if < ircd::json::is_tuple(), void>::type _assign(dst &d, src&& s) { d = dst{std::forward(s)}; } template tuple & set(tuple &t, const string_view &key, V&& val) try { at(t, key, [&key, &val] (auto &target) { _assign(target, std::forward(val)); }); return t; } catch(const std::exception &e) { throw parse_error("failed to set member '%s' (from %s): %s", key, demangle(), e.what()); } template tuple & set(tuple &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; } template tuple::tuple(const json::object &object) { std::for_each(std::begin(object), std::end(object), [this] (const auto &member) { set(*this, member.first, member.second); }); } template tuple::tuple(const json::iov &iov) { std::for_each(std::begin(iov), std::end(iov), [this] (const auto &member) { set(*this, member.first, member.second); }); } template tuple::tuple(const std::initializer_list &members) { std::for_each(std::begin(members), std::end(members), [this] (const auto &member) { set(*this, member.first, member.second); }); } template constexpr auto _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()); ++it; } return it; } template constexpr auto _key_transform(it_a it, const it_b end) { for(size_t i(0); i < tuple::size() && it != end; ++i) { *it = key(); ++it; } return it; } template auto _key_transform(const tuple &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; } }); return it; } template auto _member_transform_if(const tuple &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; if(lambda(*it, key, val)) ++it; return true; }); return it; } template auto _member_transform(const tuple &tuple, it_a it, const it_b end, closure&& lambda) { return _member_transform_if(tuple, it, end, [&lambda] (auto&& ret, const auto &key, const auto &val) { ret = lambda(key, val); return true; }); } template auto _member_transform(const tuple &tuple, it_a it, const it_b end) { return _member_transform_if(tuple, it, end, [] (auto&& ret, const auto &key, const auto &val) { ret = member { key, val }; return true; }); } template constexpr bool serialized_lex_cast() { using type = typename std::remove_reference::type; return std::is_arithmetic::value; } template typename std::enable_if(), size_t>::type serialized(T&& t) { return lex_cast(t).size(); } template typename std::enable_if(), bool>::type defined(T&& t) { return t != T{0}; } template size_t serialized(const tuple &t) { constexpr const size_t member_count { tuple::size() }; std::array sizes {0}; const auto e{_member_transform_if(t, begin(sizes), end(sizes), [] (auto &ret, const string_view &key, auto&& val) { if(!defined(val)) return false; // " " : , ret = 1 + key.size() + 1 + 1 + serialized(val) + 1; return true; })}; // Subtract one to get the final size when an extra comma is // accumulated on non-empty objects. const auto overhead { 1 + std::all_of(begin(sizes), e, is_zero{}) }; return std::accumulate(begin(sizes), e, size_t(overhead)); } template string_view stringify(mutable_buffer &buf, const tuple &tuple) { std::array members; std::sort(begin(members), end(members), [] (const auto &a, const auto &b) { return a.first < b.first; }); const auto e{_member_transform_if(tuple, begin(members), end(members), [] (auto &ret, const string_view &key, auto&& val) { if(!defined(val)) return false; ret = member { key, val }; return true; })}; return stringify(buf, begin(members), e); } template std::ostream & operator<<(std::ostream &s, const tuple &t) { s << json::string(t); return s; } template tuple::operator json::value() const { json::value ret; ret.type = OBJECT; ret.create_string(serialized(*this), [this] (mutable_buffer buffer) { stringify(buffer, *this); }); return ret; } } // namespace json } // namespace ircd