]> Dogcows Code - chaz/yoink/blob - src/moof/cml/vector/class_ops.h
fixes for newer versions of g++
[chaz/yoink] / src / moof / cml / vector / class_ops.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 Vector class operators.
11 */
12
13 #ifndef vector_class_ops_h
14 #define vector_class_ops_h
15
16 #if defined(_MSC_VER) && _MSC_VER < 1400
17 #pragma warning(disable:4003)
18 // XXX Horrible hack to turn off warnings about "not enough actual params"
19 // for the macros below.
20 #endif
21
22 /* XXX HACK!!! This is a hack to resize in the assign() functions only when
23 * auto resizing is turned off.
24 */
25 #if !defined(CML_VECTOR_RESIZE_ON_ASSIGNMENT)
26 #define _DO_VECTOR_SET_RESIZE(_N_) cml::et::detail::Resize(*this,_N_)
27 #else
28 #define _DO_VECTOR_SET_RESIZE(_N_)
29 #endif
30
31 /** Set a vector from 2 values. */
32 #define CML_ASSIGN_VEC_2 \
33 vector_type& \
34 set(ELEMENT_ARG_TYPE e0, ELEMENT_ARG_TYPE e1) { \
35 _DO_VECTOR_SET_RESIZE(2); \
36 /* This is overkill, but simplifies size checking: */ \
37 value_type v[] = {e0,e1}; \
38 typedef et::OpAssign<Element,Element> OpT; \
39 cml::vector< const value_type, external<2> > src(v); \
40 et::UnrollAssignment<OpT>(*this,src); \
41 return *this; \
42 }
43
44 /** Set a vector from 3 values. */
45 #define CML_ASSIGN_VEC_3 \
46 vector_type& \
47 set( \
48 ELEMENT_ARG_TYPE e0, \
49 ELEMENT_ARG_TYPE e1, \
50 ELEMENT_ARG_TYPE e2 \
51 ) \
52 { \
53 _DO_VECTOR_SET_RESIZE(3); \
54 /* This is overkill, but simplifies size checking: */ \
55 value_type v[] = {e0,e1,e2}; \
56 typedef et::OpAssign<Element,Element> OpT; \
57 cml::vector< const value_type, external<3> > src(v); \
58 et::UnrollAssignment<OpT>(*this,src); \
59 return *this; \
60 }
61
62 /** Create a vector from 4 values. */
63 #define CML_ASSIGN_VEC_4 \
64 vector_type& \
65 set( \
66 ELEMENT_ARG_TYPE e0, \
67 ELEMENT_ARG_TYPE e1, \
68 ELEMENT_ARG_TYPE e2, \
69 ELEMENT_ARG_TYPE e3 \
70 ) \
71 { \
72 _DO_VECTOR_SET_RESIZE(4); \
73 /* This is overkill, but simplifies size checking: */ \
74 value_type v[] = {e0,e1,e2,e3}; \
75 typedef et::OpAssign<Element,Element> OpT; \
76 cml::vector< const value_type, external<4> > src(v); \
77 et::UnrollAssignment<OpT>(*this,src); \
78 return *this; \
79 }
80
81
82 /** Create a vector from 2 values. */
83 #define CML_CONSTRUCT_VEC_2(_add_) \
84 vector(ELEMENT_ARG_TYPE e0, ELEMENT_ARG_TYPE e1) _add_ { \
85 set(e0,e1); \
86 }
87
88 /** Create a vector from 3 values. */
89 #define CML_CONSTRUCT_VEC_3(_add_) \
90 vector( \
91 ELEMENT_ARG_TYPE e0, \
92 ELEMENT_ARG_TYPE e1, \
93 ELEMENT_ARG_TYPE e2 \
94 ) _add_ \
95 { \
96 set(e0,e1,e2); \
97 }
98
99 /** Create a vector from 4 values. */
100 #define CML_CONSTRUCT_VEC_4(_add_) \
101 vector( \
102 ELEMENT_ARG_TYPE e0, \
103 ELEMENT_ARG_TYPE e1, \
104 ELEMENT_ARG_TYPE e2, \
105 ELEMENT_ARG_TYPE e3 \
106 ) _add_ \
107 { \
108 set(e0,e1,e2,e3); \
109 }
110
111 /** Create a (fixed-size) N vector from an N-1-vector and a scalar. */
112 #define CML_CONSTRUCT_FROM_SUBVEC(_add_) \
113 vector( \
114 const subvector_type& s, \
115 ELEMENT_ARG_TYPE e \
116 ) _add_ \
117 { \
118 _DO_VECTOR_SET_RESIZE(s.size()+1); \
119 for(size_t i = 0; i < s.size(); ++ i) \
120 (*this)[i] = s[i]; \
121 (*this)[s.size()] = e; \
122 }
123
124 /** Copy-construct a vector from a fixed-size array of values. */
125 #define CML_VEC_COPY_FROM_FIXED_ARRAY(_N_,_add_) \
126 vector(const value_type v[_N_]) _add_ { \
127 typedef et::OpAssign<Element,Element> OpT; \
128 cml::vector< const value_type, external<_N_> > src(v); \
129 et::UnrollAssignment<OpT>(*this,src); \
130 }
131
132 /** Copy-construct a vector from a runtime-sized array of values. */
133 #define CML_VEC_COPY_FROM_ARRAY(_add_) \
134 vector(const value_type* const v, size_t N) _add_ { \
135 typedef et::OpAssign<Element,Element> OpT; \
136 cml::vector<const value_type, external<> > src(v,N); \
137 et::UnrollAssignment<OpT>(*this,src); \
138 }
139
140 /** Copy-construct a vector.
141 *
142 * @internal This is required for GCC4, since it won't elide the default
143 * copy constructor.
144 */
145 #define CML_VEC_COPY_FROM_VECTYPE(_add_) \
146 vector(const vector_type& v) _add_ { \
147 typedef et::OpAssign<Element,Element> OpT; \
148 et::UnrollAssignment<OpT>(*this,v); \
149 }
150
151 /** Construct from an arbitrary vector.
152 *
153 * @param v the vector to copy from.
154 */
155 #define CML_VEC_COPY_FROM_VEC \
156 template<typename E, class AT> \
157 vector(const vector<E,AT>& m) { \
158 typedef et::OpAssign<Element,E> OpT; \
159 et::UnrollAssignment<OpT>(*this,m); \
160 }
161
162 /** Construct from a vector expression.
163 *
164 * @param expr the expression to copy from.
165 */
166 #define CML_VEC_COPY_FROM_VECXPR \
167 template<class XprT> \
168 vector(VECXPR_ARG_TYPE e) { \
169 /* Verify that a promotion exists at compile time: */ \
170 typedef typename et::VectorPromote< \
171 vector_type, typename XprT::result_type>::type result_type; \
172 typedef typename XprT::value_type src_value_type; \
173 typedef et::OpAssign<Element,src_value_type> OpT; \
174 et::UnrollAssignment<OpT>(*this,e); \
175 }
176
177 /** Assign from the same vector type.
178 *
179 * @param v the vector to copy from.
180 */
181 #define CML_VEC_ASSIGN_FROM_VECTYPE \
182 vector_type& operator=(const vector_type& v) { \
183 typedef et::OpAssign<Element,Element> OpT; \
184 et::UnrollAssignment<OpT>(*this,v); \
185 return *this; \
186 }
187
188 /** Assign this vector from another using the given elementwise op.
189 *
190 * This allows assignment from arbitrary vector types.
191 *
192 * @param _op_ the operator (e.g. +=)
193 * @param _op_name_ the op functor (e.g. et::OpAssign)
194 */
195 #define CML_VEC_ASSIGN_FROM_VEC(_op_, _op_name_) \
196 template<typename E, class AT> vector_type& \
197 operator _op_ (const cml::vector<E,AT>& m) { \
198 typedef _op_name_ <Element,E> OpT; \
199 cml::et::UnrollAssignment<OpT>(*this,m); \
200 return *this; \
201 }
202
203 /** Declare a function to assign this vector from a vector expression.
204 *
205 * @param _op_ the operator (e.g. +=)
206 * @param _op_name_ the op functor (e.g. et::OpAssign)
207 */
208 #define CML_VEC_ASSIGN_FROM_VECXPR(_op_, _op_name_) \
209 template<class XprT> vector_type& \
210 operator _op_ (VECXPR_ARG_TYPE e) { \
211 /* Verify that a promotion exists at compile time: */ \
212 typedef typename et::VectorPromote< \
213 vector_type, typename XprT::result_type>::type result_type; \
214 typedef typename XprT::value_type src_value_type; \
215 typedef _op_name_ <Element,src_value_type> OpT; \
216 cml::et::UnrollAssignment<OpT>(*this,e); \
217 return *this; \
218 }
219
220 /** Declare a function to assign this vector from a scalar.
221 *
222 * @param _op_ the operator (e.g. *=)
223 * @param _op_name_ the op functor (e.g. et::OpAssign)
224 *
225 * @internal This shouldn't be used for ops, like +=, which aren't
226 * defined in vector algebra.
227 */
228 #define CML_VEC_ASSIGN_FROM_SCALAR(_op_, _op_name_) \
229 vector_type& operator _op_ (ELEMENT_ARG_TYPE s) { \
230 typedef _op_name_ <Element,Element> OpT; \
231 cml::et::UnrollAssignment<OpT>(*this,s); \
232 return *this; \
233 }
234
235 #endif
236
237 // -------------------------------------------------------------------------
238 // vim:ft=cpp
This page took 0.044436 seconds and 4 git commands to generate.