Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 1 | // (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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 13 | // Description : this is an abridged version of an excelent NDNBOOST_FOREACH facility |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 14 | // 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 20 | #ifndef NDNBOOST_TEST_FOREACH_HPP_021005GER |
| 21 | #define NDNBOOST_TEST_FOREACH_HPP_021005GER |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 22 | |
| 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 | |
| 37 | namespace ndnboost { |
| 38 | |
| 39 | namespace unit_test { |
| 40 | |
| 41 | namespace for_each { |
| 42 | |
| 43 | // ************************************************************************** // |
| 44 | // ************** static_any ************** // |
| 45 | // ************************************************************************** // |
| 46 | |
| 47 | struct static_any_base |
| 48 | { |
| 49 | operator bool() const { return false; } |
| 50 | }; |
| 51 | |
| 52 | //____________________________________________________________________________// |
| 53 | |
| 54 | template<typename Iter> |
| 55 | struct 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 | |
| 64 | typedef static_any_base const& static_any_t; |
| 65 | |
| 66 | //____________________________________________________________________________// |
| 67 | |
| 68 | template<typename Iter> |
| 69 | inline Iter& |
| 70 | static_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 | |
| 81 | template<typename C> |
| 82 | inline is_const<C> |
| 83 | is_const_coll( C& ) |
| 84 | { |
| 85 | return is_const<C>(); |
| 86 | } |
| 87 | |
| 88 | //____________________________________________________________________________// |
| 89 | |
| 90 | // ************************************************************************** // |
| 91 | // ************** begin ************** // |
| 92 | // ************************************************************************** // |
| 93 | |
| 94 | template<typename C> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 95 | inline static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 96 | begin( C& t, mpl::false_ ) |
| 97 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 98 | return static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator>( t.begin() ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | //____________________________________________________________________________// |
| 102 | |
| 103 | template<typename C> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 104 | inline static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 105 | begin( C const& t, mpl::true_ ) |
| 106 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 107 | return static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( t.begin() ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | //____________________________________________________________________________// |
| 111 | |
| 112 | // ************************************************************************** // |
| 113 | // ************** end ************** // |
| 114 | // ************************************************************************** // |
| 115 | |
| 116 | template<typename C> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 117 | inline static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 118 | end( C& t, mpl::false_ ) |
| 119 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 120 | return static_any<NDNBOOST_DEDUCED_TYPENAME C::iterator>( t.end() ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | //____________________________________________________________________________// |
| 124 | |
| 125 | template<typename C> |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 126 | inline static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 127 | end( C const& t, mpl::true_ ) |
| 128 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 129 | return static_any<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( t.end() ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | //____________________________________________________________________________// |
| 133 | |
| 134 | // ************************************************************************** // |
| 135 | // ************** done ************** // |
| 136 | // ************************************************************************** // |
| 137 | |
| 138 | template<typename C> |
| 139 | inline bool |
| 140 | done( static_any_t cur, static_any_t end, C&, mpl::false_ ) |
| 141 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 142 | return static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( cur ) == |
| 143 | static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( end ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | //____________________________________________________________________________// |
| 147 | |
| 148 | template<typename C> |
| 149 | inline bool |
| 150 | done( static_any_t cur, static_any_t end, C const&, mpl::true_ ) |
| 151 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 152 | return static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( cur ) == |
| 153 | static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( end ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | //____________________________________________________________________________// |
| 157 | |
| 158 | // ************************************************************************** // |
| 159 | // ************** next ************** // |
| 160 | // ************************************************************************** // |
| 161 | |
| 162 | template<typename C> |
| 163 | inline void |
| 164 | next( static_any_t cur, C&, mpl::false_ ) |
| 165 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 166 | ++static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( cur ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | //____________________________________________________________________________// |
| 170 | |
| 171 | template<typename C> |
| 172 | inline void |
| 173 | next( static_any_t cur, C const&, mpl::true_ ) |
| 174 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 175 | ++static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( cur ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | //____________________________________________________________________________// |
| 179 | |
| 180 | // ************************************************************************** // |
| 181 | // ************** deref ************** // |
| 182 | // ************************************************************************** // |
| 183 | |
| 184 | template<class RefType,typename C> |
| 185 | inline RefType |
| 186 | deref( static_any_t cur, C&, ::ndnboost::type<RefType>, mpl::false_ ) |
| 187 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 188 | return *static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::iterator>( cur ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | //____________________________________________________________________________// |
| 192 | |
| 193 | template<class RefType,typename C> |
| 194 | inline RefType |
| 195 | deref( static_any_t cur, C const&, ::ndnboost::type<RefType>, mpl::true_ ) |
| 196 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 197 | return *static_any_cast<NDNBOOST_DEDUCED_TYPENAME C::const_iterator>( cur ); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | //____________________________________________________________________________// |
| 201 | |
| 202 | // ************************************************************************** // |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 203 | // ************** NDNBOOST_TEST_FOREACH ************** // |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 204 | // ************************************************************************** // |
| 205 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 206 | #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 Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 209 | #define NDNBOOST_TEST_FE_BEG( COL ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 210 | ::ndnboost::unit_test::for_each::begin( \ |
| 211 | COL, \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 212 | NDNBOOST_TEST_FE_IS_CONST( COL ) ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 213 | /**/ |
| 214 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 215 | #define NDNBOOST_TEST_FE_END( COL ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 216 | ::ndnboost::unit_test::for_each::end( \ |
| 217 | COL, \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 218 | NDNBOOST_TEST_FE_IS_CONST( COL ) ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 219 | /**/ |
| 220 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 221 | #define NDNBOOST_TEST_FE_DONE( COL ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 222 | ::ndnboost::unit_test::for_each::done( \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 223 | NDNBOOST_TEST_FE_CUR_VAR, \ |
| 224 | NDNBOOST_TEST_FE_END_VAR, \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 225 | COL, \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 226 | NDNBOOST_TEST_FE_IS_CONST( COL ) ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 227 | /**/ |
| 228 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 229 | #define NDNBOOST_TEST_FE_NEXT( COL ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 230 | ::ndnboost::unit_test::for_each::next( \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 231 | NDNBOOST_TEST_FE_CUR_VAR, \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 232 | COL, \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 233 | NDNBOOST_TEST_FE_IS_CONST( COL ) ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 234 | /**/ |
| 235 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 236 | #define NDNBOOST_FOREACH_NOOP(COL) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 237 | ((void)&(COL)) |
| 238 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 239 | #define NDNBOOST_TEST_FE_DEREF( COL, RefType ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 240 | ::ndnboost::unit_test::for_each::deref( \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 241 | NDNBOOST_TEST_FE_CUR_VAR, \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 242 | COL, \ |
| 243 | ::ndnboost::type<RefType >(), \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 244 | NDNBOOST_TEST_FE_IS_CONST( COL ) ) \ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 245 | /**/ |
| 246 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 247 | #if NDNBOOST_WORKAROUND( NDNBOOST_MSVC, == 1310 ) |
| 248 | #define NDNBOOST_TEST_LINE_NUM |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 249 | #else |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 250 | #define NDNBOOST_TEST_LINE_NUM __LINE__ |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 251 | #endif |
| 252 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 253 | #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 Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 256 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 257 | #define NDNBOOST_TEST_FOREACH( RefType, var, COL ) \ |
| 258 | if( NDNBOOST_TEST_FE_ANY NDNBOOST_TEST_FE_CUR_VAR = NDNBOOST_TEST_FE_BEG( COL ) ) {} else \ |
| 259 | if( NDNBOOST_TEST_FE_ANY NDNBOOST_TEST_FE_END_VAR = NDNBOOST_TEST_FE_END( COL ) ) {} else \ |
| 260 | for( 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 Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 263 | \ |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 264 | 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 Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 267 | /**/ |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame^] | 281 | #endif // NDNBOOST_TEST_FOREACH_HPP_021005GER |