Skip to content

Commit

Permalink
Fix variant unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Nov 20, 2024
1 parent f593562 commit 31d2362
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
64 changes: 26 additions & 38 deletions avogadro/core/variant-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "variant.h"

#include <iostream>
#include <sstream>

namespace Avogadro::Core {
Expand All @@ -22,7 +21,7 @@ inline Variant::Variant(double x, double y, double z) : m_type(Vector)
}

template <typename T>
inline Variant::Variant(const T v) : m_type(Null)
inline Variant::Variant(T v) : m_type(Null)
{
setValue(v);
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -101,7 +92,7 @@ inline bool Variant::setValue(double x, double y, double z)
}

template <typename T>
inline bool Variant::setValue(const T v)
inline bool Variant::setValue(T v)
{
AVO_UNUSED(v);

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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<double>();

return true;
}

template <>
inline bool Variant::setValue(const Vector3& vector)
inline bool Variant::setValue(Vector3 vector)
{
clear();

Expand All @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -513,14 +501,14 @@ inline MatrixX Variant::toMatrix() const
return value<MatrixX>();
}

inline Vector3 Variant::toVector3() const
inline const MatrixX& Variant::toMatrixRef() const
{
return value<Vector3>();
return value<const MatrixX&>();
}

inline const MatrixX& Variant::toMatrixRef() const
inline Vector3 Variant::toVector3() const
{
return value<const MatrixX&>();
return value<Vector3>();
}

// --- Operators ----------------------------------------------------------- //
Expand Down
14 changes: 7 additions & 7 deletions avogadro/core/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class AVOGADROCORE_EXPORT Variant

/** Creates a variant to store @p value. */
template <typename T>
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();

Expand All @@ -66,7 +66,7 @@ class AVOGADROCORE_EXPORT Variant

/** Sets the value of the variant to @p value. */
template <typename T>
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);
Expand Down Expand Up @@ -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. */
Expand Down
5 changes: 1 addition & 4 deletions avogadro/quantumio/orca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 31d2362

Please sign in to comment.