From 858d72fc58b311160065c8613d72395be4a70946 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Wed, 29 Jun 2022 14:22:01 +0100 Subject: [PATCH] Add missing streaming operators to GeneralUnit. --- CHANGELOG | 3 +- wrapper/Units/GeneralUnit.pypp.cpp | 7 +++ wrapper/Units/generalunit.cpp | 54 +++++++++++++++++++----- wrapper/Units/generalunit.h | 68 +++++++++++++++++------------- 4 files changed, 92 insertions(+), 40 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 521e98253..0e4de240e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,7 +13,8 @@ devel branch: Use -Oz compiler flag rather than -Os for compiling Python wrappers to avoid "illegal hardware instruction" error with Clang 14 on macOS x86_64. Fixed issue reconstructing triclinic - box objects from a binary data stream. + box objects from a binary data stream. Added missing streaming + operators to Sire.Unit.GeneralUnit. [2022.2.0] March 2022 - Fixed formatting of SOLVENT_POINTERS flag in AmberPrm7 parser. Removed duplicate definition of sigma_av diff --git a/wrapper/Units/GeneralUnit.pypp.cpp b/wrapper/Units/GeneralUnit.pypp.cpp index e0d657c4d..ff8fb11a5 100644 --- a/wrapper/Units/GeneralUnit.pypp.cpp +++ b/wrapper/Units/GeneralUnit.pypp.cpp @@ -35,6 +35,8 @@ namespace bp = boost::python; SireUnits::Dimension::GeneralUnit __copy__(const SireUnits::Dimension::GeneralUnit &other){ return SireUnits::Dimension::GeneralUnit(other); } +#include "Qt/qdatastream.hpp" + #include "Helpers/str.hpp" void register_GeneralUnit_class(){ @@ -231,6 +233,11 @@ void register_GeneralUnit_class(){ GeneralUnit_exposer.def( "__copy__", &__copy__); GeneralUnit_exposer.def( "__deepcopy__", &__copy__); GeneralUnit_exposer.def( "clone", &__copy__); + GeneralUnit_exposer.def( "__rlshift__", &__rlshift__QDataStream< ::SireUnits::Dimension::GeneralUnit >, + bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() ); + GeneralUnit_exposer.def( "__rrshift__", &__rrshift__QDataStream< ::SireUnits::Dimension::GeneralUnit >, + bp::return_internal_reference<1, bp::with_custodian_and_ward<1,2> >() ); + GeneralUnit_exposer.def_pickle(sire_pickle_suite< ::SireUnits::Dimension::GeneralUnit >()); GeneralUnit_exposer.def( "__str__", &__str__< ::SireUnits::Dimension::GeneralUnit > ); GeneralUnit_exposer.def( "__repr__", &__str__< ::SireUnits::Dimension::GeneralUnit > ); } diff --git a/wrapper/Units/generalunit.cpp b/wrapper/Units/generalunit.cpp index 9ab142576..289360238 100644 --- a/wrapper/Units/generalunit.cpp +++ b/wrapper/Units/generalunit.cpp @@ -21,6 +21,40 @@ using namespace SireUnits; using namespace SireUnits::Dimension; using namespace SireBase; +QDataStream& operator<<(QDataStream &ds, const SireUnits::Dimension::GeneralUnit &u) +{ + qint8 a = u.ANGLE(); + qint8 c = u.CHARGE(); + qint8 l = u.LENGTH(); + qint8 m = u.MASS(); + qint8 t1 = u.TEMPERATURE(); + qint8 t2 = u.TIME(); + qint8 q = u.QUANTITY(); + + ds << a << c << l << m << t1 << t2 << q + << static_cast(u); + + return ds; +} + +QDataStream& operator>>(QDataStream &ds, SireUnits::Dimension::GeneralUnit &u) +{ + qint8 a, c, l, m, t1, t2, q; + + ds >> a >> c >> l >> m >> t1 >> t2 >> q + >> static_cast(u); + + u.Angle = a; + u.Charge = c; + u.Length = l; + u.Mass = m; + u.temperature = t1; + u.Time = t2; + u.Quantity = q; + + return ds; +} + namespace SireUnits { namespace Dimension @@ -119,7 +153,7 @@ void GeneralUnit::assertCompatible(const GeneralUnit &other) const throw "Unit conversion error!!!"; } } - + QString GeneralUnit::toString() const { return SireUnits::Dimension::getUnitString(value(), Mass, Length, @@ -140,9 +174,9 @@ double GeneralUnit::to(const TempBase &other) const GeneralUnit general_temp; general_temp.temperature = 1; general_temp.setScale(other); - + this->assertCompatible(general_temp); - + return other.convertFromInternal(value()) / other.convertFromInternal(); } @@ -184,14 +218,14 @@ int GeneralUnit::ANGLE() const GeneralUnit& GeneralUnit::operator=(const GeneralUnit &other) { setScale(other.value()); - + Mass = other.MASS(); Length = other.LENGTH(); Time = other.TIME(); Charge = other.CHARGE(); temperature = other.TEMPERATURE(); Quantity = other.QUANTITY(); - Angle = other.ANGLE(); + Angle = other.ANGLE(); return *this; } @@ -253,7 +287,7 @@ GeneralUnit GeneralUnit::operator*=(const GeneralUnit &other) temperature += other.temperature; Quantity += other.Quantity; Angle += other.Angle; - + return *this; } @@ -267,7 +301,7 @@ GeneralUnit GeneralUnit::operator/=(const GeneralUnit &other) temperature -= other.temperature; Quantity -= other.Quantity; Angle -= other.Angle; - + return *this; } @@ -340,9 +374,9 @@ GeneralUnit GeneralUnit::operator/(int val) const GeneralUnit GeneralUnit::invert() const { GeneralUnit ret; - + ret.setScale( 1.0 / value() ); - + ret.Mass = -Mass; ret.Length = -Length; ret.Time = -Time; @@ -350,7 +384,7 @@ GeneralUnit GeneralUnit::invert() const ret.temperature = -temperature; ret.Quantity = -Quantity; ret.Angle = -Angle; - + return ret; } diff --git a/wrapper/Units/generalunit.h b/wrapper/Units/generalunit.h index ac3d0ca30..bee7d5b49 100644 --- a/wrapper/Units/generalunit.h +++ b/wrapper/Units/generalunit.h @@ -19,6 +19,13 @@ SIRE_BEGIN_HEADER namespace bp = boost::python; #endif //SIRE_SKIP_INLINE_FUNCTIONS +namespace SireUnits{ namespace Dimension { +class GeneralUnit; +}} + +SIREUNITS_EXPORT QDataStream& operator<<(QDataStream&, const SireUnits::Dimension::GeneralUnit&); +SIREUNITS_EXPORT QDataStream& operator>>(QDataStream&, SireUnits::Dimension::GeneralUnit&); + namespace SireUnits { @@ -36,6 +43,9 @@ void registerTypeName(const GeneralUnit &unit, const char *typnam); class GeneralUnit : public Unit { +friend QDataStream& ::operator<<(QDataStream&, const GeneralUnit&); +friend QDataStream& ::operator>>(QDataStream&, GeneralUnit&); + public: GeneralUnit(); @@ -51,7 +61,7 @@ class GeneralUnit : public Unit Angle = D::ANGLE(); detail::registerTypeName(*this, D::typeName()); } - + GeneralUnit(const GeneralUnit &other); ~GeneralUnit(); @@ -68,46 +78,46 @@ class GeneralUnit : public Unit int ANGLE() const; GeneralUnit& operator=(const GeneralUnit &other); - + bool operator==(const GeneralUnit &other) const; - + bool operator!=(const GeneralUnit &other) const; - + GeneralUnit operator-() const; - + GeneralUnit& operator+=(const GeneralUnit &other); - + GeneralUnit& operator-=(const GeneralUnit &other); - + GeneralUnit operator+(const GeneralUnit &other) const; - + GeneralUnit operator-(const GeneralUnit &other) const; GeneralUnit operator*=(const GeneralUnit &other); GeneralUnit operator/=(const GeneralUnit &other); - + GeneralUnit operator*(const GeneralUnit &other) const; - + GeneralUnit operator/(const GeneralUnit &other) const; - + GeneralUnit& operator*=(double val); GeneralUnit& operator/=(double val); - + GeneralUnit& operator*=(int val); GeneralUnit& operator/=(int val); - + GeneralUnit operator*(double val) const; GeneralUnit operator/(double val) const; - + GeneralUnit operator*(int val) const; GeneralUnit operator/(int val) const; - + GeneralUnit invert() const; - + double to(const TempBase &other) const; double to(const GeneralUnit &other) const; - + QString toString() const; SireBase::PropertyPtr toProperty() const; @@ -184,16 +194,16 @@ struct from_general_unit static void* convertible(PyObject* obj_ptr) { bp::object obj( bp::handle<>(bp::borrowed(obj_ptr)) ); - - bp::extract x(obj); - + + bp::extract x(obj); + //is this a GeneralUnit? if ( x.check() ) { //it is ;-) Get a reference to it and make sure //that it is of the right dimension const Dimension::GeneralUnit &gen_unit = x(); - + if ( gen_unit.MASS() == D::MASS() and gen_unit.LENGTH() == D::LENGTH() and gen_unit.TIME() == D::TIME() and @@ -206,7 +216,7 @@ struct from_general_unit return obj_ptr; } } - + //could not recognise the type or the dimension was wrong return 0; } @@ -217,16 +227,16 @@ struct from_general_unit boost::python::converter::rvalue_from_python_stage1_data* data) { bp::object obj( bp::handle<>(bp::borrowed(obj_ptr)) ); - - bp::extract x(obj); - + + bp::extract x(obj); + //is this a GeneralUnit? if ( x.check() ) { //it is ;-) Get a reference to it and make sure //that it is of the right dimension const Dimension::GeneralUnit &gen_unit = x(); - + if ( gen_unit.MASS() == D::MASS() and gen_unit.LENGTH() == D::LENGTH() and gen_unit.TIME() == D::TIME() and @@ -241,12 +251,12 @@ struct from_general_unit //create the T container new (storage) D( gen_unit.scaleFactor() ); - + data->convertible = storage; } } } -}; +}; template struct to_general_unit @@ -262,7 +272,7 @@ void register_dimension() { bp::to_python_converter< D, to_general_unit >(); - bp::converter::registry::push_back( + bp::converter::registry::push_back( &from_general_unit::convertible, &from_general_unit::construct, bp::type_id() );