mirror of
https://github.com/matrix-construct/construct
synced 2024-10-02 13:48:53 +02:00
ircd::json: Improve the member/value construction relationship.
This commit is contained in:
parent
3683341aa9
commit
23fb76ae8e
3 changed files with 47 additions and 29 deletions
|
@ -166,7 +166,7 @@ ircd::json::make_iov(iov &ret,
|
||||||
size_t i{0};
|
size_t i{0};
|
||||||
for(auto&& member : members)
|
for(auto&& member : members)
|
||||||
if(likely(i < size))
|
if(likely(i < size))
|
||||||
new (nodes + i++) node(ret, std::move(member));
|
new (nodes + i++) node(ret, json::member(member));
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,16 +24,12 @@ namespace ircd::json
|
||||||
/// This is slightly heavier than object::member as that only deals with
|
/// This is slightly heavier than object::member as that only deals with
|
||||||
/// a pair of strings while the value here holds more diverse native state.
|
/// a pair of strings while the value here holds more diverse native state.
|
||||||
///
|
///
|
||||||
/// The key value (member.first) should always be a STRING type. We don't use
|
|
||||||
/// string_view directly in member.first because json::value can take ownership
|
|
||||||
/// of a string or use a literal depending on the circumstance and it's more
|
|
||||||
/// consistent this way.
|
|
||||||
///
|
|
||||||
struct ircd::json::member
|
struct ircd::json::member
|
||||||
:std::pair<value, value>
|
:std::pair<value, value>
|
||||||
{
|
{
|
||||||
using std::pair<value, value>::pair;
|
member(const string_view &key, value &&);
|
||||||
template<class K, class V> member(const K &k, V&& v);
|
template<class V> member(const string_view &key, V&&);
|
||||||
|
template<class V, size_t N> member(const char (&)[N], V&&);
|
||||||
explicit member(const string_view &k);
|
explicit member(const string_view &k);
|
||||||
explicit member(const object::member &m);
|
explicit member(const object::member &m);
|
||||||
member() = default;
|
member() = default;
|
||||||
|
@ -60,13 +56,31 @@ namespace ircd::json
|
||||||
string_view stringify(mutable_buffer &, const members &);
|
string_view stringify(mutable_buffer &, const members &);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class K,
|
inline
|
||||||
class V>
|
ircd::json::member::member(const string_view &key,
|
||||||
ircd::json::member::member(const K &k,
|
value &&v)
|
||||||
|
:std::pair<value, value>
|
||||||
|
{
|
||||||
|
{ key, json::STRING }, std::move(v)
|
||||||
|
}
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<class V,
|
||||||
|
size_t N>
|
||||||
|
ircd::json::member::member(const char (&key)[N],
|
||||||
V&& v)
|
V&& v)
|
||||||
:std::pair<value, value>
|
:std::pair<value, value>
|
||||||
{
|
{
|
||||||
value { k }, value { std::forward<V>(v) }
|
{ key, json::STRING }, std::forward<V>(v)
|
||||||
|
}
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<class V>
|
||||||
|
ircd::json::member::member(const string_view &key,
|
||||||
|
V&& v)
|
||||||
|
:std::pair<value, value>
|
||||||
|
{
|
||||||
|
{ key, json::STRING }, std::forward<V>(v)
|
||||||
}
|
}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
@ -85,13 +85,15 @@ struct ircd::json::value
|
||||||
explicit operator int64_t() const;
|
explicit operator int64_t() const;
|
||||||
explicit operator std::string() const; ///< NOTE full stringify() of value
|
explicit operator std::string() const; ///< NOTE full stringify() of value
|
||||||
|
|
||||||
|
template<size_t N> value(const char (&)[N], const enum type &);
|
||||||
value(const string_view &sv, const enum type &);
|
value(const string_view &sv, const enum type &);
|
||||||
template<size_t N> value(const char (&)[N]);
|
value(const char *const &, const enum type &);
|
||||||
explicit value(const int64_t &);
|
explicit value(const int64_t &);
|
||||||
explicit value(const double &);
|
explicit value(const double &);
|
||||||
explicit value(const bool &);
|
explicit value(const bool &);
|
||||||
value(const char *const &s);
|
template<size_t N> value(const char (&)[N]);
|
||||||
value(const string_view &sv);
|
value(const string_view &sv);
|
||||||
|
value(const char *const &s);
|
||||||
value(const struct member *const &, const size_t &len);
|
value(const struct member *const &, const size_t &len);
|
||||||
value(std::unique_ptr<const struct member[]>, const size_t &len); // alloc = true
|
value(std::unique_ptr<const struct member[]>, const size_t &len); // alloc = true
|
||||||
value(const struct value *const &, const size_t &len);
|
value(const struct value *const &, const size_t &len);
|
||||||
|
@ -143,23 +145,14 @@ ircd::json::value::value(const string_view &sv,
|
||||||
{}
|
{}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
ircd::json::value::value(const char *const &s)
|
ircd::json::value::value(const char *const &str,
|
||||||
:string{s}
|
const enum type &type)
|
||||||
,len{strlen(s)}
|
:value{string_view{str}, type}
|
||||||
,type{json::type(s, std::nothrow)}
|
|
||||||
,serial{type == STRING? surrounds(s, '"') : true}
|
|
||||||
,alloc{false}
|
|
||||||
,floats{false}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<size_t N>
|
inline
|
||||||
ircd::json::value::value(const char (&str)[N])
|
ircd::json::value::value(const char *const &str)
|
||||||
:string{str}
|
:value{string_view{str}}
|
||||||
,len{strnlen(str, N)}
|
|
||||||
,type{json::type(str, std::nothrow)}
|
|
||||||
,serial{type == STRING? surrounds(str, '"') : true}
|
|
||||||
,alloc{false}
|
|
||||||
,floats{false}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
|
@ -167,6 +160,17 @@ ircd::json::value::value(const string_view &sv)
|
||||||
:value{sv, json::type(sv, std::nothrow)}
|
:value{sv, json::type(sv, std::nothrow)}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
template<size_t N>
|
||||||
|
ircd::json::value::value(const char (&str)[N])
|
||||||
|
:value{string_view{str, strnlen(str, N)}}
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<size_t N>
|
||||||
|
ircd::json::value::value(const char (&str)[N],
|
||||||
|
const enum type &type)
|
||||||
|
:value{string_view{str, strnlen(str, N)}, type}
|
||||||
|
{}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
ircd::json::value::value(const nullptr_t &)
|
ircd::json::value::value(const nullptr_t &)
|
||||||
:value
|
:value
|
||||||
|
|
Loading…
Reference in a new issue