assert.hpp 14 KB

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