foreach.hpp 12 KB

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