Added Control::SetSubState handling
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / dictionary.h
1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #include <dali/public-api/common/vector-wrapper.h>
21 #include <algorithm>
22
23 namespace Dali
24 {
25 extern bool CaseInsensitiveStringCompare( const std::string& a, const std::string& b );
26
27 namespace Toolkit
28 {
29 namespace Internal
30 {
31
32 /**
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
35  * type.
36  *
37  * It enables lookup of keys via case-insensitive match.
38  */
39 template<typename EntryType>
40 class Dictionary
41 {
42 private:
43   /**
44    * Element is a key-value pair
45    */
46   struct Element
47   {
48     std::string key;
49     EntryType entry;
50     Element( const std::string&name, EntryType entry )
51     : key( name ),
52       entry( entry )
53     {
54     }
55   };
56   typedef std::vector<Element> Elements;
57   Elements container;
58
59 public:
60   /**
61    * Only allow const iteration over the dictionary
62    */
63   typedef typename Elements::const_iterator iterator;
64
65
66   /**
67    * Constructor
68    */
69   Dictionary<EntryType>()
70   {
71   }
72
73   /**
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
77    */
78   bool Add( const std::string& name, const EntryType& entry )
79   {
80     for( typename Elements::iterator iter = container.begin(); iter != container.end(); ++iter )
81     {
82       if( iter->key == name )
83       {
84         return false;
85       }
86     }
87     container.push_back( Element(name, entry) );
88     return true;
89   }
90
91   /**
92    * Add a key-value pair to the dictionary
93    * If the entry does not already exist, add it to the dictionary
94    * (shallow copy)
95    */
96   bool Add( const char* name, const EntryType& entry )
97   {
98     bool result=false;
99     if( name != NULL )
100     {
101       std::string theName(name);
102       result=Add(theName, entry);
103     }
104     return result;
105   }
106
107   /**
108    * Find the element in the dictionary pointed at by key, and
109    * insensitive search, and return a const pointer to it, or NULL
110    */
111   const EntryType* FindConst( const std::string& key ) const
112   {
113     if( ! key.empty() )
114     {
115       for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
116       {
117         if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
118         {
119           const EntryType* result = &(iter->entry);
120           return result;
121         }
122       }
123     }
124     return NULL;
125   }
126
127   /**
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
130    */
131   EntryType* Find( const std::string& key ) const
132   {
133     EntryType* result = NULL;
134     if( ! key.empty() )
135     {
136       for( typename Elements::const_iterator iter = container.begin(); iter != container.end(); ++iter )
137       {
138         if( Dali::CaseInsensitiveStringCompare(iter->key, key ))
139         {
140           // Const cast because of const_iterator. const_iterator because, STL.
141           result = const_cast<EntryType*>(&(iter->entry));
142         }
143       }
144     }
145     return result;
146   }
147
148   /**
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
151    */
152   const EntryType* FindConst( const char* key ) const
153   {
154     if( key != NULL )
155     {
156       std::string theKey(key);
157       return FindConst( theKey );
158     }
159     return NULL;
160   }
161
162   /**
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
165    */
166   EntryType* Find( const char* key ) const
167   {
168     if( key != NULL )
169     {
170       std::string theKey(key);
171       return Find( theKey );
172     }
173     return NULL;
174   }
175   /**
176    * Return an iterator pointing at the first entry in the dictionary
177    */
178   typename Elements::const_iterator Begin() const
179   {
180     return container.begin();
181   }
182
183   /**
184    * Return an iterator pointing past the last entry in the dictionary
185    */
186   typename Elements::const_iterator End() const
187   {
188     return container.end();
189   }
190 };
191
192
193
194 }//Internal
195 }//Toolkit
196 }//Dali
197
198 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_DICTIONARY_H