Skip to content

Commit cab8d18

Browse files
committed
binary operators: added constexpr + pass by copy of simple arguments
1 parent 1446961 commit cab8d18

10 files changed

+108
-111
lines changed

src/modm/math/geometry/vector1.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ namespace modm
6060
T* ptr();
6161
const T* ptr() const;
6262

63-
Vector operator - () const;
64-
Vector operator + (const Vector &rhs) const;
65-
Vector operator - (const Vector &rhs) const;
66-
T operator * (const Vector &rhs) const;
67-
Vector operator * (const T &rhs) const;
68-
Vector operator / (const T &rhs) const;
63+
constexpr Vector operator - () const;
64+
constexpr Vector operator + (const Vector &rhs) const;
65+
constexpr Vector operator - (const Vector &rhs) const;
66+
constexpr T operator * (const Vector &rhs) const;
67+
constexpr Vector operator * (T rhs) const;
68+
constexpr Vector operator / (T rhs) const;
6969

7070
Vector& operator += (const Vector &rhs);
7171
Vector& operator -= (const Vector &rhs);
72-
Vector& operator *= (const T &rhs);
73-
Vector& operator /= (const T &rhs);
72+
Vector& operator *= (T rhs);
73+
Vector& operator /= (T rhs);
7474

7575
T getLength() const;
7676
WideType getLengthSquared() const;

src/modm/math/geometry/vector1_impl.hpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,43 @@ modm::Vector<T, 1>::ptr() const
5858

5959
// ----------------------------------------------------------------------------
6060
template<typename T>
61-
modm::Vector<T, 1>
61+
constexpr modm::Vector<T, 1>
62+
modm::Vector<T, 1>::operator - () const
63+
{
64+
return Vector<T, 1>(-x);
65+
}
66+
67+
template<typename T>
68+
constexpr modm::Vector<T, 1>
6269
modm::Vector<T, 1>::operator + (const modm::Vector<T, 1> &rhs) const
6370
{
6471
return modm::Vector<T, 1>(x+rhs.x);
6572
}
6673

6774
template<typename T>
68-
modm::Vector<T, 1>
75+
constexpr modm::Vector<T, 1>
6976
modm::Vector<T, 1>::operator - (const modm::Vector<T, 1> &rhs) const
7077
{
7178
return modm::Vector<T, 1>(x-rhs.x);
7279
}
7380

7481
template<typename T>
75-
T
82+
constexpr T
7683
modm::Vector<T, 1>::operator * (const modm::Vector<T, 1> &rhs) const
7784
{
7885
return x*rhs.x;
7986
}
8087

8188
template<typename T>
82-
modm::Vector<T, 1>
83-
modm::Vector<T, 1>::operator * (const T &rhs) const
89+
constexpr modm::Vector<T, 1>
90+
modm::Vector<T, 1>::operator * (T rhs) const
8491
{
8592
return modm::Vector<T, 1>(x*rhs);
8693
}
8794

8895
template<typename T>
89-
modm::Vector<T, 1>
90-
modm::Vector<T, 1>::operator / (const T &rhs) const
96+
constexpr modm::Vector<T, 1>
97+
modm::Vector<T, 1>::operator / (T rhs) const
9198
{
9299
return modm::Vector<T, 1>(x/rhs);
93100
}
@@ -111,28 +118,20 @@ modm::Vector<T, 1>::operator -= (const modm::Vector<T, 1> &rhs)
111118

112119
template<typename T>
113120
modm::Vector<T, 1>&
114-
modm::Vector<T, 1>::operator *= (const T &rhs)
121+
modm::Vector<T, 1>::operator *= (T rhs)
115122
{
116123
x *= rhs;
117124
return *this;
118125
}
119126

120127
template<typename T>
121128
modm::Vector<T, 1>&
122-
modm::Vector<T, 1>::operator /= (const T &rhs)
129+
modm::Vector<T, 1>::operator /= (T rhs)
123130
{
124131
x /= rhs;
125132
return *this;
126133
}
127134

128-
// ----------------------------------------------------------------------------
129-
template<typename T>
130-
modm::Vector<T, 1>
131-
modm::Vector<T, 1>::operator - () const
132-
{
133-
return Vector<T, 1>(-x);
134-
}
135-
136135
// ----------------------------------------------------------------------------
137136
template<typename T>
138137
T

src/modm/math/geometry/vector2.hpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,19 @@ namespace modm
207207
T* ptr();
208208
const T* ptr() const;
209209

210-
Vector operator - () const;
211-
constexpr Vector operator - (const Vector &rhs) const;
210+
constexpr Vector operator - () const;
212211
constexpr Vector operator + (const Vector &rhs) const;
213-
T operator * (const Vector &rhs) const;
214-
T operator ^ (const Vector &rhs) const;
215-
Vector operator * (float rhs) const;
216-
Vector operator / (float rhs) const;
212+
constexpr Vector operator - (const Vector &rhs) const;
213+
constexpr T operator * (const Vector &rhs) const;
214+
constexpr T operator ^ (const Vector &rhs) const;
215+
// TODO Why is this float for 2D-vector
216+
constexpr Vector operator * (float rhs) const;
217+
constexpr Vector operator / (float rhs) const;
217218

218219
Vector& operator += (const Vector &rhs);
219220
Vector& operator -= (const Vector &rhs);
220-
Vector& operator *= (const T &rhs);
221-
Vector& operator /= (const T &rhs);
221+
Vector& operator *= (T rhs);
222+
Vector& operator /= (T rhs);
222223
Vector& operator ~ ();
223224

224225
Matrix<T, 2, 1>&

src/modm/math/geometry/vector2_impl.hpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,12 @@ modm::Vector<T, 2>::ptr() const
232232

233233
// ----------------------------------------------------------------------------
234234
template<typename T>
235-
modm::Vector<T, 2>
235+
constexpr modm::Vector<T, 2>
236236
modm::Vector<T, 2>::operator - () const
237237
{
238238
return Vector<T, 2>(-x, -y);
239239
}
240240

241-
// ----------------------------------------------------------------------------
242241
template<typename T>
243242
constexpr modm::Vector<T, 2>
244243
modm::Vector<T, 2>::operator - (const modm::Vector<T, 2> &rhs) const
@@ -254,29 +253,29 @@ modm::Vector<T, 2>::operator + (const modm::Vector<T, 2> &rhs) const
254253
}
255254

256255
template<typename T>
257-
T
256+
constexpr T
258257
modm::Vector<T, 2>::operator * (const modm::Vector<T, 2> &rhs) const
259258
{
260259
return (x * rhs.x + y * rhs.y);
261260
}
262261

263262
template<typename T>
264-
T
263+
constexpr T
265264
modm::Vector<T, 2>::operator ^ (const modm::Vector<T, 2> &rhs) const
266265
{
267266
return (x * rhs.y - y * rhs.x);
268267
}
269268

270269
template<typename T>
271-
modm::Vector<T, 2>
270+
constexpr modm::Vector<T, 2>
272271
modm::Vector<T, 2>::operator * (float scale) const
273272
{
274273
return Vector<T, 2>(GeometricTraits<T>::round(x * scale),
275274
GeometricTraits<T>::round(y * scale));
276275
}
277276

278277
template<typename T>
279-
modm::Vector<T, 2>
278+
constexpr modm::Vector<T, 2>
280279
modm::Vector<T, 2>::operator / (float scale) const
281280
{
282281
return Vector<T, 2>(GeometricTraits<T>::round(x / scale),
@@ -306,7 +305,7 @@ modm::Vector<T, 2>::operator -= (const modm::Vector<T, 2> &rhs)
306305

307306
template<typename T>
308307
modm::Vector<T, 2>&
309-
modm::Vector<T, 2>::operator *= (const T &rhs)
308+
modm::Vector<T, 2>::operator *= (T rhs)
310309
{
311310
x *= rhs;
312311
y *= rhs;
@@ -316,7 +315,7 @@ modm::Vector<T, 2>::operator *= (const T &rhs)
316315

317316
template<typename T>
318317
modm::Vector<T, 2>&
319-
modm::Vector<T, 2>::operator /= (const T &rhs)
318+
modm::Vector<T, 2>::operator /= (T rhs)
320319
{
321320
x /= rhs;
322321
y /= rhs;

src/modm/math/geometry/vector3.hpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,18 @@ namespace modm
9292
T* ptr();
9393
const T* ptr() const;
9494

95-
Vector operator - () const;
96-
Vector operator + (const Vector &rhs) const;
97-
Vector operator - (const Vector &rhs) const;
98-
T operator * (const Vector &rhs) const;
99-
Vector operator ^ (const Vector &rhs) const;
100-
Vector operator * (const T &rhs) const;
101-
Vector operator / (const T &rhs) const;
95+
constexpr Vector operator - () const;
96+
constexpr Vector operator + (const Vector &rhs) const;
97+
constexpr Vector operator - (const Vector &rhs) const;
98+
constexpr T operator * (const Vector &rhs) const;
99+
constexpr Vector operator ^ (const Vector &rhs) const;
100+
constexpr Vector operator * (T rhs) const;
101+
constexpr Vector operator / (T rhs) const;
102102

103103
Vector& operator += (const Vector &rhs);
104104
Vector& operator -= (const Vector &rhs);
105-
Vector& operator *= (const T &rhs);
106-
Vector& operator /= (const T &rhs);
105+
Vector& operator *= (T rhs);
106+
Vector& operator /= (T rhs);
107107

108108
float getLength() const;
109109
float getLengthSquared() const;

src/modm/math/geometry/vector3_impl.hpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -63,43 +63,50 @@ modm::Vector<T, 3>::ptr() const
6363

6464
// ----------------------------------------------------------------------------
6565
template<typename T>
66-
modm::Vector<T, 3>
66+
constexpr modm::Vector<T, 3>
67+
modm::Vector<T, 3>::operator - () const
68+
{
69+
return modm::Vector<T, 3>(-x, -y, -z);
70+
}
71+
72+
template<typename T>
73+
constexpr modm::Vector<T, 3>
6774
modm::Vector<T, 3>::operator + (const modm::Vector<T, 3> &rhs) const
6875
{
6976
return modm::Vector<T, 3>(x+rhs.x, y+rhs.y, z+rhs.z);
7077
}
7178

7279
template<typename T>
73-
modm::Vector<T, 3>
80+
constexpr modm::Vector<T, 3>
7481
modm::Vector<T, 3>::operator - (const modm::Vector<T, 3> &rhs) const
7582
{
7683
return modm::Vector<T, 3>(x-rhs.x, y-rhs.y, z-rhs.z);
7784
}
7885

7986
template<typename T>
80-
T
87+
constexpr T
8188
modm::Vector<T, 3>::operator * (const modm::Vector<T, 3> &rhs) const
8289
{
8390
return x*rhs.x + y*rhs.y + z*rhs.z;
8491
}
8592

8693
template<typename T>
87-
modm::Vector<T, 3>
94+
constexpr modm::Vector<T, 3>
8895
modm::Vector<T, 3>::operator ^ (const modm::Vector<T, 3> &rhs) const
8996
{
9097
return modm::Vector<T, 3>(y*rhs.z-z*rhs.y, z*rhs.x-x*rhs.z, x*rhs.y-y*rhs.x);
9198
}
9299

93100
template<typename T>
94-
modm::Vector<T, 3>
95-
modm::Vector<T, 3>::operator * (const T &rhs) const
101+
constexpr modm::Vector<T, 3>
102+
modm::Vector<T, 3>::operator * (T rhs) const
96103
{
97104
return modm::Vector<T, 3>(x*rhs, y*rhs, z*rhs);
98105
}
99106

100107
template<typename T>
101-
modm::Vector<T, 3>
102-
modm::Vector<T, 3>::operator / (const T &rhs) const
108+
constexpr modm::Vector<T, 3>
109+
modm::Vector<T, 3>::operator / (T rhs) const
103110
{
104111
return modm::Vector<T, 3>(x/rhs, y/rhs, z/rhs);
105112
}
@@ -129,7 +136,7 @@ modm::Vector<T, 3>::operator -= (const modm::Vector<T, 3> &rhs)
129136

130137
template<typename T>
131138
modm::Vector<T, 3>&
132-
modm::Vector<T, 3>::operator *= (const T &rhs)
139+
modm::Vector<T, 3>::operator *= (T rhs)
133140
{
134141
x *= rhs;
135142
y *= rhs;
@@ -140,7 +147,7 @@ modm::Vector<T, 3>::operator *= (const T &rhs)
140147

141148
template<typename T>
142149
modm::Vector<T, 3>&
143-
modm::Vector<T, 3>::operator /= (const T &rhs)
150+
modm::Vector<T, 3>::operator /= (T rhs)
144151
{
145152
x /= rhs;
146153
y /= rhs;
@@ -149,14 +156,6 @@ modm::Vector<T, 3>::operator /= (const T &rhs)
149156
return *this;
150157
}
151158

152-
// ----------------------------------------------------------------------------
153-
template<typename T>
154-
modm::Vector<T, 3>
155-
modm::Vector<T, 3>::operator - () const
156-
{
157-
return modm::Vector<T, 3>(-x, -y, -z);
158-
}
159-
160159
// ----------------------------------------------------------------------------
161160
template<typename T>
162161
float

src/modm/math/geometry/vector4.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,17 @@ namespace modm
134134
T* ptr();
135135
const T* ptr() const;
136136

137-
Vector operator - () const;
138-
Vector operator + (const Vector &rhs) const;
139-
Vector operator - (const Vector &rhs) const;
140-
T operator * (const Vector &rhs) const;
141-
Vector operator * (const T &rhs) const;
142-
Vector operator / (const T &rhs) const;
137+
constexpr Vector operator - () const;
138+
constexpr Vector operator + (const Vector &rhs) const;
139+
constexpr Vector operator - (const Vector &rhs) const;
140+
constexpr T operator * (const Vector &rhs) const;
141+
constexpr Vector operator * (T rhs) const;
142+
constexpr Vector operator / (T rhs) const;
143143

144144
Vector& operator += (const Vector &rhs);
145145
Vector& operator -= (const Vector &rhs);
146-
Vector& operator *= (const T &rhs);
147-
Vector& operator /= (const T &rhs);
146+
Vector& operator *= (T rhs);
147+
Vector& operator /= (T rhs);
148148

149149
float getLength() const;
150150
float getLengthSquared() const;

0 commit comments

Comments
 (0)