Add NewValue/DeleteValue to Variant for owned value storage

This commit is contained in:
Wojtek Figat
2023-08-16 18:29:59 +02:00
parent 18b47257fd
commit 0cb049167b
2 changed files with 53 additions and 0 deletions

View File

@@ -2912,6 +2912,53 @@ void Variant::Inline()
}
}
Variant Variant::NewValue(const StringAnsiView& typeName)
{
Variant v;
const ScriptingTypeHandle typeHandle = Scripting::FindScriptingType(typeName);
if (typeHandle)
{
const ScriptingType& type = typeHandle.GetType();
switch (type.Type)
{
case ScriptingTypes::Script:
v.SetType(VariantType(VariantType::Object, typeName));
break;
case ScriptingTypes::Structure:
v.SetType(VariantType(VariantType::Structure, typeName));
break;
case ScriptingTypes::Enum:
v.SetType(VariantType(VariantType::Enum, typeName));
v.AsUint64 = 0;
break;
}
}
else if (typeName.HasChars())
{
LOG(Warning, "Missing scripting type \'{0}\'", String(typeName));
}
return MoveTemp(v);
}
void Variant::DeleteValue()
{
// Delete any object owned by the Variant
switch (Type.Type)
{
case VariantType::Object:
if (AsObject)
{
AsObject->Deleted.Unbind<Variant, &Variant::OnObjectDeleted>(this);
AsObject->DeleteObject();
AsObject = nullptr;
}
break;
}
// Go back to null
SetType(VariantType(VariantType::Null));
}
bool Variant::CanCast(const Variant& v, const VariantType& to)
{
if (v.Type == to)

View File

@@ -362,6 +362,12 @@ public:
// Inlines potential value type into in-built format (eg. Vector3 stored as Structure, or String stored as ManagedObject).
void Inline();
// Allocates the Variant of the specific type (eg. structure or object or value).
static Variant NewValue(const StringAnsiView& typeName);
// Frees the object or data owned by this Variant container (eg. structure or object).
void DeleteValue();
FORCE_INLINE Variant Cast(const VariantType& to) const
{
return Cast(*this, to);