- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Containers/Iterators.h

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #if !defined(AFX_ITERATORS_H__547687F6_D0CB_49D3_9813_E8811A94013D__INCLUDED_)
    00015 #define AFX_ITERATORS_H__547687F6_D0CB_49D3_9813_E8811A94013D__INCLUDED_
    00016
    00017 #if _MSC_VER > 1000
    00018 #pragma once
    00019 #endif // _MSC_VER > 1000
    00020
    00021 #include <cstdlib>
    00022
    00023 namespace Mers {
    00024         namespace Utils {
    00025                 namespace Containers {
    00026
    00028                         // ConstantIterator Class
    00030
    00034                         class IConstantIterator
    00035                         {
    00036                         public:
    00044                                 bool operator ==(const IConstantIterator &other) const;
    00052                                 bool operator !=(const IConstantIterator &other) const;
    00053
    00054                         protected:
    00063                                 IConstantIterator(void const * myList = NULL,
    00064                                         unsigned int myIndex = 0);
    00065
    00073                                 IConstantIterator(IConstantIterator const &other);
    00074
    00084                                 IConstantIterator & operator =(const IConstantIterator &other);
    00085
    00093                                 inline IConstantIterator & operator ++() {
    00094                                         ++m_Index;
    00095                                         return *this;
    00096                                 }
    00097
    00105                                 inline IConstantIterator operator ++(int) {
    00106                                         IConstantIterator temporaryIterator(*this);
    00107                                         operator++();
    00108                                         return (temporaryIterator);
    00109                                 }
    00110
    00118                                 inline IConstantIterator & operator --() {
    00119                                         if(m_Index > 0)
    00120                                                 m_Index--;
    00121                                         return *this;
    00122                                 }
    00123
    00131                                 inline IConstantIterator operator --(int) {
    00132                                         IConstantIterator temporaryIterator(*this);
    00133                                         operator--();
    00134                                         return (temporaryIterator);
    00135                                 }
    00136
    00142                                 IConstantIterator & operator +=(int ival);
    00143
    00149                                 IConstantIterator & operator -=(int ival);
    00150
    00151                         protected:
    00154                                 void const * m_List;
    00156                                 unsigned int m_Index;
    00157
    00158                         };
    00159
    00161                         // Iterator Class
    00163
    00167                         class IIterator : public IConstantIterator
    00168                         {
    00169                         protected:
    00178                                 IIterator(void * myList = NULL, unsigned int myIndex = 0);
    00186                                 IIterator(IIterator const &other);
    00195                                 IIterator & operator =(const IIterator &other);
    00196
    00197                         protected:
    00199                                 void * m_List;
    00200
    00201                         };
    00202
    00208                         template<typename t_Iterand>
    00209                         class IteratorTraits {
    00210                         public:
    00211                                 typedef typename t_Iterand::difference_type difference_type;
    00212                                 typedef t_Iterand value_type;
    00213                                 typedef t_Iterand * pointer;
    00214                                 typedef t_Iterand & reference;
    00215                                 typedef const t_Iterand & const_reference;
    00216                         };
    00217
    00223                         template<typename t_Iterand>
    00224                         class IteratorTraits<t_Iterand *> {
    00225                         public:
    00226                                 typedef int difference_type;
    00227                                 typedef t_Iterand value_type;
    00228                                 typedef t_Iterand * pointer;
    00229                                 typedef t_Iterand & reference;
    00230                                 typedef const t_Iterand & const_reference;
    00231                         };
    00232
    00238                         template<typename t_Iterand>
    00239                         class IteratorTraits<const t_Iterand *> {
    00240                         public:
    00241                                 typedef int difference_type;
    00242                                 typedef t_Iterand value_type;
    00243                                 typedef const t_Iterand * pointer;
    00244                                 typedef const t_Iterand & reference;
    00245                                 typedef reference const_reference;
    00246                         };
    00247
    00253                         template<typename t_Iterand>
    00254                         class IteratorTraits<const t_Iterand> {
    00255                         public:
    00256                                 typedef typename t_Iterand::difference_type difference_type;
    00257                                 typedef t_Iterand value_type;
    00258                                 typedef const t_Iterand * pointer;
    00259                                 typedef const t_Iterand & reference;
    00260                                 typedef reference const_reference;
    00261                         };
    00262
    00267                         template< typename t_Iterand, typename t_Container >
    00268                         class IteratorTemplate :
    00269                                 protected IConstantIterator
    00270                         {
    00271                         private:
    00272                                 template< typename tf_Iterand, typename tf_Container > friend
    00273                                         class IteratorTemplate;
    00274
    00275                         public:
    00278                                 typedef typename IteratorTraits<t_Iterand>::difference_type
    00279                                         difference_type;
    00281                                 typedef typename IteratorTraits<t_Iterand>::value_type value_type;
    00283                                 typedef typename IteratorTraits<t_Iterand>::pointer pointer;
    00285                                 typedef typename IteratorTraits<t_Iterand>::reference reference;
    00287                                 typedef typename IteratorTraits<t_Iterand>::const_reference const_reference;
    00289                                 typedef IteratorTemplate<t_Iterand, t_Container> iterator;
    00290
    00294                                 inline IteratorTemplate() : IConstantIterator(NULL, 0) {
    00295                                 }
    00296
    00305                                 inline IteratorTemplate(t_Container * container,
    00306                                         unsigned int myIndex = 0)
    00307                                         : IConstantIterator(container, myIndex) {
    00308                                 }
    00309
    00316                                 template<typename to_Iterand> inline
    00317                                 IteratorTemplate(const IteratorTemplate<to_Iterand, t_Container> &other)
    00318                                         : IConstantIterator(other) {
    00319                                         typename IteratorTraits<to_Iterand>::pointer op = NULL;
    00320                                         pointer p = op;
    00321                                         (void) p;
    00322                                 }
    00323
    00327                                 inline reference operator*() const {
    00328                                         return *((*((t_Container *)m_List))[m_Index]);
    00329                                 }
    00330
    00334                                 inline pointer operator->() const {
    00335                                         return (*((t_Container *)m_List))[m_Index];
    00336                                 }
    00337
    00343                                 inline iterator & operator++() {
    00344                                         IConstantIterator::operator++();
    00345                                         return *this;
    00346                                 }
    00347
    00357                                 inline iterator operator++(int) {
    00358                                         iterator tmpIterator(*this);
    00359                                         IConstantIterator::operator++();
    00360                                         return tmpIterator;
    00361                                 }
    00362
    00368                                 inline iterator & operator--() {
    00369                                         IConstantIterator::operator--();
    00370                                         return *this;
    00371                                 }
    00372
    00382                                 inline iterator operator--(int) {
    00383                                         iterator tmpIterator(*this);
    00384                                         IConstantIterator::operator--();
    00385                                         return tmpIterator;
    00386                                 }
    00387
    00394                                 inline reference operator[](const difference_type& offset) const {
    00395                                         return *((*((t_Container *)m_List))
    00396                                                 [(difference_type)m_Index + offset]);
    00397                                 }
    00398
    00404                                 inline iterator & operator+=(const difference_type& offset) {
    00405                                         IConstantIterator::operator+=(offset);
    00406                                         return *this;
    00407                                 }
    00408
    00415                                 inline iterator operator+(const difference_type& offset) const {
    00416                                         iterator tmpIterator(*this);
    00417                                         tmpIterator += offset;
    00418                                         return tmpIterator;
    00419                                 }
    00420
    00426                                 inline iterator & operator-=(const difference_type& offset) {
    00427                                         IConstantIterator::operator-=(offset);
    00428                                         return *this;
    00429                                 }
    00430
    00437                                 inline iterator operator-(const difference_type& offset) const {
    00438                                         iterator tmpIterator(*this);
    00439                                         tmpIterator -= offset;
    00440                                         return tmpIterator;
    00441                                 }
    00442
    00446                                 inline const_reference base() const {
    00447                                         return *((*((t_Container *)m_List))[m_Index]);
    00448                                 }
    00449
    00457                                 template<typename to_Iterand>
    00458                                 inline bool
    00459                                 operator ==(const IteratorTemplate<to_Iterand, t_Container> &other)
    00460                                         const {
    00461                                         return IConstantIterator::operator==(other);
    00462                                 }
    00470                                 template<typename to_Iterand>
    00471                                 inline bool
    00472                                 operator !=(const IteratorTemplate<to_Iterand, t_Container> &other)
    00473                                         const {
    00474                                         return IConstantIterator::operator!=(other);
    00475                                 }
    00476
    00477                         };
    00478
    00479                 } // Namespace Containers
    00480         } // Namespace Utils
    00481 } // Namespace Mers
    00482
    00483 #endif // !defined(AFX_ITERATORS_H__547687F6_D0CB_49D3_9813_E8811A94013D__INCLUDED_)