1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
5 * Copyright (c) 2017 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.
43 typedef std::vector<std::string> DictionaryKeys;
44 inline void Merge( DictionaryKeys& toDict, const DictionaryKeys& fromDict )
46 for( DictionaryKeys::const_iterator fromIter = fromDict.begin(); fromIter != fromDict.end(); ++fromIter )
48 const std::string& fromKey = (*fromIter);
49 DictionaryKeys::iterator toIter = std::find( toDict.begin(), toDict.end(), fromKey );
50 if( toIter == toDict.end() )
52 toDict.push_back( fromKey );
58 template<typename EntryType>
63 * Element is a key-value pair
69 Element( const std::string&name, EntryType entry )
75 typedef std::vector<Element> Elements;
80 * Only allow const iteration over the dictionary
82 typedef typename Elements::const_iterator iterator;
87 Dictionary<EntryType>()
92 * Add a key value pair to the dictionary.
93 * If the entry does not already exist, add it to the dictionary
94 * using a shallow copy
96 bool Add( const std::string& name, const EntryType& entry )
98 for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
100 if( iter->key == name )
105 container.push_back( Element(name, entry) );
110 * Add a key-value pair to the dictionary
111 * If the entry does not already exist, add it to the dictionary
114 bool Add( const char* name, const EntryType& entry )
119 std::string theName(name);
120 result=Add(theName, entry);
126 * Remove a key value pair from the dictionary.
128 void Remove( const std::string& name )
130 for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
132 if( iter->key == name )
134 container.erase( iter );
141 * Remove a key value pair from the dictionary.
143 void Remove( const char* name )
147 std::string theName(name);
152 void Merge( const Dictionary<EntryType>& dictionary )
154 for( typename Elements::const_iterator fromIter = dictionary.container.begin(); fromIter != dictionary.container.end(); ++fromIter )
157 for( typename Elements::iterator toIter = container.begin(); toIter != container.end(); ++toIter )
159 if( fromIter->key == toIter->key )
162 toIter->entry = fromIter->entry;
167 container.push_back( Element(fromIter->key, fromIter->entry) );
173 * Find the element in the dictionary pointed at by key, and
174 * insensitive search, and return a const pointer to it, or NULL
176 const EntryType* FindConst( const std::string& key ) const
180 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
182 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
184 const EntryType* result = &(iter->entry);
193 * Find the element in the dictionary pointed at by key using a case
194 * insensitive search, and return a non-const pointer to it, or NULL
196 EntryType* Find( const std::string& key ) const
198 EntryType* result = NULL;
201 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
203 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
205 // Const cast because of const_iterator. const_iterator because, STL.
206 result = const_cast<EntryType*>(&(iter->entry));
214 * Find the element in the dictionary pointed at by key using a case
215 * insensitive search, and return a const pointer to it, or NULL
217 const EntryType* FindConst( const char* key ) const
221 std::string theKey(key);
222 return FindConst( theKey );
228 * Find the element in the dictionary pointed at by key using a case
229 * insensitive search, and return a non-const pointer to it, or NULL
231 EntryType* Find( const char* key ) const
235 std::string theKey(key);
236 return Find( theKey );
241 * Return an iterator pointing at the first entry in the dictionary
243 typename Elements::const_iterator Begin() const
245 return container.begin();
249 * Return an iterator pointing past the last entry in the dictionary
251 typename Elements::const_iterator End() const
253 return container.end();
256 void GetKeys( DictionaryKeys& keys ) const
259 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
261 keys.push_back( (*iter).key );
277 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H