//
-// Open Service Platform
// Copyright (c) 2013 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
#ifndef _FBASE_COL_ITERATOR_T_H_
#define _FBASE_COL_ITERATOR_T_H_
-#include <algorithm> // std::swap (Before C++11)
+#include <algorithm> // std::swap (Before C++11)
#include <iterator>
#include <unique_ptr.h>
#include <FBaseLog.h>
{
/**
* @class IteratorT
- * @brief This class provides an iterator that is used to convert IList to STL containers.
- * StlConverter provides static methods to get this iterator from IList.
+ * @brief This class provides an iterator that is used to convert %IList to STL containers. @n
+ * %StlConverter provides static methods to get this iterator from %IList.
*
* @since 2.1
*
- * @remarks This 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 %IteratorT class satisfies requirements of the C++ standard library mutable BidirectionalIterator.
+ * It satisfies the requirements of an OutputIterator which can be dereferenced as an lvalue.
+ *
+ * The %IteratorT class provides an iterator that is used to convert IList to STL containers.
+ * StlConverter provides static methods to get this iterator from IList.
*/
-template < typename T >
+template< typename T >
class IteratorT
- : public std::iterator< std::input_iterator_tag, T >
+ : public std::iterator< std::bidirectional_iterator_tag, T >
{
public:
+ typedef std::bidirectional_iterator_tag iterator_category;
+ typedef typename std::iterator_traits< IteratorT< T > >::value_type value_type;
+ typedef typename std::iterator_traits< IteratorT< T > >::difference_type difference_type;
+ typedef typename std::iterator_traits< IteratorT< T > >::pointer pointer;
+ typedef typename std::iterator_traits< IteratorT< T > >::reference reference;
+
+ /**
+ * This is the default constructor for this class.
+ *
+ * @since 3.0
+ */
+ IteratorT(void)
+ : __pList(null)
+ , __isPostEnd(false)
+ , __index(0)
+ , __pEnum(null)
+ {
+ }
+
/**
- * Initializes this instance of %IteratorT class.
+ * Initializes an instance of %IteratorT class.
*
* @since 2.1
*
* @param[in] list A reference to the IList instance to convert
- * @param[in] isPostEnd A boolean value to check the end
+ * @param[in] isPostEnd A boolean value to check the end of a list
*/
- explicit IteratorT(const IList& list, bool isPostEnd = false)
+ explicit IteratorT(IList& list, bool isPostEnd = false)
: __pList(&list)
, __isPostEnd(isPostEnd)
, __index(0)
, __pEnum(__pList->GetBidirectionalEnumeratorN())
- , __currentObj(null)
{
if (__pList->GetCount() != 0)
{
if (!__isPostEnd)
{
__pEnum->MoveNext();
- __currentObj = static_cast< T >(__pEnum->GetCurrent());
}
else
{
}
/**
- * This is copy constructor of %IteratorT class.
+ * This is the copy constructor of the %IteratorT class.
*
* @since 2.1
*
, __isPostEnd(rhs.__isPostEnd)
, __index(rhs.__index)
, __pEnum(__pList->GetBidirectionalEnumeratorN())
- , __currentObj(rhs.__currentObj)
{
if (!__isPostEnd)
{
}
/**
- * This is assignment operator of %IteratorT class.
+ * This is an assignment operator of the %IteratorT class.
*
* @since 2.1
*
* @return A reference to the %IteratorT instance
* @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
*/
- IteratorT< T >& operator=(const IteratorT< T >& rhs)
+ IteratorT< T >& operator =(const IteratorT< T >& rhs)
{
IteratorT< T > tmp(rhs);
tmp.swap(*this);
}
/**
- * This is the indirection operator for %IteratorT class.
+ * This is the indirection operator for the %IteratorT class.
*
* @since 2.1
*
* @return A T type reference
*/
- T& operator*(void) const
+ reference operator *(void) const
{
AppAssertf(!__isPostEnd && __index >= 0, "It is out of range.");
- return const_cast< T& >(__currentObj);
+ return reinterpret_cast< T& >(__pEnum->GetCurrentRef());
}
/**
- * This is the structure dereference operator for %IteratorT class.
+ * This is a structure dereference operator for the %IteratorT class.
*
* @since 2.1
*
- * @return A T type pointer equivalent to the pointer address
+ * @return A T type pointer that is equivalent to the pointer address
*/
- T* operator->(void) const
+ pointer operator ->(void) const
{
- return &(operator*());
+ return &(operator *());
}
/**
- * Moves to the next element of the collection.
+ * Moves to the next element in the collection.
*
* @since 2.1
*
* @return A reference to the %IteratorT type instance
* @exception E_SUCCESS The method is successful.
* @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
- * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation
+ * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
* the collection is modified after the enumerator is created.
* @remarks The specific error code can be accessed using GetLastResult() method.
*/
- IteratorT< T >& operator++(void)
+ IteratorT< T >& operator ++(void)
{
const int PRE_BEGIN_IDX = -1;
TryCatchResult(__index >= PRE_BEGIN_IDX, , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
if (__index != PRE_BEGIN_IDX)
{
result r = __pEnum->MoveNext();
- TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj = null, r, "[%s] It already reached the end.", GetErrorMessage(r));
+ TryCatchResult(r == E_SUCCESS, __isPostEnd = true, r, "[%s] It already reached the end.", GetErrorMessage(r));
}
- __currentObj = static_cast< T >(__pEnum->GetCurrent());
-
- CATCH:
+CATCH:
++__index;
return *this;
}
*
* @since 2.1
*
- * @return An IteratorT instance
+ * @return An %IteratorT instance
* @exception E_SUCCESS The method is successful.
* @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
- * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation or
+ * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
* the collection is modified after the enumerator is created.
- * @remarks It takes O(n) time to get current Enumerate point to return. So use this operator as little as possible.
* @remarks The specific error code can be accessed using GetLastResult() method.
*/
- IteratorT< T > operator++(int)
+ IteratorT< T > operator ++(int)
{
IteratorT< T > tempIter = *this;
- operator++();
+ operator ++();
return tempIter;
}
* @return A reference to the %IteratorT type instance
* @exception E_SUCCESS The method is successful.
* @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
- * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation or
+ * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
* the collection is modified after the enumerator is created.
* @remarks The specific error code can be accessed using GetLastResult() method.
*/
- IteratorT< T >& operator--(void)
+ IteratorT< T >& operator --(void)
{
TryCatchResult(__index <= __pList->GetCount(), , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
if (!__isPostEnd)
{
result r = __pEnum->MovePrevious();
- TryCatchResult(r == E_SUCCESS, __currentObj = null, r, "[%s] It already reached the front.", GetErrorMessage(r));
+ TryCatchResult(r == E_SUCCESS, , r, "[%s] It already reached the front.", GetErrorMessage(r));
}
else
{
__isPostEnd = false;
}
- __currentObj = static_cast< T >(__pEnum->GetCurrent());
-
CATCH:
--__index;
return *this;
*
* @since 2.1
*
- * @return An IteratorT instance
+ * @return An %IteratorT instance
* @exception E_SUCCESS The method is successful.
* @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
- * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation or
+ * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
* the collection is modified after the enumerator is created.
- * @remarks It takes O(n) time to get current Enumerate point to return. So use this operator as little as possible.
* @remarks The specific error code can be accessed using GetLastResult() method.
*/
- IteratorT< T > operator--(int)
+ IteratorT< T > operator --(int)
{
IteratorT< T > tempIter = *this;
- operator--();
+ operator --();
return tempIter;
}
/**
- * Checks the two %IteratorT instances for equality
+ * Checks the two %IteratorT instances for equality.
*
* @since 2.1
*
* else @c false
* @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
*/
- bool operator==(const IteratorT< T >& rhs) const
+ bool operator ==(const IteratorT< T >& rhs) const
{
if (__pList != rhs.__pList)
{
}
else if (__isPostEnd && rhs.__isPostEnd)
{
- // In this case, __currentObj state is invalid
+ // In this case, the current object which the iterator refers to is invalid.
return true;
}
// If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
- return __currentObj == rhs.__currentObj;
+ return __pEnum->GetCurrentRef() == rhs.__pEnum->GetCurrentRef();
}
/**
*
* @since 2.1
*
- * @return @c true if every member of the specified %IteratorT instance is not equal to the calling instance's members, @n
+ * @return @c true if any member of the specified %IteratorT instance is not equal to the calling instance's members, @n
* else @c false
* @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
*/
- bool operator!=(const IteratorT< T >& rhs) const
+ bool operator !=(const IteratorT< T >& rhs) const
{
- return !operator==(rhs);
+ return !operator ==(rhs);
}
/**
- * Exchanges values of the two %IteratorT instances
+ * Swaps values of two %IteratorT instances.
*
* @since 2.1
*
- * @param[in] rhs A reference to the %IteratorT instance to swap
+ * @param[in] rhs A reference to a %IteratorT instance to swap
*/
void swap(IteratorT< T >& rhs)
{
std::swap(__isPostEnd, rhs.__isPostEnd);
std::swap(__index, rhs.__index);
std::swap(__pEnum, rhs.__pEnum);
- std::swap(__currentObj, rhs.__currentObj);
}
private:
- const IList* __pList;
+ IList* __pList;
bool __isPostEnd;
- int __index;
+ difference_type __index;
std::unique_ptr< IBidirectionalEnumerator > __pEnum;
- T __currentObj;
}; // IteratorT
}}} // Tizen::Base::Collection
-#endif //_FBASE_COL_ITERATOR_T_H_
+#endif //_FBASE_COL_ITERATOR_T_H_
\ No newline at end of file