mirror of
https://github.com/matrix-construct/construct
synced 2025-01-13 08:23:56 +01:00
ircd::json: Add val::empty() and full default initialization.
This commit is contained in:
parent
ff84531ea3
commit
673c8ca068
2 changed files with 29 additions and 2 deletions
|
@ -43,6 +43,7 @@ struct val
|
|||
uint64_t floats : 1;
|
||||
|
||||
public:
|
||||
bool empty() const;
|
||||
size_t size() const;
|
||||
operator string_view() const;
|
||||
explicit operator std::string() const;
|
||||
|
@ -54,7 +55,7 @@ struct val
|
|||
val(const char *const &s);
|
||||
val(const struct obj *const &); // alloc = false
|
||||
val(std::unique_ptr<obj>); // alloc = true
|
||||
val() = default;
|
||||
val();
|
||||
val(val &&) noexcept;
|
||||
val(const val &) = delete;
|
||||
val &operator=(val &&) noexcept;
|
||||
|
@ -82,6 +83,16 @@ static_assert(sizeof(val) == 16, "");
|
|||
} // namespace json
|
||||
} // namespace ircd
|
||||
|
||||
inline
|
||||
ircd::json::val::val()
|
||||
:string{""}
|
||||
,len{0}
|
||||
,type{STRING}
|
||||
,serial{true}
|
||||
,alloc{false}
|
||||
,floats{false}
|
||||
{}
|
||||
|
||||
template<size_t N>
|
||||
ircd::json::val::val(const char (&str)[N])
|
||||
:val{string_view{str}}
|
||||
|
|
18
ircd/json.cc
18
ircd/json.cc
|
@ -877,6 +877,22 @@ const
|
|||
throw type_error("value type[%d] is not a string", int(type));
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::json::val::empty()
|
||||
const
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case NUMBER: return lex_cast(integer).empty();
|
||||
case STRING: return !len;
|
||||
case OBJECT: return serial? !len : object? object->empty() : true;
|
||||
case ARRAY: return serial? !len : array? false : true; //TODO: XXX arr
|
||||
case LITERAL: return !len;
|
||||
};
|
||||
|
||||
throw type_error("deciding if a type[%u] is empty is undefined", int(type));
|
||||
}
|
||||
|
||||
size_t
|
||||
ircd::json::val::size()
|
||||
const
|
||||
|
@ -890,7 +906,7 @@ const
|
|||
case LITERAL: return len;
|
||||
};
|
||||
|
||||
throw type_error("cannot size type[%u]", int(type));
|
||||
throw type_error("deciding the size of a type[%u] is undefined", int(type));
|
||||
}
|
||||
|
||||
std::ostream &
|
||||
|
|
Loading…
Reference in a new issue