Extend Property::Map to support Index-Value pairs
[platform/core/uifw/dali-core.git] / dali / public-api / object / property-map.h
1 #ifndef __DALI_PROPERTY_MAP_H__
2 #define __DALI_PROPERTY_MAP_H__
3
4 /*
5  * Copyright (c) 2015 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
21 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/property-value.h>
27 #include <dali/public-api/object/property.h>
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_object
33  * @{
34  */
35
36 typedef std::pair<std::string, Property::Value> StringValuePair;
37
38 /**
39  * @brief A Map of property values, the key type could be String or Property::Index.
40  * @SINCE_1_0.0
41  */
42 class DALI_IMPORT_API Property::Map
43 {
44 public:
45
46   typedef std::size_t SizeType;
47
48   /**
49    * @brief Default constructor.
50    * @SINCE_1_0.0
51    */
52   Map();
53
54   /**
55    * @brief Copy Constructor.
56    *
57    * @SINCE_1_0.0
58    * @param[in] other The Map to copy from.
59    */
60   Map( const Map& other );
61
62   /**
63    * @brief Non-virtual destructor.
64    * @SINCE_1_0.0
65    */
66   ~Map();
67
68   /**
69    * @brief Retrieve the number of elements in the map.
70    *
71    * @SINCE_1_0.0
72    * @return The number of elements in the map.
73    */
74   SizeType Count() const;
75
76   /**
77    * @brief Returns whether the map is empty.
78    *
79    * @SINCE_1_0.0
80    * @return true if empty, false otherwise
81    */
82   bool Empty() const;
83
84   /**
85    * @brief Inserts the key-value pair in the Map, with the key type as string.
86    *
87    * Does not check for duplicates
88    * @SINCE_1_0.0
89    * @param key to insert
90    * @param value to insert
91    */
92   void Insert( const char* key, const Value& value );
93
94   /**
95    * @brief Inserts the key-value pair in the Map, with the key type as string.
96    *
97    * Does not check for duplicates
98    * @SINCE_1_0.0
99    * @param key to insert
100    * @param value to insert
101    */
102   void Insert( const std::string& key, const Value& value );
103
104   /**
105    * @brief Inserts the key-value pair in the Map, with the key type as index.
106    *
107    * Does not check for duplicates
108    * @SINCE_1_1.39
109    * @param key to insert
110    * @param value to insert
111    */
112   void Insert( Property::Index key, const Value& value );
113
114   /**
115    * DEPRECATED_1_1.39. Retrieve the value with key instead of position, Use Find( key ) instead.
116    *
117    * @brief Retrieve the value of the string-value pair at the specified position.
118    *
119    * @SINCE_1_0.0
120    * @return A reference to the value at the specified position.
121    *
122    * @note Will assert if position >= Count()
123    */
124   Value& GetValue( SizeType position ) const;
125
126   /**
127    * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
128    *
129    * @brief Retrieve the key at the specified position.
130    *
131    * @SINCE_1_0.0
132    * @return A const reference to the key at the specified position.
133    *
134    * @note Will assert if position >= Count()
135    */
136   const std::string& GetKey( SizeType position ) const;
137
138   /**
139    * DEPRECATED_1_1.39 Position based retrieval is no longer supported after extending the key type to both Index and String.
140    *
141    * @brief Retrieve the key & the value at the specified position.
142    *
143    * @SINCE_1_0.0
144    * @return A reference to the pair of key and value at the specified position.
145    *
146    * @note Will assert if position >= Count()
147    */
148   StringValuePair& GetPair( SizeType position ) const;
149
150   /**
151    * @brief Finds the value for the specified key if it exists.
152    *
153    * @SINCE_1_0.0
154    * @param[in]  key   The key to find.
155    *
156    * @return A const pointer to the value if it exists, NULL otherwise
157    */
158   Value* Find( const char* key ) const;
159
160   /**
161    * @brief Finds the value for the specified key if it exists.
162    *
163    * @SINCE_1_0.0
164    * @param[in]  key   The key to find.
165    *
166    * @return A const pointer to the value if it exists, NULL otherwise
167    */
168   Value* Find( const std::string& key ) const;
169
170   /**
171    * @brief Finds the value for the specified key if it exists.
172    *
173    * @SINCE_1_1.39
174    * @param[in]  key   The key to find.
175    *
176    * @return A const pointer to the value if it exists, NULL otherwise
177    */
178   Value* Find( Property::Index key ) const;
179
180   /**
181    * @brief Finds the value for the specified key if it exists and its type is type
182    *
183    * @SINCE_1_0.0
184    * @param[in]  key   The key to find.
185    * @param[in]  type  The type to check.
186    *
187    * @return A const pointer to the value if it exists, NULL otherwise
188    */
189   Value* Find( const std::string& key, Property::Type type ) const;
190
191   /**
192    * @brief Finds the value for the specified key if it exists and its type is type
193    *
194    * @SINCE_1_1.39
195    * @param[in]  key   The key to find.
196    * @param[in]  type  The type to check.
197    *
198    * @return A const pointer to the value if it exists, NULL otherwise
199    */
200   Value* Find( Property::Index key, Property::Type type ) const;
201
202   /**
203    * @brief Clears the map.
204    * @SINCE_1_0.0
205    */
206   void Clear();
207
208   /**
209    * @brief Merges values from the map 'from' to the current.
210    *
211    * Any values in 'from' will overwrite the values in the current map.
212    *
213    * @SINCE_1_0.0
214    * @param[in]  from  The map to merge from.
215    */
216   void Merge( const Map& from );
217
218   /**
219    * @brief Const operator to access element with the specified string key.
220    *
221    * @SINCE_1_0.0
222    * @param[in] key The key whose value to access.
223    *
224    * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned.
225    *
226    * @note Will assert if invalid-key is given.
227    */
228   const Value& operator[]( const std::string& key ) const;
229
230   /**
231    * @brief Operator to access the element with the specified string key.
232    *
233    * @SINCE_1_0.0
234    * @param[in] key The key whose value to access.
235    *
236    * @return A reference to the value for the element with the specified key.
237    *
238    * @note If an element with the key does not exist, then it is created.
239    */
240   Value& operator[]( const std::string& key );
241
242   /**
243    * @brief Const operator to access element with the specified index key.
244    *
245    * @SINCE_1_1.39
246    * @param[in] key The key whose value to access.
247    *
248    * @return The value for the element with the specified key, if key doesn't exist, then Property::NONE is returned.
249    *
250    * @note Will assert if invalid-key is given.
251    */
252   const Value& operator[]( Property::Index key ) const;
253
254   /**
255    * @brief Operator to access the element with the specified index key.
256    *
257    * @SINCE_1_1.39
258    * @param[in] key The key whose value to access.
259    *
260    * @return A reference to the value for the element with the specified key.
261    *
262    * @note If an element with the key does not exist, then it is created.
263    */
264   Value& operator[]( Property::Index key );
265
266   /**
267    * @brief Assignment Operator
268    *
269    * @SINCE_1_0.0
270    * @param[in] other The map to copy from.
271    *
272    * @return The copied map.
273    */
274   Map& operator=( const Map& other );
275
276   /**
277    * @brief output to stream
278    * @SINCE_1_1.28
279    */
280   friend std::ostream& operator<<( std::ostream& stream, const Property::Map& map );
281
282 private:
283   struct DALI_INTERNAL Impl; ///< Private data
284   Impl* mImpl; ///< Pointer to private data
285 };
286
287 /**
288  * @brief Convert the key/value pairs of the property map into a string and append to an output stream.
289  *
290  * @SINCE_1_1.28
291  * @param [in] stream The output stream operator.
292  * @param [in] map The map to insert
293  * @return The output stream operator.
294  */
295 DALI_IMPORT_API std::ostream& operator<<( std::ostream& stream, const Property::Map& map );
296
297
298 /**
299  * @}
300  */
301 } // namespace Dali
302
303 #endif // __DALI_PROPERTY_MAP_H__