| 1 | // LAF Base Library |
| 2 | // Copyright (c) 2022 Igara Studio S.A. |
| 3 | // Copyright (c) 2014-2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef BASE_SCOPED_VALUE_H_INCLUDED |
| 9 | #define BASE_SCOPED_VALUE_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | namespace base { |
| 13 | |
| 14 | template<typename T, |
| 15 | typename U = T> |
| 16 | class ScopedValue { |
| 17 | public: |
| 18 | ScopedValue(T& instance, const U& inScopeValue, const U& outScopeValue) |
| 19 | : m_instance(instance) |
| 20 | , m_outScopeValue(outScopeValue) { |
| 21 | m_instance = inScopeValue; |
| 22 | } |
| 23 | |
| 24 | ~ScopedValue() { |
| 25 | m_instance = m_outScopeValue; |
| 26 | } |
| 27 | |
| 28 | private: |
| 29 | T& m_instance; |
| 30 | U m_outScopeValue; |
| 31 | }; |
| 32 | |
| 33 | } // namespace base |
| 34 | |
| 35 | #endif |
| 36 | |