// (C) Copyright Eric Niebler 2004-2005 // (C) Copyright Gennadiy Rozental 2001. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org/libs/test for the library home page. // // File : $RCSfile$ // // Version : $Revision$ // // Description : this is an abridged version of an excelent ACTIV_BOOST_FOREACH facility // presented by Eric Niebler. I am so fond of it so I can't wait till it // going to be accepted into Boost. Also I need version with less number of dependencies // and more portable. This version doesn't support rvalues and will reeveluate it's // parameters, but should be good enough for my purposes. // *************************************************************************** #ifndef ACTIV_BOOST_TEST_UTILS_FOREACH_HPP #define ACTIV_BOOST_TEST_UTILS_FOREACH_HPP // Boost.Test #include // Boost #include #include #include #include //____________________________________________________________________________// namespace Boost { namespace unit_test { namespace for_each { // ************************************************************************** // // ************** static_any ************** // // ************************************************************************** // struct static_any_base { operator bool() const { return false; } }; //____________________________________________________________________________// template struct static_any : static_any_base { static_any( Iter const& t ) : m_it( t ) {} mutable Iter m_it; }; //____________________________________________________________________________// typedef static_any_base const& static_any_t; //____________________________________________________________________________// template inline Iter& static_any_cast( static_any_t a, Iter* = 0 ) { return static_cast( static_cast const&>( a ).m_it ); } //____________________________________________________________________________// // ************************************************************************** // // ************** is_const ************** // // ************************************************************************** // template inline is_const is_const_coll( C& ) { return is_const(); } //____________________________________________________________________________// // ************************************************************************** // // ************** begin ************** // // ************************************************************************** // template inline static_any begin( C& t, mpl::false_ ) { return static_any( t.begin() ); } //____________________________________________________________________________// template inline static_any begin( C const& t, mpl::true_ ) { return static_any( t.begin() ); } //____________________________________________________________________________// // ************************************************************************** // // ************** end ************** // // ************************************************************************** // template inline static_any end( C& t, mpl::false_ ) { return static_any( t.end() ); } //____________________________________________________________________________// template inline static_any end( C const& t, mpl::true_ ) { return static_any( t.end() ); } //____________________________________________________________________________// // ************************************************************************** // // ************** done ************** // // ************************************************************************** // template inline bool done( static_any_t cur, static_any_t end, C&, mpl::false_ ) { return static_any_cast( cur ) == static_any_cast( end ); } //____________________________________________________________________________// template inline bool done( static_any_t cur, static_any_t end, C const&, mpl::true_ ) { return static_any_cast( cur ) == static_any_cast( end ); } //____________________________________________________________________________// // ************************************************************************** // // ************** next ************** // // ************************************************************************** // template inline void next( static_any_t cur, C&, mpl::false_ ) { ++static_any_cast( cur ); } //____________________________________________________________________________// template inline void next( static_any_t cur, C const&, mpl::true_ ) { ++static_any_cast( cur ); } //____________________________________________________________________________// // ************************************************************************** // // ************** prev ************** // // ************************************************************************** // template inline void prev( static_any_t cur, C&, mpl::false_ ) { --static_any_cast( cur ); } //____________________________________________________________________________// template inline void prev( static_any_t cur, C const&, mpl::true_ ) { --static_any_cast( cur ); } //____________________________________________________________________________// // ************************************************************************** // // ************** deref ************** // // ************************************************************************** // template inline RefType deref( static_any_t cur, C&, ::Boost::type, mpl::false_ ) { return *static_any_cast( cur ); } //____________________________________________________________________________// template inline RefType deref( static_any_t cur, C const&, ::Boost::type, mpl::true_ ) { return *static_any_cast( cur ); } //____________________________________________________________________________// // ************************************************************************** // // ************** ACTIV_BOOST_TEST_FOREACH ************** // // ************************************************************************** // #define ACTIV_BOOST_TEST_FE_ANY ::Boost::unit_test::for_each::static_any_t #define ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ::Boost::unit_test::for_each::is_const_coll( COL ) #define ACTIV_BOOST_TEST_FE_BEG( COL ) \ ::Boost::unit_test::for_each::begin( \ COL, \ ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ) \ /**/ #define ACTIV_BOOST_TEST_FE_END( COL ) \ ::Boost::unit_test::for_each::end( \ COL, \ ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ) \ /**/ #define ACTIV_BOOST_TEST_FE_DONE( COL ) \ ::Boost::unit_test::for_each::done( \ ACTIV_BOOST_TEST_FE_CUR_VAR, \ ACTIV_BOOST_TEST_FE_END_VAR, \ COL, \ ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ) \ /**/ #define ACTIV_BOOST_TEST_FE_NEXT( COL ) \ ::Boost::unit_test::for_each::next( \ ACTIV_BOOST_TEST_FE_CUR_VAR, \ COL, \ ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ) \ /**/ #define ACTIV_BOOST_TEST_FE_PREV( COL ) \ ::Boost::unit_test::for_each::prev( \ ACTIV_BOOST_TEST_FE_CUR_VAR, \ COL, \ ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ) \ /**/ #define ACTIV_BOOST_FOREACH_NOOP(COL) \ ((void)&(COL)) #define ACTIV_BOOST_TEST_FE_DEREF( COL, RefType ) \ ::Boost::unit_test::for_each::deref( \ ACTIV_BOOST_TEST_FE_CUR_VAR, \ COL, \ ::Boost::type(), \ ACTIV_BOOST_TEST_FE_IS_CONST( COL ) ) \ /**/ #if ACTIV_BOOST_WORKAROUND( ACTIV_BOOST_MSVC, == 1310 ) #define ACTIV_BOOST_TEST_LINE_NUM #else #define ACTIV_BOOST_TEST_LINE_NUM __LINE__ #endif #define ACTIV_BOOST_TEST_FE_CUR_VAR ACTIV_BOOST_JOIN( _fe_cur_, ACTIV_BOOST_TEST_LINE_NUM ) #define ACTIV_BOOST_TEST_FE_END_VAR ACTIV_BOOST_JOIN( _fe_end_, ACTIV_BOOST_TEST_LINE_NUM ) #define ACTIV_BOOST_TEST_FE_CON_VAR ACTIV_BOOST_JOIN( _fe_con_, ACTIV_BOOST_TEST_LINE_NUM ) #define ACTIV_BOOST_TEST_FOREACH( RefType, var, COL ) \ if( ACTIV_BOOST_TEST_FE_ANY ACTIV_BOOST_TEST_FE_CUR_VAR = ACTIV_BOOST_TEST_FE_BEG( COL ) ) {} else \ if( ACTIV_BOOST_TEST_FE_ANY ACTIV_BOOST_TEST_FE_END_VAR = ACTIV_BOOST_TEST_FE_END( COL ) ) {} else \ for( bool ACTIV_BOOST_TEST_FE_CON_VAR = true; \ ACTIV_BOOST_TEST_FE_CON_VAR && !ACTIV_BOOST_TEST_FE_DONE( COL ); \ ACTIV_BOOST_TEST_FE_CON_VAR ? ACTIV_BOOST_TEST_FE_NEXT( COL ) : ACTIV_BOOST_FOREACH_NOOP( COL )) \ \ if( (ACTIV_BOOST_TEST_FE_CON_VAR = false, false) ) {} else \ for( RefType var = ACTIV_BOOST_TEST_FE_DEREF( COL, RefType ); \ !ACTIV_BOOST_TEST_FE_CON_VAR; ACTIV_BOOST_TEST_FE_CON_VAR = true ) \ /**/ #define ACTIV_BOOST_TEST_REVERSE_FOREACH( RefType, var, COL ) \ if( ACTIV_BOOST_TEST_FE_ANY ACTIV_BOOST_TEST_FE_CUR_VAR = ACTIV_BOOST_TEST_FE_END( COL ) ) {} else \ if( ACTIV_BOOST_TEST_FE_ANY ACTIV_BOOST_TEST_FE_END_VAR = ACTIV_BOOST_TEST_FE_BEG( COL ) ) {} else \ for( bool ACTIV_BOOST_TEST_FE_CON_VAR = true; \ ACTIV_BOOST_TEST_FE_CON_VAR && !ACTIV_BOOST_TEST_FE_DONE( COL ); ) \ \ if( (ACTIV_BOOST_TEST_FE_CON_VAR = false, false) ) {} else \ if( (ACTIV_BOOST_TEST_FE_PREV( COL ), false) ) {} else \ for( RefType var = ACTIV_BOOST_TEST_FE_DEREF( COL, RefType ); \ !ACTIV_BOOST_TEST_FE_CON_VAR; ACTIV_BOOST_TEST_FE_CON_VAR = true ) \ /**/ //____________________________________________________________________________// } // namespace for_each } // namespace unit_test } // namespace Boost #include #endif // ACTIV_BOOST_TEST_UTILS_FOREACH_HPP