MCRO
C++23 utilities for Unreal Engine.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
Construct.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/FunctionTraits.h"
16
18{
19 using namespace Mcro::FunctionTraits;
20
21 /**
22 * @brief
23 * Simply makes a new object and allows to initialize it in place with a lambda function. The object type is derived
24 * from the first argument of the initializer lambda function.
25 *
26 * Usage:
27 * @code
28 * using namespace Mcro::Construct;
29 *
30 * auto myObject = Construct([](MyObject& _)
31 * {
32 * _.Foo = 42;
33 * _.Initialize();
34 * // etc...
35 * });
36 * static_assert(std::is_same_v<decltype(myObject), MyObject>);
37 * @endcode
38 *
39 * @param init A lambda function with a single l-value reference parameter of the object type to initialize.
40 * @param args Arguments of the object constructor
41 * @return A new instance of the object.
42 * @remarks The C++ 20 designated initializers with named arguments has annoying limitations, therefore this exists
43 */
44 template<
45 CFunctorObject Initializer,
46 typename... Args,
47 typename ResultArg = TFunction_Arg<Initializer, 0>,
48 typename Result = std::decay_t<ResultArg>
49 >
50 requires std::is_lvalue_reference_v<ResultArg>
51 Result Construct(Initializer&& init, Args&&... args)
52 {
53 Result result {Forward<Args>(args)...};
54 init(result);
55 return result;
56 }
57
58 /**
59 * @brief
60 * Simply makes a new object on the heap and allows to initialize it in place with a lambda function. The object
61 * type is derived from the first argument of the initializer lambda function.
62 *
63 * Usage:
64 * @code
65 * using namespace Mcro::Construct;
66 *
67 * auto myObject = ConstructNew([](MyObject& _)
68 * {
69 * _.Foo = 42;
70 * _.Initialize();
71 * // etc...
72 * });
73 * static_assert(std::is_same_v<decltype(myObject), MyObject*>);
74 * @endcode
75 *
76 * @param init A lambda function with a single l-value reference parameter of the object type to initialize.
77 * @param args Arguments of the object constructor
78 * @return A pointer to the object instance on heap.
79 * @remarks The C++ 20 designated initializers with named arguments has annoying limitations, therefore this exists
80 */
81 template<
82 CFunctorObject Initializer,
83 typename... Args,
84 typename ResultArg = TFunction_Arg<Initializer, 0>,
85 typename Result = std::decay_t<ResultArg>
86 >
87 requires std::is_lvalue_reference_v<ResultArg>
88 Result* ConstructNew(Initializer&& init, Args&&... args)
89 {
90 Result* result = new Result {Forward<Args>(args)...};
91 init(*result);
92 return result;
93 }
94}
Result * ConstructNew(Initializer &&init, Args &&... args)
Simply makes a new object on the heap and allows to initialize it in place with a lambda function....
Definition Construct.h:88
Result Construct(Initializer &&init, Args &&... args)
Simply makes a new object and allows to initialize it in place with a lambda function....
Definition Construct.h:51
typename TFunctionTraits< std::decay_t< T > >::template Arg< I > TFunction_Arg
Shorthand for getting a type of a function argument at given position I.