2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseColIMap.h
20 * @brief This is the header file for the %IMap interface.
22 * This header file contains the declarations of the %IMap interface.
24 #ifndef _FBASE_COL_IMAP_H_
25 #define _FBASE_COL_IMAP_H_
27 #include <FBaseColICollection.h>
28 #include <FBaseColTypes.h>
29 #include <FBaseColIMapEnumerator.h>
30 #include <FBaseObject.h>
33 namespace Tizen { namespace Base { namespace Collection
41 * @brief This interface represents a collection of key-value pairs.
45 * The %IMap interface abstracts a collection of key-value pairs. An %IMap instance
46 * contains unique keys and each key maps to a single value.
47 * The key and value cannot be a @c null reference.
49 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/hashmap_multihashmap.htm">HashMap and MultiHashMap</a>.
51 class _OSP_EXPORT_ IMap
52 : public virtual ICollection
56 * This polymorphic destructor should be overridden if required. @n
57 * This way, the destructors of the derived classes are called when the destructor of this interface is called.
61 virtual ~IMap(void) {}
65 * Adds the specified key-value pair to the map.
67 * @brief <i> [Deprecated] </i>
68 * @deprecated This method is deprecated because it has a problem of const reference argument.
69 * Instead of using this method, use Add(Object* pKey, Object* pValue).
72 * @return An error code
73 * @param[in] key The key to add
74 * @param[in] value The corresponding value to add
75 * @exception E_SUCCESS The method is successful.
76 * @exception E_INVALID_ARG A specified input parameter is invalid, or
77 * the comparer has failed to compare the keys.
78 * @exception E_OBJ_ALREADY_EXIST The specified @c key already exists.
79 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
83 result Add(const Object& key, const Object& value)
85 return Add(const_cast< Object* >(&key), const_cast< Object* >(&value));
89 * Adds the specified key-value pair to the map.
93 * @return An error code
94 * @param[in] pKey The pointer to key to add
95 * @param[in] pValue The pointer to corresponding value to add
96 * @exception E_SUCCESS The method is successful.
97 * @exception E_INVALID_ARG A specified input parameter is invalid, or
98 * the comparer has failed to compare the keys.
99 * @exception E_OBJ_ALREADY_EXIST The specified @c pKey already exists.
100 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
103 virtual result Add(Object* pKey, Object* pValue) = 0;
106 * Checks if the key exists. If the key doesn't exist, Add() is called. Unless, SetValue() is called.
110 * @return An error code
111 * @param[in] pKey The pointer to key to add
112 * @param[in] pValue The pointer to corresponding value to add
113 * @exception E_SUCCESS The method is successful.
114 * @exception E_INVALID_ARG A specified input parameter is invalid, or
115 * the comparer has failed to compare the keys.
116 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
120 result AddIfNotExistOrSet(Object* pKey, Object* pValue)
122 if (!ContainsKey(*pKey))
124 return Add(pKey, pValue);
128 return SetValue(*pKey, pValue);
133 * Gets the value associated with the specified key.
137 * @return The value associated with the specified key, @n
138 * else @c null if an exception occurs
139 * @param[in] key The key to find the associated value
140 * @exception E_SUCCESS The method is successful.
141 * @exception E_INVALID_ARG The specified input parameter is invalid, or
142 * the comparer has failed to compare the keys.
143 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
144 * @remarks The specific error code can be accessed using the GetLastResult() method.
147 virtual const Object* GetValue(const Object& key) const = 0;
150 * Gets the value associated with the specified key.
154 * @return The value associated with the specified key, @n
155 * else @c null if an exception occurs
156 * @param[in] key The key to find the associated value
157 * @exception E_SUCCESS The method is successful.
158 * @exception E_INVALID_ARG The specified input parameter is invalid, or
159 * the comparer has failed to compare the keys.
160 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
161 * @remarks The specific error code can be accessed using the GetLastResult() method.
164 virtual Object* GetValue(const Object& key) = 0;
167 * Gets a list of all the keys in the map.
171 * @return A pointer to a list of all the keys in the map, @n
172 * else @c null if an exception occurs
173 * @remarks The order of the keys is the same as the corresponding values in the IList interface returned by the GetValuesN() method.
174 * @remarks The %IList interface stores just the pointers to the elements in the map, not the elements themselves.
175 * @remarks The specific error code can be accessed using the GetLastResult() method.
178 virtual IList* GetKeysN(void) const = 0;
181 * Gets a list of all the values in the map.
185 * @return A pointer to a list of all the values in the map, @n
186 * else @c null if an exception occurs
187 * @remarks The IList stores just the pointers to the elements in the map, not the elements themselves.
188 * @remarks 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.
206 * @remarks 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
207 * The element deleter style is recommended rather than using the @c forceDeletetion argument in the remove method.@n
208 * If both an element deleter and forceDeleteion are set, the remove operation follows @c forceDeletion setting.
209 * @remarks Remove(key, @b true) internally works as the below code:
211 * DeleterFunctionType deleter = GetDeleter();
212 * SetDeleter(SingleObjectDeleter);
214 * SetDeleter(deleter);
216 * @remarks Remove(key, @b false) internally works as the below code:
218 * DeleterFunctionType deleter = GetDeleter();
219 * SetDeleter(NoOpDeleter);
221 * SetDeleter(deleter);
225 result Remove(const Object& key, bool forceDeletion)
227 DeleterFunctionType deleter = GetDeleter();
231 SetDeleter(SingleObjectDeleter);
235 SetDeleter(NoOpDeleter);
238 result r = Remove(key);
244 * Removes the value associated with the specified key.
248 * @return An error code
249 * @param[in] key The key for which the value is to remove
250 * @exception E_SUCCESS The method is successful.
251 * @exception E_INVALID_ARG A specified input parameter is invalid, or
252 * the comparer has failed to compare the keys.
253 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
256 virtual result Remove(const Object& key) = 0;
259 * Removes all the object pointers in the collection. @n
260 * 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.
264 * @param[in] forceDeletion Set to @c true to deallocate all the objects, @n
266 * @remarks 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
267 * The element deleter style is recommended rather than using the @c forceDeletetion argument in the remove method.@n
268 * If both an element deleter and forceDeleteion are set, the remove operation follows @c forceDeletion setting.
269 * @remarks RemoveAll(@b true) internally works as the below code:
271 * DeleterFunctionType deleter = GetDeleter();
272 * SetDeleter(SingleObjectDeleter);
274 * SetDeleter(deleter);
276 * @remarks RemoveAll(@b false) internally works as the below code:
278 * DeleterFunctionType deleter = GetDeleter();
279 * SetDeleter(NoOpDeleter);
281 * SetDeleter(deleter);
284 void RemoveAll(bool forceDeletion)
286 DeleterFunctionType deleter = GetDeleter();
290 SetDeleter(SingleObjectDeleter);
294 SetDeleter(NoOpDeleter);
302 * Removes all the object pointers in the collection. @n
306 virtual void RemoveAll(void) = 0;
309 * Replaces the value associated with the specified key with the specified value.
313 * @return An error code
314 * @param[in] key The key for which the value is to replace
315 * @param[in] value The new value
316 * @param[in] forceDeletion Set to @c true to deallocate the object, @n
318 * @exception E_SUCCESS The method is successful.
319 * @exception E_INVALID_ARG A specified input parameter is invalid, or
320 * the comparer has failed to compare the keys.
321 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
322 * @remarks Use the Add() method to add a new key-value pair.
323 * @remarks 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
324 * The element deleter style is recommended rather than using the @c forceDeletetion argument in the set method.@n
325 * If both an element deleter and forceDeleteion are set, the set operation follows @c forceDeletion setting.
326 * @remarks SetValue(key, value, @b true) internally works as the below code:
328 * DeleterFunctionType deleter = GetDeleter();
329 * SetDeleter(SingleObjectDeleter);
330 * SetValue(key, const_cast< Object* >(&value));
331 * SetDeleter(deleter);
333 * @remarks SetValue(key, value, @b false) internally works as the below code:
335 * DeleterFunctionType deleter = GetDeleter();
336 * SetDeleter(NoOpDeleter);
337 * SetValue(key, const_cast< Object* >(&value));
338 * SetDeleter(deleter);
343 result SetValue(const Object& key, const Object& value, bool forceDeletion = false)
345 DeleterFunctionType deleter = GetDeleter();
349 SetDeleter(SingleObjectDeleter);
353 SetDeleter(NoOpDeleter);
356 result r = SetValue(key, const_cast< Object* >(&value));
362 * Replaces the value associated with the specified key with the specified value.
366 * @return An error code
367 * @param[in] key The key for which the value is to replace
368 * @param[in] pValue The pointer to new value
369 * @exception E_SUCCESS The method is successful.
370 * @exception E_INVALID_ARG A specified input parameter is invalid, or
371 * the comparer has failed to compare the keys.
372 * @exception E_OBJ_NOT_FOUND The specified @c key is not found in the map.
373 * @remarks Use the Add() method to add a new key-value pair.
377 virtual result SetValue(const Object& key, Object* pValue) = 0;
381 * Checks whether the map contains the specified key.
383 * @brief <i> [Deprecated] </i>
384 * @deprecated This method is deprecated because it transfers a result of comparison in out-parameter form.
385 * The return type will be changed into boolean type and this method will return the result.
386 * Instead of using this method, use bool ContainsKey(const Object& key).
389 * @return An error code
390 * @param[in] key The key to locate
391 * @param[out] out Set to @c true if the map contains the specified key, @n
393 * @exception E_SUCCESS The method is successful.
394 * @exception E_INVALID_ARG A specified input parameter is invalid, or
395 * the comparer has failed to compare the keys.
396 * @see ContainsValue()
399 result ContainsKey(const Object& key, bool& out) const
401 out = ContainsKey(key);
402 result r = GetLastResult();
407 * Checks whether the map contains the specified key.
411 * @return @c true if the map contains the specified key, @n
413 * @param[in] key The key to locate
414 * @exception E_SUCCESS The method is successful.
415 * @exception E_INVALID_ARG A specified input parameter is invalid, or
416 * the comparer has failed to compare the keys.
417 * @remarks The specific error code can be accessed using the GetLastResult() method.
418 * @see ContainsValue()
420 virtual bool ContainsKey(const Object& key) const = 0;
423 * Checks whether the map contains the specified value.
427 * @return @c true if the map contains the specified value, @n
429 * @param[in] value The value to locate
433 virtual bool ContainsValue(const Object& value) const = 0;
436 * Gets an instance of the IMapEnumerator for the map.
440 * @return IMapEnumerator object of this map, @n
441 * else @c null if an exception occurs
442 * @exception E_SUCCESS The method is successful.
443 * @remarks The specific error code can be accessed using the GetLastResult() method.
445 * @see IMapEnumerator
447 virtual IMapEnumerator* GetMapEnumeratorN(void) const = 0;
450 * Gets the element deleter of the collection.
454 * @return An function pointer to the existing element deleter
456 virtual DeleterFunctionType GetDeleter(void) const = 0;
460 // This method is for internal use only. Using this method can cause behavioral, security-related,
461 // and consistency-related issues in the application.
462 // This method is reserved and may change its name at any time without prior notice.
466 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_