Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ScopeExit.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2024 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
12template <class F>
13class ScopeExit : public NonCopyable
14{
15public:
17 JPH_INLINE explicit ScopeExit(F &&inFunction) : mFunction(std::move(inFunction)) { }
18
20 JPH_INLINE ~ScopeExit() { if (!mInvoked) mFunction(); }
21
23 JPH_INLINE void Invoke()
24 {
25 if (!mInvoked)
26 {
27 mFunction();
28 mInvoked = true;
29 }
30 }
31
33 JPH_INLINE void Release()
34 {
35 mInvoked = true;
36 }
37
38private:
39 F mFunction;
40 bool mInvoked = false;
41};
42
43#define JPH_SCOPE_EXIT_TAG2(line) scope_exit##line
44#define JPH_SCOPE_EXIT_TAG(line) JPH_SCOPE_EXIT_TAG2(line)
45
47#define JPH_SCOPE_EXIT(...) ScopeExit JPH_SCOPE_EXIT_TAG(__LINE__)(__VA_ARGS__)
48
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Class that calls a function when it goes out of scope.
Definition ScopeExit.h:14
JPH_INLINE ~ScopeExit()
Destructor calls the exit function.
Definition ScopeExit.h:20
JPH_INLINE void Release()
No longer call the exit function when going out of scope.
Definition ScopeExit.h:33
JPH_INLINE void Invoke()
Call the exit function now instead of when going out of scope.
Definition ScopeExit.h:23
JPH_INLINE ScopeExit(F &&inFunction)
Constructor specifies the exit function.
Definition ScopeExit.h:17
Definition Array.h:590