guest_stack_var: allow creation without constructing underlying type

This commit is contained in:
Hyper 2024-11-25 04:37:12 +00:00
parent 292fe71e05
commit be2ebebfda

View file

@ -6,7 +6,7 @@
// DO NOT use this type as anything other than a local variable. // DO NOT use this type as anything other than a local variable.
// This includes returning. It'll cause memory to leak in the guest stack! // This includes returning. It'll cause memory to leak in the guest stack!
template<typename T> template<typename T, bool Init = true>
class guest_stack_var class guest_stack_var
{ {
private: private:
@ -36,18 +36,24 @@ public:
guest_stack_var(Args&&... args) guest_stack_var(Args&&... args)
{ {
AllocGuestStackMemory(); AllocGuestStackMemory();
if (Init)
new (get()) T(std::forward<Args>(args)...); new (get()) T(std::forward<Args>(args)...);
} }
guest_stack_var(const guest_stack_var<T>& other) guest_stack_var(const guest_stack_var<T>& other)
{ {
AllocGuestStackMemory(); AllocGuestStackMemory();
if (Init)
new (get()) T(*other->get()); new (get()) T(*other->get());
} }
guest_stack_var(guest_stack_var<T>&& other) guest_stack_var(guest_stack_var<T>&& other)
{ {
AllocGuestStackMemory(); AllocGuestStackMemory();
if (Init)
new (get()) T(std::move(*other->get())); new (get()) T(std::move(*other->get()));
} }