• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_STATIC_ASSERT_H
12 #define EIGEN_STATIC_ASSERT_H
13 
14 /* Some notes on Eigen's static assertion mechanism:
15  *
16  *  - in EIGEN_STATIC_ASSERT(CONDITION,MSG) the parameter CONDITION must be a compile time boolean
17  *    expression, and MSG an enum listed in struct internal::static_assertion<true>
18  *
19  *  - define EIGEN_NO_STATIC_ASSERT to disable them (and save compilation time)
20  *    in that case, the static assertion is converted to the following runtime assert:
21  *      eigen_assert(CONDITION && "MSG")
22  *
23  *  - currently EIGEN_STATIC_ASSERT can only be used in function scope
24  *
25  */
26 
27 #ifndef EIGEN_NO_STATIC_ASSERT
28 
29   #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(_MSC_VER) && (_MSC_VER >= 1600))
30 
31     // if native static_assert is enabled, let's use it
32     #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
33 
34   #else // not CXX0X
35 
36     namespace Eigen {
37 
38     namespace internal {
39 
40     template<bool condition>
41     struct static_assertion {};
42 
43     template<>
44     struct static_assertion<true>
45     {
46       enum {
47         YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX,
48         YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES,
49         YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES,
50         THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE,
51         THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE,
52         THIS_METHOD_IS_ONLY_FOR_OBJECTS_OF_A_SPECIFIC_SIZE,
53         YOU_MADE_A_PROGRAMMING_MISTAKE,
54         EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT,
55         EIGEN_INTERNAL_COMPILATION_ERROR_OR_YOU_MADE_A_PROGRAMMING_MISTAKE,
56         YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR,
57         YOU_CALLED_A_DYNAMIC_SIZE_METHOD_ON_A_FIXED_SIZE_MATRIX_OR_VECTOR,
58         UNALIGNED_LOAD_AND_STORE_OPERATIONS_UNIMPLEMENTED_ON_ALTIVEC,
59         THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES,
60         FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED,
61         NUMERIC_TYPE_MUST_BE_REAL,
62         COEFFICIENT_WRITE_ACCESS_TO_SELFADJOINT_NOT_SUPPORTED,
63         WRITING_TO_TRIANGULAR_PART_WITH_UNIT_DIAGONAL_IS_NOT_SUPPORTED,
64         THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE,
65         INVALID_MATRIX_PRODUCT,
66         INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS,
67         INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION,
68         YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY,
69         THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES,
70         THIS_METHOD_IS_ONLY_FOR_ROW_MAJOR_MATRICES,
71         INVALID_MATRIX_TEMPLATE_PARAMETERS,
72         INVALID_MATRIXBASE_TEMPLATE_PARAMETERS,
73         BOTH_MATRICES_MUST_HAVE_THE_SAME_STORAGE_ORDER,
74         THIS_METHOD_IS_ONLY_FOR_DIAGONAL_MATRIX,
75         THE_MATRIX_OR_EXPRESSION_THAT_YOU_PASSED_DOES_NOT_HAVE_THE_EXPECTED_TYPE,
76         THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_WITH_DIRECT_MEMORY_ACCESS_SUCH_AS_MAP_OR_PLAIN_MATRICES,
77         YOU_ALREADY_SPECIFIED_THIS_STRIDE,
78         INVALID_STORAGE_ORDER_FOR_THIS_VECTOR_EXPRESSION,
79         THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD,
80         PACKET_ACCESS_REQUIRES_TO_HAVE_INNER_STRIDE_FIXED_TO_1,
81         THIS_METHOD_IS_ONLY_FOR_SPECIFIC_TRANSFORMATIONS,
82         YOU_CANNOT_MIX_ARRAYS_AND_MATRICES,
83         YOU_PERFORMED_AN_INVALID_TRANSFORMATION_CONVERSION,
84         THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY,
85         YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT,
86         THIS_METHOD_IS_ONLY_FOR_1x1_EXPRESSIONS,
87         THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_OF_BOOL,
88         THIS_METHOD_IS_ONLY_FOR_ARRAYS_NOT_MATRICES,
89         YOU_PASSED_A_ROW_VECTOR_BUT_A_COLUMN_VECTOR_WAS_EXPECTED,
90         YOU_PASSED_A_COLUMN_VECTOR_BUT_A_ROW_VECTOR_WAS_EXPECTED,
91         THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE,
92         THE_STORAGE_ORDER_OF_BOTH_SIDES_MUST_MATCH
93       };
94     };
95 
96     } // end namespace internal
97 
98     } // end namespace Eigen
99 
100     // Specialized implementation for MSVC to avoid "conditional
101     // expression is constant" warnings.  This implementation doesn't
102     // appear to work under GCC, hence the multiple implementations.
103     #ifdef _MSC_VER
104 
105       #define EIGEN_STATIC_ASSERT(CONDITION,MSG) \
106         {Eigen::internal::static_assertion<bool(CONDITION)>::MSG;}
107 
108     #else
109 
110       #define EIGEN_STATIC_ASSERT(CONDITION,MSG) \
111         if (Eigen::internal::static_assertion<bool(CONDITION)>::MSG) {}
112 
113     #endif
114 
115   #endif // not CXX0X
116 
117 #else // EIGEN_NO_STATIC_ASSERT
118 
119   #define EIGEN_STATIC_ASSERT(CONDITION,MSG) eigen_assert((CONDITION) && #MSG);
120 
121 #endif // EIGEN_NO_STATIC_ASSERT
122 
123 
124 // static assertion failing if the type \a TYPE is not a vector type
125 #define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE) \
126   EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime, \
127                       YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX)
128 
129 // static assertion failing if the type \a TYPE is not fixed-size
130 #define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE) \
131   EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime!=Eigen::Dynamic, \
132                       YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR)
133 
134 // static assertion failing if the type \a TYPE is not dynamic-size
135 #define EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(TYPE) \
136   EIGEN_STATIC_ASSERT(TYPE::SizeAtCompileTime==Eigen::Dynamic, \
137                       YOU_CALLED_A_DYNAMIC_SIZE_METHOD_ON_A_FIXED_SIZE_MATRIX_OR_VECTOR)
138 
139 // static assertion failing if the type \a TYPE is not a vector type of the given size
140 #define EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE) \
141   EIGEN_STATIC_ASSERT(TYPE::IsVectorAtCompileTime && TYPE::SizeAtCompileTime==SIZE, \
142                       THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE)
143 
144 // static assertion failing if the type \a TYPE is not a vector type of the given size
145 #define EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(TYPE, ROWS, COLS) \
146   EIGEN_STATIC_ASSERT(TYPE::RowsAtCompileTime==ROWS && TYPE::ColsAtCompileTime==COLS, \
147                       THIS_METHOD_IS_ONLY_FOR_MATRICES_OF_A_SPECIFIC_SIZE)
148 
149 // static assertion failing if the two vector expression types are not compatible (same fixed-size or dynamic size)
150 #define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0,TYPE1) \
151   EIGEN_STATIC_ASSERT( \
152       (int(TYPE0::SizeAtCompileTime)==Eigen::Dynamic \
153     || int(TYPE1::SizeAtCompileTime)==Eigen::Dynamic \
154     || int(TYPE0::SizeAtCompileTime)==int(TYPE1::SizeAtCompileTime)),\
155     YOU_MIXED_VECTORS_OF_DIFFERENT_SIZES)
156 
157 #define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1) \
158      ( \
159         (int(TYPE0::SizeAtCompileTime)==0 && int(TYPE1::SizeAtCompileTime)==0) \
160     || (\
161           (int(TYPE0::RowsAtCompileTime)==Eigen::Dynamic \
162         || int(TYPE1::RowsAtCompileTime)==Eigen::Dynamic \
163         || int(TYPE0::RowsAtCompileTime)==int(TYPE1::RowsAtCompileTime)) \
164       &&  (int(TYPE0::ColsAtCompileTime)==Eigen::Dynamic \
165         || int(TYPE1::ColsAtCompileTime)==Eigen::Dynamic \
166         || int(TYPE0::ColsAtCompileTime)==int(TYPE1::ColsAtCompileTime))\
167        ) \
168      )
169 
170 #ifdef EIGEN2_SUPPORT
171   #define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE) \
172     eigen_assert(!NumTraits<Scalar>::IsInteger);
173 #else
174   #define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE) \
175     EIGEN_STATIC_ASSERT(!NumTraits<TYPE>::IsInteger, THIS_FUNCTION_IS_NOT_FOR_INTEGER_NUMERIC_TYPES)
176 #endif
177 
178 
179 // static assertion failing if it is guaranteed at compile-time that the two matrix expression types have different sizes
180 #define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) \
181   EIGEN_STATIC_ASSERT( \
182      EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1),\
183     YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES)
184 
185 #define EIGEN_STATIC_ASSERT_SIZE_1x1(TYPE) \
186       EIGEN_STATIC_ASSERT((TYPE::RowsAtCompileTime == 1 || TYPE::RowsAtCompileTime == Dynamic) && \
187                           (TYPE::ColsAtCompileTime == 1 || TYPE::ColsAtCompileTime == Dynamic), \
188                           THIS_METHOD_IS_ONLY_FOR_1x1_EXPRESSIONS)
189 
190 #define EIGEN_STATIC_ASSERT_LVALUE(Derived) \
191       EIGEN_STATIC_ASSERT(internal::is_lvalue<Derived>::value, \
192                           THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY)
193 
194 #define EIGEN_STATIC_ASSERT_ARRAYXPR(Derived) \
195       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::traits<Derived>::XprKind, ArrayXpr>::value), \
196                           THIS_METHOD_IS_ONLY_FOR_ARRAYS_NOT_MATRICES)
197 
198 #define EIGEN_STATIC_ASSERT_SAME_XPR_KIND(Derived1, Derived2) \
199       EIGEN_STATIC_ASSERT((internal::is_same<typename internal::traits<Derived1>::XprKind, \
200                                              typename internal::traits<Derived2>::XprKind \
201                                             >::value), \
202                           YOU_CANNOT_MIX_ARRAYS_AND_MATRICES)
203 
204 
205 #endif // EIGEN_STATIC_ASSERT_H
206