Skip to content

Commit e532a62

Browse files
committed
constexpr constructors, #pragma once
1 parent d46c09d commit e532a62

17 files changed

+490
-1013
lines changed

src/modm/math/geometry/quaternion.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
namespace modm
2121
{
2222
// forward declaration
23-
template<class T, uint8_t N>
23+
template<class T, std::size_t N>
2424
class Vector;
2525

26-
template<class T, uint8_t ROWS, uint8_t COLUMNS>
26+
template<class T, std::size_t ROWS, std::size_t COLUMNS>
2727
class Matrix;
2828

2929
/**

src/modm/math/geometry/vector.hpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
*/
1313
// ----------------------------------------------------------------------------
1414

15-
#ifndef MODM_VECTOR_HPP
16-
#define MODM_VECTOR_HPP
15+
#pragma once
1716

1817
#include <cmath>
1918
#include <stdint.h>
@@ -24,7 +23,8 @@
2423
namespace modm
2524
{
2625
// forward declaration
27-
template<typename T, uint8_t W, uint8_t H> class Matrix;
26+
template<typename T, std::size_t, std::size_t>
27+
class Matrix;
2828

2929
/**
3030
* \brief Class for handling common point operations
@@ -54,11 +54,11 @@ namespace modm
5454
* \ingroup modm_math_geometry
5555
* \author Niklas Hauser
5656
*/
57-
template<typename T, uint8_t N>
57+
template<typename T, std::size_t N>
5858
class Vector
5959
{
6060
public:
61-
Vector();
61+
Vector() = default;
6262
Vector(const T *ptData);
6363

6464
Vector(const Matrix<T, N, 1> &rhs);
@@ -71,8 +71,8 @@ namespace modm
7171
bool operator > (const Vector &rhs) const;
7272
bool operator >= (const Vector &rhs) const;
7373

74-
const T& operator [] (uint8_t index) const;
75-
T& operator [] (uint8_t index);
74+
const T& operator [] (std::size_t index) const;
75+
T& operator [] (std::size_t index);
7676

7777
T* ptr();
7878
const T* ptr() const;
@@ -104,21 +104,21 @@ namespace modm
104104
asTransposedMatrix() const;
105105

106106
public:
107-
static inline uint8_t
107+
static inline std::size_t
108108
getSize();
109109

110-
T coords[N];
110+
T coords[N] = {0};
111111
};
112112

113-
template< typename T, uint8_t N >
113+
template< typename T, std::size_t N >
114114
struct detail::MakeSigned< Vector<T, N> >
115115
{ using type = Vector< SignedType<T>, N >; };
116116

117-
template< typename T, uint8_t N >
117+
template< typename T, std::size_t N >
118118
struct detail::MakeUnsigned< Vector<T, N> >
119119
{ using type = Vector< UnsignedType<T>, N >; };
120120

121-
template< typename T, uint8_t N >
121+
template< typename T, std::size_t N >
122122
struct detail::WideType< Vector<T, N> >
123123
{ using type = Vector< WideType<T>, N >; };
124124
}
@@ -146,6 +146,4 @@ namespace modm
146146
#include "vector1.hpp"
147147
#include "vector2.hpp"
148148
#include "vector3.hpp"
149-
#include "vector4.hpp"
150-
151-
#endif // MODM_VECTOR_HPP
149+
#include "vector4.hpp"

src/modm/math/geometry/vector1.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
*/
1212
// ----------------------------------------------------------------------------
1313

14-
#ifndef MODM_VECTOR1_HPP
15-
#define MODM_VECTOR1_HPP
14+
#pragma once
1615

1716
#include <cmath>
1817
#include <cstdlib>
@@ -36,9 +35,9 @@ namespace modm
3635
class Vector<T, 1>
3736
{
3837
public:
39-
Vector();
40-
Vector(T inX);
41-
Vector(const Matrix<T, 1, 1> &rhs);
38+
constexpr Vector() = default;
39+
constexpr Vector(T inX);
40+
constexpr Vector(const Matrix<T, 1, 1> &rhs);
4241

4342
inline void
4443
set(const T& x);
@@ -58,8 +57,8 @@ namespace modm
5857
bool operator > (const Vector &rhs) const;
5958
bool operator >= (const Vector &rhs) const;
6059

61-
const T& operator [] (uint8_t index) const;
62-
T& operator [] (uint8_t index);
60+
const T& operator [] (std::size_t index) const;
61+
T& operator [] (std::size_t index);
6362
T* ptr();
6463
const T* ptr() const;
6564

@@ -88,7 +87,7 @@ namespace modm
8887
bool hasInf() const;
8988

9089
public:
91-
T x;
90+
T x = 0;
9291

9392
public:
9493
#ifndef __DOXYGEN__
@@ -109,5 +108,3 @@ namespace modm
109108
}
110109

111110
#include "vector1_impl.hpp"
112-
113-
#endif // MODM_VECTOR1_HPP

src/modm/math/geometry/vector1_impl.hpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,30 @@
1111
*/
1212
// ----------------------------------------------------------------------------
1313

14-
#ifndef MODM_VECTOR1_HPP
15-
#error "Don't include this file directly, use 'vector1.hpp' instead!"
16-
#endif
14+
#pragma once
15+
#include "vector1.hpp"
1716

18-
// ----------------------------------------------------------------------------
1917
template<typename T>
20-
modm::Vector<T, 1>::Vector() :
21-
x()
22-
{
23-
}
24-
25-
template<typename T>
26-
modm::Vector<T, 1>::Vector(T inX) :
18+
constexpr modm::Vector<T, 1>::Vector(T inX) :
2719
x(inX)
28-
{
29-
}
20+
{}
3021

3122
template<typename T>
32-
modm::Vector<T, 1>::Vector(const modm::Matrix<T, 1, 1> &rhs) :
23+
constexpr modm::Vector<T, 1>::Vector(const modm::Matrix<T, 1, 1> &rhs) :
3324
x(*reinterpret_cast<const T*>(&rhs))
34-
{
35-
}
25+
{}
3626

3727
// ----------------------------------------------------------------------------
3828
template<typename T>
3929
void
4030
modm::Vector<T, 1>::set(const T& value)
41-
{
42-
this->x = value;
43-
}
31+
{ this->x = value; }
4432

4533
// ----------------------------------------------------------------------------
4634
template<typename T>
4735
void
4836
modm::Vector<T, 1>::setX(const T& value)
49-
{
50-
this->x = value;
51-
}
37+
{ this->x = value; }
5238

5339
// ----------------------------------------------------------------------------
5440
template<typename T>
@@ -118,14 +104,14 @@ modm::Vector<T, 1>::operator >= (const modm::Vector<T, 1> &rhs) const
118104
// ----------------------------------------------------------------------------
119105
template<typename T>
120106
const T&
121-
modm::Vector<T, 1>::operator [] (uint8_t index) const
107+
modm::Vector<T, 1>::operator [] (std::size_t index) const
122108
{
123109
return reinterpret_cast<const T*>(this)[index];
124110
}
125111

126112
template<typename T>
127113
T&
128-
modm::Vector<T, 1>::operator [] (uint8_t index)
114+
modm::Vector<T, 1>::operator [] (std::size_t index)
129115
{
130116
return reinterpret_cast<T*>(this)[index];
131117
}

src/modm/math/geometry/vector2.hpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
*/
1313
// ----------------------------------------------------------------------------
1414

15-
#ifndef MODM_VECTOR2_HPP
16-
#define MODM_VECTOR2_HPP
15+
#pragma once
1716

1817
#include <cmath>
1918
#include <stdint.h>
@@ -62,20 +61,13 @@ namespace modm
6261
typedef typename GeometricTraits<T>::FloatType FloatType;
6362

6463
public:
65-
/**
66-
* \brief Default-Constructor
67-
*
68-
* Creates a Vector with coordinates (0, 0).
69-
*/
70-
Vector();
71-
72-
Vector(const T& inX, const T& inY);
73-
74-
Vector(const Vector<T, 1> &inX, const Vector<T, 1> &inY);
75-
Vector(const T &inX, const Vector<T, 1> &inY);
76-
Vector(const Vector<T, 1> &inX, const T &inY);
77-
explicit Vector(T inVal);
78-
Vector(const Matrix<T, 2, 1> &rhs);
64+
constexpr Vector() = default;
65+
constexpr Vector(const T& inX, const T& inY);
66+
constexpr Vector(const Vector<T, 1> &inX, const Vector<T, 1> &inY);
67+
constexpr Vector(const T &inX, const Vector<T, 1> &inY);
68+
constexpr Vector(const Vector<T, 1> &inX, const T &inY);
69+
constexpr explicit Vector(T inVal);
70+
constexpr Vector(const Matrix<T, 2, 1> &rhs);
7971

8072
inline void
8173
setX(const T& value);
@@ -239,15 +231,15 @@ namespace modm
239231
bool operator > (const Vector &rhs) const;
240232
bool operator >= (const Vector &rhs) const;
241233

242-
const T& operator [] (uint8_t index) const;
243-
T& operator [] (uint8_t index);
234+
const T& operator [] (std::size_t index) const;
235+
T& operator [] (std::size_t index);
244236

245237
T* ptr();
246238
const T* ptr() const;
247239

248240
Vector operator - () const;
249-
Vector operator - (const Vector &rhs) const;
250-
Vector operator + (const Vector &rhs) const;
241+
constexpr Vector operator - (const Vector &rhs) const;
242+
constexpr Vector operator + (const Vector &rhs) const;
251243
T operator * (const Vector &rhs) const;
252244
T operator ^ (const Vector &rhs) const;
253245
Vector operator * (float rhs) const;
@@ -295,8 +287,8 @@ namespace modm
295287
#endif
296288

297289
public:
298-
T x;
299-
T y;
290+
T x = 0;
291+
T y = 0;
300292

301293
protected:
302294
template<typename U>
@@ -379,6 +371,4 @@ namespace modm
379371
}
380372
}
381373

382-
#include "vector2_impl.hpp"
383-
384-
#endif // MODM_VECTOR2_HPP
374+
#include "vector2_impl.hpp"

0 commit comments

Comments
 (0)