]> Dogcows Code - chaz/yoink/blob - src/cml/core/cml_assert.h
now using cml for vectors and math stuff
[chaz/yoink] / src / cml / core / cml_assert.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 *
12 * Macros and template metaprogramming to implement compile- and run-time
13 * assertions.
14 */
15
16 #ifndef cml_assert_h
17 #define cml_assert_h
18
19 #include <cml/core/cml_meta.h>
20
21 namespace cml {
22
23 /* Join preprocessor macros into a new preprocessor macro: */
24 #define CML_JOIN(X,Y) CML_DO_JOIN(X,Y)
25 #define CML_DO_JOIN(X,Y) CML_DO_JOIN2(X,Y)
26 #define CML_DO_JOIN2(X,Y) X##Y
27
28 /* Change a macro value into a string: */
29 #define TO_STRING(X) TO_STRING2(X)
30 #define TO_STRING2(X) #X
31
32 /** Default undefined compile-time assertion struct. */
33 template<bool T> struct STATIC_ASSERTION_FAILURE;
34
35 /** Struct instantiated when a true assertion is made at compile-time. */
36 template<> struct STATIC_ASSERTION_FAILURE<true> {
37 typedef true_type result;
38 enum { value = true };
39 };
40
41 /** Create a compile-time assertion.
42 *
43 * @note Compile-time assertions must be expressions that can be evaluated at
44 * comile time. This means that the expression must only rely on constants,
45 * enums, and/or template parameters, not variables having run-time storage
46 * requirements.
47 *
48 * @warning Enclose expressions that have commas with parens, otherwise the
49 * preprocessor will parse the commas as macro argument separators!
50 *
51 * @sa STATIC_ASSERTION_FAILURE
52 */
53 #define CML_STATIC_REQUIRE(_E_) \
54 typedef typename STATIC_ASSERTION_FAILURE<(_E_)>::result \
55 CML_JOIN(__cml_assert_test_typedef_, __LINE__)
56
57
58 /** A more meaningful compile-time assertion struct.
59 *
60 * The parameter M is a struct type which has been declared but not
61 * defined; e.g. struct this_is_an_error.
62 *
63 * When used with CML_STATIC_REQUIRE_M(<expr>,M), the compiler errors will
64 * contain the struct name at the point of the error.
65 */
66 template<bool T, typename M> struct STATIC_ASSERTION_FAILURE_M {
67 typename M::bogus result;
68 };
69
70 /** Instantiated for true assertions. */
71 template<typename M> struct STATIC_ASSERTION_FAILURE_M<true,M> {
72 typedef true_type result;
73 enum { value = true };
74 };
75
76 /** Create a compile-time assertion with a message.
77 *
78 * @note Compile-time assertions must be expressions that can be evaluated at
79 * comile time. This means that the expression must only rely on constants,
80 * enums, and/or template parameters, not variables having run-time storage
81 * requirements.
82 *
83 * @warning Enclose expressions that have commas with parens, otherwise the
84 * preprocessor will parse the commas as macro argument separators!
85 *
86 * @sa STATIC_ASSERTION_FAILURE_M
87 */
88 #define CML_STATIC_REQUIRE_M(_E_, _M_) \
89 typedef typename STATIC_ASSERTION_FAILURE_M<(_E_),_M_> \
90 ::result CML_JOIN(__bogus_assert_type_, __LINE__)
91
92 } // namespace cml
93
94 #endif
95
96 // -------------------------------------------------------------------------
97 // vim:ft=cpp
This page took 0.033254 seconds and 4 git commands to generate.