1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
5 * Copyright (c) 2020 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);
35 * The Dictionary template class enables a means of storing key-value
36 * pairs where the keys are strings and the value can be a complex
39 * It enables lookup of keys via case-insensitive match.
42 using DictionaryKeys = std::vector<std::string>;
44 inline void Merge( DictionaryKeys& toDict, const DictionaryKeys& fromDict )
46 for(const auto& element : fromDict)
48 auto iter = std::find(toDict.cbegin(), toDict.cend(), element);
49 if(iter == toDict.cend())
51 toDict.push_back(element);
57 template<typename EntryType>
62 * Element is a key-value pair
68 Element(std::string name, EntryType entry)
69 : key(std::move(name)),
70 entry(std::move(entry))
74 using Elements = std::vector<Element>;
77 auto FindElementCaseInsensitive(std::string_view key) const
80 Begin(), End(), [key](auto& e) { return Dali::CaseInsensitiveStringCompare(e.key, key); });
83 auto FindElement(std::string_view key)
85 return std::find_if(container.begin(), container.end(), [key](auto& e){
86 return bool(key == e.key);
92 * Only allow const iteration over the dictionary
94 using iterator = typename Elements::const_iterator;
99 Dictionary<EntryType>() = default;
102 * Add a key value pair to the dictionary.
103 * If the entry does not already exist, add it to the dictionary
105 bool Add(std::string name, EntryType entry)
107 auto iter = FindElement(name);
113 container.push_back(Element(std::move(name), std::move(entry)));
118 * Add a key-value pair to the dictionary
119 * If the entry does not already exist, add it to the dictionary
121 bool Add(const char* name, EntryType entry)
125 return Add(std::string(name), std::move(entry));
131 * Remove a key value pair from the dictionary.
133 void Remove(std::string_view name)
137 auto iter = FindElement(name);
141 container.erase( iter );
146 void Merge( const Dictionary<EntryType>& dictionary )
148 for(const auto& element : dictionary.container)
150 auto iter = FindElement(element.key);
154 container.push_back(Element(element.key, element.entry));
158 iter->entry = element.entry;
164 * Find the element in the dictionary pointed at by key, and
165 * insensitive search, and return a const pointer to it, or NULL
167 const EntryType* FindConst(std::string_view key) const
171 auto iter = FindElementCaseInsensitive(key);
175 return &(iter->entry);
182 * Find the element in the dictionary pointed at by key using a case
183 * insensitive search, and return a non-const pointer to it, or NULL
185 EntryType* Find(std::string_view key) const
189 auto iter = FindElementCaseInsensitive(key);
193 return const_cast<EntryType*>(&(iter->entry));
199 iterator Begin() const
201 return container.cbegin();
205 * Return an iterator pointing past the last entry in the dictionary
209 return container.cend();
212 void GetKeys( DictionaryKeys& keys ) const
215 for(const auto& element : container)
217 keys.push_back(element.key);
233 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H