blob: 4bc2027fb7c5688a43ada4afd38b4c33eeb1edb1 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// (C) Copyright Eric Niebler 2004-2005
2// (C) Copyright Gennadiy Rozental 2005-2008.
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$
10//
11// Version : $Revision: 54633 $
12//
Jeff Thompson3d613fd2013-10-15 15:39:04 -070013// Description : this is an abridged version of an excelent NDNBOOST_FOREACH facility
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070014// presented by Eric Niebler. I am so fond of it so I can'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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070020#ifndef NDNBOOST_TEST_FOREACH_HPP_021005GER
21#define NDNBOOST_TEST_FOREACH_HPP_021005GER
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070022
23// Boost.Test
24#include <ndnboost/test/detail/config.hpp>
25
26// Boost
27#include <ndnboost/type.hpp>
28#include <ndnboost/mpl/bool.hpp>
29#include <ndnboost/test/detail/workaround.hpp>
30
31#include <ndnboost/type_traits/is_const.hpp>
32
33#include <ndnboost/test/detail/suppress_warnings.hpp>
34
35//____________________________________________________________________________//
36
37namespace ndnboost {
38
39namespace unit_test {
40
41namespace for_each {
42
43// ************************************************************************** //
44// ************** static_any ************** //
45// ************************************************************************** //
46
47struct static_any_base
48{
49 operator bool() const { return false; }
50};
51
52//____________________________________________________________________________//
53
54template<typename Iter>
55struct static_any : static_any_base
56{
57 static_any( Iter const& t ) : m_it( t ) {}
58
59 mutable Iter m_it;
60};
61
62//____________________________________________________________________________//
63
64typedef static_any_base const& static_any_t;
65
66//____________________________________________________________________________//
67
68template<typename Iter>
69inline Iter&
70static_any_cast( static_any_t a, Iter* = 0 )
71{
72 return static_cast<Iter&>( static_cast<static_any<Iter> const&>( a ).m_it );
73}
74
75//____________________________________________________________________________//
76
77// ************************************************************************** //
78// ************** is_const ************** //
79// ************************************************************************** //
80
81template<typename C>
82inline is_const<C>
83is_const_coll( C& )
84{
85 return is_const<C>();
86}
87
88//____________________________________________________________________________//
89
90// ************************************************************************** //
91// ************** begin ************** //
92// ************************************************************************** //
93
94template<typename C>
Jeff Thompson3d613fd2013-10-15 15:39:04 -070095inline static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070096begin( C& t, mpl::false_ )
97{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070098 return static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator>( t.begin() );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070099}
100
101//____________________________________________________________________________//
102
103template<typename C>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700104inline static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700105begin( C const& t, mpl::true_ )
106{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700107 return static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( t.begin() );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700108}
109
110//____________________________________________________________________________//
111
112// ************************************************************************** //
113// ************** end ************** //
114// ************************************************************************** //
115
116template<typename C>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700117inline static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700118end( C& t, mpl::false_ )
119{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700120 return static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator>( t.end() );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700121}
122
123//____________________________________________________________________________//
124
125template<typename C>
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700126inline static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700127end( C const& t, mpl::true_ )
128{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700129 return static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( t.end() );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700130}
131
132//____________________________________________________________________________//
133
134// ************************************************************************** //
135// ************** done ************** //
136// ************************************************************************** //
137
138template<typename C>
139inline bool
140done( static_any_t cur, static_any_t end, C&, mpl::false_ )
141{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700142 return static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( cur ) ==
143 static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( end );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700144}
145
146//____________________________________________________________________________//
147
148template<typename C>
149inline bool
150done( static_any_t cur, static_any_t end, C const&, mpl::true_ )
151{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700152 return static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( cur ) ==
153 static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( end );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700154}
155
156//____________________________________________________________________________//
157
158// ************************************************************************** //
159// ************** next ************** //
160// ************************************************************************** //
161
162template<typename C>
163inline void
164next( static_any_t cur, C&, mpl::false_ )
165{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700166 ++static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( cur );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700167}
168
169//____________________________________________________________________________//
170
171template<typename C>
172inline void
173next( static_any_t cur, C const&, mpl::true_ )
174{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700175 ++static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700176}
177
178//____________________________________________________________________________//
179
180// ************************************************************************** //
181// ************** deref ************** //
182// ************************************************************************** //
183
184template<class RefType,typename C>
185inline RefType
186deref( static_any_t cur, C&, ::ndnboost::type<RefType>, mpl::false_ )
187{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700188 return *static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( cur );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700189}
190
191//____________________________________________________________________________//
192
193template<class RefType,typename C>
194inline RefType
195deref( static_any_t cur, C const&, ::ndnboost::type<RefType>, mpl::true_ )
196{
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700197 return *static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700198}
199
200//____________________________________________________________________________//
201
202// ************************************************************************** //
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700203// ************** NDNBOOST_TEST_FOREACH ************** //
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700204// ************************************************************************** //
205
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700206#define NDNBOOST_TEST_FE_ANY ::ndnboost::unit_test::for_each::static_any_t
207#define NDNBOOST_TEST_FE_IS_CONST( COL ) ::ndnboost::unit_test::for_each::is_const_coll( COL )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700208
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700209#define NDNBOOST_TEST_FE_BEG( COL ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700210 ::ndnboost::unit_test::for_each::begin( \
211 COL, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700212 NDNBOOST_TEST_FE_IS_CONST( COL ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700213/**/
214
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700215#define NDNBOOST_TEST_FE_END( COL ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700216 ::ndnboost::unit_test::for_each::end( \
217 COL, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700218 NDNBOOST_TEST_FE_IS_CONST( COL ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700219/**/
220
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700221#define NDNBOOST_TEST_FE_DONE( COL ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700222 ::ndnboost::unit_test::for_each::done( \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700223 NDNBOOST_TEST_FE_CUR_VAR, \
224 NDNBOOST_TEST_FE_END_VAR, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700225 COL, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700226 NDNBOOST_TEST_FE_IS_CONST( COL ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700227/**/
228
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700229#define NDNBOOST_TEST_FE_NEXT( COL ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700230 ::ndnboost::unit_test::for_each::next( \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700231 NDNBOOST_TEST_FE_CUR_VAR, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700232 COL, \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700233 NDNBOOST_TEST_FE_IS_CONST( COL ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700234/**/
235
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700236#define NDNBOOST_FOREACH_NOOP(COL) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700237 ((void)&(COL))
238
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700239#define NDNBOOST_TEST_FE_DEREF( COL, RefType ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700240 ::ndnboost::unit_test::for_each::deref( \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700241 NDNBOOST_TEST_FE_CUR_VAR, \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700242 COL, \
243 ::ndnboost::type<RefType >(), \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700244 NDNBOOST_TEST_FE_IS_CONST( COL ) ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700245/**/
246
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700247#if NDNBOOST_WORKAROUND( NDNBOOST_MSVC, == 1310 )
248#define NDNBOOST_TEST_LINE_NUM
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700249#else
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700250#define NDNBOOST_TEST_LINE_NUM __LINE__
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700251#endif
252
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700253#define NDNBOOST_TEST_FE_CUR_VAR NDNBOOST_JOIN( _fe_cur_, NDNBOOST_TEST_LINE_NUM )
254#define NDNBOOST_TEST_FE_END_VAR NDNBOOST_JOIN( _fe_end_, NDNBOOST_TEST_LINE_NUM )
255#define NDNBOOST_TEST_FE_CON_VAR NDNBOOST_JOIN( _fe_con_, NDNBOOST_TEST_LINE_NUM )
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700256
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700257#define NDNBOOST_TEST_FOREACH( RefType, var, COL ) \
258if( NDNBOOST_TEST_FE_ANY NDNBOOST_TEST_FE_CUR_VAR = NDNBOOST_TEST_FE_BEG( COL ) ) {} else \
259if( NDNBOOST_TEST_FE_ANY NDNBOOST_TEST_FE_END_VAR = NDNBOOST_TEST_FE_END( COL ) ) {} else \
260for( bool NDNBOOST_TEST_FE_CON_VAR = true; \
261 NDNBOOST_TEST_FE_CON_VAR && !NDNBOOST_TEST_FE_DONE( COL ); \
262 NDNBOOST_TEST_FE_CON_VAR ? NDNBOOST_TEST_FE_NEXT( COL ) : NDNBOOST_FOREACH_NOOP( COL )) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700263 \
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700264 if( (NDNBOOST_TEST_FE_CON_VAR = false, false) ) {} else \
265 for( RefType var = NDNBOOST_TEST_FE_DEREF( COL, RefType ); \
266 !NDNBOOST_TEST_FE_CON_VAR; NDNBOOST_TEST_FE_CON_VAR = true ) \
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700267/**/
268
269//____________________________________________________________________________//
270
271} // namespace for_each
272
273} // namespace unit_test
274
275} // namespace ndnboost
276
277//____________________________________________________________________________//
278
279#include <ndnboost/test/detail/enable_warnings.hpp>
280
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700281#endif // NDNBOOST_TEST_FOREACH_HPP_021005GER