source: NonGTP/Boost/boost/test/utils/foreach.hpp @ 857

Revision 857, 13.0 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  (C) Copyright Eric Niebler 2004-2005
2//  (C) Copyright Gennadiy Rozental 2005.
3//  Distributed under the Boost Software License, Version 1.0.
4//  (See accompanying file LICENSE_1_0.txt or copy at
5//  http://www.boost.org/LICENSE_1_0.txt)
6
7//  See http://www.boost.org/libs/test for the library home page.
8//
9//  File        : $RCSfile: foreach.hpp,v $
10//
11//  Version     : $Revision: 1.4 $
12//
13//  Description : this is an abridged version of an excelent BOOST_FOREACH facility
14//  presented by Eric Niebler. I am so fond of it so I couldn't wait till it
15//  going to be accepted into Boost. Also I need version with less number of dependencies
16//  and more portable. This version doesn't support rvalues and will reeveluate it's
17//  parameters, but should be good enough for my purposes.
18// ***************************************************************************
19
20#ifndef BOOST_TEST_FOREACH_HPP_021005GER
21#define BOOST_TEST_FOREACH_HPP_021005GER
22
23// Boost.Test
24#include <boost/test/detail/config.hpp>
25
26// Boost
27#include <boost/type.hpp>
28#include <boost/mpl/bool.hpp>
29#include <boost/test/detail/workaround.hpp>
30
31#if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530) )
32#include <boost/type_traits/is_const.hpp>
33#endif
34
35#include <boost/test/detail/suppress_warnings.hpp>
36
37//____________________________________________________________________________//
38
39namespace boost {
40
41namespace unit_test {
42
43namespace for_each {
44
45// ************************************************************************** //
46// **************                  static_any                  ************** //
47// ************************************************************************** //
48
49struct static_any_base
50{
51    operator bool() const { return false; }
52};
53
54//____________________________________________________________________________//
55
56template<typename Iter>
57struct static_any : static_any_base
58{
59    static_any( Iter const& t ) : m_it( t ) {}
60
61    mutable Iter m_it;
62};
63
64//____________________________________________________________________________//
65
66typedef static_any_base const& static_any_t;
67
68//____________________________________________________________________________//
69
70template<typename Iter>
71inline Iter&
72static_any_cast( static_any_t a, Iter* = 0 )
73{
74    return static_cast<Iter&>( static_cast<static_any<Iter> const&>( a ).m_it );
75}
76
77//____________________________________________________________________________//
78
79// ************************************************************************** //
80// **************                   is_const                   ************** //
81// ************************************************************************** //
82
83#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530) )
84
85template<typename C>
86inline mpl::false_
87is_const_coll( C& )
88{
89    return mpl::false_();
90}
91
92//____________________________________________________________________________//
93
94template<typename C>
95inline mpl::true_
96is_const_coll( C const& )
97{
98    return mpl::true_();
99}
100
101//____________________________________________________________________________//
102
103#else
104
105template<typename C>
106inline is_const<C>
107is_const_coll( C& )
108{
109    return is_const<C>();
110}
111
112//____________________________________________________________________________//
113
114#endif
115
116//____________________________________________________________________________//
117
118// ************************************************************************** //
119// **************                     begin                    ************** //
120// ************************************************************************** //
121
122template<typename C>
123inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
124begin( C& t, mpl::false_ )
125{
126    return static_any<BOOST_DEDUCED_TYPENAME C::iterator>( t.begin() );
127}
128
129//____________________________________________________________________________//
130
131#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530) )
132
133template<typename C>
134inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
135begin( C const& t, mpl::true_ )
136{
137    typedef typename C::iterator it;
138    return static_any<it>( const_cast<it>( t.begin() ) );
139}
140
141#else
142
143template<typename C>
144inline static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>
145begin( C const& t, mpl::true_ )
146{
147    return static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>( t.begin() );
148}
149
150#endif
151
152//____________________________________________________________________________//
153
154// ************************************************************************** //
155// **************                      end                     ************** //
156// ************************************************************************** //
157
158template<typename C>
159inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
160end( C& t, mpl::false_ )
161{
162    return static_any<BOOST_DEDUCED_TYPENAME C::iterator>( t.end() );
163}
164
165//____________________________________________________________________________//
166
167#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530) )
168
169template<typename C>
170inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
171end( C const& t, mpl::true_ )
172{
173    typedef typename C::iterator it;
174    return static_any<it>( const_cast<it>( t.end() ) );
175}
176
177#else
178
179template<typename C>
180inline static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>
181end( C const& t, mpl::true_ )
182{
183    return static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>( t.end() );
184}
185
186#endif
187
188//____________________________________________________________________________//
189
190// ************************************************************************** //
191// **************                      done                    ************** //
192// ************************************************************************** //
193
194template<typename C>
195inline bool
196done( static_any_t cur, static_any_t end, C&, mpl::false_ )
197{
198    return  static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur ) ==
199            static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( end );
200}
201
202//____________________________________________________________________________//
203
204template<typename C>
205inline bool
206done( static_any_t cur, static_any_t end, C const&, mpl::true_ )
207{
208    return  static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur ) ==
209            static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( end );
210}
211
212//____________________________________________________________________________//
213
214// ************************************************************************** //
215// **************                      next                    ************** //
216// ************************************************************************** //
217
218template<typename C>
219inline void
220next( static_any_t cur, C&, mpl::false_ )
221{
222    ++static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
223}
224
225//____________________________________________________________________________//
226
227template<typename C>
228inline void
229next( static_any_t cur, C const&, mpl::true_ )
230{
231    ++static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
232}
233
234//____________________________________________________________________________//
235
236// ************************************************************************** //
237// **************                      deref                   ************** //
238// ************************************************************************** //
239
240template<class RefType,typename C>
241inline RefType
242deref( static_any_t cur, C&, ::boost::type<RefType>, mpl::false_ )
243{
244    return *static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
245}
246
247//____________________________________________________________________________//
248
249template<class RefType,typename C>
250inline RefType
251deref( static_any_t cur, C const&, ::boost::type<RefType>, mpl::true_ )
252{
253    return *static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
254}
255
256//____________________________________________________________________________//
257
258// ************************************************************************** //
259// **************              BOOST_TEST_FOREACH              ************** //
260// ************************************************************************** //
261
262#if BOOST_WORKAROUND(__GNUC__, < 3)
263#define BOOST_TEST_FE_MULTISTATEMENT
264#endif
265
266#define BOOST_TEST_FE_ANY                   ::boost::unit_test::for_each::static_any_t
267#define BOOST_TEST_FE_IS_CONST( COL )       ::boost::unit_test::for_each::is_const_coll( COL )
268
269#define BOOST_TEST_FE_BEG( COL )            \
270    ::boost::unit_test::for_each::begin(    \
271        COL,                                \
272        BOOST_TEST_FE_IS_CONST( COL ) )     \
273/**/
274
275#define BOOST_TEST_FE_END( COL )            \
276    ::boost::unit_test::for_each::end(      \
277        COL,                                \
278        BOOST_TEST_FE_IS_CONST( COL ) )     \
279/**/
280
281#define BOOST_TEST_FE_DONE( COL )           \
282    ::boost::unit_test::for_each::done(     \
283        BOOST_TEST_FE_CUR_VAR,              \
284        BOOST_TEST_FE_END_VAR,              \
285        COL,                                \
286        BOOST_TEST_FE_IS_CONST( COL ) )     \
287/**/
288
289#define BOOST_TEST_FE_NEXT( COL )           \
290    ::boost::unit_test::for_each::next(     \
291        BOOST_TEST_FE_CUR_VAR,              \
292        COL,                                \
293        BOOST_TEST_FE_IS_CONST( COL ) )     \
294/**/
295
296#define BOOST_FOREACH_NOOP(COL)             \
297    ((void)&(COL))
298
299#define BOOST_TEST_FE_DEREF( COL, RefType ) \
300    ::boost::unit_test::for_each::deref(    \
301        BOOST_TEST_FE_CUR_VAR,              \
302        COL,                                \
303        ::boost::type<RefType >(),          \
304        BOOST_TEST_FE_IS_CONST( COL ) )     \
305/**/
306
307#if BOOST_WORKAROUND( BOOST_MSVC, == 1310 )
308#define BOOST_TEST_LINE_NUM
309#else
310#define BOOST_TEST_LINE_NUM     __LINE__
311#endif
312
313#define BOOST_TEST_FE_CUR_VAR   BOOST_JOIN( _fe_cur_, BOOST_TEST_LINE_NUM )
314#define BOOST_TEST_FE_END_VAR   BOOST_JOIN( _fe_end_, BOOST_TEST_LINE_NUM )
315#define BOOST_TEST_FE_CON_VAR   BOOST_JOIN( _fe_con_, BOOST_TEST_LINE_NUM )
316
317#ifndef BOOST_TEST_FE_MULTISTATEMENT
318
319#define BOOST_TEST_FOREACH( RefType, var, COL )                                             \
320if( BOOST_TEST_FE_ANY BOOST_TEST_FE_CUR_VAR = BOOST_TEST_FE_BEG( COL ) ) {} else            \
321if( BOOST_TEST_FE_ANY BOOST_TEST_FE_END_VAR = BOOST_TEST_FE_END( COL ) ) {} else            \
322for( bool BOOST_TEST_FE_CON_VAR = true;                                                     \
323          BOOST_TEST_FE_CON_VAR && !BOOST_TEST_FE_DONE( COL );                              \
324          BOOST_TEST_FE_CON_VAR ? BOOST_TEST_FE_NEXT( COL ) : BOOST_FOREACH_NOOP( COL ))    \
325                                                                                            \
326    if( (BOOST_TEST_FE_CON_VAR = false, false) ) {} else                                    \
327    for( RefType var = BOOST_TEST_FE_DEREF( COL, RefType );                                 \
328         !BOOST_TEST_FE_CON_VAR; BOOST_TEST_FE_CON_VAR = true )                             \
329/**/
330
331#else
332
333#define BOOST_TEST_FOREACH( RefType, var, COL )                                     \
334BOOST_TEST_FE_ANY BOOST_TEST_FE_CUR_VAR = BOOST_TEST_FE_BEG( COL ),                 \
335                  BOOST_TEST_FE_END_VAR = BOOST_TEST_FE_END( COL );                 \
336                                                                                    \
337for( bool BOOST_TEST_FE_CON_VAR = true; BOOST_TEST_FE_CON_VAR; )                    \
338for( ;                                                                              \
339     BOOST_TEST_FE_CON_VAR && (BOOST_TEST_FE_CON_VAR = !BOOST_TEST_FE_DONE( COL )); \
340     BOOST_TEST_FE_CON_VAR ? BOOST_TEST_FE_NEXT( COL ) : BOOST_FOREACH_NOOP( COL )) \
341                                                                                    \
342    if( (BOOST_TEST_FE_CON_VAR = false, false) ) {} else                            \
343    for( RefType var = BOOST_TEST_FE_DEREF( COL, RefType );                         \
344         !BOOST_TEST_FE_CON_VAR; BOOST_TEST_FE_CON_VAR = true )                     \
345/**/
346
347#endif
348
349//____________________________________________________________________________//
350
351} // namespace for_each
352
353} // namespace unit_test
354
355} // namespace boost
356
357//____________________________________________________________________________//
358
359#include <boost/test/detail/enable_warnings.hpp>
360
361// ***************************************************************************
362//  Revision History :
363//
364//  $Log: foreach.hpp,v $
365//  Revision 1.4  2005/03/24 04:02:33  rogeeff
366//  portability fixes
367//
368//  Revision 1.3  2005/03/23 21:02:26  rogeeff
369//  Sunpro CC 5.3 fixes
370//
371//  Revision 1.2  2005/02/21 10:15:45  rogeeff
372//  vc 7.1 workaround
373//
374//  Revision 1.1  2005/02/20 08:27:08  rogeeff
375//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
376//
377// ***************************************************************************
378
379#endif // BOOST_TEST_FOREACH_HPP_021005GER
Note: See TracBrowser for help on using the repository browser.