0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-10-02 05:38:53 +02:00

ircd::json: Add strongly typed empty() for object/array with an ADL.

This commit is contained in:
Jason Volk 2018-01-29 11:48:53 -08:00
parent aefca6bea4
commit 08d00bb3c8
2 changed files with 36 additions and 1 deletions

View file

@ -26,6 +26,8 @@ namespace ircd::json
{
struct array;
bool empty(const array &);
string_view stringify(mutable_buffer &buf, const string_view *const &begin, const string_view *const &end);
string_view stringify(mutable_buffer &buf, const std::string *const &begin, const std::string *const &end);
@ -59,8 +61,9 @@ struct ircd::json::array
const_iterator end() const;
const_iterator begin() const;
const_iterator find(size_t i) const;
bool empty() const;
size_t count() const;
size_t size() const;
@ -165,6 +168,21 @@ const
return std::distance(begin(), end());
}
inline bool
ircd::json::empty(const json::array &array)
{
return array.empty();
}
inline bool
ircd::json::array::empty()
const
{
const string_view &sv{*this};
assert(sv.size() > 2 || (sv.empty() || sv == empty_array));
return sv.size() <= 2;
}
inline bool
ircd::json::operator==(const array::const_iterator &a, const array::const_iterator &b)
{

View file

@ -26,6 +26,7 @@ namespace ircd::json
{
struct object;
bool empty(const object &);
template<name_hash_t key, class T = string_view> T at(const object &);
template<name_hash_t key, class T = string_view> T get(const object &, const T &def = {});
}
@ -89,6 +90,7 @@ struct ircd::json::object
const_iterator find(const name_hash_t &key) const;
// util
bool empty() const;
size_t count() const;
size_t size() const; // warns if used; use count()
bool has(const string_view &key) const;
@ -346,6 +348,21 @@ const
return std::distance(begin(), end());
}
inline bool
ircd::json::empty(const object &object)
{
return object.empty();
}
inline bool
ircd::json::object::empty()
const
{
const string_view &sv{*this};
assert(sv.size() > 2 || (sv.empty() || sv == empty_object));
return sv.size() <= 2;
}
inline bool
ircd::json::object::has(const path &path)
const