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.

99 lines
3.1KB

  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. #pragma once
  24. //==============================================================================
  25. /**
  26. Helper class providing an RAII-based mechanism for temporarily setting and
  27. then re-setting a value.
  28. E.g. @code
  29. int x = 1;
  30. {
  31. ScopedValueSetter setter (x, 2);
  32. // x is now 2
  33. }
  34. // x is now 1 again
  35. {
  36. ScopedValueSetter setter (x, 3, 4);
  37. // x is now 3
  38. }
  39. // x is now 4
  40. @endcode
  41. */
  42. template <typename ValueType>
  43. class ScopedValueSetter
  44. {
  45. public:
  46. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  47. given new value, and will then reset it to its original value when this object is deleted.
  48. */
  49. ScopedValueSetter (ValueType& valueToSet,
  50. ValueType newValue)
  51. : value (valueToSet),
  52. originalValue (valueToSet)
  53. {
  54. valueToSet = newValue;
  55. }
  56. /** Creates a ScopedValueSetter that will immediately change the specified value to the
  57. given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
  58. */
  59. ScopedValueSetter (ValueType& valueToSet,
  60. ValueType newValue,
  61. ValueType valueWhenDeleted)
  62. : value (valueToSet),
  63. originalValue (valueWhenDeleted)
  64. {
  65. valueToSet = newValue;
  66. }
  67. ~ScopedValueSetter()
  68. {
  69. value = originalValue;
  70. }
  71. private:
  72. //==============================================================================
  73. ValueType& value;
  74. const ValueType originalValue;
  75. JUCE_DECLARE_NON_COPYABLE (ScopedValueSetter)
  76. };