Chris@16
|
1 // (C) Copyright Eric Niebler 2004-2005
|
Chris@16
|
2 // (C) Copyright Gennadiy Rozental 2005-2008.
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6
|
Chris@16
|
7 // See http://www.boost.org/libs/test for the library home page.
|
Chris@16
|
8 //
|
Chris@16
|
9 // File : $RCSfile$
|
Chris@16
|
10 //
|
Chris@101
|
11 // Version : $Revision$
|
Chris@16
|
12 //
|
Chris@16
|
13 // Description : this is an abridged version of an excelent BOOST_FOREACH facility
|
Chris@16
|
14 // presented by Eric Niebler. I am so fond of it so I can't wait till it
|
Chris@16
|
15 // going to be accepted into Boost. Also I need version with less number of dependencies
|
Chris@16
|
16 // and more portable. This version doesn't support rvalues and will reeveluate it's
|
Chris@16
|
17 // parameters, but should be good enough for my purposes.
|
Chris@16
|
18 // ***************************************************************************
|
Chris@16
|
19
|
Chris@16
|
20 #ifndef BOOST_TEST_FOREACH_HPP_021005GER
|
Chris@16
|
21 #define BOOST_TEST_FOREACH_HPP_021005GER
|
Chris@16
|
22
|
Chris@16
|
23 // Boost.Test
|
Chris@16
|
24 #include <boost/test/detail/config.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 // Boost
|
Chris@16
|
27 #include <boost/type.hpp>
|
Chris@16
|
28 #include <boost/mpl/bool.hpp>
|
Chris@16
|
29 #include <boost/test/detail/workaround.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 #include <boost/type_traits/is_const.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/test/detail/suppress_warnings.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 //____________________________________________________________________________//
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38
|
Chris@16
|
39 namespace unit_test {
|
Chris@16
|
40
|
Chris@16
|
41 namespace for_each {
|
Chris@16
|
42
|
Chris@16
|
43 // ************************************************************************** //
|
Chris@16
|
44 // ************** static_any ************** //
|
Chris@16
|
45 // ************************************************************************** //
|
Chris@16
|
46
|
Chris@16
|
47 struct static_any_base
|
Chris@16
|
48 {
|
Chris@16
|
49 operator bool() const { return false; }
|
Chris@16
|
50 };
|
Chris@16
|
51
|
Chris@16
|
52 //____________________________________________________________________________//
|
Chris@16
|
53
|
Chris@16
|
54 template<typename Iter>
|
Chris@16
|
55 struct static_any : static_any_base
|
Chris@16
|
56 {
|
Chris@16
|
57 static_any( Iter const& t ) : m_it( t ) {}
|
Chris@16
|
58
|
Chris@16
|
59 mutable Iter m_it;
|
Chris@16
|
60 };
|
Chris@16
|
61
|
Chris@16
|
62 //____________________________________________________________________________//
|
Chris@16
|
63
|
Chris@16
|
64 typedef static_any_base const& static_any_t;
|
Chris@16
|
65
|
Chris@16
|
66 //____________________________________________________________________________//
|
Chris@16
|
67
|
Chris@16
|
68 template<typename Iter>
|
Chris@16
|
69 inline Iter&
|
Chris@16
|
70 static_any_cast( static_any_t a, Iter* = 0 )
|
Chris@16
|
71 {
|
Chris@16
|
72 return static_cast<Iter&>( static_cast<static_any<Iter> const&>( a ).m_it );
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 //____________________________________________________________________________//
|
Chris@16
|
76
|
Chris@16
|
77 // ************************************************************************** //
|
Chris@16
|
78 // ************** is_const ************** //
|
Chris@16
|
79 // ************************************************************************** //
|
Chris@16
|
80
|
Chris@16
|
81 template<typename C>
|
Chris@16
|
82 inline is_const<C>
|
Chris@16
|
83 is_const_coll( C& )
|
Chris@16
|
84 {
|
Chris@16
|
85 return is_const<C>();
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 //____________________________________________________________________________//
|
Chris@16
|
89
|
Chris@16
|
90 // ************************************************************************** //
|
Chris@16
|
91 // ************** begin ************** //
|
Chris@16
|
92 // ************************************************************************** //
|
Chris@16
|
93
|
Chris@16
|
94 template<typename C>
|
Chris@16
|
95 inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
|
Chris@16
|
96 begin( C& t, mpl::false_ )
|
Chris@16
|
97 {
|
Chris@16
|
98 return static_any<BOOST_DEDUCED_TYPENAME C::iterator>( t.begin() );
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 //____________________________________________________________________________//
|
Chris@16
|
102
|
Chris@16
|
103 template<typename C>
|
Chris@16
|
104 inline static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>
|
Chris@16
|
105 begin( C const& t, mpl::true_ )
|
Chris@16
|
106 {
|
Chris@16
|
107 return static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>( t.begin() );
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 //____________________________________________________________________________//
|
Chris@16
|
111
|
Chris@16
|
112 // ************************************************************************** //
|
Chris@16
|
113 // ************** end ************** //
|
Chris@16
|
114 // ************************************************************************** //
|
Chris@16
|
115
|
Chris@16
|
116 template<typename C>
|
Chris@16
|
117 inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
|
Chris@16
|
118 end( C& t, mpl::false_ )
|
Chris@16
|
119 {
|
Chris@16
|
120 return static_any<BOOST_DEDUCED_TYPENAME C::iterator>( t.end() );
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@16
|
123 //____________________________________________________________________________//
|
Chris@16
|
124
|
Chris@16
|
125 template<typename C>
|
Chris@16
|
126 inline static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>
|
Chris@16
|
127 end( C const& t, mpl::true_ )
|
Chris@16
|
128 {
|
Chris@16
|
129 return static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>( t.end() );
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 //____________________________________________________________________________//
|
Chris@16
|
133
|
Chris@16
|
134 // ************************************************************************** //
|
Chris@16
|
135 // ************** done ************** //
|
Chris@16
|
136 // ************************************************************************** //
|
Chris@16
|
137
|
Chris@16
|
138 template<typename C>
|
Chris@16
|
139 inline bool
|
Chris@16
|
140 done( static_any_t cur, static_any_t end, C&, mpl::false_ )
|
Chris@16
|
141 {
|
Chris@16
|
142 return static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur ) ==
|
Chris@16
|
143 static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( end );
|
Chris@16
|
144 }
|
Chris@16
|
145
|
Chris@16
|
146 //____________________________________________________________________________//
|
Chris@16
|
147
|
Chris@16
|
148 template<typename C>
|
Chris@16
|
149 inline bool
|
Chris@16
|
150 done( static_any_t cur, static_any_t end, C const&, mpl::true_ )
|
Chris@16
|
151 {
|
Chris@16
|
152 return static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur ) ==
|
Chris@16
|
153 static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( end );
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 //____________________________________________________________________________//
|
Chris@16
|
157
|
Chris@16
|
158 // ************************************************************************** //
|
Chris@16
|
159 // ************** next ************** //
|
Chris@16
|
160 // ************************************************************************** //
|
Chris@16
|
161
|
Chris@16
|
162 template<typename C>
|
Chris@16
|
163 inline void
|
Chris@16
|
164 next( static_any_t cur, C&, mpl::false_ )
|
Chris@16
|
165 {
|
Chris@16
|
166 ++static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 //____________________________________________________________________________//
|
Chris@16
|
170
|
Chris@16
|
171 template<typename C>
|
Chris@16
|
172 inline void
|
Chris@16
|
173 next( static_any_t cur, C const&, mpl::true_ )
|
Chris@16
|
174 {
|
Chris@16
|
175 ++static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 //____________________________________________________________________________//
|
Chris@16
|
179
|
Chris@16
|
180 // ************************************************************************** //
|
Chris@16
|
181 // ************** deref ************** //
|
Chris@16
|
182 // ************************************************************************** //
|
Chris@16
|
183
|
Chris@16
|
184 template<class RefType,typename C>
|
Chris@16
|
185 inline RefType
|
Chris@16
|
186 deref( static_any_t cur, C&, ::boost::type<RefType>, mpl::false_ )
|
Chris@16
|
187 {
|
Chris@16
|
188 return *static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
|
Chris@16
|
189 }
|
Chris@16
|
190
|
Chris@16
|
191 //____________________________________________________________________________//
|
Chris@16
|
192
|
Chris@16
|
193 template<class RefType,typename C>
|
Chris@16
|
194 inline RefType
|
Chris@16
|
195 deref( static_any_t cur, C const&, ::boost::type<RefType>, mpl::true_ )
|
Chris@16
|
196 {
|
Chris@16
|
197 return *static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 //____________________________________________________________________________//
|
Chris@16
|
201
|
Chris@16
|
202 // ************************************************************************** //
|
Chris@16
|
203 // ************** BOOST_TEST_FOREACH ************** //
|
Chris@16
|
204 // ************************************************************************** //
|
Chris@16
|
205
|
Chris@16
|
206 #define BOOST_TEST_FE_ANY ::boost::unit_test::for_each::static_any_t
|
Chris@16
|
207 #define BOOST_TEST_FE_IS_CONST( COL ) ::boost::unit_test::for_each::is_const_coll( COL )
|
Chris@16
|
208
|
Chris@16
|
209 #define BOOST_TEST_FE_BEG( COL ) \
|
Chris@16
|
210 ::boost::unit_test::for_each::begin( \
|
Chris@16
|
211 COL, \
|
Chris@16
|
212 BOOST_TEST_FE_IS_CONST( COL ) ) \
|
Chris@16
|
213 /**/
|
Chris@16
|
214
|
Chris@16
|
215 #define BOOST_TEST_FE_END( COL ) \
|
Chris@16
|
216 ::boost::unit_test::for_each::end( \
|
Chris@16
|
217 COL, \
|
Chris@16
|
218 BOOST_TEST_FE_IS_CONST( COL ) ) \
|
Chris@16
|
219 /**/
|
Chris@16
|
220
|
Chris@16
|
221 #define BOOST_TEST_FE_DONE( COL ) \
|
Chris@16
|
222 ::boost::unit_test::for_each::done( \
|
Chris@16
|
223 BOOST_TEST_FE_CUR_VAR, \
|
Chris@16
|
224 BOOST_TEST_FE_END_VAR, \
|
Chris@16
|
225 COL, \
|
Chris@16
|
226 BOOST_TEST_FE_IS_CONST( COL ) ) \
|
Chris@16
|
227 /**/
|
Chris@16
|
228
|
Chris@16
|
229 #define BOOST_TEST_FE_NEXT( COL ) \
|
Chris@16
|
230 ::boost::unit_test::for_each::next( \
|
Chris@16
|
231 BOOST_TEST_FE_CUR_VAR, \
|
Chris@16
|
232 COL, \
|
Chris@16
|
233 BOOST_TEST_FE_IS_CONST( COL ) ) \
|
Chris@16
|
234 /**/
|
Chris@16
|
235
|
Chris@16
|
236 #define BOOST_FOREACH_NOOP(COL) \
|
Chris@16
|
237 ((void)&(COL))
|
Chris@16
|
238
|
Chris@16
|
239 #define BOOST_TEST_FE_DEREF( COL, RefType ) \
|
Chris@16
|
240 ::boost::unit_test::for_each::deref( \
|
Chris@16
|
241 BOOST_TEST_FE_CUR_VAR, \
|
Chris@16
|
242 COL, \
|
Chris@16
|
243 ::boost::type<RefType >(), \
|
Chris@16
|
244 BOOST_TEST_FE_IS_CONST( COL ) ) \
|
Chris@16
|
245 /**/
|
Chris@16
|
246
|
Chris@16
|
247 #if BOOST_WORKAROUND( BOOST_MSVC, == 1310 )
|
Chris@16
|
248 #define BOOST_TEST_LINE_NUM
|
Chris@16
|
249 #else
|
Chris@16
|
250 #define BOOST_TEST_LINE_NUM __LINE__
|
Chris@16
|
251 #endif
|
Chris@16
|
252
|
Chris@16
|
253 #define BOOST_TEST_FE_CUR_VAR BOOST_JOIN( _fe_cur_, BOOST_TEST_LINE_NUM )
|
Chris@16
|
254 #define BOOST_TEST_FE_END_VAR BOOST_JOIN( _fe_end_, BOOST_TEST_LINE_NUM )
|
Chris@16
|
255 #define BOOST_TEST_FE_CON_VAR BOOST_JOIN( _fe_con_, BOOST_TEST_LINE_NUM )
|
Chris@16
|
256
|
Chris@16
|
257 #define BOOST_TEST_FOREACH( RefType, var, COL ) \
|
Chris@16
|
258 if( BOOST_TEST_FE_ANY BOOST_TEST_FE_CUR_VAR = BOOST_TEST_FE_BEG( COL ) ) {} else \
|
Chris@16
|
259 if( BOOST_TEST_FE_ANY BOOST_TEST_FE_END_VAR = BOOST_TEST_FE_END( COL ) ) {} else \
|
Chris@16
|
260 for( bool BOOST_TEST_FE_CON_VAR = true; \
|
Chris@16
|
261 BOOST_TEST_FE_CON_VAR && !BOOST_TEST_FE_DONE( COL ); \
|
Chris@16
|
262 BOOST_TEST_FE_CON_VAR ? BOOST_TEST_FE_NEXT( COL ) : BOOST_FOREACH_NOOP( COL )) \
|
Chris@16
|
263 \
|
Chris@16
|
264 if( (BOOST_TEST_FE_CON_VAR = false, false) ) {} else \
|
Chris@16
|
265 for( RefType var = BOOST_TEST_FE_DEREF( COL, RefType ); \
|
Chris@16
|
266 !BOOST_TEST_FE_CON_VAR; BOOST_TEST_FE_CON_VAR = true ) \
|
Chris@16
|
267 /**/
|
Chris@16
|
268
|
Chris@16
|
269 //____________________________________________________________________________//
|
Chris@16
|
270
|
Chris@16
|
271 } // namespace for_each
|
Chris@16
|
272
|
Chris@16
|
273 } // namespace unit_test
|
Chris@16
|
274
|
Chris@16
|
275 } // namespace boost
|
Chris@16
|
276
|
Chris@16
|
277 //____________________________________________________________________________//
|
Chris@16
|
278
|
Chris@16
|
279 #include <boost/test/detail/enable_warnings.hpp>
|
Chris@16
|
280
|
Chris@16
|
281 #endif // BOOST_TEST_FOREACH_HPP_021005GER
|