Path: blob/master/thirdparty/jolt_physics/Jolt/Core/ScopeExit.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2024 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/NonCopyable.h>78JPH_NAMESPACE_BEGIN910/// Class that calls a function when it goes out of scope11template <class F>12class ScopeExit : public NonCopyable13{14public:15/// Constructor specifies the exit function16JPH_INLINE explicit ScopeExit(F &&inFunction) : mFunction(std::move(inFunction)) { }1718/// Destructor calls the exit function19JPH_INLINE ~ScopeExit() { if (!mInvoked) mFunction(); }2021/// Call the exit function now instead of when going out of scope22JPH_INLINE void Invoke()23{24if (!mInvoked)25{26mFunction();27mInvoked = true;28}29}3031/// No longer call the exit function when going out of scope32JPH_INLINE void Release()33{34mInvoked = true;35}3637private:38F mFunction;39bool mInvoked = false;40};4142#define JPH_SCOPE_EXIT_TAG2(line) scope_exit##line43#define JPH_SCOPE_EXIT_TAG(line) JPH_SCOPE_EXIT_TAG2(line)4445/// Usage: JPH_SCOPE_EXIT([]{ code to call on scope exit });46#define JPH_SCOPE_EXIT(...) ScopeExit JPH_SCOPE_EXIT_TAG(__LINE__)(__VA_ARGS__)4748JPH_NAMESPACE_END495051