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.

155 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_STATEVARIABLE_H__
  25. #define __DROWAUDIO_STATEVARIABLE_H__
  26. //==============================================================================
  27. /** Variable that holds its previous value.
  28. This can be used instead of keeping track of a current and previous state
  29. of a primitive variable. Just calling set() will automatically update the
  30. new and previous states.
  31. */
  32. template <class VariableType>
  33. class StateVariable
  34. {
  35. public:
  36. //==============================================================================
  37. /** Create a StateVariable with an initial value of 0.
  38. */
  39. StateVariable()
  40. {
  41. previous = current = 0;
  42. }
  43. /** Create a StateVariable with an initial value.
  44. To begin with the previous value will be the same as the initial.
  45. */
  46. StateVariable (VariableType initialValue)
  47. {
  48. previous = current = initialValue;
  49. }
  50. /** Destructor. */
  51. ~StateVariable() {}
  52. /** Returns the current value.
  53. */
  54. inline VariableType getCurrent() { return current; }
  55. /** Returns the previous value.
  56. */
  57. inline VariableType getPrevious() { return previous; }
  58. /** Sets a new value, copying the current to the previous.
  59. */
  60. inline void set (VariableType newValue)
  61. {
  62. previous = current;
  63. current = newValue;
  64. }
  65. /** Sets the current value, leaving the previous unchanged.
  66. */
  67. inline void setOnlyCurrent (VariableType newValue)
  68. {
  69. current = newValue;
  70. }
  71. /** Sets the previous value, leaving the current unchanged.
  72. */
  73. inline void setPrevious(VariableType newValue)
  74. {
  75. previous = newValue;
  76. }
  77. /** Sets the current and the previous value to be the same.
  78. */
  79. inline void setBoth (VariableType newValue)
  80. {
  81. current = previous = newValue;
  82. }
  83. /** Returns true if the current and previous states are equal.
  84. */
  85. inline bool areEqual()
  86. {
  87. return (previous == current);
  88. }
  89. /** Returns true if the two are almost equal to a given precision.
  90. */
  91. inline bool areAlmostEqual (double precision = 0.00001)
  92. {
  93. return almostEqual (current, previous, precision);
  94. }
  95. /** The same as calling set().
  96. */
  97. inline void operator= (VariableType newValue)
  98. {
  99. previous = current;
  100. current = newValue;
  101. }
  102. /** Incriments the current value by newValue and updates the previous.
  103. */
  104. inline void operator+= (VariableType newValue)
  105. {
  106. previous = current;
  107. current += newValue;
  108. }
  109. /** Multiplies the current value by newValue and updates the previous.
  110. */
  111. inline void operator*= (VariableType newValue)
  112. {
  113. previous = current;
  114. current *= newValue;
  115. }
  116. /** This returns the difference between the current and the previous state.
  117. */
  118. inline VariableType getDifference()
  119. {
  120. return current - previous;
  121. }
  122. private:
  123. //==============================================================================
  124. VariableType current, previous;
  125. //==============================================================================
  126. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StateVariable);
  127. };
  128. #endif //__DROWAUDIO_STATEVARIABLE_H__