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.

181 lines
4.4KB

  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. NamedValueSet::~NamedValueSet() noexcept
  37. {
  38. }
  39. void NamedValueSet::clear()
  40. {
  41. values.clear();
  42. }
  43. bool NamedValueSet::operator== (const NamedValueSet& other) const
  44. {
  45. return values == other.values;
  46. }
  47. bool NamedValueSet::operator!= (const NamedValueSet& other) const
  48. {
  49. return ! operator== (other);
  50. }
  51. int NamedValueSet::size() const noexcept
  52. {
  53. return values.size();
  54. }
  55. bool NamedValueSet::isEmpty() const noexcept
  56. {
  57. return values.isEmpty();
  58. }
  59. static const var& getNullVarRef() noexcept
  60. {
  61. static var nullVar;
  62. return nullVar;
  63. }
  64. const var& NamedValueSet::operator[] (const Identifier& name) const noexcept
  65. {
  66. if (const var* v = getVarPointer (name))
  67. return *v;
  68. return getNullVarRef();
  69. }
  70. var NamedValueSet::getWithDefault (const Identifier& name, const var& defaultReturnValue) const
  71. {
  72. if (const var* const v = getVarPointer (name))
  73. return *v;
  74. return defaultReturnValue;
  75. }
  76. var* NamedValueSet::getVarPointer (const Identifier& name) const noexcept
  77. {
  78. for (NamedValue* e = values.end(), *i = values.begin(); i != e; ++i)
  79. if (i->name == name)
  80. return &(i->value);
  81. return nullptr;
  82. }
  83. bool NamedValueSet::set (const Identifier& name, const var& newValue)
  84. {
  85. if (var* const v = getVarPointer (name))
  86. {
  87. if (v->equalsWithSameType (newValue))
  88. return false;
  89. *v = newValue;
  90. return true;
  91. }
  92. values.add (NamedValue (name, newValue));
  93. return true;
  94. }
  95. bool NamedValueSet::contains (const Identifier& name) const noexcept
  96. {
  97. return getVarPointer (name) != nullptr;
  98. }
  99. int NamedValueSet::indexOf (const Identifier& name) const noexcept
  100. {
  101. const int numValues = values.size();
  102. for (int i = 0; i < numValues; ++i)
  103. if (values.getReference(i).name == name)
  104. return i;
  105. return -1;
  106. }
  107. bool NamedValueSet::remove (const Identifier& name)
  108. {
  109. const int numValues = values.size();
  110. for (int i = 0; i < numValues; ++i)
  111. {
  112. if (values.getReference(i).name == name)
  113. {
  114. values.remove (i);
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. Identifier NamedValueSet::getName (const int index) const noexcept
  121. {
  122. if (isPositiveAndBelow (index, values.size()))
  123. return values.getReference (index).name;
  124. wassertfalse;
  125. return Identifier();
  126. }
  127. const var& NamedValueSet::getValueAt (const int index) const noexcept
  128. {
  129. if (isPositiveAndBelow (index, values.size()))
  130. return values.getReference (index).value;
  131. wassertfalse;
  132. return getNullVarRef();
  133. }
  134. var* NamedValueSet::getVarPointerAt (int index) const noexcept
  135. {
  136. if (isPositiveAndBelow (index, values.size()))
  137. return &(values.getReference (index).value);
  138. return nullptr;
  139. }
  140. }