assert.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #ifndef BOOST_MPL_ASSERT_HPP_INCLUDED
  2. #define BOOST_MPL_ASSERT_HPP_INCLUDED
  3. // Copyright Aleksey Gurtovoy 2000-2006
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/mpl for documentation.
  10. // $Id: assert.hpp 49267 2008-10-11 06:19:02Z agurtovoy $
  11. // $Date: 2008-10-11 02:19:02 -0400 (Sat, 11 Oct 2008) $
  12. // $Revision: 49267 $
  13. #include <boost/mpl/not.hpp>
  14. #include <boost/mpl/aux_/value_wknd.hpp>
  15. #include <boost/mpl/aux_/nested_type_wknd.hpp>
  16. #include <boost/mpl/aux_/yes_no.hpp>
  17. #include <boost/mpl/aux_/na.hpp>
  18. #include <boost/mpl/aux_/adl_barrier.hpp>
  19. #include <boost/mpl/aux_/config/nttp.hpp>
  20. #include <boost/mpl/aux_/config/dtp.hpp>
  21. #include <boost/mpl/aux_/config/gcc.hpp>
  22. #include <boost/mpl/aux_/config/msvc.hpp>
  23. #include <boost/mpl/aux_/config/static_constant.hpp>
  24. #include <boost/mpl/aux_/config/pp_counter.hpp>
  25. #include <boost/mpl/aux_/config/workaround.hpp>
  26. #include <boost/preprocessor/cat.hpp>
  27. #include <boost/config.hpp> // make sure 'size_t' is placed into 'std'
  28. #include <cstddef>
  29. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  30. || (BOOST_MPL_CFG_GCC != 0) \
  31. || BOOST_WORKAROUND(__IBMCPP__, <= 600)
  32. # define BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES
  33. #endif
  34. #if BOOST_WORKAROUND(__MWERKS__, < 0x3202) \
  35. || BOOST_WORKAROUND(__EDG_VERSION__, <= 238) \
  36. || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  37. || BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x840))
  38. # define BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER
  39. #endif
  40. // agurt, 10/nov/06: use enums for Borland (which cannot cope with static constants)
  41. // and GCC (which issues "unused variable" warnings when static constants are used
  42. // at a function scope)
  43. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  44. || (BOOST_MPL_CFG_GCC != 0)
  45. # define BOOST_MPL_AUX_ASSERT_CONSTANT(T, expr) enum { expr }
  46. #else
  47. # define BOOST_MPL_AUX_ASSERT_CONSTANT(T, expr) BOOST_STATIC_CONSTANT(T, expr)
  48. #endif
  49. BOOST_MPL_AUX_ADL_BARRIER_NAMESPACE_OPEN
  50. struct failed {};
  51. // agurt, 24/aug/04: MSVC 7.1 workaround here and below: return/accept
  52. // 'assert<false>' by reference; can't apply it unconditionally -- apparently it
  53. // degrades the quality of GCC diagnostics
  54. #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  55. # define AUX778076_ASSERT_ARG(x) x&
  56. #else
  57. # define AUX778076_ASSERT_ARG(x) x
  58. #endif
  59. template< bool C > struct assert { typedef void* type; };
  60. template<> struct assert<false> { typedef AUX778076_ASSERT_ARG(assert) type; };
  61. template< bool C >
  62. int assertion_failed( typename assert<C>::type );
  63. template< bool C >
  64. struct assertion
  65. {
  66. static int failed( assert<false> );
  67. };
  68. template<>
  69. struct assertion<true>
  70. {
  71. static int failed( void* );
  72. };
  73. struct assert_
  74. {
  75. #if !defined(BOOST_MPL_CFG_NO_DEFAULT_PARAMETERS_IN_NESTED_TEMPLATES)
  76. template< typename T1, typename T2 = na, typename T3 = na, typename T4 = na > struct types {};
  77. #endif
  78. static assert_ const arg;
  79. enum relations { equal = 1, not_equal, greater, greater_equal, less, less_equal };
  80. };
  81. #if !defined(BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES)
  82. bool operator==( failed, failed );
  83. bool operator!=( failed, failed );
  84. bool operator>( failed, failed );
  85. bool operator>=( failed, failed );
  86. bool operator<( failed, failed );
  87. bool operator<=( failed, failed );
  88. #if defined(__EDG_VERSION__)
  89. template< bool (*)(failed, failed), long x, long y > struct assert_relation {};
  90. # define BOOST_MPL_AUX_ASSERT_RELATION(x, y, r) assert_relation<r,x,y>
  91. #else
  92. template< BOOST_MPL_AUX_NTTP_DECL(long, x), BOOST_MPL_AUX_NTTP_DECL(long, y), bool (*)(failed, failed) >
  93. struct assert_relation {};
  94. # define BOOST_MPL_AUX_ASSERT_RELATION(x, y, r) assert_relation<x,y,r>
  95. #endif
  96. #else // BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES
  97. boost::mpl::aux::weighted_tag<1>::type operator==( assert_, assert_ );
  98. boost::mpl::aux::weighted_tag<2>::type operator!=( assert_, assert_ );
  99. boost::mpl::aux::weighted_tag<3>::type operator>( assert_, assert_ );
  100. boost::mpl::aux::weighted_tag<4>::type operator>=( assert_, assert_ );
  101. boost::mpl::aux::weighted_tag<5>::type operator<( assert_, assert_ );
  102. boost::mpl::aux::weighted_tag<6>::type operator<=( assert_, assert_ );
  103. template< assert_::relations r, long x, long y > struct assert_relation {};
  104. #endif
  105. #if !defined(BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER)
  106. template< bool > struct assert_arg_pred_impl { typedef int type; };
  107. template<> struct assert_arg_pred_impl<true> { typedef void* type; };
  108. template< typename P > struct assert_arg_pred
  109. {
  110. typedef typename P::type p_type;
  111. typedef typename assert_arg_pred_impl< p_type::value >::type type;
  112. };
  113. template< typename P > struct assert_arg_pred_not
  114. {
  115. typedef typename P::type p_type;
  116. BOOST_MPL_AUX_ASSERT_CONSTANT( bool, p = !p_type::value );
  117. typedef typename assert_arg_pred_impl<p>::type type;
  118. };
  119. template< typename Pred >
  120. failed ************ (Pred::************
  121. assert_arg( void (*)(Pred), typename assert_arg_pred<Pred>::type )
  122. );
  123. template< typename Pred >
  124. failed ************ (boost::mpl::not_<Pred>::************
  125. assert_not_arg( void (*)(Pred), typename assert_arg_pred_not<Pred>::type )
  126. );
  127. template< typename Pred >
  128. AUX778076_ASSERT_ARG(assert<false>)
  129. assert_arg( void (*)(Pred), typename assert_arg_pred_not<Pred>::type );
  130. template< typename Pred >
  131. AUX778076_ASSERT_ARG(assert<false>)
  132. assert_not_arg( void (*)(Pred), typename assert_arg_pred<Pred>::type );
  133. #else // BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER
  134. template< bool c, typename Pred > struct assert_arg_type_impl
  135. {
  136. typedef failed ************ Pred::* mwcw83_wknd;
  137. typedef mwcw83_wknd ************* type;
  138. };
  139. template< typename Pred > struct assert_arg_type_impl<true,Pred>
  140. {
  141. typedef AUX778076_ASSERT_ARG(assert<false>) type;
  142. };
  143. template< typename Pred > struct assert_arg_type
  144. : assert_arg_type_impl< BOOST_MPL_AUX_VALUE_WKND(BOOST_MPL_AUX_NESTED_TYPE_WKND(Pred))::value, Pred >
  145. {
  146. };
  147. template< typename Pred >
  148. typename assert_arg_type<Pred>::type
  149. assert_arg(void (*)(Pred), int);
  150. template< typename Pred >
  151. typename assert_arg_type< boost::mpl::not_<Pred> >::type
  152. assert_not_arg(void (*)(Pred), int);
  153. # if !defined(BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES)
  154. template< long x, long y, bool (*r)(failed, failed) >
  155. typename assert_arg_type_impl< false,BOOST_MPL_AUX_ASSERT_RELATION(x,y,r) >::type
  156. assert_rel_arg( BOOST_MPL_AUX_ASSERT_RELATION(x,y,r) );
  157. # else
  158. template< assert_::relations r, long x, long y >
  159. typename assert_arg_type_impl< false,assert_relation<r,x,y> >::type
  160. assert_rel_arg( assert_relation<r,x,y> );
  161. # endif
  162. #endif // BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER
  163. #undef AUX778076_ASSERT_ARG
  164. BOOST_MPL_AUX_ADL_BARRIER_NAMESPACE_CLOSE
  165. // BOOST_MPL_ASSERT((pred<x,...>))
  166. #define BOOST_MPL_ASSERT(pred) \
  167. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  168. std::size_t \
  169. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  170. boost::mpl::assertion_failed<false>( \
  171. boost::mpl::assert_arg( (void (*) pred)0, 1 ) \
  172. ) \
  173. ) \
  174. ) \
  175. /**/
  176. // BOOST_MPL_ASSERT_NOT((pred<x,...>))
  177. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  178. # define BOOST_MPL_ASSERT_NOT(pred) \
  179. enum { \
  180. BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  181. boost::mpl::assertion<false>::failed( \
  182. boost::mpl::assert_not_arg( (void (*) pred)0, 1 ) \
  183. ) \
  184. ) \
  185. }\
  186. /**/
  187. #else
  188. # define BOOST_MPL_ASSERT_NOT(pred) \
  189. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  190. std::size_t \
  191. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  192. boost::mpl::assertion_failed<false>( \
  193. boost::mpl::assert_not_arg( (void (*) pred)0, 1 ) \
  194. ) \
  195. ) \
  196. ) \
  197. /**/
  198. #endif
  199. // BOOST_MPL_ASSERT_RELATION(x, ==|!=|<=|<|>=|>, y)
  200. #if defined(BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES)
  201. # if !defined(BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER)
  202. // agurt, 9/nov/06: 'enum' below is a workaround for gcc 4.0.4/4.1.1 bugs #29522 and #29518
  203. # define BOOST_MPL_ASSERT_RELATION_IMPL(counter, x, rel, y) \
  204. enum { BOOST_PP_CAT(mpl_assert_rel_value,counter) = (x rel y) }; \
  205. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  206. std::size_t \
  207. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  208. boost::mpl::assertion_failed<BOOST_PP_CAT(mpl_assert_rel_value,counter)>( \
  209. (boost::mpl::failed ************ ( boost::mpl::assert_relation< \
  210. boost::mpl::assert_::relations( sizeof( \
  211. boost::mpl::assert_::arg rel boost::mpl::assert_::arg \
  212. ) ) \
  213. , x \
  214. , y \
  215. >::************)) 0 ) \
  216. ) \
  217. ) \
  218. /**/
  219. # else
  220. # define BOOST_MPL_ASSERT_RELATION_IMPL(counter, x, rel, y) \
  221. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  222. std::size_t \
  223. , BOOST_PP_CAT(mpl_assert_rel,counter) = sizeof( \
  224. boost::mpl::assert_::arg rel boost::mpl::assert_::arg \
  225. ) \
  226. ); \
  227. BOOST_MPL_AUX_ASSERT_CONSTANT( bool, BOOST_PP_CAT(mpl_assert_rel_value,counter) = (x rel y) ); \
  228. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  229. std::size_t \
  230. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  231. boost::mpl::assertion_failed<BOOST_PP_CAT(mpl_assert_rel_value,counter)>( \
  232. boost::mpl::assert_rel_arg( boost::mpl::assert_relation< \
  233. boost::mpl::assert_::relations(BOOST_PP_CAT(mpl_assert_rel,counter)) \
  234. , x \
  235. , y \
  236. >() ) \
  237. ) \
  238. ) \
  239. ) \
  240. /**/
  241. # endif
  242. # define BOOST_MPL_ASSERT_RELATION(x, rel, y) \
  243. BOOST_MPL_ASSERT_RELATION_IMPL(BOOST_MPL_AUX_PP_COUNTER(), x, rel, y) \
  244. /**/
  245. #else // !BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES
  246. # if defined(BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER)
  247. # define BOOST_MPL_ASSERT_RELATION(x, rel, y) \
  248. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  249. std::size_t \
  250. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  251. boost::mpl::assertion_failed<(x rel y)>( boost::mpl::assert_rel_arg( \
  252. boost::mpl::BOOST_MPL_AUX_ASSERT_RELATION(x,y,(&boost::mpl::operator rel))() \
  253. ) ) \
  254. ) \
  255. ) \
  256. /**/
  257. # else
  258. # define BOOST_MPL_ASSERT_RELATION(x, rel, y) \
  259. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  260. std::size_t \
  261. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  262. boost::mpl::assertion_failed<(x rel y)>( (boost::mpl::failed ************ ( \
  263. boost::mpl::BOOST_MPL_AUX_ASSERT_RELATION(x,y,(&boost::mpl::operator rel))::************))0 ) \
  264. ) \
  265. ) \
  266. /**/
  267. # endif
  268. #endif
  269. // BOOST_MPL_ASSERT_MSG( (pred<x,...>::value), USER_PROVIDED_MESSAGE, (types<x,...>) )
  270. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202))
  271. # define BOOST_MPL_ASSERT_MSG_IMPL( counter, c, msg, types_ ) \
  272. struct msg; \
  273. typedef struct BOOST_PP_CAT(msg,counter) : boost::mpl::assert_ \
  274. { \
  275. using boost::mpl::assert_::types; \
  276. static boost::mpl::failed ************ (msg::************ assert_arg()) types_ \
  277. { return 0; } \
  278. } BOOST_PP_CAT(mpl_assert_arg,counter); \
  279. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  280. std::size_t \
  281. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  282. boost::mpl::assertion<(c)>::failed( BOOST_PP_CAT(mpl_assert_arg,counter)::assert_arg() ) \
  283. ) \
  284. ) \
  285. /**/
  286. #else
  287. # define BOOST_MPL_ASSERT_MSG_IMPL( counter, c, msg, types_ ) \
  288. struct msg; \
  289. typedef struct BOOST_PP_CAT(msg,counter) : boost::mpl::assert_ \
  290. { \
  291. static boost::mpl::failed ************ (msg::************ assert_arg()) types_ \
  292. { return 0; } \
  293. } BOOST_PP_CAT(mpl_assert_arg,counter); \
  294. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  295. std::size_t \
  296. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  297. boost::mpl::assertion_failed<(c)>( BOOST_PP_CAT(mpl_assert_arg,counter)::assert_arg() ) \
  298. ) \
  299. ) \
  300. /**/
  301. #endif
  302. #define BOOST_MPL_ASSERT_MSG( c, msg, types_ ) \
  303. BOOST_MPL_ASSERT_MSG_IMPL( BOOST_MPL_AUX_PP_COUNTER(), c, msg, types_ ) \
  304. /**/
  305. #endif // BOOST_MPL_ASSERT_HPP_INCLUDED