Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
ScopeExit.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6{
7 /**
8 * Not meant for direct consumption : use ON_SCOPE_EXIT instead.
9 *
10 * RAII class that calls a lambda when it is destroyed.
11 */
12 template <typename FuncType>
14 {
16 TScopeGuard(const TScopeGuard&) = delete;
18 TScopeGuard& operator=(const TScopeGuard&) = delete;
19
20 public:
21 // Given a lambda, constructs an RAII scope guard.
22 explicit TScopeGuard(FuncType&& InFunc)
23 : Func((FuncType&&)InFunc)
24 {
25 }
26
27 // Causes the lambda to be executed.
29 {
30 Func();
31 }
32
33 private:
34 // The lambda to be executed when this guard goes out of scope.
35 FuncType Func;
36 };
37
39 {
40 template <typename FuncType>
41 TScopeGuard<FuncType> operator+(FuncType&& InFunc)
42 {
44 }
45 };
46}
47
48#define UE_PRIVATE_SCOPE_EXIT_JOIN(A, B) UE_PRIVATE_SCOPE_EXIT_JOIN_INNER(A, B)
49#define UE_PRIVATE_SCOPE_EXIT_JOIN_INNER(A, B) A##B
50
51
52
53/**
54 * Enables a lambda to be executed on scope exit.
55 *
56 * Example:
57 * {
58 * FileHandle* Handle = GetFileHandle();
59 * ON_SCOPE_EXIT
60 * {
61 * CloseFile(Handle);
62 * };
63 *
64 * DoSomethingWithFile( Handle );
65 *
66 * // File will be closed automatically no matter how the scope is exited, e.g.:
67 * // * Any return statement.
68 * // * break or continue (if the scope is a loop body).
69 * // * An exception is thrown outside the block.
70 * // * Execution reaches the end of the block.
71 * }
72 */
73#define ON_SCOPE_EXIT const auto UE_PRIVATE_SCOPE_EXIT_JOIN(ScopeGuard_, __LINE__) = ::ScopeExitSupport::FScopeGuardSyntaxSupport() + [&]()
#define UE_PRIVATE_SCOPE_EXIT_JOIN_INNER(A, B)
Definition ScopeExit.h:49
#define UE_PRIVATE_SCOPE_EXIT_JOIN(A, B)
Definition ScopeExit.h:48
TScopeGuard & operator=(TScopeGuard &&)=delete
TScopeGuard(const TScopeGuard &)=delete
TScopeGuard(FuncType &&InFunc)
Definition ScopeExit.h:22
TScopeGuard & operator=(const TScopeGuard &)=delete
TScopeGuard(TScopeGuard &&)=delete
TScopeGuard< FuncType > operator+(FuncType &&InFunc)
Definition ScopeExit.h:41