From 27c14a5c948185644d2f910362c3c826f70f8811 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Sun, 3 Dec 2023 21:21:18 -0500 Subject: [PATCH 1/2] Add specialization for char* and MatrixXf types Implicit conversion does not work for these types Signed-off-by: Geoff Hutchison --- avogadro/core/variant-inline.h | 20 +++++++++++++++----- avogadro/core/variant.h | 10 ++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/avogadro/core/variant-inline.h b/avogadro/core/variant-inline.h index 4b6426d2ab..6706e4cf5e 100644 --- a/avogadro/core/variant-inline.h +++ b/avogadro/core/variant-inline.h @@ -13,9 +13,7 @@ namespace Avogadro { namespace Core { -inline Variant::Variant() : m_type(Null) -{ -} +inline Variant::Variant() : m_type(Null) {} template inline Variant::Variant(T v) : m_type(Null) @@ -23,6 +21,18 @@ inline Variant::Variant(T v) : m_type(Null) setValue(v); } +inline Variant::Variant(const char* v) : m_type(String) +{ + m_value.string = new std::string(v); +} + +inline Variant::Variant(const MatrixXf& v) : m_type(Matrix) +{ + MatrixX* m = new MatrixX(v.rows(), v.cols()); + *m = v.cast(); + m_value.matrix = m; +} + inline Variant::Variant(const Variant& variant) : m_type(variant.type()) { if (m_type == String) @@ -444,7 +454,7 @@ inline T Variant::lexical_cast(const std::string& str) return value; } -} // end Core namespace -} // end Avogadro namespace +} // namespace Core +} // namespace Avogadro #endif // AVOGADRO_CORE_VARIANT_INLINE_H diff --git a/avogadro/core/variant.h b/avogadro/core/variant.h index 3f171aebfc..c2b08bd56c 100644 --- a/avogadro/core/variant.h +++ b/avogadro/core/variant.h @@ -48,6 +48,12 @@ class AVOGADROCORE_EXPORT Variant template Variant(T value); + /** Creates a std::string variant to store @p value */ + Variant(const char* value); + + /** Creates a MatrixX variant to store @p value */ + Variant(const MatrixXf& value); + /** Creates a new copy of @p variant. */ inline Variant(const Variant& variant); @@ -146,8 +152,8 @@ class AVOGADROCORE_EXPORT Variant } m_value; }; -} // end Core namespace -} // end Avogadro namespace +} // namespace Core +} // namespace Avogadro #include "variant-inline.h" From 1260e1a03fa72ae0b3b3b6fa9323e5084dfde313 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Mon, 4 Dec 2023 12:39:29 -0500 Subject: [PATCH 2/2] Better to use template specialization Signed-off-by: Geoff Hutchison --- avogadro/core/variant-inline.h | 2 ++ avogadro/core/variant.h | 6 ------ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/avogadro/core/variant-inline.h b/avogadro/core/variant-inline.h index 6706e4cf5e..16836f4dcb 100644 --- a/avogadro/core/variant-inline.h +++ b/avogadro/core/variant-inline.h @@ -21,11 +21,13 @@ inline Variant::Variant(T v) : m_type(Null) setValue(v); } +template <> inline Variant::Variant(const char* v) : m_type(String) { m_value.string = new std::string(v); } +template <> inline Variant::Variant(const MatrixXf& v) : m_type(Matrix) { MatrixX* m = new MatrixX(v.rows(), v.cols()); diff --git a/avogadro/core/variant.h b/avogadro/core/variant.h index c2b08bd56c..84eda3186d 100644 --- a/avogadro/core/variant.h +++ b/avogadro/core/variant.h @@ -48,12 +48,6 @@ class AVOGADROCORE_EXPORT Variant template Variant(T value); - /** Creates a std::string variant to store @p value */ - Variant(const char* value); - - /** Creates a MatrixX variant to store @p value */ - Variant(const MatrixXf& value); - /** Creates a new copy of @p variant. */ inline Variant(const Variant& variant);