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