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.

103 lines
3.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_SCOPEDVALUESETTER_H_INCLUDED
  24. #define JUCE_SCOPEDVALUESETTER_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Helper class providing an RAII-based mechanism for temporarily setting and
  28. then re-setting a value.
  29. E.g. @code
  30. int x = 1;
  31. {
  32. ScopedValueSetter setter (x, 2);
  33. // x is now 2
  34. }
  35. // x is now 1 again
  36. {
  37. ScopedValueSetter setter (x, 3, 4);
  38. // x is now 3
  39. }
  40. // x is now 4
  41. @endcode
  42. */
  43. template <typename ValueType>
  44. class ScopedValueSetter
  45. {
  46. public:
  47. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  48. given new value, and will then reset it to its original value when this object is deleted.
  49. */
  50. ScopedValueSetter (ValueType& valueToSet,
  51. ValueType newValue)
  52. : value (valueToSet),
  53. originalValue (valueToSet)
  54. {
  55. valueToSet = newValue;
  56. }
  57. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  58. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  59. */
  60. ScopedValueSetter (ValueType& valueToSet,
  61. ValueType newValue,
  62. ValueType valueWhenDeleted)
  63. : value (valueToSet),
  64. originalValue (valueWhenDeleted)
  65. {
  66. valueToSet = newValue;
  67. }
  68. ~ScopedValueSetter()
  69. {
  70. value = originalValue;
  71. }
  72. private:
  73. //==============================================================================
  74. ValueType& value;
  75. const ValueType originalValue;
  76. JUCE_DECLARE_NON_COPYABLE (ScopedValueSetter)
  77. };
  78. #endif // JUCE_SCOPEDVALUESETTER_H_INCLUDED