2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseColIMap.h
19 * @brief This is the header file for the %IMap interface.
21 * This header file contains the declarations of the %IMap interface.
23 #ifndef _FBASE_COL_IMAP_H_
24 #define _FBASE_COL_IMAP_H_
26 #include <FBaseColICollection.h>
27 #include <FBaseColTypes.h>
28 #include <FBaseColIMapEnumerator.h>
29 #include <FBaseObject.h>
31 namespace Tizen { namespace Base { namespace Collection
39 * @brief This interface represents a collection of key-value pairs.
43 * The %IMap interface abstracts a collection of key-value pairs. An %IMap instance
44 * contains unique keys and each key maps to a single value.
45 * The key and value cannot be a @c null reference.
47 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/hashmap_multihashmap.htm">HashMap and MultiHashMap</a>.
49 class _OSP_EXPORT_ IMap
50 : public virtual ICollection
54 * This polymorphic destructor should be overridden if required. @n
55 * This way, the destructors of the derived classes are called when the destructor of this interface is called.
59 virtual ~IMap(void) {}
63 * Adds the specified key-value pair to the map.
65 * @brief <i> [Deprecated] </i>
66 * @deprecated This method is deprecated because it has a problem of const reference argument.
67 * Instead of using this method, use Add(Object* pKey, Object* pValue).
70 * @return An error code
71 * @param[in] key The key to add
72 * @param[in] value The corresponding value to add
73 * @exception E_SUCCESS The method is successful.
74 * @exception E_INVALID_ARG A specified input parameter is invalid, or
75 * the comparer has failed to compare the keys.
76 * @exception E_OBJ_ALREADY_EXIST The specified @c key already exists.
77 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
81 result Add(const Object& key, const Object& value)
83 return Add(const_cast< Object* >(&key), const_cast< Object* >(&value));
87 * Adds the specified key-value pair to the map.
91 * @return An error code
92 * @param[in] pKey The pointer to key to add
93 * @param[in] pValue The pointer to corresponding value to add
94 * @exception E_SUCCESS The method is successful.
95 * @exception E_INVALID_ARG A specified input parameter is invalid, or
96 * the comparer has failed to compare the keys.
97 * @exception E_OBJ_ALREADY_EXIST The specified @c pKey already exists.
98 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
101 virtual result Add(Object* pKey, Object* pValue) = 0;
104 * Checks if the key exists. If the key does not exist, Add() is called. Unless, SetValue() is called.
108 * @return An error code
109 * @param[in] pKey The pointer to key to add
110 * @param[in] pValue The pointer to corresponding value to add
111 * @exception E_SUCCESS The method is successful.
112 * @exception E_INVALID_ARG A specified input parameter is invalid, or
113 * the comparer has failed to compare the keys.
114 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
115 * @see Add(Object*, Object*)
118 result AddIfNotExistOrSet(Object* pKey, Object* pValue)
120 if (!ContainsKey(*pKey))
122 return Add(pKey, pValue);
126 return SetValue(*pKey, pValue);
131 * Gets the value associated with the specified key.
135 * @return The value associated with the specified key, @n
136 * else @c null if an exception occurs
137 * @param[in] key The key to find the associated value
138 * @exception E_SUCCESS The method is successful.
139 * @exception E_INVALID_ARG The specified input parameter is invalid, or
140 * the comparer has failed to compare the keys.
141 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
142 * @remarks The specific error code can be accessed using the GetLastResult() method.
145 virtual const Object* GetValue(const Object& key) const = 0;
148 * Gets the value associated with the specified key.
152 * @return The value associated with the specified key, @n
153 * else @c null if an exception occurs
154 * @param[in] key The key to find the associated value
155 * @exception E_SUCCESS The method is successful.
156 * @exception E_INVALID_ARG The specified input parameter is invalid, or
157 * the comparer has failed to compare the keys.
158 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
159 * @remarks The specific error code can be accessed using the GetLastResult() method.
162 virtual Object* GetValue(const Object& key) = 0;
165 * Gets a list of all the keys in the map.
169 * @return A pointer to a list of all the keys in the map, @n
170 * else @c null if an exception occurs
172 * - The order of the keys is the same as the corresponding values in the IList interface returned by the GetValuesN() method.
173 * - The %IList interface stores just the pointers to the elements in the map, not the elements themselves.
174 * - The specific error code can be accessed using the GetLastResult() method.
177 virtual IList* GetKeysN(void) const = 0;
180 * Gets a list of all the values in the map.
184 * @return A pointer to a list of all the values in the map, @n
185 * else @c null if an exception occurs
187 * - The IList stores just the pointers to the elements in the map, not the elements themselves.
188 * - The specific error code can be accessed using the GetLastResult() method.
191 virtual IList* GetValuesN(void) const = 0;
194 * Removes the value associated with the specified key.
198 * @return An error code
199 * @param[in] key The key to remove
200 * @param[in] forceDeletion Set to @c true to deallocate the object, @n
202 * @exception E_SUCCESS The method is successful.
203 * @exception E_INVALID_ARG A specified input parameter is invalid, or
204 * the comparer has failed to compare the keys.
205 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
207 * - Based on the specified element deleter, the remove operation not only gets rid of an element from a list, but also deletes its object instance. @n
208 * The element deleter style is recommended rather than using the @c forceDeletetion argument in the remove method. @n
209 * If both an element deleter and forceDeleteion are set, the remove operation follows @c forceDeletion setting.
210 * - Remove(key, @b true) internally works as the below code:
212 * DeleterFunctionType deleter = GetDeleter();
213 * SetDeleter(SingleObjectDeleter);
215 * SetDeleter(deleter);
217 * - Remove(key, @b false) internally works as the below code:
219 * DeleterFunctionType deleter = GetDeleter();
220 * SetDeleter(NoOpDeleter);
222 * SetDeleter(deleter);
224 * @see Add(Object*, Object*)
226 result Remove(const Object& key, bool forceDeletion)
228 DeleterFunctionType deleter = GetDeleter();
232 SetDeleter(SingleObjectDeleter);
236 SetDeleter(NoOpDeleter);
239 result r = Remove(key);
245 * Removes the value associated with the specified key.
249 * @return An error code
250 * @param[in] key The key for which the value is to remove
251 * @exception E_SUCCESS The method is successful.
252 * @exception E_INVALID_ARG A specified input parameter is invalid, or
253 * the comparer has failed to compare the keys.
254 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
255 * @see Add(Object*, Object*)
257 virtual result Remove(const Object& key) = 0;
260 * Removes all the object pointers in the collection. @n
261 * If the @c forceDeletion parameter is set to @c true, the method also removes all the objects. This method can be called before deleting the collection.
265 * @param[in] forceDeletion Set to @c true to deallocate all the objects, @n
268 * - Based on the specified element deleter, the remove operation not only gets rid of an element from a list, but also deletes its object instance. @n
269 * The element deleter style is recommended rather than using the @c forceDeletetion argument in the remove method. @n
270 * If both an element deleter and forceDeleteion are set, the remove operation follows @c forceDeletion setting.
271 * - RemoveAll(@b true) internally works as the below code:
273 * DeleterFunctionType deleter = GetDeleter();
274 * SetDeleter(SingleObjectDeleter);
276 * SetDeleter(deleter);
278 * - RemoveAll(@b false) internally works as the below code:
280 * DeleterFunctionType deleter = GetDeleter();
281 * SetDeleter(NoOpDeleter);
283 * SetDeleter(deleter);
286 void RemoveAll(bool forceDeletion)
288 DeleterFunctionType deleter = GetDeleter();
292 SetDeleter(SingleObjectDeleter);
296 SetDeleter(NoOpDeleter);
304 * Removes all the object pointers in the collection. @n
308 virtual void RemoveAll(void) = 0;
311 * Replaces the value associated with the specified key with the specified value.
315 * @return An error code
316 * @param[in] key The key for which the value is to replace
317 * @param[in] value The new value
318 * @param[in] forceDeletion Set to @c true to deallocate the object, @n
320 * @exception E_SUCCESS The method is successful.
321 * @exception E_INVALID_ARG A specified input parameter is invalid, or
322 * the comparer has failed to compare the keys.
323 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
325 * - Use the Add(Object*, Object*) method to add a new key-value pair.
326 * - Based on the specified element deleter, the set operation not only gets rid of an element from a list, but also deletes its object instance. @n
327 * The element deleter style is recommended rather than using the @c forceDeletetion argument in the set method. @n
328 * If both an element deleter and forceDeleteion are set, the set operation follows @c forceDeletion setting.
329 * - SetValue(key, value, @b true) internally works as the below code:
331 * DeleterFunctionType deleter = GetDeleter();
332 * SetDeleter(SingleObjectDeleter);
333 * SetValue(key, const_cast< Object* >(&value));
334 * SetDeleter(deleter);
336 * - SetValue(key, value, @b false) internally works as the below code:
338 * DeleterFunctionType deleter = GetDeleter();
339 * SetDeleter(NoOpDeleter);
340 * SetValue(key, const_cast< Object* >(&value));
341 * SetDeleter(deleter);
345 result SetValue(const Object& key, const Object& value, bool forceDeletion = false)
347 DeleterFunctionType deleter = GetDeleter();
351 SetDeleter(SingleObjectDeleter);
355 SetDeleter(NoOpDeleter);
358 result r = SetValue(key, const_cast< Object* >(&value));
364 * Replaces the value associated with the specified key with the specified value.
368 * @return An error code
369 * @param[in] key The key for which the value is to replace
370 * @param[in] pValue The pointer to new value
371 * @exception E_SUCCESS The method is successful.
372 * @exception E_INVALID_ARG A specified input parameter is invalid, or
373 * the comparer has failed to compare the keys.
374 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
375 * @remarks Use the Add(Object*, Object*) method to add a new key-value pair.
378 virtual result SetValue(const Object& key, Object* pValue) = 0;
382 * Checks whether the map contains the specified key.
384 * @brief <i> [Deprecated] </i>
385 * @deprecated This method is deprecated because it transfers a result of comparison in out-parameter form.
386 * The return type will be changed into boolean type and this method will return the result.
387 * Instead of using this method, use bool ContainsKey(const Object& key).
390 * @return An error code
391 * @param[in] key The key to locate
392 * @param[out] out Set to @c true if the map contains the specified key, @n
394 * @exception E_SUCCESS The method is successful.
395 * @exception E_INVALID_ARG A specified input parameter is invalid, or
396 * the comparer has failed to compare the keys.
397 * @see ContainsValue()
400 result ContainsKey(const Object& key, bool& out) const
402 out = ContainsKey(key);
403 result r = GetLastResult();
408 * Checks whether the map contains the specified key.
412 * @return @c true if the map contains the specified key, @n
414 * @param[in] key The key to locate
415 * @exception E_SUCCESS The method is successful.
416 * @exception E_INVALID_ARG A specified input parameter is invalid, or
417 * the comparer has failed to compare the keys.
418 * @remarks The specific error code can be accessed using the GetLastResult() method.
419 * @see ContainsValue()
421 virtual bool ContainsKey(const Object& key) const = 0;
424 * Checks whether the map contains the specified value.
428 * @return @c true if the map contains the specified value, @n
430 * @param[in] value The value to locate
432 * @see ContainsKey(const Object&)
434 virtual bool ContainsValue(const Object& value) const = 0;
437 * Gets an instance of the IMapEnumerator for the map.
441 * @return IMapEnumerator object of this map, @n
442 * else @c null if an exception occurs
443 * @exception E_SUCCESS The method is successful.
444 * @remarks The specific error code can be accessed using the GetLastResult() method.
446 * @see IMapEnumerator
448 virtual IMapEnumerator* GetMapEnumeratorN(void) const = 0;
451 * Gets the element deleter of the collection.
455 * @return A function pointer to the existing element deleter
457 virtual DeleterFunctionType GetDeleter(void) const = 0;
461 // This method is for internal use only. Using this method can cause behavioral, security-related,
462 // and consistency-related issues in the application.
463 // This method is reserved and may change its name at any time without prior notice.
467 virtual void IMap_Reserved1(void) { }
470 // This method is for internal use only. Using this method can cause behavioral, security-related,
471 // and consistency-related issues in the application.
472 // This method is reserved and may change its name at any time without prior notice.
476 virtual void IMap_Reserved2(void) { }
480 * Sets the element deleter of the collection.
484 * @param[in] deleter A function pointer to the element deleter to set
486 virtual void SetDeleter(DeleterFunctionType deleter) = 0;
490 }}} // Tizen::Base::Collection
492 #endif // _FBASE_COL_IMAP_H_