1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
5 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <dali/public-api/common/vector-wrapper.h>
23 #include <string_view>
27 extern bool CaseInsensitiveStringCompare(std::string_view a, std::string_view b);
34 * The Dictionary template class enables a means of storing key-value
35 * pairs where the keys are strings and the value can be a complex
38 * It enables lookup of keys via case-insensitive match.
41 using DictionaryKeys = std::vector<std::string>;
43 inline void Merge(DictionaryKeys& toDict, const DictionaryKeys& fromDict)
45 for(const auto& element : fromDict)
47 auto iter = std::find(toDict.cbegin(), toDict.cend(), element);
48 if(iter == toDict.cend())
50 toDict.push_back(element);
55 template<typename EntryType>
60 * Element is a key-value pair
66 Element(std::string name, EntryType entry)
67 : key(std::move(name)),
68 entry(std::move(entry))
72 using Elements = std::vector<Element>;
75 auto FindElementCaseInsensitive(std::string_view key) const
78 Begin(), End(), [key](auto& e)
79 { return Dali::CaseInsensitiveStringCompare(e.key, key); });
82 auto FindElement(std::string_view key)
84 return std::find_if(container.begin(), container.end(), [key](auto& e)
85 { return bool(key == e.key); });
90 * Only allow const iteration over the dictionary
92 using iterator = typename Elements::const_iterator;
97 Dictionary() = default;
100 * Add a key value pair to the dictionary.
101 * If the entry does not already exist, add it to the dictionary
103 bool Add(std::string name, EntryType entry)
105 auto iter = FindElement(name);
111 container.push_back(Element(std::move(name), std::move(entry)));
116 * Add a key-value pair to the dictionary
117 * If the entry does not already exist, add it to the dictionary
119 bool Add(const char* name, EntryType entry)
123 return Add(std::string(name), std::move(entry));
129 * Remove a key value pair from the dictionary.
131 void Remove(std::string_view name)
135 auto iter = FindElement(name);
139 container.erase(iter);
144 void Merge(const Dictionary<EntryType>& dictionary)
146 for(const auto& element : dictionary.container)
148 auto iter = FindElement(element.key);
152 container.push_back(Element(element.key, element.entry));
156 iter->entry = element.entry;
162 * Find the element in the dictionary pointed at by key, and
163 * insensitive search, and return a const pointer to it, or NULL
165 const EntryType* FindConst(std::string_view key) const
169 auto iter = FindElementCaseInsensitive(key);
173 return &(iter->entry);
180 * Find the element in the dictionary pointed at by key using a case
181 * insensitive search, and return a non-const pointer to it, or NULL
183 EntryType* Find(std::string_view key) const
187 auto iter = FindElementCaseInsensitive(key);
191 return const_cast<EntryType*>(&(iter->entry));
197 iterator Begin() const
199 return container.cbegin();
203 * Return an iterator pointing past the last entry in the dictionary
207 return container.cend();
210 void GetKeys(DictionaryKeys& keys) const
213 for(const auto& element : container)
215 keys.push_back(element.key);
225 } // namespace Internal
226 } // namespace Toolkit
229 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H