OpenShot Library | libopenshot-audio  0.2.0
juce_ScopedValueSetter.h
1 
2 /** @weakgroup juce_core-containers
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Helper class providing an RAII-based mechanism for temporarily setting and
33  then re-setting a value.
34 
35  E.g. @code
36  int x = 1;
37 
38  {
39  ScopedValueSetter setter (x, 2);
40 
41  // x is now 2
42  }
43 
44  // x is now 1 again
45 
46  {
47  ScopedValueSetter setter (x, 3, 4);
48 
49  // x is now 3
50  }
51 
52  // x is now 4
53  @endcode
54 
55  @tags{Core}
56 */
57 template <typename ValueType>
59 {
60 public:
61  /** Creates a ScopedValueSetter that will immediately change the specified value to the
62  given new value, and will then reset it to its original value when this object is deleted.
63  */
64  ScopedValueSetter (ValueType& valueToSet,
65  ValueType newValue)
66  : value (valueToSet),
67  originalValue (valueToSet)
68  {
69  valueToSet = newValue;
70  }
71 
72  /** Creates a ScopedValueSetter that will immediately change the specified value to the
73  given new value, and will then reset it to be valueWhenDeleted when this object is deleted.
74  */
75  ScopedValueSetter (ValueType& valueToSet,
76  ValueType newValue,
77  ValueType valueWhenDeleted)
78  : value (valueToSet),
79  originalValue (valueWhenDeleted)
80  {
81  valueToSet = newValue;
82  }
83 
85  {
86  value = originalValue;
87  }
88 
89 private:
90  //==============================================================================
91  ValueType& value;
92  const ValueType originalValue;
93 
94  JUCE_DECLARE_NON_COPYABLE (ScopedValueSetter)
95 };
96 
97 } // namespace juce
98 
99 /** @}*/
ScopedValueSetter(ValueType &valueToSet, ValueType newValue, ValueType valueWhenDeleted)
Creates a ScopedValueSetter that will immediately change the specified value to the given new value...
ScopedValueSetter(ValueType &valueToSet, ValueType newValue)
Creates a ScopedValueSetter that will immediately change the specified value to the given new value...
Helper class providing an RAII-based mechanism for temporarily setting and then re-setting a value...