MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
ScopeObject.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10*/
11
12#pragma once
13
14#include "CoreMinimal.h"
15#include "UObject/StrongObjectPtr.h"
16#include "Mcro/UObjects/Init.h"
17
19{
20 /**
21 * @brief
22 * A struct to emulate regular C++ RAII object behavior with UObjects. When this struct is instantiated the given
23 * object type is also created. An object wrapped in this struct is never invalid, and doesn't get garbage
24 * collected until it's in scope.
25 *
26 * It is safe to create this struct on any thread, GC is deferred until constructor is finished. The underlying
27 * storage is a `TStrongObjectPtr`.
28 *
29 * If the given object type happen to have an `Intialize` member method, that's also called with the extra
30 * arguments provided in the constructor.
31 */
32 template <CUObject T>
34 {
35 template <typename... Args>
36 TScopeObject(FConstructObjectParameters&& params, Args&&... args)
37 {
38 FGCScopeGuard avoidGC;
39 Storage.Reset(NewInit<T>(FWD(params), FWD(args)...));
40 }
41
42 const T* operator -> () const { return Storage.Get(); }
43 T* operator -> () { return Storage.Get(); }
44
45 operator const T* () const { return Storage.Get(); }
46 operator T* () { return Storage.Get(); }
47
48 T const& Get() const { return *Storage.Get(); }
49 T& Get() { return *Storage.Get(); }
50
51 private:
52 TStrongObjectPtr<T> Storage;
53 };
54}
#define FWD(...)
Shorten forwarding expression with this macro so one may not need to specify explicit type.
Definition Macros.h:100
T * NewInit(FConstructObjectParameters &&params, Args &&... args)
Create a new object which can also be initialized with an Initialize function if it has one....
Definition Init.h:114
Mirror of FStaticConstructObjectParameters but it's a plain C++ object and doesn't have a constructor...
Definition Init.h:33
A struct to emulate regular C++ RAII object behavior with UObjects. When this struct is instantiated ...
Definition ScopeObject.h:34
TScopeObject(FConstructObjectParameters &&params, Args &&... args)
Definition ScopeObject.h:36