2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 //==============================================================================
28 A container for holding a set of strings which are keyed by another string.
34 class JUCE_API StringPairArray
37 //==============================================================================
38 /** Creates an empty array */
39 StringPairArray (bool ignoreCaseWhenComparingKeys
= true);
41 /** Creates a copy of another array */
42 StringPairArray (const StringPairArray
& other
);
45 ~StringPairArray() = default;
47 /** Copies the contents of another string array into this one */
48 StringPairArray
& operator= (const StringPairArray
& other
);
50 //==============================================================================
51 /** Compares two arrays.
52 Comparisons are case-sensitive.
53 @returns true only if the other array contains exactly the same strings with the same keys
55 bool operator== (const StringPairArray
& other
) const;
57 /** Compares two arrays.
58 Comparisons are case-sensitive.
59 @returns false if the other array contains exactly the same strings with the same keys
61 bool operator!= (const StringPairArray
& other
) const;
63 //==============================================================================
64 /** Finds the value corresponding to a key string.
66 If no such key is found, this will just return an empty string. To check whether
67 a given key actually exists (because it might actually be paired with an empty string), use
68 the getAllKeys() method to obtain a list.
70 Obviously the reference returned shouldn't be stored for later use, as the
71 string it refers to may disappear when the array changes.
75 const String
& operator[] (StringRef key
) const;
77 /** Finds the value corresponding to a key string.
78 If no such key is found, this will just return the value provided as a default.
81 String
getValue (StringRef
, const String
& defaultReturnValue
) const;
83 /** Returns true if the given key exists. */
84 bool containsKey (StringRef key
) const noexcept
;
86 /** Returns a list of all keys in the array. */
87 const StringArray
& getAllKeys() const noexcept
{ return keys
; }
89 /** Returns a list of all values in the array. */
90 const StringArray
& getAllValues() const noexcept
{ return values
; }
92 /** Returns the number of strings in the array */
93 inline int size() const noexcept
{ return keys
.size(); }
96 //==============================================================================
97 /** Adds or amends a key/value pair.
98 If a value already exists with this key, its value will be overwritten,
99 otherwise the key/value pair will be added to the array.
101 void set (const String
& key
, const String
& value
);
103 /** Adds the items from another array to this one.
104 This is equivalent to using set() to add each of the pairs from the other array.
106 void addArray (const StringPairArray
& other
);
108 //==============================================================================
109 /** Removes all elements from the array. */
112 /** Removes a string from the array based on its key.
113 If the key isn't found, nothing will happen.
115 void remove (StringRef key
);
117 /** Removes a string from the array based on its index.
118 If the index is out-of-range, no action will be taken.
120 void remove (int index
);
122 //==============================================================================
123 /** Indicates whether to use a case-insensitive search when looking up a key string.
125 void setIgnoresCase (bool shouldIgnoreCase
);
127 /** Indicates whether a case-insensitive search is used when looking up a key string.
129 bool getIgnoresCase() const noexcept
;
131 //==============================================================================
132 /** Returns a descriptive string containing the items.
133 This is handy for dumping the contents of an array.
135 String
getDescription() const;
137 //==============================================================================
138 /** Reduces the amount of storage being used by the array.
140 Arrays typically allocate slightly more storage than they need, and after
141 removing elements, they may have quite a lot of unused space allocated.
142 This method will reduce the amount of allocated storage to a minimum.
144 void minimiseStorageOverheads();
146 //==============================================================================
147 /** Adds the contents of a map to this StringPairArray. */
148 void addMap (const std::map
<String
, String
>& mapToAdd
);
150 /** Adds the contents of an unordered map to this StringPairArray. */
151 void addUnorderedMap (const std::unordered_map
<String
, String
>& mapToAdd
);
154 //==============================================================================
155 template <typename Map
>
156 void addMapImpl (const Map
& mapToAdd
);
158 StringArray keys
, values
;
161 JUCE_LEAK_DETECTOR (StringPairArray
)