David Snopek 2024-01-31 21:38:44 +00:00 committed by GitHub
commit 6e624af301
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View File

@ -190,6 +190,25 @@ struct _GlobalNilClass {
static _GlobalNil _nil; static _GlobalNil _nil;
}; };
template <class T, size_t Alignment = alignof(T)>
class UninitializedValue {
typename std::aligned_storage<sizeof(T), Alignment>::type data;
public:
UninitializedValue() {}
UninitializedValue(const UninitializedValue &) = delete;
UninitializedValue &operator=(const UninitializedValue &) = delete;
T &get() {
return *reinterpret_cast<T *>(&data);
}
T *ptr() {
return reinterpret_cast<T *>(&data);
}
};
} // namespace godot } // namespace godot
#endif // GODOT_MEMORY_HPP #endif // GODOT_MEMORY_HPP

View File

@ -459,15 +459,15 @@ Variant::operator Signal() const {
} }
Variant::operator Dictionary() const { Variant::operator Dictionary() const {
Dictionary result; UninitializedValue<Dictionary> result;
to_type_constructor[DICTIONARY](result._native_ptr(), _native_ptr()); to_type_constructor[DICTIONARY](result.ptr(), _native_ptr());
return result; return result.get();
} }
Variant::operator Array() const { Variant::operator Array() const {
Array result; UninitializedValue<Array> result;
to_type_constructor[ARRAY](result._native_ptr(), _native_ptr()); to_type_constructor[ARRAY](result.ptr(), _native_ptr());
return result; return result.get();
} }
Variant::operator PackedByteArray() const { Variant::operator PackedByteArray() const {