UnrealImGui/Source/ImGui/Private/Utilities/ScopeGuards.h
Sebastian 35f2d342a0 Added support for ImGui context update and rendering:
- Added ImGui Module Manager that that implements module logic and manages other module resources.
- Added Texture Manager to manage texture resources and maps them to index that can be passed to ImGui context.
- Added Context Proxy that represents and manages a single ImGui context.
- Added Slate ImGui Widget to render ImGui output.
2017-03-26 21:32:57 +01:00

59 lines
1.3 KiB
C++

// Distributed under the MIT License (MIT) (see accompanying LICENSE file)
#pragma once
namespace ScopeGuards
{
// Saves snapshot of the object state and restores it during destruction.
template<typename T>
class TStateSaver
{
public:
// Constructor taking target object in state that we want to save.
TStateSaver(T& Target)
: Ptr(&Target)
, Value(Target)
{
}
// Move constructor allowing to transfer state out of scope.
TStateSaver(TStateSaver&& Other)
: Ptr(Other.Ptr)
, Value(MoveTemp(Other.Value))
{
// Release responsibility from the other object (std::exchange currently not supported by all platforms).
Other.Ptr = nullptr;
}
// Non-assignable to enforce acquisition on construction.
TStateSaver& operator=(TStateSaver&&) = delete;
// Non-copyable.
TStateSaver(const TStateSaver&) = delete;
TStateSaver& operator=(const TStateSaver&) = delete;
~TStateSaver()
{
if (Ptr)
{
*Ptr = Value;
}
}
private:
T* Ptr;
T Value;
};
// Create a state saver for target object. Unless saver is moved, state will be restored at the end of scope.
// @param Target - Target object in state that we want to save
// @returns State saver that unless moved, will restore target's state during scope exit
template<typename T>
TStateSaver<T> MakeStateSaver(T& Target)
{
return TStateSaver<T>{ Target };
}
}