Implementation of ImmutableString
[platform/framework/native/appfw.git] / inc / FBaseColRandomIteratorT.h
index 6024733..8d95793 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
@@ -25,7 +24,7 @@
 #ifndef _FBASE_COL_RANDOM_ITERATOR_T_H_
 #define _FBASE_COL_RANDOM_ITERATOR_T_H_
 
-#include <algorithm>   // std::swap (Before C++11)
+#include <algorithm>    // std::swap (Before C++11)
 #include <iterator>
 #include <FBaseColIList.h>
 #include <FBaseLog.h>
@@ -35,34 +34,53 @@ namespace Tizen { namespace Base { namespace Collection
 {
 /**
  * @class      RandomIteratorT
- * @brief      This class provides an random iterator that is used to convert IList to STL containers.
- *                     StlConverter provides static methods to get this random iterator from IList.
+ * @brief      This class provides a random iterator that is used to convert %IList to STL containers. @n
+ *                     %StlConverter provides static methods to get this random iterator from %IList.
  *
  * @since      2.1
  *
- * @remarks    The %RandomIteratorT class satisfies only requirements of C++ standard library InputIterator concept due to limitations of Tizen Collection.
- *                     So, this class can be used with C++ standard library algorithms which requires only InputIterator concept for their arguments.
+ * @remarks    The %RandomIteratorT class satisfies requirements of the C++ standard library mutable RandomAccessIterator.
+ *                     It satisfies the requirements of an OutputIterator which can be dereferenced as an lvalue.
+ *
+ * The %RandomIteratorT class provides a random iterator that is used to convert IList to STL containers.
+ * StlConverter provides static methods to get this random iterator from IList.
  */
 
-template < typename T >
+template< typename T >
 class RandomIteratorT
-       : public std::iterator< std::input_iterator_tag, T >
+       : public std::iterator< std::random_access_iterator_tag, T >
 {
 public:
+       typedef std::random_access_iterator_tag iterator_category;
+       typedef typename std::iterator_traits< RandomIteratorT< T > >::value_type value_type;
+       typedef typename std::iterator_traits< RandomIteratorT< T > >::difference_type difference_type;
+       typedef typename std::iterator_traits< RandomIteratorT< T > >::pointer pointer;
+       typedef typename std::iterator_traits< RandomIteratorT< T > >::reference reference;
+
+       /**
+        * This is the default constructor for this class.
+        *
+        * @since               3.0
+        */
+       RandomIteratorT(void)
+               : __pList(null)
+               , __index(0)
+       {
+       }
+
        /**
         * Initializes this instance of %RandomIteratorT class.
         *
         * @since               2.1
         *
-        * @param[in]   list            A reference to the IList instance to convert
-        * @param[in]   position        A start position
-        * @remarks                     %RandomIteratorT only supports random accessible collection for performance.
-        * @see                         Tizen::Base::Collection::IList::IsRandomAccessible()
+        * @param[in]   list    A reference to the IList instance to convert
+        * @param[in]   index   A start index
+        * @remarks             %RandomIteratorT only supports random accessible collection for performance.
+        * @see                 Tizen::Base::Collection::IList::IsRandomAccessible()
         */
-       explicit RandomIteratorT(const IList& list, int index = 0)
+       explicit RandomIteratorT(IList& list, difference_type index = 0)
                : __pList(&list)
                , __index(index)
-               , __currentObj(static_cast< T >(const_cast< Object* >(__pList->GetAt(__index))))
        {
                AppAssertf(list.IsRandomAccessible(), "The list is not randomly accessible. RandomIteratorT only supports random accessible collection.");
        }
@@ -77,7 +95,6 @@ public:
        RandomIteratorT(const RandomIteratorT< T >& rhs)
                : __pList(rhs.__pList)
                , __index(rhs.__index)
-               , __currentObj(rhs.__currentObj)
        {
        }
 
@@ -89,7 +106,7 @@ public:
         * @return              A reference to the %RandomIteratorT instance
         * @param[in]   rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
         */
-       RandomIteratorT< T >& operator=(const RandomIteratorT< T >& rhs)
+       RandomIteratorT< T >& operator =(const RandomIteratorT< T >& rhs)
        {
                RandomIteratorT< T > tmp(rhs);
                tmp.swap(*this);
@@ -103,10 +120,10 @@ public:
         *
         * @return              A T type reference
         */
-       T& operator*(void) const
+       reference operator *(void) const
        {
                AppAssertf(__index >= 0 && __index < __pList->GetCount(), "It is out of range.");
-               return const_cast< T& >(__currentObj);
+               return reinterpret_cast< T& >(__pList->GetAtRef(__index));
        }
 
        /**
@@ -116,86 +133,78 @@ public:
         *
         * @return              A T type pointer equivalent to the pointer address
         */
-       T* operator->(void) const
+       pointer operator ->(void) const
        {
-               return &(operator*());
+               return &(operator *());
        }
 
        /**
-        * Increases __index by 1.
+        * Moves to the next element in the collection.
         *
         * @since               2.1
         *
         * @return              A reference to the %RandomIteratorT instance
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       RandomIteratorT< T >& operator++(void)
+       RandomIteratorT< T >& operator ++(void)
        {
+               ClearLastResult();
                ++__index;
-
-               // GetAt() will return null if __index is out of range.
-               __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
-               TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
+               TryReturnResult(__index >= 0 && __index < __pList->GetCount(), *this, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
                return *this;
        }
 
        /**
-        * Increases __index by 1 and returns the previous state.
+        * Moves to the next element in the collection and returns the previous state.
         *
         * @since               2.1
         *
         * @return              A %RandomIteratorT instance
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       RandomIteratorT< T > operator++(int)
+       RandomIteratorT< T > operator ++(int)
        {
                RandomIteratorT< T > tempIter = *this;
-               operator++();
+               operator ++();
                return tempIter;
        }
 
        /**
-        * Decrease __index by 1.
+        * Moves to the previous element of the collection.
         *
         * @since               2.1
         *
         * @return              A reference to the %RandomIteratorT instance
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       RandomIteratorT< T >& operator--(void)
+       RandomIteratorT< T >& operator --(void)
        {
+               ClearLastResult();
                --__index;
-
-               // GetAt() will return null if __index is out of range.
-               __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
-               TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
+               TryReturnResult(__index >= 0 && __index < __pList->GetCount(), *this, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
                return *this;
        }
 
        /**
-        * Decrease __index by 1 and returns the previous state.
+        * Moves to the previous element of the collection and returns the previous state.
         *
         * @since               2.1
         *
         * @return              A %RandomIteratorT instance
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       RandomIteratorT< T > operator--(int)
+       RandomIteratorT< T > operator --(int)
        {
                RandomIteratorT< T > tempIter = *this;
-               operator--();
+               operator --();
                return tempIter;
        }
 
@@ -204,13 +213,13 @@ public:
         *
         *      @since          2.1
         *
-        *      @return         @c true if every member of the specified %RandomIteratorT instance equals the calling instance's members, @n
+        *      @return         @c true if every member of the specified %RandomIteratorT instance equals the current instance's members, @n
         *                              else @c false
         *      @param[in]      rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
         */
-       bool operator==(const RandomIteratorT< T >& rhs) const
+       bool operator ==(const RandomIteratorT< T >& rhs) const
        {
-               return ((__pList == rhs.__pList) && (__index == rhs.__index) && (__currentObj == rhs.__currentObj));
+               return ((__pList == rhs.__pList) && (__index == rhs.__index));
        }
 
        /**
@@ -218,25 +227,25 @@ public:
         *
         *      @since          2.1
         *
-        *      @return         @c true if any member of the specified %RandomIteratorT instance is not equal to the calling instance's members, @n
+        *      @return         @c true if any member of the specified %RandomIteratorT instance is not equal to the current instance's members, @n
         *                              else @c false
         *      @param[in]      rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
         */
-       bool operator!=(const RandomIteratorT< T >& rhs) const
+       bool operator !=(const RandomIteratorT< T >& rhs) const
        {
-               return !operator==(rhs);
+               return !operator ==(rhs);
        }
 
        /**
-        *      Checks l-value is less than r-value.
+        *  Checks whether l-value is less than r-value.
         *
         *      @since          2.1
         *
-        *      @return         @c true if l-value of the specified %RandomIteratorT instance is less than the calling instance's members, @n
+        *      @return         @c true if the current instance is less than the specified %RandomIteratorT instance @n
         *                              else @c false
         *      @param[in]      rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
         */
-       bool operator<(const RandomIteratorT< T >& rhs) const
+       bool operator <(const RandomIteratorT< T >& rhs) const
        {
                return __index < rhs.__index;
        }
@@ -246,62 +255,130 @@ public:
         *
         *      @since          2.1
         *
-        *      @return         @c true if l-value of the specified %RandomIteratorT instance is greater than the calling instance's members, @n
+        *      @return         @c true if if the current instance is greater than the specified %RandomIteratorT instance @n
         *                              else @c false
         *      @param[in]      rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
         */
-       bool operator>(const RandomIteratorT< T >& rhs) const
+       bool operator >(const RandomIteratorT< T >& rhs) const
        {
                return __index > rhs.__index;
        }
 
        /**
-        * Increases __index as specified by the diff parameter.
+        *  Checks whether l-value is less than or equal to r-value.
+        *
+        *      @since          3.0
+        *
+        *      @return         @c true if the current instance is less than or equal to the specified %RandomIteratorT instance @n
+        *                              else @c false
+        *      @param[in]      rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
+        */
+       bool operator <=(const RandomIteratorT< T >& rhs) const
+       {
+               return __index <= rhs.__index;
+       }
+
+       /**
+        *      Checks whether l-value is greater than or equal to r-value.
+        *
+        *      @since          3.0
+        *
+        *      @return         @c true if the current instance is greater than or equal to the specified %RandomIteratorT instance @n
+        *                              else @c false
+        *      @param[in]      rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
+        */
+       bool operator >=(const RandomIteratorT< T >& rhs) const
+       {
+               return __index >= rhs.__index;
+       }
+
+       /**
+        * Gets the %RandomIteratorT instance which moves forward as much as specified @c diff parameter.
         *
         * @since               2.1
         *
         * @return              A %RandomIteratorT instance
-        * @param[in]   diff            The length to move forward
+        * @param[in]   diff                            The length to move forward
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       RandomIteratorT< T > operator+(int diff)
+       RandomIteratorT< T > operator +(difference_type diff)
        {
+               ClearLastResult();
                RandomIteratorT< T > tempIter = *this;
                tempIter.__index += diff;
-
-               // GetAt() will return null if __index is out of range.
-               tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
-               TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
+               TryReturnResult(tempIter.__index >= 0 && tempIter.__index < __pList->GetCount(), tempIter, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
                return tempIter;
        }
 
        /**
-        * Decrease __index as specified by the diff parameter.
+        * Gets the %RandomIteratorT instance which moves backward as much as specified @c diff parameter.
         *
         * @since               2.1
         *
         * @return              A %RandomIteratorT instance
-        * @param[in]   diff            The length to move backward
+        * @param[in]   diff                            The length to move backward
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       RandomIteratorT< T > operator-(int diff)
+       RandomIteratorT< T > operator -(difference_type diff)
        {
+               ClearLastResult();
                RandomIteratorT< T > tempIter = *this;
                tempIter.__index -= diff;
-
-               // GetAt() will return null if __index is out of range.
-               tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
-               TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
+               TryReturnResult(tempIter.__index >= 0 && tempIter.__index < __pList->GetCount(), tempIter, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
                return tempIter;
        }
 
-       int operator-(const RandomIteratorT< T >& rhs)
+       /**
+        * Moves forward as much as specified @c diff parameter.
+        *
+        * @since               3.0
+        *
+        * @return              A %RandomIteratorT instance
+        * @param[in]   diff    The length to move forward
+        * @exception   E_SUCCESS                       The method is successful.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
+        * @remarks             The specific error code can be accessed using GetLastResult() method.
+        */
+       RandomIteratorT< T >& operator +=(difference_type diff)
+       {
+               ClearLastResult();
+               __index += diff;
+               TryReturnResult(__index >= 0 && __index < __pList->GetCount(), *this, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
+               return *this;
+       }
+
+       /**
+        * Moves backward as much as specified @c diff parameter.
+        *
+        * @since               3.0
+        *
+        * @return              A %RandomIteratorT instance
+        * @param[in]   diff    The length to move backward
+        * @exception   E_SUCCESS                       The method is successful.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
+        * @remarks             The specific error code can be accessed using GetLastResult() method.
+        */
+       RandomIteratorT< T >& operator -=(difference_type diff)
+       {
+               ClearLastResult();
+               __index -= diff;
+               TryReturnResult(__index >= 0 && __index < __pList->GetCount(), *this, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
+               return *this;
+       }
+
+       /**
+        * Get the difference between the current instance and the specified %RandomIteratorT instance
+        *
+        * @since               3.0
+        *
+        * @return              The difference between the current instance and the specified %RandomIteratorT instance
+        * @param[in]   rhs     A reference to the %RandomIteratorT instance on the right-hand side of the operator
+        */
+       difference_type operator -(const RandomIteratorT< T >& rhs)
        {
                return __index - rhs.__index;
        }
@@ -312,18 +389,17 @@ public:
         * @since               2.1
         *
         * @return              A reference to the T type instance
-        * @param[in]   index   An index to reach
+        * @param[in]           index                           An index to move from current position
         * @exception   E_SUCCESS                       The method is successful.
-        * @exception   E_OUT_OF_RANGE          The specified index is outside the bounds of the data structure,
-        *                                                                      or the specified index is either equal to or greater than the number of elements in the list or less than 0.
+        * @exception   E_OUT_OF_RANGE          The position of the iterator is outside the bounds of the list.
         * @remarks             The specific error code can be accessed using GetLastResult() method.
         */
-       T& operator[](int index) const
+       reference operator [](difference_type index)
        {
-               // GetAt() will return null if __index is out of range.
-               const T& tempObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(index)));
-               TryReturnResult(tempObj != null, const_cast< T& >(tempObj), GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
-               return const_cast< T& >(tempObj);
+               ClearLastResult();
+               reference r = reinterpret_cast< T& >(__pList->GetAtRef(__index));
+               TryReturnResult(__index + index >= 0 && __index + index < __pList->GetCount(), r, E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
+               return reinterpret_cast< T& >(__pList->GetAtRef(__index + index));
        }
 
        /**
@@ -337,13 +413,11 @@ public:
        {
                std::swap(__pList, rhs.__pList);
                std::swap(__index, rhs.__index);
-               std::swap(__currentObj, rhs.__currentObj);
        }
 
 private:
-       const IList* __pList;
-       int __index;
-       T __currentObj;
+       IList* __pList;
+       difference_type __index;
 }; // RandomIteratorT
 
 }}} // Tizen::Base::Collection