MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
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 * Simply makes a new object and allows to initialize it in place with a lambda function. The object type is derived
23 * from the first argument of the initializer lambda function.
24 * Usage:
25 *
26 * @code
27 * using namespace Mcro::Construct;
28 *
29 * auto myObject = Construct([](MyObject& _)
30 * {
31 * _.Foo = 42;
32 * _.Initialize();
33 * // etc...
34 * });
35 * static_assert(std::is_same_v<decltype(myObject), MyObject>);
36 * @endcode
37 *
38 * @param init A lambda function with a single l-value reference parameter of the object type to initialize.
39 * @param args
40 * @return A new instance of the object.
41 * @remarks The C++ 20 designated initializers with named arguments has annoying limitations, therefore this exists
42 */
43 template<
44 CFunctorObject Initializer,
45 typename... Args,
46 typename ResultArg = TFunction_Arg<Initializer, 0>,
47 typename Result = std::decay_t<ResultArg>
48 >
49 requires std::is_lvalue_reference_v<ResultArg>
50 Result Construct(Initializer&& init, Args&&... args)
51 {
52 Result result {Forward<Args>(args)...};
53 init(result);
54 return result;
55 }
56
57 /**
58 * Simply makes a new object on the heap and allows to initialize it in place with a lambda function. The object
59 * type is derived from the first argument of the initializer lambda function.
60 * Usage:
61 *
62 * @code
63 * using namespace Mcro::Construct;
64 *
65 * auto myObject = ConstructNew([](MyObject& _)
66 * {
67 * _.Foo = 42;
68 * _.Initialize();
69 * // etc...
70 * });
71 * static_assert(std::is_same_v<decltype(myObject), MyObject*>);
72 * @endcode
73 *
74 * @param init A lambda function with a single l-value reference parameter of the object type to initialize.
75 * @param args
76 * @return A pointer to the object instance on heap.
77 * @remarks The C++ 20 designated initializers with named arguments has annoying limitations, therefore this exists
78 */
79 template<
80 CFunctorObject Initializer,
81 typename... Args,
82 typename ResultArg = TFunction_Arg<Initializer, 0>,
83 typename Result = std::decay_t<ResultArg>
84 >
85 requires std::is_lvalue_reference_v<ResultArg>
86 Result* ConstructNew(Initializer&& init, Args&&... args)
87 {
88 Result* result = new Result {Forward<Args>(args)...};
89 init(*result);
90 return result;
91 }
92
93 /**
94 * A type wrapper around a default initializeable object which may not be copyable but which needs to be a member
95 * of a copyable class. On each instance of such class the wrapped value may not need to be copied and default
96 * constructing it is enough. Useful for mutexes for example.
97 */
98 template <CDefaultInitializable T>
99 requires (!CCopyable<T>)
101 {
102 TInitializeOnCopy() : Value() {}
104 TInitializeOnCopy(TInitializeOnCopy&&) noexcept : Value() {}
105 auto operator=(TInitializeOnCopy const&) -> TInitializeOnCopy& { return *this; }
106 auto operator=(TInitializeOnCopy&& other) noexcept -> TInitializeOnCopy& { return *this; }
107
108 TUniqueObj<T> Value;
109
110 T* operator -> () { return &Value.Get(); }
111 const T* operator -> () const { return &Value.Get(); }
112
113 T& Get() { return Value.Get(); }
114 const T& Get() const { return Value.Get(); }
115
116 template <typename Self>
117 operator typename TCopyQualifiersFromTo<Self, T&>::Type (this Self&& self) { return self.Get(); }
118 };
119}
Result * ConstructNew(Initializer &&init, Args &&... args)
Definition Construct.h:86
Result Construct(Initializer &&init, Args &&... args)
Definition Construct.h:50
typename TFunctionTraits< std::decay_t< T > >::template Arg< I > TFunction_Arg
TInitializeOnCopy(TInitializeOnCopy &&) noexcept
Definition Construct.h:104
auto operator=(TInitializeOnCopy const &) -> TInitializeOnCopy &
Definition Construct.h:105
TInitializeOnCopy(TInitializeOnCopy const &)
Definition Construct.h:103
auto operator=(TInitializeOnCopy &&other) noexcept -> TInitializeOnCopy &
Definition Construct.h:106