assert.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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$
  11. // $Date$
  12. // $Revision$
  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/gpu.hpp>
  24. #include <boost/mpl/aux_/config/static_constant.hpp>
  25. #include <boost/mpl/aux_/config/pp_counter.hpp>
  26. #include <boost/mpl/aux_/config/workaround.hpp>
  27. #include <boost/preprocessor/cat.hpp>
  28. #include <boost/config.hpp> // make sure 'size_t' is placed into 'std'
  29. #include <cstddef>
  30. #if BOOST_WORKAROUND(BOOST_MSVC, == 1700)
  31. #include <boost/mpl/if.hpp>
  32. #endif
  33. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  34. || (BOOST_MPL_CFG_GCC != 0) \
  35. || BOOST_WORKAROUND(__IBMCPP__, <= 600)
  36. # define BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES
  37. #endif
  38. #if BOOST_WORKAROUND(__MWERKS__, < 0x3202) \
  39. || BOOST_WORKAROUND(__EDG_VERSION__, <= 238) \
  40. || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  41. || BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x840))
  42. # define BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER
  43. #endif
  44. // agurt, 10/nov/06: use enums for Borland (which cannot cope with static constants)
  45. // and GCC (which issues "unused variable" warnings when static constants are used
  46. // at a function scope)
  47. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  48. || (BOOST_MPL_CFG_GCC != 0) || (BOOST_MPL_CFG_GPU != 0) || defined(__PGI)
  49. # define BOOST_MPL_AUX_ASSERT_CONSTANT(T, expr) enum { expr }
  50. #else
  51. # define BOOST_MPL_AUX_ASSERT_CONSTANT(T, expr) BOOST_STATIC_CONSTANT(T, expr)
  52. #endif
  53. BOOST_MPL_AUX_ADL_BARRIER_NAMESPACE_OPEN
  54. struct failed {};
  55. // agurt, 24/aug/04: MSVC 7.1 workaround here and below: return/accept
  56. // 'assert<false>' by reference; can't apply it unconditionally -- apparently it
  57. // degrades the quality of GCC diagnostics
  58. #if BOOST_WORKAROUND(BOOST_MSVC, == 1310)
  59. # define AUX778076_ASSERT_ARG(x) x&
  60. #else
  61. # define AUX778076_ASSERT_ARG(x) x
  62. #endif
  63. template< bool C > struct assert { typedef void* type; };
  64. template<> struct assert<false> { typedef AUX778076_ASSERT_ARG(assert) type; };
  65. template< bool C >
  66. int assertion_failed( typename assert<C>::type );
  67. template< bool C >
  68. struct assertion
  69. {
  70. static int failed( assert<false> );
  71. };
  72. template<>
  73. struct assertion<true>
  74. {
  75. static int failed( void* );
  76. };
  77. struct assert_
  78. {
  79. #if !defined(BOOST_MPL_CFG_NO_DEFAULT_PARAMETERS_IN_NESTED_TEMPLATES)
  80. template< typename T1, typename T2 = na, typename T3 = na, typename T4 = na > struct types {};
  81. #endif
  82. static assert_ const arg;
  83. enum relations { equal = 1, not_equal, greater, greater_equal, less, less_equal };
  84. };
  85. #if !defined(BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES)
  86. bool operator==( failed, failed );
  87. bool operator!=( failed, failed );
  88. bool operator>( failed, failed );
  89. bool operator>=( failed, failed );
  90. bool operator<( failed, failed );
  91. bool operator<=( failed, failed );
  92. #if defined(__EDG_VERSION__)
  93. template< bool (*)(failed, failed), long x, long y > struct assert_relation {};
  94. # define BOOST_MPL_AUX_ASSERT_RELATION(x, y, r) assert_relation<r,x,y>
  95. #else
  96. template< BOOST_MPL_AUX_NTTP_DECL(long, x), BOOST_MPL_AUX_NTTP_DECL(long, y), bool (*)(failed, failed) >
  97. struct assert_relation {};
  98. # define BOOST_MPL_AUX_ASSERT_RELATION(x, y, r) assert_relation<x,y,r>
  99. #endif
  100. #else // BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES
  101. boost::mpl::aux::weighted_tag<1>::type operator==( assert_, assert_ );
  102. boost::mpl::aux::weighted_tag<2>::type operator!=( assert_, assert_ );
  103. boost::mpl::aux::weighted_tag<3>::type operator>( assert_, assert_ );
  104. boost::mpl::aux::weighted_tag<4>::type operator>=( assert_, assert_ );
  105. boost::mpl::aux::weighted_tag<5>::type operator<( assert_, assert_ );
  106. boost::mpl::aux::weighted_tag<6>::type operator<=( assert_, assert_ );
  107. template< assert_::relations r, long x, long y > struct assert_relation {};
  108. #endif
  109. #if BOOST_WORKAROUND(BOOST_MSVC, == 1700)
  110. template<class Pred>
  111. struct extract_assert_pred;
  112. template<class Pred>
  113. struct extract_assert_pred<void(Pred)> { typedef Pred type; };
  114. template<class Pred>
  115. struct eval_assert {
  116. typedef typename extract_assert_pred<Pred>::type P;
  117. typedef typename P::type p_type;
  118. typedef typename ::boost::mpl::if_c<p_type::value,
  119. AUX778076_ASSERT_ARG(assert<false>),
  120. failed ************ P::************
  121. >::type type;
  122. };
  123. template<class Pred>
  124. struct eval_assert_not {
  125. typedef typename extract_assert_pred<Pred>::type P;
  126. typedef typename P::type p_type;
  127. typedef typename ::boost::mpl::if_c<!p_type::value,
  128. AUX778076_ASSERT_ARG(assert<false>),
  129. failed ************ ::boost::mpl::not_<P>::************
  130. >::type type;
  131. };
  132. template< typename T >
  133. T make_assert_arg();
  134. #elif !defined(BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER)
  135. template< bool > struct assert_arg_pred_impl { typedef int type; };
  136. template<> struct assert_arg_pred_impl<true> { typedef void* type; };
  137. template< typename P > struct assert_arg_pred
  138. {
  139. typedef typename P::type p_type;
  140. typedef typename assert_arg_pred_impl< p_type::value >::type type;
  141. };
  142. template< typename P > struct assert_arg_pred_not
  143. {
  144. typedef typename P::type p_type;
  145. BOOST_MPL_AUX_ASSERT_CONSTANT( bool, p = !p_type::value );
  146. typedef typename assert_arg_pred_impl<p>::type type;
  147. };
  148. #if defined(BOOST_GCC) && BOOST_GCC >= 80000
  149. #define BOOST_MPL_IGNORE_PARENTHESES_WARNING
  150. #pragma GCC diagnostic push
  151. #pragma GCC diagnostic ignored "-Wparentheses"
  152. #endif
  153. template< typename Pred >
  154. failed ************ (Pred::************
  155. assert_arg( void (*)(Pred), typename assert_arg_pred<Pred>::type )
  156. );
  157. template< typename Pred >
  158. failed ************ (boost::mpl::not_<Pred>::************
  159. assert_not_arg( void (*)(Pred), typename assert_arg_pred_not<Pred>::type )
  160. );
  161. #ifdef BOOST_MPL_IGNORE_PARENTHESES_WARNING
  162. #undef BOOST_MPL_IGNORE_PARENTHESES_WARNING
  163. #pragma GCC diagnostic pop
  164. #endif
  165. template< typename Pred >
  166. AUX778076_ASSERT_ARG(assert<false>)
  167. assert_arg( void (*)(Pred), typename assert_arg_pred_not<Pred>::type );
  168. template< typename Pred >
  169. AUX778076_ASSERT_ARG(assert<false>)
  170. assert_not_arg( void (*)(Pred), typename assert_arg_pred<Pred>::type );
  171. #else // BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER
  172. template< bool c, typename Pred > struct assert_arg_type_impl
  173. {
  174. typedef failed ************ Pred::* mwcw83_wknd;
  175. typedef mwcw83_wknd ************* type;
  176. };
  177. template< typename Pred > struct assert_arg_type_impl<true,Pred>
  178. {
  179. typedef AUX778076_ASSERT_ARG(assert<false>) type;
  180. };
  181. template< typename Pred > struct assert_arg_type
  182. : assert_arg_type_impl< BOOST_MPL_AUX_VALUE_WKND(BOOST_MPL_AUX_NESTED_TYPE_WKND(Pred))::value, Pred >
  183. {
  184. };
  185. template< typename Pred >
  186. typename assert_arg_type<Pred>::type
  187. assert_arg(void (*)(Pred), int);
  188. template< typename Pred >
  189. typename assert_arg_type< boost::mpl::not_<Pred> >::type
  190. assert_not_arg(void (*)(Pred), int);
  191. # if !defined(BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES)
  192. template< long x, long y, bool (*r)(failed, failed) >
  193. typename assert_arg_type_impl< false,BOOST_MPL_AUX_ASSERT_RELATION(x,y,r) >::type
  194. assert_rel_arg( BOOST_MPL_AUX_ASSERT_RELATION(x,y,r) );
  195. # else
  196. template< assert_::relations r, long x, long y >
  197. typename assert_arg_type_impl< false,assert_relation<r,x,y> >::type
  198. assert_rel_arg( assert_relation<r,x,y> );
  199. # endif
  200. #endif // BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER
  201. #undef AUX778076_ASSERT_ARG
  202. BOOST_MPL_AUX_ADL_BARRIER_NAMESPACE_CLOSE
  203. #if BOOST_WORKAROUND(BOOST_MSVC, == 1700)
  204. // BOOST_MPL_ASSERT((pred<x,...>))
  205. #define BOOST_MPL_ASSERT(pred) \
  206. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  207. std::size_t \
  208. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  209. boost::mpl::assertion_failed<false>( \
  210. boost::mpl::make_assert_arg< \
  211. typename boost::mpl::eval_assert<void pred>::type \
  212. >() \
  213. ) \
  214. ) \
  215. ) \
  216. /**/
  217. // BOOST_MPL_ASSERT_NOT((pred<x,...>))
  218. #define BOOST_MPL_ASSERT_NOT(pred) \
  219. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  220. std::size_t \
  221. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  222. boost::mpl::assertion_failed<false>( \
  223. boost::mpl::make_assert_arg< \
  224. typename boost::mpl::eval_assert_not<void pred>::type \
  225. >() \
  226. ) \
  227. ) \
  228. ) \
  229. /**/
  230. #else
  231. // BOOST_MPL_ASSERT((pred<x,...>))
  232. #define BOOST_MPL_ASSERT(pred) \
  233. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  234. std::size_t \
  235. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  236. boost::mpl::assertion_failed<false>( \
  237. boost::mpl::assert_arg( (void (*) pred)0, 1 ) \
  238. ) \
  239. ) \
  240. ) \
  241. /**/
  242. // BOOST_MPL_ASSERT_NOT((pred<x,...>))
  243. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  244. # define BOOST_MPL_ASSERT_NOT(pred) \
  245. enum { \
  246. BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  247. boost::mpl::assertion<false>::failed( \
  248. boost::mpl::assert_not_arg( (void (*) pred)0, 1 ) \
  249. ) \
  250. ) \
  251. }\
  252. /**/
  253. #else
  254. # define BOOST_MPL_ASSERT_NOT(pred) \
  255. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  256. std::size_t \
  257. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  258. boost::mpl::assertion_failed<false>( \
  259. boost::mpl::assert_not_arg( (void (*) pred)0, 1 ) \
  260. ) \
  261. ) \
  262. ) \
  263. /**/
  264. #endif
  265. #endif
  266. // BOOST_MPL_ASSERT_RELATION(x, ==|!=|<=|<|>=|>, y)
  267. #if defined(BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES)
  268. # if !defined(BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER)
  269. // agurt, 9/nov/06: 'enum' below is a workaround for gcc 4.0.4/4.1.1 bugs #29522 and #29518
  270. # define BOOST_MPL_ASSERT_RELATION_IMPL(counter, x, rel, y) \
  271. enum { BOOST_PP_CAT(mpl_assert_rel_value,counter) = (x rel y) }; \
  272. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  273. std::size_t \
  274. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  275. boost::mpl::assertion_failed<BOOST_PP_CAT(mpl_assert_rel_value,counter)>( \
  276. (boost::mpl::failed ************ ( boost::mpl::assert_relation< \
  277. boost::mpl::assert_::relations( sizeof( \
  278. boost::mpl::assert_::arg rel boost::mpl::assert_::arg \
  279. ) ) \
  280. , x \
  281. , y \
  282. >::************)) 0 ) \
  283. ) \
  284. ) \
  285. /**/
  286. # else
  287. # define BOOST_MPL_ASSERT_RELATION_IMPL(counter, x, rel, y) \
  288. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  289. std::size_t \
  290. , BOOST_PP_CAT(mpl_assert_rel,counter) = sizeof( \
  291. boost::mpl::assert_::arg rel boost::mpl::assert_::arg \
  292. ) \
  293. ); \
  294. BOOST_MPL_AUX_ASSERT_CONSTANT( bool, BOOST_PP_CAT(mpl_assert_rel_value,counter) = (x rel y) ); \
  295. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  296. std::size_t \
  297. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  298. boost::mpl::assertion_failed<BOOST_PP_CAT(mpl_assert_rel_value,counter)>( \
  299. boost::mpl::assert_rel_arg( boost::mpl::assert_relation< \
  300. boost::mpl::assert_::relations(BOOST_PP_CAT(mpl_assert_rel,counter)) \
  301. , x \
  302. , y \
  303. >() ) \
  304. ) \
  305. ) \
  306. ) \
  307. /**/
  308. # endif
  309. # define BOOST_MPL_ASSERT_RELATION(x, rel, y) \
  310. BOOST_MPL_ASSERT_RELATION_IMPL(BOOST_MPL_AUX_PP_COUNTER(), x, rel, y) \
  311. /**/
  312. #else // !BOOST_MPL_CFG_ASSERT_USE_RELATION_NAMES
  313. # if defined(BOOST_MPL_CFG_ASSERT_BROKEN_POINTER_TO_POINTER_TO_MEMBER)
  314. # define BOOST_MPL_ASSERT_RELATION(x, rel, y) \
  315. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  316. std::size_t \
  317. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  318. boost::mpl::assertion_failed<(x rel y)>( boost::mpl::assert_rel_arg( \
  319. boost::mpl::BOOST_MPL_AUX_ASSERT_RELATION(x,y,(&boost::mpl::operator rel))() \
  320. ) ) \
  321. ) \
  322. ) \
  323. /**/
  324. # else
  325. # define BOOST_MPL_ASSERT_RELATION(x, rel, y) \
  326. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  327. std::size_t \
  328. , BOOST_PP_CAT(mpl_assertion_in_line_,BOOST_MPL_AUX_PP_COUNTER()) = sizeof( \
  329. boost::mpl::assertion_failed<(x rel y)>( (boost::mpl::failed ************ ( \
  330. boost::mpl::BOOST_MPL_AUX_ASSERT_RELATION(x,y,(&boost::mpl::operator rel))::************))0 ) \
  331. ) \
  332. ) \
  333. /**/
  334. # endif
  335. #endif
  336. // BOOST_MPL_ASSERT_MSG( (pred<x,...>::value), USER_PROVIDED_MESSAGE, (types<x,...>) )
  337. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202))
  338. # define BOOST_MPL_ASSERT_MSG_IMPL( counter, c, msg, types_ ) \
  339. struct msg; \
  340. typedef struct BOOST_PP_CAT(msg,counter) : boost::mpl::assert_ \
  341. { \
  342. using boost::mpl::assert_::types; \
  343. static boost::mpl::failed ************ (msg::************ assert_arg()) types_ \
  344. { return 0; } \
  345. } BOOST_PP_CAT(mpl_assert_arg,counter); \
  346. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  347. std::size_t \
  348. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  349. boost::mpl::assertion<(c)>::failed( BOOST_PP_CAT(mpl_assert_arg,counter)::assert_arg() ) \
  350. ) \
  351. ) \
  352. /**/
  353. #else
  354. # define BOOST_MPL_ASSERT_MSG_IMPL( counter, c, msg, types_ ) \
  355. struct msg; \
  356. typedef struct BOOST_PP_CAT(msg,counter) : boost::mpl::assert_ \
  357. { \
  358. static boost::mpl::failed ************ (msg::************ assert_arg()) types_ \
  359. { return 0; } \
  360. } BOOST_PP_CAT(mpl_assert_arg,counter); \
  361. BOOST_MPL_AUX_ASSERT_CONSTANT( \
  362. std::size_t \
  363. , BOOST_PP_CAT(mpl_assertion_in_line_,counter) = sizeof( \
  364. boost::mpl::assertion_failed<(c)>( BOOST_PP_CAT(mpl_assert_arg,counter)::assert_arg() ) \
  365. ) \
  366. ) \
  367. /**/
  368. #endif
  369. #if 0
  370. // Work around BOOST_MPL_ASSERT_MSG_IMPL generating multiple definition linker errors on VC++8.
  371. // #if defined(BOOST_MSVC) && BOOST_MSVC < 1500
  372. # include <boost/static_assert.hpp>
  373. # define BOOST_MPL_ASSERT_MSG( c, msg, types_ ) \
  374. BOOST_STATIC_ASSERT_MSG( c, #msg ) \
  375. /**/
  376. #else
  377. # define BOOST_MPL_ASSERT_MSG( c, msg, types_ ) \
  378. BOOST_MPL_ASSERT_MSG_IMPL( BOOST_MPL_AUX_PP_COUNTER(), c, msg, types_ ) \
  379. /**/
  380. #endif
  381. #endif // BOOST_MPL_ASSERT_HPP_INCLUDED