The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_SCOPEDVALUESETTER_JUCEHEADER__
  19. #define __JUCE_SCOPEDVALUESETTER_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Helper class providing an RAII-based mechanism for temporarily setting and
  23. then re-setting a value.
  24. E.g. @code
  25. int x = 1;
  26. {
  27. ScopedValueSetter setter (x, 2);
  28. // x is now 2
  29. }
  30. // x is now 1 again
  31. {
  32. ScopedValueSetter setter (x, 3, 4);
  33. // x is now 3
  34. }
  35. // x is now 4
  36. @endcode
  37. */
  38. template <typename ValueType>
  39. class ScopedValueSetter
  40. {
  41. public:
  42. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  43. given new value, and will then reset it to its original value when this object is deleted.
  44. */
  45. ScopedValueSetter (ValueType& valueToSet,
  46. const ValueType& newValue)
  47. : value (valueToSet),
  48. originalValue (valueToSet)
  49. {
  50. valueToSet = newValue;
  51. }
  52. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  53. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  54. */
  55. ScopedValueSetter (ValueType& valueToSet,
  56. const ValueType& newValue,
  57. const ValueType& valueWhenDeleted)
  58. : value (valueToSet),
  59. originalValue (valueWhenDeleted)
  60. {
  61. valueToSet = newValue;
  62. }
  63. ~ScopedValueSetter()
  64. {
  65. value = originalValue;
  66. }
  67. private:
  68. //==============================================================================
  69. ValueType& value;
  70. const ValueType originalValue;
  71. JUCE_DECLARE_NON_COPYABLE (ScopedValueSetter);
  72. };
  73. #endif // __JUCE_SCOPEDVALUESETTER_JUCEHEADER__