1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_Iterator.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 13145 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_UT_ITERATOR_H_
17 #define NW_UT_ITERATOR_H_
18 
19 #include <nw/types.h>
20 #include <nw/assert.h>
21 #include <nw/ut/ut_Inlines.h>
22 
23 #include <iterator>
24 
25 namespace nw {
26 namespace ut   {
27 
28 /* イテレータ ***************************************************************/
29 
30 namespace internal {
31 
32 template <typename TIt>
33 struct iterator_traits
34 {
35     typedef typename TIt::value_type        value_type;
36     typedef typename TIt::pointer           pointer;
37     typedef typename TIt::reference         reference;
38     typedef typename TIt::difference_type   difference_type;
39     typedef typename TIt::iterator_category iterator_category;
40 };
41 
42 template <typename T>
43 struct iterator_traits<T*>
44 {
45     typedef T           value_type;
46     typedef T*          pointer;
47     typedef T&          reference;
48     typedef PtrDiff     difference_type;
49     typedef std::random_access_iterator_tag iterator_category;
50 };
51 
52 template <typename T>
53 struct iterator_traits<const T*>
54 {
55     typedef T           value_type;
56     typedef const T*    pointer;
57     typedef const T&    reference;
58     typedef PtrDiff     difference_type;
59     typedef std::random_access_iterator_tag iterator_category;
60 };
61 
62 
63 template <
64     typename Category,
65     typename T,
66     typename Difference = PtrDiff,
67     typename Pointer    = T*,
68     typename Reference  = T& >
69 struct iterator
70 {
71     typedef T           value_type;
72     typedef Difference  difference_type;
73     typedef Pointer     pointer;
74     typedef Reference   reference;
75     typedef Category    iterator_category;
76 };
77 
78 
79 template <typename TIt>
80 class reverse_iterator : public iterator<
81                                 typename iterator_traits<TIt>::iterator_category,
82                                 typename iterator_traits<TIt>::value_type,
83                                 typename iterator_traits<TIt>::difference_type,
84                                 typename iterator_traits<TIt>::pointer,
85                                 typename iterator_traits<TIt>::reference >
86 {
87 private:
88 
89     typedef iterator<
90         typename iterator_traits<TIt>::iterator_category,
91         typename iterator_traits<TIt>::value_type,
92         typename iterator_traits<TIt>::difference_type,
93         typename iterator_traits<TIt>::pointer,
94         typename iterator_traits<TIt>::reference >    BaseIt;
95 
96 public:
97 
98   #if defined( NN_PLATFORM_CTR )
99 
100     typedef typename BaseIt::value_type      value_type;
101     typedef typename BaseIt::difference_type difference_type;
102     typedef typename BaseIt::pointer         pointer;
103     typedef typename BaseIt::reference       reference;
104     typedef typename BaseIt::iterator_category iterator_category;
105 
106   #endif
107 
108     typedef reverse_iterator<TIt>      Self;
109 
110     /**************************************************************************/
111 
112     explicit reverse_iterator() {}
113     explicit reverse_iterator(TIt it) : m_Current(it) {}
114     template<typename T>
115     explicit reverse_iterator(const reverse_iterator<T>& rhs ) : m_Current( rhs.GetBase() ) {}
116 
117     TIt       GetBase()    const { return m_Current; }
118     reference operator*()  const { TIt tmp = m_Current; return (*--tmp); }
119     pointer   operator->() const { return &(this->operator*()); }
120 
121     Self& operator++()    { (void)--m_Current; return *this; }
122     Self  operator++(int) { Self tmp = *this; (void)--m_Current; return tmp; }
123     Self& operator--()    { (void)++m_Current; return *this;}
124     Self  operator--(int) { Self tmp = *this; (void)++m_Current; return tmp; }
125 
126     friend bool operator==(const Self &r1_,const Self &r2_) { return r1_.m_Current==r2_.m_Current; }
127     friend bool operator!=(const Self &r1,const Self &r2) { return !(r1==r2); }
128 
129 private:  /****************************************************************/
130 
131     TIt m_Current;
132 };
133 
134 } // namespace internal
135 
136 }  // namespace ut
137 }  // namespace nw
138 
139 #endif  /* NW_UT_ITERATOR_H_ */
140