From 31d23625c6d967bc1b12b2a66257d25bf6b30a9e Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Wed, 20 Nov 2024 11:18:35 -0500 Subject: [PATCH] Fix variant unit tests Signed-off-by: Geoff Hutchison --- avogadro/core/variant-inline.h | 64 ++++++++++++++-------------------- avogadro/core/variant.h | 14 ++++---- avogadro/quantumio/orca.cpp | 5 +-- 3 files changed, 34 insertions(+), 49 deletions(-) diff --git a/avogadro/core/variant-inline.h b/avogadro/core/variant-inline.h index 7ad0d1672b..8777fa40cd 100644 --- a/avogadro/core/variant-inline.h +++ b/avogadro/core/variant-inline.h @@ -8,7 +8,6 @@ #include "variant.h" -#include #include namespace Avogadro::Core { @@ -22,7 +21,7 @@ inline Variant::Variant(double x, double y, double z) : m_type(Vector) } template -inline Variant::Variant(const T v) : m_type(Null) +inline Variant::Variant(T v) : m_type(Null) { setValue(v); } @@ -41,14 +40,6 @@ inline Variant::Variant(const MatrixXf& v) : m_type(Matrix) m_value.matrix = m; } -template <> -inline Variant::Variant(const MatrixX& v) : m_type(Matrix) -{ - MatrixX* m = new MatrixX(v.rows(), v.cols()); - *m = v; - m_value.matrix = m; -} - template <> inline Variant::Variant(const Vector3& v) : m_type(Vector) { @@ -101,7 +92,7 @@ inline bool Variant::setValue(double x, double y, double z) } template -inline bool Variant::setValue(const T v) +inline bool Variant::setValue(T v) { AVO_UNUSED(v); @@ -121,7 +112,7 @@ inline bool Variant::setValue(const T v) } template <> -inline bool Variant::setValue(const bool v) +inline bool Variant::setValue(bool v) { clear(); @@ -132,7 +123,7 @@ inline bool Variant::setValue(const bool v) } template <> -inline bool Variant::setValue(const char v) +inline bool Variant::setValue(char v) { clear(); @@ -143,7 +134,7 @@ inline bool Variant::setValue(const char v) } template <> -inline bool Variant::setValue(const short v) +inline bool Variant::setValue(short v) { clear(); @@ -154,7 +145,7 @@ inline bool Variant::setValue(const short v) } template <> -inline bool Variant::setValue(const int v) +inline bool Variant::setValue(int v) { clear(); @@ -165,7 +156,7 @@ inline bool Variant::setValue(const int v) } template <> -inline bool Variant::setValue(const long v) +inline bool Variant::setValue(long v) { clear(); @@ -176,7 +167,7 @@ inline bool Variant::setValue(const long v) } template <> -inline bool Variant::setValue(const float v) +inline bool Variant::setValue(float v) { clear(); @@ -187,7 +178,7 @@ inline bool Variant::setValue(const float v) } template <> -inline bool Variant::setValue(const double v) +inline bool Variant::setValue(double v) { clear(); @@ -198,7 +189,7 @@ inline bool Variant::setValue(const double v) } template <> -inline bool Variant::setValue(const std::string string) +inline bool Variant::setValue(std::string string) { clear(); @@ -226,7 +217,7 @@ inline bool Variant::setValue(void* pointer) } template <> -inline bool Variant::setValue(const MatrixX& matrix) +inline bool Variant::setValue(MatrixX matrix) { clear(); @@ -237,19 +228,7 @@ inline bool Variant::setValue(const MatrixX& matrix) } template <> -inline bool Variant::setValue(const MatrixXf& matrix) -{ - clear(); - - m_type = Matrix; - m_value.matrix = new MatrixX(matrix.rows(), matrix.cols()); - *m_value.matrix = matrix.cast(); - - return true; -} - -template <> -inline bool Variant::setValue(const Vector3& vector) +inline bool Variant::setValue(Vector3 vector) { clear(); @@ -260,7 +239,7 @@ inline bool Variant::setValue(const Vector3& vector) } template <> -inline bool Variant::setValue(const Vector3f& vector) +inline bool Variant::setValue(Vector3f vector) { clear(); @@ -425,6 +404,15 @@ inline Vector3 Variant::value() const return Vector3(); } +template <> +inline const Vector3& Variant::value() const +{ + if (m_type == Vector) + return *m_value.vector; + + return Vector3::Zero(); +} + inline void Variant::clear() { if (m_type == String) { @@ -513,14 +501,14 @@ inline MatrixX Variant::toMatrix() const return value(); } -inline Vector3 Variant::toVector3() const +inline const MatrixX& Variant::toMatrixRef() const { - return value(); + return value(); } -inline const MatrixX& Variant::toMatrixRef() const +inline Vector3 Variant::toVector3() const { - return value(); + return value(); } // --- Operators ----------------------------------------------------------- // diff --git a/avogadro/core/variant.h b/avogadro/core/variant.h index 83662c0f97..c7b1d09f53 100644 --- a/avogadro/core/variant.h +++ b/avogadro/core/variant.h @@ -47,14 +47,14 @@ class AVOGADROCORE_EXPORT Variant /** Creates a variant to store @p value. */ template - Variant(const T value); - - /** Creates a variant to store a 3D vector */ - Variant(double x, double y, double z); + Variant(T value); /** Creates a new copy of @p variant. */ inline Variant(const Variant& variant); + /** Creates a variant to store a 3D vector */ + Variant(double x, double y, double z); + /** Destroys the variant object. */ inline ~Variant(); @@ -66,7 +66,7 @@ class AVOGADROCORE_EXPORT Variant /** Sets the value of the variant to @p value. */ template - bool setValue(const T value); + bool setValue(T value); /** Sets the value of the variant to a 3D vector */ bool setValue(double x, double y, double z); @@ -99,10 +99,10 @@ class AVOGADROCORE_EXPORT Variant /** @return the value of the variant as an \c unsigned \c int. */ inline unsigned int toUInt() const; - /** @return the value of the variant as a \c long. */ + /** @return the value of the variant as a \c long. */ inline long toLong() const; - /** @return the value of the variant as an \c unsigned \c long. */ + /** @return the value of the variant as an \c unsigned \c long. */ inline unsigned long toULong() const; /** @return the value of the variant as a \c float. */ diff --git a/avogadro/quantumio/orca.cpp b/avogadro/quantumio/orca.cpp index d0cbe920cd..387fa589b5 100644 --- a/avogadro/quantumio/orca.cpp +++ b/avogadro/quantumio/orca.cpp @@ -132,10 +132,7 @@ bool ORCAOutput::read(std::istream& in, Core::Molecule& molecule) molecule.setData("totalCharge", m_charge); molecule.setData("totalSpinMultiplicity", m_spin); - // at the moment, Variant doesn't want to take Vector3d - Core::Variant dipole(m_dipoleMoment.x(), m_dipoleMoment.y(), - m_dipoleMoment.z()); - molecule.setData("dipoleMoment", dipole); + molecule.setData("dipoleMoment", m_dipoleMoment); return true; }