Add Value::clear()

This commit is contained in:
Michael Davidsaver
2022-11-27 08:20:13 -08:00
parent bdcbf130d2
commit 53f83b6429
3 changed files with 79 additions and 1 deletions
+35
View File
@@ -175,6 +175,41 @@ Value Value::allocMember()
return Value::Helper::build(fld, *this);
}
void Value::clear()
{
if(!desc)
return;
for(auto i : range(size_t(0u), desc->size())) {
auto& s = store.get()[i];
s.valid = false;
switch(s.code) {
case StoreType::Array:
s.as<shared_array<const void>>().clear();
break;
case StoreType::Compound:
{
auto& v = s.as<Value>();
v.desc = nullptr;
v.store.reset();
}
break;
case StoreType::String:
s.as<std::string>().clear();
break;
case StoreType::Null:
break; // nothing to do
case StoreType::Bool:
case StoreType::UInteger:
case StoreType::Integer:
case StoreType::Real:
memset(&s.store, 0, sizeof(s.store)); // just zero
break;
}
}
}
bool Value::isMarked(bool parents, bool children) const
{
if(!desc)
+9
View File
@@ -550,6 +550,15 @@ public:
//! Use to allocate members for an array of Struct and array of Union
Value allocMember();
/** Restore to newly allocated state.
*
* Free any allocation for array or string values, zero numeric values.
* unmark() all fields.
*
* @since UNRELEASED
*/
void clear();
//! Does this Value actually reference some underlying storage
inline bool valid() const { return desc; }
inline explicit operator bool() const { return desc; }