ScopedVariableSetter.h 941 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYCOMMON_SCOPEDVARIABLESETTER_H
  9. #define CRYINCLUDE_CRYCOMMON_SCOPEDVARIABLESETTER_H
  10. #pragma once
  11. // The idea of this class is to set the value of a variable in its constructor,
  12. // and then in the destructor to reset it to its initial value.
  13. template <typename T>
  14. class CScopedVariableSetter
  15. {
  16. public:
  17. typedef T Value;
  18. CScopedVariableSetter(Value& variable, const Value& temporaryValue)
  19. : m_oldValue(variable)
  20. , m_variable(variable)
  21. {
  22. m_variable = temporaryValue;
  23. }
  24. ~CScopedVariableSetter()
  25. {
  26. m_variable = m_oldValue;
  27. }
  28. private:
  29. Value m_oldValue;
  30. Value& m_variable;
  31. };
  32. #endif // CRYINCLUDE_CRYCOMMON_SCOPEDVARIABLESETTER_H