xref: /AOO41X/main/autodoc/source/ary/inc/store/s_iterator.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef ARY_STORE_S_ITERATOR_HXX
29 #define ARY_STORE_S_ITERATOR_HXX
30 
31 // USED SERVICES
32 #include <ary/getncast.hxx>
33 #include "s_base.hxx"
34 
35 
36 
37 
38 namespace ary
39 {
40 namespace stg
41 {
42 
43 
44 template <class>        class const_iterator;
45 template <class, class> class const_filter_iterator;
46 
47 
48 /** A non-const iterator that runs on a ->Storage<>.
49 
50     @collab Storage<>
51 */
52 template <class ENTITY>
53 class iterator : public std::iterator<std::forward_iterator_tag, ENTITY>
54 {
55   public:
56     typedef iterator<ENTITY>                            self;
57     typedef typename Base<ENTITY>::impl_type            impl_container;
58     typedef typename impl_container::const_iterator     impl_type;
59 
60     // OPERATORS
61                         iterator()
62                             :   itImpl()        {}
63     explicit            iterator(
64                             impl_type           i_impl)
65                             :   itImpl(i_impl)  {}
66                         ~iterator()             {}
67 
68     bool                operator==(
69                             self                i_other ) const
70                                                 { return itImpl == i_other.itImpl; }
71     bool                operator!=(
72                             self                i_other ) const
73                                                 { return itImpl != i_other.itImpl; }
74     ENTITY &            operator*() const       { csv_assert(*itImpl != 0);
75                                                   return *(*itImpl); }
76     self &              operator++()            { ++itImpl; return *this; }
77     self                operator++(int)         { return self(itImpl++); }
78 
79   private:
80     friend class const_iterator<ENTITY>;        // For const_iterator(iterator);
81     impl_type           ImplIterator() const    { return itImpl; }
82 
83     // DATA
84     impl_type           itImpl;
85 };
86 
87 
88 /** A const iterator that runs on a ->Storage<>.
89 
90     @collab Storage<>
91 */
92 template <class ENTITY>
93 class const_iterator :
94     public std::iterator<std::forward_iterator_tag, const ENTITY>
95 {
96   public:
97     typedef const_iterator<ENTITY>                      self;
98     typedef typename Base<ENTITY>::impl_type            impl_container;
99     typedef typename impl_container::const_iterator     impl_type;
100 
101     // OPERATORS
102                         const_iterator()
103                             :   itImpl()        {}
104     explicit            const_iterator(
105                             impl_type           i_impl)
106                             :   itImpl(i_impl)  {}
107                         const_iterator(         // implicit conversions allowed
108                             ::ary::stg::iterator<ENTITY>    i_it )
109                             :   itImpl(i_it.ImplIterator()) {}
110                         ~const_iterator()       {}
111 
112     bool                operator==(
113                             self                i_other ) const
114                                                 { return itImpl == i_other.itImpl; }
115     bool                operator!=(
116                             self                i_other ) const
117                                                 { return itImpl != i_other.itImpl; }
118     const ENTITY &      operator*() const       { csv_assert(*itImpl != 0);
119                                                   return *(*itImpl); }
120     self &              operator++()            { ++itImpl; return *this; }
121     self                operator++(int)         { return self(itImpl++); }
122 
123   private:
124     // DATA
125     impl_type           itImpl;
126 };
127 
128 
129 
130 
131 
132 /** A non const iterator that runs on a ->Storage<> and returns only
133     the elements of a specific type.
134 
135     @tpl ENTITY
136     The element type of the ->Storage<>
137 
138     @tpl FILTER
139     The actual type of the returned items. FILTER needs to be derived from
140     ENTITY.
141 
142     @collab Storage<>
143 */
144 template <class ENTITY, class FILTER>
145 class filter_iterator :
146     public std::iterator<std::forward_iterator_tag, FILTER>
147 {
148   public:
149     typedef filter_iterator<ENTITY,FILTER>              self;
150     typedef ::ary::stg::iterator<ENTITY>                impl_type;
151 
152     // OPERATORS
153                         filter_iterator()
154                             :   itCur()         {}
155     explicit            filter_iterator(
156                             impl_type           i_cur )
157                             :   itCur(i_cur)    {}
158                         ~filter_iterator()      {}
159 
160     bool                operator==(
161                             self                i_other ) const
162                                                 { return itCur == i_other.itCur; }
163     bool                operator!=(
164                             self                i_other ) const
165                                                 { return itCur != i_other.itCur; }
166     FILTER &            operator*() const       { csv_assert(IsValid());
167                                                   return static_cast< FILTER& >(*itCur); }
168     self &              operator++()            { ++itCur;
169                                                   return *this; }
170     self                operator++(int)         { return self(itCur++); }
171     bool                IsValid() const         { return ary::is_type<FILTER>(*itCur); }
172 
173   private:
174     friend class const_filter_iterator<ENTITY,FILTER>;  // For const_filter_iterator(filter_iterator);
175     impl_type           ImplCur() const         { return itCur; }
176 
177     // DATA
178     impl_type           itCur;
179 };
180 
181 
182 /** A const iterator that runs on a ->Storage<> and returns only
183     the elements of a specific type.
184 
185     @tpl ENTITY
186     The element type of the ->Storage<>
187 
188     @tpl FILTER
189     The actual type of the returned items. FILTER needs to be derived from
190     ENTITY.
191 
192     @collab Storage<>
193 */
194 template <class ENTITY, class FILTER>
195 class const_filter_iterator :
196     public std::iterator<std::forward_iterator_tag, const FILTER>
197 {
198   public:
199     typedef const_filter_iterator<ENTITY,FILTER>        self;
200     typedef ::ary::stg::const_iterator<ENTITY>          impl_type;
201 
202     // OPERATORS
203                         const_filter_iterator()
204                             :   itCur()         {}
205     explicit            const_filter_iterator(
206                             impl_type           i_cur )
207                             :   itCur(i_cur)    {}
208     explicit            const_filter_iterator(  // implicit conversions allowed
209                             filter_iterator<ENTITY,FILTER>
210                                                 i_it )
211                             :   itCur(i_it.ImplCur())   {}
212                         ~const_filter_iterator()
213                                                 {}
214     bool                operator==(
215                             self                i_other ) const
216                                                 { return itCur == i_other.itCur; }
217     bool                operator!=(
218                             self                i_other ) const
219                                                 { return itCur != i_other.itCur; }
220     const FILTER &      operator*() const       { csv_assert(IsValid());
221                                                   return static_cast< const FILTER& >(*itCur); }
222     self &              operator++()            { ++itCur;
223                                                   return *this; }
224     self                operator++(int)         { return self(itCur++); }
225     bool                IsValid() const         { return ary::is_type<FILTER>(*itCur); }
226 
227   private:
228     // DATA
229     impl_type           itCur;
230 };
231 
232 
233 
234 
235 }   // namespace stg
236 }   // namespace ary
237 #endif
238