source: projectionDesigner/trunk/projdesigner/include/gmtl/Util/StaticAssert.h @ 217

Last change on this file since 217 was 4, checked in by Torben Dannhauer, 15 years ago
File size: 3.4 KB
Line 
1/** Static assertion macros
2* Borrowed from boost and Loki.
3*/
4#ifndef GMTL_STATIC_ASSERT_HPP
5#define GMTL_STATIC_ASSERT_HPP
6
7namespace gmtl
8{
9   /**
10    * Define a structure that will contain our static assert; the name
11    * CompileTimeError will hopefully be displayed and catch the eye of the
12    * programmer.
13    */
14   template <int> struct CompileTimeError;
15   template <> struct CompileTimeError<true> {};
16}
17/**
18 * GMTL_STATIC_ASSERT macro.
19 * This macro will evaluate a compile time integral or pointer expression;
20 * if the expression is zero, the macro will generate a message in the form
21 * of an undefined identifier.
22 *
23 * @param   expr     the expression to evaluate.
24 * @param   msg      the message to display if expr is zero; msg cannot
25 *                   contain spaces!
26 */
27#define GMTL_STATIC_ASSERT(expr, msg) \
28   { gmtl::CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
29
30// -- OLD Static assert --- //
31// -- To be used if the new one causes problems -- //
32/*
33#include <gmtl/Defines.h>
34#include <gmtl/Util/Meta.h>
35
36#ifdef __BORLANDC__
37//
38// workaround for buggy integral-constant expression support:
39#define GMTL_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
40#endif
41
42namespace gmtl
43{
44
45// HP aCC cannot deal with missing names for template value parameters
46template <bool x> struct STATIC_ASSERTION_FAILURE;
47
48template <> struct STATIC_ASSERTION_FAILURE<true>{};
49
50// HP aCC cannot deal with missing names for template value parameters
51template<int x> struct static_assert_test{};
52
53}
54
55//
56// Implicit instantiation requires that all member declarations be
57// instantiated, but that the definitions are *not* instantiated.
58//
59// It's not particularly clear how this applies to enum's or typedefs;
60// both are described as declarations [7.1.3] and [7.2] in the standard,
61// however some compilers use "delayed evaluation" of one or more of
62// these when implicitly instantiating templates.  We use typedef declarations
63// by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
64// version gets better results from your compiler...
65//
66// Implementation:
67// Both of these versions rely on sizeof(incomplete_type) generating an error
68// message containing the name of the incomplete type.  We use
69// "STATIC_ASSERTION_FAILURE" as the type name here to generate
70// an eye catching error message.  The result of the sizeof expression is either
71// used as an enum initialiser, or as a template argument depending which version
72// is in use...
73// Note that the argument to the assert is explicitly cast to bool using old-
74// style casts: too many compilers currently have problems with static_cast
75// when used inside integral constant expressions.
76//
77#if !defined(GMTL_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS) && !defined(__MWERKS__)
78#ifndef GMTL_MSVC
79#define GMTL_STATIC_ASSERT( B ) \
80   typedef ::gmtl::static_assert_test<\
81      sizeof(::gmtl::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
82         GMTL_JOIN(gmtl_static_assert_typedef_, __LINE__)
83#else
84// __LINE__ macro broken when -ZI is used see Q199057
85// fortunately MSVC ignores duplicate typedef's.
86#define GMTL_STATIC_ASSERT( B ) \
87   typedef ::gmtl::static_assert_test<\
88      sizeof(::gmtl::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
89      > gmtl_static_assert_typedef_
90#endif
91#else
92// alternative enum based implementation:
93#define GMTL_STATIC_ASSERT( B ) \
94   enum { GMTL_JOIN(gmtl_static_assert_enum_, __LINE__) \
95      = sizeof(::gmtl::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
96#endif
97
98*/
99
100#endif // GMTL_STATIC_ASSERT_HPP
Note: See TracBrowser for help on using the repository browser.