mirror of
https://github.com/matrix-construct/construct
synced 2024-12-26 15:33:54 +01: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};
|
||||
for(auto&& member : members)
|
||||
if(likely(i < size))
|
||||
new (nodes + i++) node(ret, std::move(member));
|
||||
new (nodes + i++) node(ret, json::member(member));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -24,16 +24,12 @@ namespace ircd::json
|
|||
/// 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.
|
||||
///
|
||||
/// 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
|
||||
:std::pair<value, value>
|
||||
{
|
||||
using std::pair<value, value>::pair;
|
||||
template<class K, class V> member(const K &k, V&& v);
|
||||
member(const string_view &key, value &&);
|
||||
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 object::member &m);
|
||||
member() = default;
|
||||
|
@ -60,13 +56,31 @@ namespace ircd::json
|
|||
string_view stringify(mutable_buffer &, const members &);
|
||||
}
|
||||
|
||||
template<class K,
|
||||
class V>
|
||||
ircd::json::member::member(const K &k,
|
||||
inline
|
||||
ircd::json::member::member(const string_view &key,
|
||||
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)
|
||||
: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 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 &);
|
||||
template<size_t N> value(const char (&)[N]);
|
||||
value(const char *const &, const enum type &);
|
||||
explicit value(const int64_t &);
|
||||
explicit value(const double &);
|
||||
explicit value(const bool &);
|
||||
value(const char *const &s);
|
||||
template<size_t N> value(const char (&)[N]);
|
||||
value(const string_view &sv);
|
||||
value(const char *const &s);
|
||||
value(const struct member *const &, const size_t &len);
|
||||
value(std::unique_ptr<const struct member[]>, const size_t &len); // alloc = true
|
||||
value(const struct value *const &, const size_t &len);
|
||||
|
@ -143,23 +145,14 @@ ircd::json::value::value(const string_view &sv,
|
|||
{}
|
||||
|
||||
inline
|
||||
ircd::json::value::value(const char *const &s)
|
||||
:string{s}
|
||||
,len{strlen(s)}
|
||||
,type{json::type(s, std::nothrow)}
|
||||
,serial{type == STRING? surrounds(s, '"') : true}
|
||||
,alloc{false}
|
||||
,floats{false}
|
||||
ircd::json::value::value(const char *const &str,
|
||||
const enum type &type)
|
||||
:value{string_view{str}, type}
|
||||
{}
|
||||
|
||||
template<size_t N>
|
||||
ircd::json::value::value(const char (&str)[N])
|
||||
:string{str}
|
||||
,len{strnlen(str, N)}
|
||||
,type{json::type(str, std::nothrow)}
|
||||
,serial{type == STRING? surrounds(str, '"') : true}
|
||||
,alloc{false}
|
||||
,floats{false}
|
||||
inline
|
||||
ircd::json::value::value(const char *const &str)
|
||||
:value{string_view{str}}
|
||||
{}
|
||||
|
||||
inline
|
||||
|
@ -167,6 +160,17 @@ ircd::json::value::value(const string_view &sv)
|
|||
: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
|
||||
ircd::json::value::value(const nullptr_t &)
|
||||
:value
|
||||
|
|
Loading…
Reference in a new issue