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.

156 lines
6.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. A container for holding a set of strings which are keyed by another string.
  27. @see StringArray
  28. */
  29. class JUCE_API StringPairArray
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty array */
  34. StringPairArray (bool ignoreCaseWhenComparingKeys = true);
  35. /** Creates a copy of another array */
  36. StringPairArray (const StringPairArray& other);
  37. /** Destructor. */
  38. ~StringPairArray();
  39. /** Copies the contents of another string array into this one */
  40. StringPairArray& operator= (const StringPairArray& other);
  41. //==============================================================================
  42. /** Compares two arrays.
  43. Comparisons are case-sensitive.
  44. @returns true only if the other array contains exactly the same strings with the same keys
  45. */
  46. bool operator== (const StringPairArray& other) const;
  47. /** Compares two arrays.
  48. Comparisons are case-sensitive.
  49. @returns false if the other array contains exactly the same strings with the same keys
  50. */
  51. bool operator!= (const StringPairArray& other) const;
  52. //==============================================================================
  53. /** Finds the value corresponding to a key string.
  54. If no such key is found, this will just return an empty string. To check whether
  55. a given key actually exists (because it might actually be paired with an empty string), use
  56. the getAllKeys() method to obtain a list.
  57. Obviously the reference returned shouldn't be stored for later use, as the
  58. string it refers to may disappear when the array changes.
  59. @see getValue
  60. */
  61. const String& operator[] (StringRef key) const;
  62. /** Finds the value corresponding to a key string.
  63. If no such key is found, this will just return the value provided as a default.
  64. @see operator[]
  65. */
  66. String getValue (StringRef, const String& defaultReturnValue) const;
  67. /** Returns true if the given key exists. */
  68. bool containsKey (StringRef key) const noexcept;
  69. /** Returns a list of all keys in the array. */
  70. const StringArray& getAllKeys() const noexcept { return keys; }
  71. /** Returns a list of all values in the array. */
  72. const StringArray& getAllValues() const noexcept { return values; }
  73. /** Returns the number of strings in the array */
  74. inline int size() const noexcept { return keys.size(); };
  75. //==============================================================================
  76. /** Adds or amends a key/value pair.
  77. If a value already exists with this key, its value will be overwritten,
  78. otherwise the key/value pair will be added to the array.
  79. */
  80. void set (const String& key, const String& value);
  81. /** Adds the items from another array to this one.
  82. This is equivalent to using set() to add each of the pairs from the other array.
  83. */
  84. void addArray (const StringPairArray& other);
  85. //==============================================================================
  86. /** Removes all elements from the array. */
  87. void clear();
  88. /** Removes a string from the array based on its key.
  89. If the key isn't found, nothing will happen.
  90. */
  91. void remove (StringRef key);
  92. /** Removes a string from the array based on its index.
  93. If the index is out-of-range, no action will be taken.
  94. */
  95. void remove (int index);
  96. //==============================================================================
  97. /** Indicates whether to use a case-insensitive search when looking up a key string.
  98. */
  99. void setIgnoresCase (bool shouldIgnoreCase);
  100. //==============================================================================
  101. /** Returns a descriptive string containing the items.
  102. This is handy for dumping the contents of an array.
  103. */
  104. String getDescription() const;
  105. //==============================================================================
  106. /** Reduces the amount of storage being used by the array.
  107. Arrays typically allocate slightly more storage than they need, and after
  108. removing elements, they may have quite a lot of unused space allocated.
  109. This method will reduce the amount of allocated storage to a minimum.
  110. */
  111. void minimiseStorageOverheads();
  112. private:
  113. //==============================================================================
  114. StringArray keys, values;
  115. bool ignoreCase;
  116. JUCE_LEAK_DETECTOR (StringPairArray)
  117. };