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