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.
39 template<typename EntryType>
44 * Element is a key-value pair
50 Element( const std::string&name, EntryType entry )
56 typedef std::vector<Element> Elements;
61 * Only allow const iteration over the dictionary
63 typedef typename Elements::const_iterator iterator;
69 Dictionary<EntryType>()
74 * Add a key value pair to the dictionary.
75 * If the entry does not already exist, add it to the dictionary
76 * using a shallow copy
78 bool Add( const std::string& name, const EntryType& entry )
80 for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
82 if( iter->key == name )
87 container.push_back( Element(name, entry) );
92 * Add a key-value pair to the dictionary
93 * If the entry does not already exist, add it to the dictionary
96 bool Add( const char* name, const EntryType& entry )
101 std::string theName(name);
102 result=Add(theName, entry);
108 * Find the element in the dictionary pointed at by key, and
109 * return a pointer to it, or NULL.
111 EntryType* Find( const std::string& key ) const
113 EntryType* result=NULL;
117 for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
119 if( iter->key == key )
121 result = &(iter->entry);
130 * Find the element in the dictionary pointed at by key, and
131 * return a pointer to it, or NULL
133 EntryType* Find( const char* key ) const
137 std::string theKey(key);
144 * Find the element in the dictionary pointed at by key using a case
145 * insensitive search, and return a const pointer to it, or NULL
147 const EntryType* FindCaseInsensitiveC( const std::string& key ) const
151 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
153 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
155 const EntryType* result = &(iter->entry);
164 * Find the element in the dictionary pointed at by key using a case
165 * insensitive search, and return a non-const pointer to it, or NULL
167 EntryType* FindCaseInsensitive( const std::string& key ) const
169 EntryType* result = NULL;
172 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
174 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
176 // Const cast because of const_iterator. const_iterator because, STL.
177 result = const_cast<EntryType*>(&(iter->entry));
185 * Find the element in the dictionary pointed at by key using a case
186 * insensitive search, and return a const pointer to it, or NULL
188 const EntryType* FindCaseInsensitiveC( const char* key ) const
192 std::string theKey(key);
193 return FindCaseInsensitiveC( theKey );
199 * Find the element in the dictionary pointed at by key using a case
200 * insensitive search, and return a non-const pointer to it, or NULL
202 EntryType* FindCaseInsensitive( const char* key ) const
206 std::string theKey(key);
207 return FindCaseInsensitive( theKey );
213 * Return an iterator pointing at the first entry in the dictionary
215 typename Elements::const_iterator Begin() const
217 return container.begin();
221 * Return an iterator pointing past the last entry in the dictionary
223 typename Elements::const_iterator End() const
225 return container.end();
235 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H