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.

159 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A container for holding a set of strings which are keyed by another string.
  22. @see StringArray
  23. @tags{Core}
  24. */
  25. class JUCE_API StringPairArray
  26. {
  27. public:
  28. //==============================================================================
  29. /** Creates an empty array */
  30. StringPairArray (bool ignoreCaseWhenComparingKeys = true);
  31. /** Creates a copy of another array */
  32. StringPairArray (const StringPairArray& other);
  33. /** Destructor. */
  34. ~StringPairArray();
  35. /** Copies the contents of another string array into this one */
  36. StringPairArray& operator= (const StringPairArray& other);
  37. //==============================================================================
  38. /** Compares two arrays.
  39. Comparisons are case-sensitive.
  40. @returns true only if the other array contains exactly the same strings with the same keys
  41. */
  42. bool operator== (const StringPairArray& other) const;
  43. /** Compares two arrays.
  44. Comparisons are case-sensitive.
  45. @returns false if the other array contains exactly the same strings with the same keys
  46. */
  47. bool operator!= (const StringPairArray& other) const;
  48. //==============================================================================
  49. /** Finds the value corresponding to a key string.
  50. If no such key is found, this will just return an empty string. To check whether
  51. a given key actually exists (because it might actually be paired with an empty string), use
  52. the getAllKeys() method to obtain a list.
  53. Obviously the reference returned shouldn't be stored for later use, as the
  54. string it refers to may disappear when the array changes.
  55. @see getValue
  56. */
  57. const String& operator[] (StringRef key) const;
  58. /** Finds the value corresponding to a key string.
  59. If no such key is found, this will just return the value provided as a default.
  60. @see operator[]
  61. */
  62. String getValue (StringRef, const String& defaultReturnValue) const;
  63. /** Returns true if the given key exists. */
  64. bool containsKey (StringRef key) const noexcept;
  65. /** Returns a list of all keys in the array. */
  66. const StringArray& getAllKeys() const noexcept { return keys; }
  67. /** Returns a list of all values in the array. */
  68. const StringArray& getAllValues() const noexcept { return values; }
  69. /** Returns the number of strings in the array */
  70. inline int size() const noexcept { return keys.size(); }
  71. //==============================================================================
  72. /** Adds or amends a key/value pair.
  73. If a value already exists with this key, its value will be overwritten,
  74. otherwise the key/value pair will be added to the array.
  75. */
  76. void set (const String& key, const String& value);
  77. /** Adds the items from another array to this one.
  78. This is equivalent to using set() to add each of the pairs from the other array.
  79. */
  80. void addArray (const StringPairArray& other);
  81. //==============================================================================
  82. /** Removes all elements from the array. */
  83. void clear();
  84. /** Removes a string from the array based on its key.
  85. If the key isn't found, nothing will happen.
  86. */
  87. void remove (StringRef key);
  88. /** Removes a string from the array based on its index.
  89. If the index is out-of-range, no action will be taken.
  90. */
  91. void remove (int index);
  92. //==============================================================================
  93. /** Indicates whether to use a case-insensitive search when looking up a key string.
  94. */
  95. void setIgnoresCase (bool shouldIgnoreCase);
  96. /** Indicates whether a case-insensitive search is used when looking up a key string.
  97. */
  98. bool getIgnoresCase() const noexcept;
  99. //==============================================================================
  100. /** Returns a descriptive string containing the items.
  101. This is handy for dumping the contents of an array.
  102. */
  103. String getDescription() const;
  104. //==============================================================================
  105. /** Reduces the amount of storage being used by the array.
  106. Arrays typically allocate slightly more storage than they need, and after
  107. removing elements, they may have quite a lot of unused space allocated.
  108. This method will reduce the amount of allocated storage to a minimum.
  109. */
  110. void minimiseStorageOverheads();
  111. //==============================================================================
  112. /** Adds the contents of a map to this StringPairArray. */
  113. void addMap (const std::map<String, String>& mapToAdd);
  114. private:
  115. //==============================================================================
  116. StringArray keys, values;
  117. bool ignoreCase;
  118. JUCE_LEAK_DETECTOR (StringPairArray)
  119. };
  120. } // namespace juce