1 // Created on: 2014-04-15
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_StlIterator_HeaderFile
17 #define NCollection_StlIterator_HeaderFile
19 #include <Standard_Assert.hxx>
22 //! Helper class that allows to use NCollection iterators as STL iterators.
23 //! NCollection iterator can be extended to STL iterator of any category by
24 //! adding necessary methods: STL forward iterator requires IsEqual method,
25 //! STL bidirectional iterator requires Previous method, and STL random access
26 //! iterator requires Offset and Differ methods. See NCollection_Vector as
27 //! example of declaring custom STL iterators.
28 template<class Category, class BaseIterator, class ItemType, bool IsConstant>
29 class NCollection_StlIterator :
30 public std::iterator<Category, ItemType, ptrdiff_t,
31 typename opencascade::std::conditional<IsConstant, const ItemType*, ItemType*>::type,
32 typename opencascade::std::conditional<IsConstant, const ItemType&, ItemType&>::type>
36 //! Default constructor
37 NCollection_StlIterator () {}
39 //! Constructor from NCollection iterator
40 NCollection_StlIterator (const BaseIterator& theIterator)
41 : myIterator (theIterator)
44 //! Cast from non-const variant to const one
45 NCollection_StlIterator (const NCollection_StlIterator<Category, BaseIterator, ItemType, false>& theIterator)
46 : myIterator (theIterator.Iterator())
49 //! Assignment of non-const iterator to const one
50 NCollection_StlIterator& operator= (const NCollection_StlIterator<Category, BaseIterator, ItemType, false>& theIterator)
52 myIterator = theIterator.myIterator;
56 //! Access to NCollection iterator instance
57 const BaseIterator& Iterator () const
62 //! Access to NCollection iterator instance
63 BaseIterator& ChangeIterator()
68 protected: //! @name methods related to forward STL iterator
70 // Note: Here we use SFINAE (Substitution failure is not an error) to choose
71 // an appropriate method based on template arguments (at instantiation time).
73 template<bool Condition>
74 typename opencascade::std::enable_if<!Condition, ItemType&>::type Reference() const
76 return myIterator.ChangeValue();
79 template<bool Condition>
80 typename opencascade::std::enable_if<Condition, const ItemType&>::type Reference() const
82 return myIterator.Value();
85 public: //! @name methods related to forward STL iterator
88 bool operator== (const NCollection_StlIterator& theOther) const
90 return myIterator.More() == theOther.myIterator.More() &&
91 (!myIterator.More() || myIterator.IsEqual (theOther.myIterator));
94 //! Test for inequality
95 bool operator!= (const NCollection_StlIterator& theOther) const
97 return !(*this == theOther);
100 //! Get reference to current item
101 typename NCollection_StlIterator::reference operator*() const
103 return Reference<IsConstant>();
106 //! Dereferencing operator
107 typename NCollection_StlIterator::pointer operator->() const
109 return &Reference<IsConstant>();
113 NCollection_StlIterator& operator++()
119 //! Postfix increment
120 NCollection_StlIterator operator++(int)
122 const NCollection_StlIterator theOld (*this);
127 public: //! @name methods related to bidirectional STL iterator
130 NCollection_StlIterator& operator--()
132 Standard_STATIC_ASSERT((opencascade::std::is_same<std::bidirectional_iterator_tag,Category>::value ||
133 opencascade::std::is_same<std::random_access_iterator_tag,Category>::value));
134 myIterator.Previous();
138 //! Postfix decrement
139 NCollection_StlIterator operator--(int)
141 NCollection_StlIterator theOld (*this);
146 public: //! @name methods related to random access STL iterator
149 NCollection_StlIterator& operator+= (typename NCollection_StlIterator::difference_type theOffset)
151 Standard_STATIC_ASSERT((opencascade::std::is_same<std::random_access_iterator_tag,Category>::value));
152 myIterator.Offset (theOffset);
157 NCollection_StlIterator operator+ (typename NCollection_StlIterator::difference_type theOffset) const
159 NCollection_StlIterator aTemp (*this);
160 return aTemp += theOffset;
164 NCollection_StlIterator& operator-= (typename NCollection_StlIterator::difference_type theOffset)
166 return *this += -theOffset;
170 NCollection_StlIterator operator- (typename NCollection_StlIterator::difference_type theOffset) const
172 NCollection_StlIterator aTemp (*this);
173 return aTemp += -theOffset;
177 typename NCollection_StlIterator::difference_type operator- (const NCollection_StlIterator& theOther) const
179 Standard_STATIC_ASSERT((opencascade::std::is_same<std::random_access_iterator_tag,Category>::value));
180 return myIterator.Differ (theOther.myIterator);
183 //! Get item at offset from current
184 typename NCollection_StlIterator::reference operator[] (typename NCollection_StlIterator::difference_type theOffset) const
186 return *(*this + theOffset);
190 bool operator< (const NCollection_StlIterator& theOther) const
192 return (*this - theOther) < 0;
196 bool operator> (const NCollection_StlIterator& theOther) const
198 return theOther < *this;
202 bool operator<= (const NCollection_StlIterator& theOther) const
204 return !(theOther < *this);
208 bool operator>= (const NCollection_StlIterator& theOther) const
210 return !(*this < theOther);
214 //! NCollection iterator
215 BaseIterator myIterator;
218 #endif // NCollection_StlIterator_HeaderFile