foreach.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/test/detail/workaround.hpp>
  26. #include <boost/type_traits/is_const.hpp>
  27. #include <boost/test/detail/suppress_warnings.hpp>
  28. //____________________________________________________________________________//
  29. namespace boost {
  30. namespace unit_test {
  31. namespace for_each {
  32. // ************************************************************************** //
  33. // ************** static_any ************** //
  34. // ************************************************************************** //
  35. struct static_any_base
  36. {
  37. operator bool() const { return false; }
  38. };
  39. //____________________________________________________________________________//
  40. template<typename Iter>
  41. struct static_any : static_any_base
  42. {
  43. static_any( Iter const& t ) : m_it( t ) {}
  44. mutable Iter m_it;
  45. };
  46. //____________________________________________________________________________//
  47. typedef static_any_base const& static_any_t;
  48. //____________________________________________________________________________//
  49. template<typename Iter>
  50. inline Iter&
  51. static_any_cast( static_any_t a, Iter* = 0 )
  52. {
  53. return static_cast<Iter&>( static_cast<static_any<Iter> const&>( a ).m_it );
  54. }
  55. //____________________________________________________________________________//
  56. // ************************************************************************** //
  57. // ************** is_const ************** //
  58. // ************************************************************************** //
  59. template<typename C>
  60. inline is_const<C>
  61. is_const_coll( C& )
  62. {
  63. return is_const<C>();
  64. }
  65. //____________________________________________________________________________//
  66. // ************************************************************************** //
  67. // ************** begin ************** //
  68. // ************************************************************************** //
  69. template<typename C>
  70. inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
  71. begin( C& t, mpl::false_ )
  72. {
  73. return static_any<BOOST_DEDUCED_TYPENAME C::iterator>( t.begin() );
  74. }
  75. //____________________________________________________________________________//
  76. template<typename C>
  77. inline static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>
  78. begin( C const& t, mpl::true_ )
  79. {
  80. return static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>( t.begin() );
  81. }
  82. //____________________________________________________________________________//
  83. // ************************************************************************** //
  84. // ************** end ************** //
  85. // ************************************************************************** //
  86. template<typename C>
  87. inline static_any<BOOST_DEDUCED_TYPENAME C::iterator>
  88. end( C& t, mpl::false_ )
  89. {
  90. return static_any<BOOST_DEDUCED_TYPENAME C::iterator>( t.end() );
  91. }
  92. //____________________________________________________________________________//
  93. template<typename C>
  94. inline static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>
  95. end( C const& t, mpl::true_ )
  96. {
  97. return static_any<BOOST_DEDUCED_TYPENAME C::const_iterator>( t.end() );
  98. }
  99. //____________________________________________________________________________//
  100. // ************************************************************************** //
  101. // ************** done ************** //
  102. // ************************************************************************** //
  103. template<typename C>
  104. inline bool
  105. done( static_any_t cur, static_any_t end, C&, mpl::false_ )
  106. {
  107. return static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur ) ==
  108. static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( end );
  109. }
  110. //____________________________________________________________________________//
  111. template<typename C>
  112. inline bool
  113. done( static_any_t cur, static_any_t end, C const&, mpl::true_ )
  114. {
  115. return static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur ) ==
  116. static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( end );
  117. }
  118. //____________________________________________________________________________//
  119. // ************************************************************************** //
  120. // ************** next ************** //
  121. // ************************************************************************** //
  122. template<typename C>
  123. inline void
  124. next( static_any_t cur, C&, mpl::false_ )
  125. {
  126. ++static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
  127. }
  128. //____________________________________________________________________________//
  129. template<typename C>
  130. inline void
  131. next( static_any_t cur, C const&, mpl::true_ )
  132. {
  133. ++static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
  134. }
  135. //____________________________________________________________________________//
  136. // ************************************************************************** //
  137. // ************** prev ************** //
  138. // ************************************************************************** //
  139. template<typename C>
  140. inline void
  141. prev( static_any_t cur, C&, mpl::false_ )
  142. {
  143. --static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
  144. }
  145. //____________________________________________________________________________//
  146. template<typename C>
  147. inline void
  148. prev( static_any_t cur, C const&, mpl::true_ )
  149. {
  150. --static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
  151. }
  152. //____________________________________________________________________________//
  153. // ************************************************************************** //
  154. // ************** deref ************** //
  155. // ************************************************************************** //
  156. template<class RefType,typename C>
  157. inline RefType
  158. deref( static_any_t cur, C&, ::boost::type<RefType>, mpl::false_ )
  159. {
  160. return *static_any_cast<BOOST_DEDUCED_TYPENAME C::iterator>( cur );
  161. }
  162. //____________________________________________________________________________//
  163. template<class RefType,typename C>
  164. inline RefType
  165. deref( static_any_t cur, C const&, ::boost::type<RefType>, mpl::true_ )
  166. {
  167. return *static_any_cast<BOOST_DEDUCED_TYPENAME C::const_iterator>( cur );
  168. }
  169. //____________________________________________________________________________//
  170. // ************************************************************************** //
  171. // ************** BOOST_TEST_FOREACH ************** //
  172. // ************************************************************************** //
  173. #define BOOST_TEST_FE_ANY ::boost::unit_test::for_each::static_any_t
  174. #define BOOST_TEST_FE_IS_CONST( COL ) ::boost::unit_test::for_each::is_const_coll( COL )
  175. #define BOOST_TEST_FE_BEG( COL ) \
  176. ::boost::unit_test::for_each::begin( \
  177. COL, \
  178. BOOST_TEST_FE_IS_CONST( COL ) ) \
  179. /**/
  180. #define BOOST_TEST_FE_END( COL ) \
  181. ::boost::unit_test::for_each::end( \
  182. COL, \
  183. BOOST_TEST_FE_IS_CONST( COL ) ) \
  184. /**/
  185. #define BOOST_TEST_FE_DONE( COL ) \
  186. ::boost::unit_test::for_each::done( \
  187. BOOST_TEST_FE_CUR_VAR, \
  188. BOOST_TEST_FE_END_VAR, \
  189. COL, \
  190. BOOST_TEST_FE_IS_CONST( COL ) ) \
  191. /**/
  192. #define BOOST_TEST_FE_NEXT( COL ) \
  193. ::boost::unit_test::for_each::next( \
  194. BOOST_TEST_FE_CUR_VAR, \
  195. COL, \
  196. BOOST_TEST_FE_IS_CONST( COL ) ) \
  197. /**/
  198. #define BOOST_TEST_FE_PREV( COL ) \
  199. ::boost::unit_test::for_each::prev( \
  200. BOOST_TEST_FE_CUR_VAR, \
  201. COL, \
  202. BOOST_TEST_FE_IS_CONST( COL ) ) \
  203. /**/
  204. #define BOOST_FOREACH_NOOP(COL) \
  205. ((void)&(COL))
  206. #define BOOST_TEST_FE_DEREF( COL, RefType ) \
  207. ::boost::unit_test::for_each::deref( \
  208. BOOST_TEST_FE_CUR_VAR, \
  209. COL, \
  210. ::boost::type<RefType >(), \
  211. BOOST_TEST_FE_IS_CONST( COL ) ) \
  212. /**/
  213. #if BOOST_WORKAROUND( BOOST_MSVC, == 1310 )
  214. #define BOOST_TEST_LINE_NUM
  215. #else
  216. #define BOOST_TEST_LINE_NUM __LINE__
  217. #endif
  218. #define BOOST_TEST_FE_CUR_VAR BOOST_JOIN( _fe_cur_, BOOST_TEST_LINE_NUM )
  219. #define BOOST_TEST_FE_END_VAR BOOST_JOIN( _fe_end_, BOOST_TEST_LINE_NUM )
  220. #define BOOST_TEST_FE_CON_VAR BOOST_JOIN( _fe_con_, BOOST_TEST_LINE_NUM )
  221. #define BOOST_TEST_FOREACH( RefType, var, COL ) \
  222. if( BOOST_TEST_FE_ANY BOOST_TEST_FE_CUR_VAR = BOOST_TEST_FE_BEG( COL ) ) {} else \
  223. if( BOOST_TEST_FE_ANY BOOST_TEST_FE_END_VAR = BOOST_TEST_FE_END( COL ) ) {} else \
  224. for( bool BOOST_TEST_FE_CON_VAR = true; \
  225. BOOST_TEST_FE_CON_VAR && !BOOST_TEST_FE_DONE( COL ); \
  226. BOOST_TEST_FE_CON_VAR ? BOOST_TEST_FE_NEXT( COL ) : BOOST_FOREACH_NOOP( COL )) \
  227. \
  228. if( (BOOST_TEST_FE_CON_VAR = false, false) ) {} else \
  229. for( RefType var = BOOST_TEST_FE_DEREF( COL, RefType ); \
  230. !BOOST_TEST_FE_CON_VAR; BOOST_TEST_FE_CON_VAR = true ) \
  231. /**/
  232. #define BOOST_TEST_REVERSE_FOREACH( RefType, var, COL ) \
  233. if( BOOST_TEST_FE_ANY BOOST_TEST_FE_CUR_VAR = BOOST_TEST_FE_END( COL ) ) {} else \
  234. if( BOOST_TEST_FE_ANY BOOST_TEST_FE_END_VAR = BOOST_TEST_FE_BEG( COL ) ) {} else \
  235. for( bool BOOST_TEST_FE_CON_VAR = true; \
  236. BOOST_TEST_FE_CON_VAR && !BOOST_TEST_FE_DONE( COL ); ) \
  237. \
  238. if( (BOOST_TEST_FE_CON_VAR = false, false) ) {} else \
  239. if( (BOOST_TEST_FE_PREV( COL ), false) ) {} else \
  240. for( RefType var = BOOST_TEST_FE_DEREF( COL, RefType ); \
  241. !BOOST_TEST_FE_CON_VAR; BOOST_TEST_FE_CON_VAR = true ) \
  242. /**/
  243. //____________________________________________________________________________//
  244. } // namespace for_each
  245. } // namespace unit_test
  246. } // namespace boost
  247. #include <boost/test/detail/enable_warnings.hpp>
  248. #endif // BOOST_TEST_UTILS_FOREACH_HPP