1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
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.
15 
16    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18    DISCLAIMED.
19 
20   ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
27 /**
28     A container for holding a set of strings which are keyed by another string.
29 
30     @see StringArray
31 
32     @tags{Core}
33 */
34 class JUCE_API  StringPairArray
35 {
36 public:
37     //==============================================================================
38     /** Creates an empty array */
39     StringPairArray (bool ignoreCaseWhenComparingKeys = true);
40 
41     /** Creates a copy of another array */
42     StringPairArray (const StringPairArray& other);
43 
44     /** Destructor. */
45     ~StringPairArray();
46 
47     /** Copies the contents of another string array into this one */
48     StringPairArray& operator= (const StringPairArray& other);
49 
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
54     */
55     bool operator== (const StringPairArray& other) const;
56 
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
60     */
61     bool operator!= (const StringPairArray& other) const;
62 
63     //==============================================================================
64     /** Finds the value corresponding to a key string.
65 
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.
69 
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.
72 
73         @see getValue
74     */
75     const String& operator[] (StringRef key) const;
76 
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.
79         @see operator[]
80     */
81     String getValue (StringRef, const String& defaultReturnValue) const;
82 
83     /** Returns true if the given key exists. */
84     bool containsKey (StringRef key) const noexcept;
85 
86     /** Returns a list of all keys in the array. */
getAllKeys()87     const StringArray& getAllKeys() const noexcept          { return keys; }
88 
89     /** Returns a list of all values in the array. */
getAllValues()90     const StringArray& getAllValues() const noexcept        { return values; }
91 
92     /** Returns the number of strings in the array */
size()93     inline int size() const noexcept                        { return keys.size(); }
94 
95 
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.
100     */
101     void set (const String& key, const String& value);
102 
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.
105     */
106     void addArray (const StringPairArray& other);
107 
108     //==============================================================================
109     /** Removes all elements from the array. */
110     void clear();
111 
112     /** Removes a string from the array based on its key.
113         If the key isn't found, nothing will happen.
114     */
115     void remove (StringRef key);
116 
117     /** Removes a string from the array based on its index.
118         If the index is out-of-range, no action will be taken.
119     */
120     void remove (int index);
121 
122     //==============================================================================
123     /** Indicates whether to use a case-insensitive search when looking up a key string.
124     */
125     void setIgnoresCase (bool shouldIgnoreCase);
126 
127     //==============================================================================
128     /** Returns a descriptive string containing the items.
129         This is handy for dumping the contents of an array.
130     */
131     String getDescription() const;
132 
133     //==============================================================================
134     /** Reduces the amount of storage being used by the array.
135 
136         Arrays typically allocate slightly more storage than they need, and after
137         removing elements, they may have quite a lot of unused space allocated.
138         This method will reduce the amount of allocated storage to a minimum.
139     */
140     void minimiseStorageOverheads();
141 
142 
143 private:
144     //==============================================================================
145     StringArray keys, values;
146     bool ignoreCase;
147 
148     JUCE_LEAK_DETECTOR (StringPairArray)
149 };
150 
151 } // namespace juce
152