juce_StringPairArray.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_STRINGPAIRARRAY_H_INCLUDED
  22. #define JUCE_STRINGPAIRARRAY_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A container for holding a set of strings which are keyed by another string.
  26. @see StringArray
  27. */
  28. class JUCE_API StringPairArray
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty array */
  33. StringPairArray (bool ignoreCaseWhenComparingKeys = true);
  34. /** Creates a copy of another array */
  35. StringPairArray (const StringPairArray& other);
  36. /** Destructor. */
  37. ~StringPairArray();
  38. /** Copies the contents of another string array into this one */
  39. StringPairArray& operator= (const StringPairArray& other);
  40. //==============================================================================
  41. /** Compares two arrays.
  42. Comparisons are case-sensitive.
  43. @returns true only if the other array contains exactly the same strings with the same keys
  44. */
  45. bool operator== (const StringPairArray& other) const;
  46. /** Compares two arrays.
  47. Comparisons are case-sensitive.
  48. @returns false if the other array contains exactly the same strings with the same keys
  49. */
  50. bool operator!= (const StringPairArray& other) const;
  51. //==============================================================================
  52. /** Finds the value corresponding to a key string.
  53. If no such key is found, this will just return an empty string. To check whether
  54. a given key actually exists (because it might actually be paired with an empty string), use
  55. the getAllKeys() method to obtain a list.
  56. Obviously the reference returned shouldn't be stored for later use, as the
  57. string it refers to may disappear when the array changes.
  58. @see getValue
  59. */
  60. const String& operator[] (StringRef key) const;
  61. /** Finds the value corresponding to a key string.
  62. If no such key is found, this will just return the value provided as a default.
  63. @see operator[]
  64. */
  65. String getValue (StringRef, const String& defaultReturnValue) const;
  66. /** Returns true if the given key exists. */
  67. bool containsKey (StringRef key) const noexcept;
  68. /** Returns a list of all keys in the array. */
  69. const StringArray& getAllKeys() const noexcept { return keys; }
  70. /** Returns a list of all values in the array. */
  71. const StringArray& getAllValues() const noexcept { return values; }
  72. /** Returns the number of strings in the array */
  73. inline int size() const noexcept { return keys.size(); };
  74. //==============================================================================
  75. /** Adds or amends a key/value pair.
  76. If a value already exists with this key, its value will be overwritten,
  77. otherwise the key/value pair will be added to the array.
  78. */
  79. void set (const String& key, const String& value);
  80. /** Adds the items from another array to this one.
  81. This is equivalent to using set() to add each of the pairs from the other array.
  82. */
  83. void addArray (const StringPairArray& other);
  84. //==============================================================================
  85. /** Removes all elements from the array. */
  86. void clear();
  87. /** Removes a string from the array based on its key.
  88. If the key isn't found, nothing will happen.
  89. */
  90. void remove (StringRef key);
  91. /** Removes a string from the array based on its index.
  92. If the index is out-of-range, no action will be taken.
  93. */
  94. void remove (int index);
  95. //==============================================================================
  96. /** Indicates whether to use a case-insensitive search when looking up a key string.
  97. */
  98. void setIgnoresCase (bool shouldIgnoreCase);
  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. private:
  112. //==============================================================================
  113. StringArray keys, values;
  114. bool ignoreCase;
  115. JUCE_LEAK_DETECTOR (StringPairArray)
  116. };
  117. #endif // JUCE_STRINGPAIRARRAY_H_INCLUDED