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>
25 extern bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b );
33 * The Dictionary template class enables a means of storing key-value
34 * pairs where the keys are strings and the value can be a complex
37 * It enables lookup of keys via case-insensitive match.
41 typedef std::vector<std::string> DictionaryKeys;
42 inline void Merge( DictionaryKeys& toDict, const DictionaryKeys& fromDict )
44 for( DictionaryKeys::const_iterator fromIter = fromDict.begin(); fromIter != fromDict.end(); ++fromIter )
46 const std::string& fromKey = (*fromIter);
47 DictionaryKeys::iterator toIter = std::find( toDict.begin(), toDict.end(), fromKey );
48 if( toIter == toDict.end() )
50 toDict.push_back( fromKey );
56 template<typename EntryType>
61 * Element is a key-value pair
67 Element( const std::string&name, EntryType entry )
73 typedef std::vector<Element> Elements;
78 * Only allow const iteration over the dictionary
80 typedef typename Elements::const_iterator iterator;
85 Dictionary<EntryType>()
90 * Add a key value pair to the dictionary.
91 * If the entry does not already exist, add it to the dictionary
92 * using a shallow copy
94 bool Add( const std::string& name, const EntryType& entry )
96 for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
98 if( iter->key == name )
103 container.push_back( Element(name, entry) );
108 * Add a key-value pair to the dictionary
109 * If the entry does not already exist, add it to the dictionary
112 bool Add( const char* name, const EntryType& entry )
117 std::string theName(name);
118 result=Add(theName, entry);
124 * Remove a key value pair from the dictionary.
126 void Remove( const std::string& name )
128 for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
130 if( iter->key == name )
132 container.erase( iter );
139 * Remove a key value pair from the dictionary.
141 void Remove( const char* name )
145 std::string theName(name);
150 void Merge( const Dictionary<EntryType>& dictionary )
152 for( typename Elements::const_iterator fromIter = dictionary.container.begin(); fromIter != dictionary.container.end(); ++fromIter )
155 for( typename Elements::iterator toIter = container.begin(); toIter != container.end(); ++toIter )
157 if( fromIter->key == toIter->key )
160 toIter->entry = fromIter->entry;
165 container.push_back( Element(fromIter->key, fromIter->entry) );
171 * Find the element in the dictionary pointed at by key, and
172 * insensitive search, and return a const pointer to it, or NULL
174 const EntryType* FindConst( const std::string& key ) const
178 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
180 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
182 const EntryType* result = &(iter->entry);
191 * Find the element in the dictionary pointed at by key using a case
192 * insensitive search, and return a non-const pointer to it, or NULL
194 EntryType* Find( const std::string& key ) const
196 EntryType* result = NULL;
199 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
201 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
203 // Const cast because of const_iterator. const_iterator because, STL.
204 result = const_cast<EntryType*>(&(iter->entry));
212 * Find the element in the dictionary pointed at by key using a case
213 * insensitive search, and return a const pointer to it, or NULL
215 const EntryType* FindConst( const char* key ) const
219 std::string theKey(key);
220 return FindConst( theKey );
226 * Find the element in the dictionary pointed at by key using a case
227 * insensitive search, and return a non-const pointer to it, or NULL
229 EntryType* Find( const char* key ) const
233 std::string theKey(key);
234 return Find( theKey );
239 * Return an iterator pointing at the first entry in the dictionary
241 typename Elements::const_iterator Begin() const
243 return container.begin();
247 * Return an iterator pointing past the last entry in the dictionary
249 typename Elements::const_iterator End() const
251 return container.end();
254 void GetKeys( DictionaryKeys& keys ) const
257 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
259 keys.push_back( (*iter).key );
275 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H