Audio plugin host https://kx.studio/carla
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.

NamedValueSet.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "NamedValueSet.h"
  21. namespace water {
  22. //==============================================================================
  23. NamedValueSet::NamedValueSet() noexcept
  24. {
  25. }
  26. NamedValueSet::NamedValueSet (const NamedValueSet& other)
  27. : values (other.values)
  28. {
  29. }
  30. NamedValueSet& NamedValueSet::operator= (const NamedValueSet& other)
  31. {
  32. clear();
  33. values = other.values;
  34. return *this;
  35. }
  36. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  37. NamedValueSet::NamedValueSet (NamedValueSet&& other) noexcept
  38. : values (static_cast<Array<NamedValue>&&> (other.values))
  39. {
  40. }
  41. NamedValueSet& NamedValueSet::operator= (NamedValueSet&& other) noexcept
  42. {
  43. other.values.swapWith (values);
  44. return *this;
  45. }
  46. #endif
  47. NamedValueSet::~NamedValueSet() noexcept
  48. {
  49. }
  50. void NamedValueSet::clear()
  51. {
  52. values.clear();
  53. }
  54. bool NamedValueSet::operator== (const NamedValueSet& other) const
  55. {
  56. return values == other.values;
  57. }
  58. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  59. {
  60. return ! operator== (other);
  61. }
  62. int NamedValueSet::size() const noexcept
  63. {
  64. return values.size();
  65. }
  66. bool NamedValueSet::isEmpty() const noexcept
  67. {
  68. return values.isEmpty();
  69. }
  70. static const var& getNullVarRef() noexcept
  71. {
  72. static var nullVar;
  73. return nullVar;
  74. }
  75. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  76. {
  77. if (const var* v = getVarPointer (name))
  78. return *v;
  79. return getNullVarRef();
  80. }
  81. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  82. {
  83. if (const var* const v = getVarPointer (name))
  84. return *v;
  85. return defaultReturnValue;
  86. }
  87. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  88. {
  89. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  90. if (i->name == name)
  91. return &(i->value);
  92. return nullptr;
  93. }
  94. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  95. bool NamedValueSet::set (const Identifier& name, var&& newValue)
  96. {
  97. if (var* const v = getVarPointer (name))
  98. {
  99. if (v->equalsWithSameType (newValue))
  100. return false;
  101. *v = static_cast<var&&> (newValue);
  102. return true;
  103. }
  104. values.add (NamedValue (name, static_cast<var&&> (newValue)));
  105. return true;
  106. }
  107. #endif
  108. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  109. {
  110. if (var* const v = getVarPointer (name))
  111. {
  112. if (v->equalsWithSameType (newValue))
  113. return false;
  114. *v = newValue;
  115. return true;
  116. }
  117. values.add (NamedValue (name, newValue));
  118. return true;
  119. }
  120. bool NamedValueSet::contains (const Identifier& name) const noexcept
  121. {
  122. return getVarPointer (name) != nullptr;
  123. }
  124. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  125. {
  126. const int numValues = values.size();
  127. for (int i = 0; i < numValues; ++i)
  128. if (values.getReference(i).name == name)
  129. return i;
  130. return -1;
  131. }
  132. bool NamedValueSet::remove (const Identifier& name)
  133. {
  134. const int numValues = values.size();
  135. for (int i = 0; i < numValues; ++i)
  136. {
  137. if (values.getReference(i).name == name)
  138. {
  139. values.remove (i);
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. Identifier NamedValueSet::getName (const int index) const noexcept
  146. {
  147. if (isPositiveAndBelow (index, values.size()))
  148. return values.getReference (index).name;
  149. wassertfalse;
  150. return Identifier();
  151. }
  152. const var& NamedValueSet::getValueAt (const int index) const noexcept
  153. {
  154. if (isPositiveAndBelow (index, values.size()))
  155. return values.getReference (index).value;
  156. wassertfalse;
  157. return getNullVarRef();
  158. }
  159. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  160. {
  161. if (isPositiveAndBelow (index, values.size()))
  162. return &(values.getReference (index).value);
  163. return nullptr;
  164. }
  165. }