MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
New.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 "Mcro/Macros.h"
16#include "HAL/MallocAnsi.h"
17
18namespace Mcro::Ansi
19{
20 /**
21 * @brief Force using the ANSI memory allocation behavior, instead of the Unreal default.
22 *
23 * This may be less performant, may need more memory and will not be considered by AutoRTFM. Use it only when Unreal
24 * overrides cause problems and comment the reason why this was necessary. This is paired with `Ansi::Delete` instead
25 * of `delete`.
26 * @code
27 * FFoobar* myVar = Ansi::New<FFoobar>();
28 * Ansi::Delete(myVar);
29 * @endcode
30 */
31 template <typename T, typename... Args>
32 T* New(Args&&... args)
33 {
34 T* result = static_cast<T*>(AnsiMalloc(sizeof(T), alignof(T)));
35 return new (result) T(FWD(args)...);
36 }
37
38 /**
39 * @brief Force using the ANSI memory release behavior, instead of the Unreal default.
40 *
41 * This may be less performant and will not be considered by AutoRTFM. Use it only when Unreal overrides cause
42 * problems and comment the reason why this was necessary. This is paired with `Ansi::New<T>()`.
43 * @code
44 * FFoobar* myVar = Ansi::New<FFoobar>();
45 * Ansi::Delete(myVar);
46 * @endcode
47 */
48 template <typename T>
49 void Delete(T* ptr)
50 {
51 if (!ptr) return;
52 ptr->~T();
53 AnsiFree(ptr);
54 }
55}
#define FWD(...)
Shorten forwarding expression with this macro so one may not need to specify explicit type.
Definition Macros.h:100
Epic Games may not agree with standards because they know better, but sometimes we have to bare conse...
Definition Allocator.h:23
void Delete(T *ptr)
Force using the ANSI memory release behavior, instead of the Unreal default.
Definition New.h:49
T * New(Args &&... args)
Force using the ANSI memory allocation behavior, instead of the Unreal default.
Definition New.h:32