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 * insensitive search, and return a const pointer to it, or NULL
111 const EntryType* FindConst( const std::string& key ) const
115 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
117 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
119 const EntryType* result = &(iter->entry);
128 * Find the element in the dictionary pointed at by key using a case
129 * insensitive search, and return a non-const pointer to it, or NULL
131 EntryType* Find( const std::string& key ) const
133 EntryType* result = NULL;
136 for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
138 if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
140 // Const cast because of const_iterator. const_iterator because, STL.
141 result = const_cast<EntryType*>(&(iter->entry));
149 * Find the element in the dictionary pointed at by key using a case
150 * insensitive search, and return a const pointer to it, or NULL
152 const EntryType* FindConst( const char* key ) const
156 std::string theKey(key);
157 return FindConst( theKey );
163 * Find the element in the dictionary pointed at by key using a case
164 * insensitive search, and return a non-const pointer to it, or NULL
166 EntryType* Find( const char* key ) const
170 std::string theKey(key);
171 return Find( theKey );
176 * Return an iterator pointing at the first entry in the dictionary
178 typename Elements::const_iterator Begin() const
180 return container.begin();
184 * Return an iterator pointing past the last entry in the dictionary
186 typename Elements::const_iterator End() const
188 return container.end();
198 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H