From b5be0777f3aa9d16c047f3e09f4bf3648b40bcf2 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 23 Jan 2023 16:56:56 +0200 Subject: [PATCH] Use default members initializers and defaulted special members --- source/geometry/affinetransform.h | 15 +++++++-------- source/geometry/angle.h | 4 ++-- source/geometry/boundingbox.h | 14 ++++---------- source/geometry/boundingsphere.h | 11 +++-------- source/geometry/box.h | 2 +- source/geometry/circle.h | 2 +- source/geometry/compositeshape.h | 2 +- source/geometry/extrudedshape.h | 4 ++-- source/geometry/hypersphere.h | 4 ++-- source/geometry/intersection.h | 2 +- source/geometry/loader.h | 2 +- source/geometry/negation.h | 2 +- source/geometry/quaternion.h | 7 +++++-- source/geometry/ray.h | 8 +++----- source/geometry/rectangle.h | 2 +- source/geometry/shape.h | 4 ++-- source/geometry/surfacepoint.h | 4 ++-- source/geometry/transformedshape.h | 2 +- source/geometry/union.h | 2 +- source/interpolate/polynomial.h | 2 +- source/interpolate/spline.h | 22 +++++----------------- source/linal/dynamicmatrix.h | 6 +++--- source/linal/dynamicvector.h | 4 ++-- source/linal/vector.h | 8 ++++---- 24 files changed, 56 insertions(+), 79 deletions(-) diff --git a/source/geometry/affinetransform.h b/source/geometry/affinetransform.h index f9660c6..a380615 100644 --- a/source/geometry/affinetransform.h +++ b/source/geometry/affinetransform.h @@ -21,14 +21,14 @@ template class AffineTransformOps { protected: - AffineTransformOps() { } + AffineTransformOps() = default; }; template class AffineTransformOps { protected: - AffineTransformOps() { } + AffineTransformOps() = default; public: static AffineTransform rotation(Angle); @@ -38,7 +38,7 @@ template class AffineTransformOps { protected: - AffineTransformOps() { } + AffineTransformOps() = default; public: static AffineTransform rotation(Angle, const LinAl::Vector &); @@ -78,12 +78,11 @@ public: BoundingBox transform(const BoundingBox &) const; }; -template -inline AffineTransform::AffineTransform() -{ - this->matrix = LinAl::Matrix::identity(); -} +template +inline AffineTransform::AffineTransform(): + matrix(LinAl::Matrix::identity()) +{ } template AffineTransform AffineTransform::translation(const LinAl::Vector &v) diff --git a/source/geometry/angle.h b/source/geometry/angle.h index bcd7cca..c9d898c 100644 --- a/source/geometry/angle.h +++ b/source/geometry/angle.h @@ -14,11 +14,11 @@ template class Angle { private: - T value; + T value = T(0); explicit Angle(T v): value(v) { } public: - Angle(): value(0) { } + Angle() = default; template Angle(Angle other): value(other.radians()) { } diff --git a/source/geometry/boundingbox.h b/source/geometry/boundingbox.h index ae9da2b..d24aab9 100644 --- a/source/geometry/boundingbox.h +++ b/source/geometry/boundingbox.h @@ -14,11 +14,11 @@ class BoundingBox private: LinAl::Vector min_pt; LinAl::Vector max_pt; - bool empty; - bool negated; + bool empty = true; + bool negated = false; public: - BoundingBox(); + BoundingBox() = default; BoundingBox(const LinAl::Vector &, const LinAl::Vector &); static BoundingBox negate(const BoundingBox &); @@ -35,18 +35,12 @@ public: bool is_negated() const { return negated; } }; -template -inline BoundingBox::BoundingBox(): - empty(true), - negated(false) -{ } template inline BoundingBox::BoundingBox(const LinAl::Vector &n, const LinAl::Vector &x): min_pt(n), max_pt(x), - empty(false), - negated(false) + empty(false) { for(unsigned i=0; imax_pt[i]) diff --git a/source/geometry/boundingsphere.h b/source/geometry/boundingsphere.h index 66c14d5..311b70e 100644 --- a/source/geometry/boundingsphere.h +++ b/source/geometry/boundingsphere.h @@ -12,11 +12,11 @@ class BoundingSphere { private: LinAl::Vector center; - T radius; - bool empty; + T radius = T(0); + bool empty = true; public: - BoundingSphere(); + BoundingSphere() = default; BoundingSphere(const LinAl::Vector &, T); template @@ -27,11 +27,6 @@ public: bool is_empty() const { return empty; } }; -template -BoundingSphere::BoundingSphere(): - radius(0), - empty(true) -{ } template BoundingSphere::BoundingSphere(const LinAl::Vector &c, T r): diff --git a/source/geometry/box.h b/source/geometry/box.h index b46eb67..14fb8ac 100644 --- a/source/geometry/box.h +++ b/source/geometry/box.h @@ -15,7 +15,7 @@ template class Box: public HyperBox { public: - Box() { } + Box() = default; explicit Box(const LinAl::Vector &d): HyperBox(d) { } Box(T w, T h, T d): HyperBox(LinAl::Vector(w, h, d)) { } diff --git a/source/geometry/circle.h b/source/geometry/circle.h index 3a2cfc2..317c088 100644 --- a/source/geometry/circle.h +++ b/source/geometry/circle.h @@ -15,7 +15,7 @@ template class Circle: public HyperSphere { public: - Circle() { } + Circle() = default; explicit Circle(T r): HyperSphere(r) { } }; diff --git a/source/geometry/compositeshape.h b/source/geometry/compositeshape.h index c472b43..b620636 100644 --- a/source/geometry/compositeshape.h +++ b/source/geometry/compositeshape.h @@ -22,7 +22,7 @@ protected: ShapeArray shapes; unsigned max_isect; - CompositeShape() { } + CompositeShape() = default; CompositeShape(const Shape &, const Shape &); template void init_from_iter_range(const Iter &, const Iter &); diff --git a/source/geometry/extrudedshape.h b/source/geometry/extrudedshape.h index fd4597b..e57ee94 100644 --- a/source/geometry/extrudedshape.h +++ b/source/geometry/extrudedshape.h @@ -17,8 +17,8 @@ template class ExtrudedShape: public Shape { private: - Shape *base; - T length; + Shape *base = 0; + T length = T(1); public: ExtrudedShape(const Shape &, T); diff --git a/source/geometry/hypersphere.h b/source/geometry/hypersphere.h index 13e4278..0b327cf 100644 --- a/source/geometry/hypersphere.h +++ b/source/geometry/hypersphere.h @@ -17,10 +17,10 @@ template class HyperSphere: public Shape { private: - T radius; + T radius = T(1); public: - HyperSphere(): radius(1) { } + HyperSphere() = default; explicit HyperSphere(T); virtual HyperSphere *clone() const; diff --git a/source/geometry/intersection.h b/source/geometry/intersection.h index 24e668d..9bf3416 100644 --- a/source/geometry/intersection.h +++ b/source/geometry/intersection.h @@ -21,7 +21,7 @@ template class Intersection: public CompositeShape > { private: - Intersection() { } + Intersection() = default; public: Intersection(const Shape &, const Shape &); diff --git a/source/geometry/loader.h b/source/geometry/loader.h index 0a9c4b5..58ca8a0 100644 --- a/source/geometry/loader.h +++ b/source/geometry/loader.h @@ -20,7 +20,7 @@ template class DimensionIndependentLoader: public DataFile::Loader { protected: - bool single; + bool single = true; std::list *> shapes; DimensionIndependentLoader(bool = true); diff --git a/source/geometry/negation.h b/source/geometry/negation.h index 60b92e3..c2f96e9 100644 --- a/source/geometry/negation.h +++ b/source/geometry/negation.h @@ -14,7 +14,7 @@ template class Negation: public Shape { private: - Shape *shape; + Shape *shape = 0; public: Negation(const Shape &); diff --git a/source/geometry/quaternion.h b/source/geometry/quaternion.h index 7c03612..8092597 100644 --- a/source/geometry/quaternion.h +++ b/source/geometry/quaternion.h @@ -12,9 +12,12 @@ namespace Geometry { template struct Quaternion { - T a, b, c, d; + T a = T(0); + T b = T(0); + T c = T(0); + T d = T(0); - Quaternion(): a(0), b(0), c(0), d(0) { } + Quaternion() = default; Quaternion(T a_, T b_, T c_, T d_): a(a_), b(b_), c(c_), d(d_) { } static Quaternion one() { return Quaternion(1, 0, 0, 0); } diff --git a/source/geometry/ray.h b/source/geometry/ray.h index d8eb7d8..efe47fe 100644 --- a/source/geometry/ray.h +++ b/source/geometry/ray.h @@ -16,7 +16,7 @@ class Ray private: LinAl::Vector start; LinAl::Vector direction; - T limit; + T limit = T(0); public: Ray(); @@ -30,8 +30,7 @@ public: }; template -inline Ray::Ray(): - limit(0) +inline Ray::Ray() { direction[0] = T(1); } @@ -39,8 +38,7 @@ inline Ray::Ray(): template inline Ray::Ray(const LinAl::Vector &s, const LinAl::Vector &d): start(s), - direction(normalize(d)), - limit(0) + direction(normalize(d)) { } template diff --git a/source/geometry/rectangle.h b/source/geometry/rectangle.h index cbf625c..273becb 100644 --- a/source/geometry/rectangle.h +++ b/source/geometry/rectangle.h @@ -15,7 +15,7 @@ template class Rectangle: public HyperBox { public: - Rectangle() { } + Rectangle() = default; explicit Rectangle(const LinAl::Vector &d): HyperBox(d) { } Rectangle(T w, T h): HyperBox(LinAl::Vector(w, h)) { } diff --git a/source/geometry/shape.h b/source/geometry/shape.h index 920e456..543550d 100644 --- a/source/geometry/shape.h +++ b/source/geometry/shape.h @@ -35,9 +35,9 @@ protected: Coverage coverage; }; - Shape() { } + Shape() = default; public: - virtual ~Shape() { } + virtual ~Shape() = default; virtual Shape *clone() const = 0; diff --git a/source/geometry/surfacepoint.h b/source/geometry/surfacepoint.h index 3b38e1d..e5faed1 100644 --- a/source/geometry/surfacepoint.h +++ b/source/geometry/surfacepoint.h @@ -15,8 +15,8 @@ struct SurfacePoint { LinAl::Vector position; LinAl::Vector normal; - T distance; - bool entry; + T distance = T(0); + bool entry = true; }; template diff --git a/source/geometry/transformedshape.h b/source/geometry/transformedshape.h index 168fbd9..b53f5b8 100644 --- a/source/geometry/transformedshape.h +++ b/source/geometry/transformedshape.h @@ -14,7 +14,7 @@ template class TransformedShape: public Shape { private: - Shape *shape; + Shape *shape = 0; AffineTransform transformation; AffineTransform inverse_trans; diff --git a/source/geometry/union.h b/source/geometry/union.h index 8c02f2b..0a2de4f 100644 --- a/source/geometry/union.h +++ b/source/geometry/union.h @@ -21,7 +21,7 @@ template class Union: public CompositeShape > { private: - Union() { } + Union() = default; public: Union(const Shape &, const Shape &); diff --git a/source/interpolate/polynomial.h b/source/interpolate/polynomial.h index 859eea8..e076204 100644 --- a/source/interpolate/polynomial.h +++ b/source/interpolate/polynomial.h @@ -18,7 +18,7 @@ private: LinAl::Vector coeff; public: - Polynomial() { } + Polynomial() = default; Polynomial(const LinAl::Vector &c): coeff(c) { } template Polynomial(const Polynomial &); diff --git a/source/interpolate/spline.h b/source/interpolate/spline.h index bd606d9..7c5e032 100644 --- a/source/interpolate/spline.h +++ b/source/interpolate/spline.h @@ -63,12 +63,12 @@ public: private: enum { STRIDE = sizeof(Segment)-sizeof(Knot) }; - unsigned short n_segments; - unsigned short capacity; - char *segments; + unsigned short n_segments = 0; + unsigned short capacity = 0; + char *segments = 0; public: - Spline(); + Spline() = default; protected: Spline(const Knot &); public: @@ -92,27 +92,15 @@ public: Value operator()(T) const; }; -template -inline Spline::Spline(): - n_segments(0), - capacity(0), - segments(0) -{ } - template inline Spline::Spline(const Knot &k): - n_segments(0), - capacity(0), segments(new char[sizeof(Knot)]) { new(segments) Knot(k); } template -inline Spline::Spline(const Spline &s): - n_segments(0), - capacity(0), - segments(0) +inline Spline::Spline(const Spline &s) { *this = s; } diff --git a/source/linal/dynamicmatrix.h b/source/linal/dynamicmatrix.h index de4511c..74c9361 100644 --- a/source/linal/dynamicmatrix.h +++ b/source/linal/dynamicmatrix.h @@ -21,9 +21,9 @@ public: typedef T ElementType; private: - unsigned rows_; - unsigned columns_; - T *data; + unsigned rows_ = 0; + unsigned columns_ = 0; + T *data = 0; public: DynamicMatrix(unsigned, unsigned); diff --git a/source/linal/dynamicvector.h b/source/linal/dynamicvector.h index a5379d8..7016a74 100644 --- a/source/linal/dynamicvector.h +++ b/source/linal/dynamicvector.h @@ -29,8 +29,8 @@ public: typedef T ElementType; private: - unsigned size_; - T *data; + unsigned size_ = 0; + T *data = 0; public: DynamicVector(unsigned); diff --git a/source/linal/vector.h b/source/linal/vector.h index 610ac21..1587411 100644 --- a/source/linal/vector.h +++ b/source/linal/vector.h @@ -19,7 +19,7 @@ private: T data[N]; protected: - VectorComponents() { } + VectorComponents() = default; public: T &operator[](unsigned i) { return data[i]; } @@ -33,7 +33,7 @@ public: T x, y; protected: - VectorComponents() { } + VectorComponents() = default; public: T &operator[](unsigned i) { return *(&x+i); } @@ -47,7 +47,7 @@ public: T x, y, z; protected: - VectorComponents() { } + VectorComponents() = default; public: T &operator[](unsigned i) { return *(&x+i); } @@ -61,7 +61,7 @@ public: T x, y, z, w; protected: - VectorComponents() { } + VectorComponents() = default; public: T &operator[](unsigned i) { return *(&x+i); } -- 2.45.2