Skip to content

Commit

Permalink
Add missing streaming operators to GeneralUnit.
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Jun 29, 2022
1 parent fe8ebb5 commit 858d72f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 40 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions wrapper/Units/GeneralUnit.pypp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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 > );
}
Expand Down
54 changes: 44 additions & 10 deletions wrapper/Units/generalunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Unit&>(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<Unit&>(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
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -253,7 +287,7 @@ GeneralUnit GeneralUnit::operator*=(const GeneralUnit &other)
temperature += other.temperature;
Quantity += other.Quantity;
Angle += other.Angle;

return *this;
}

Expand All @@ -267,7 +301,7 @@ GeneralUnit GeneralUnit::operator/=(const GeneralUnit &other)
temperature -= other.temperature;
Quantity -= other.Quantity;
Angle -= other.Angle;

return *this;
}

Expand Down Expand Up @@ -340,17 +374,17 @@ 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;
ret.Charge = -Charge;
ret.temperature = -temperature;
ret.Quantity = -Quantity;
ret.Angle = -Angle;

return ret;
}

Expand Down
68 changes: 39 additions & 29 deletions wrapper/Units/generalunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

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

Expand All @@ -51,7 +61,7 @@ class GeneralUnit : public Unit
Angle = D::ANGLE();
detail::registerTypeName(*this, D::typeName());
}

GeneralUnit(const GeneralUnit &other);

~GeneralUnit();
Expand All @@ -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;
Expand Down Expand Up @@ -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<const Dimension::GeneralUnit&> x(obj);

bp::extract<const Dimension::GeneralUnit&> 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
Expand All @@ -206,7 +216,7 @@ struct from_general_unit
return obj_ptr;
}
}

//could not recognise the type or the dimension was wrong
return 0;
}
Expand All @@ -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<const Dimension::GeneralUnit&> x(obj);

bp::extract<const Dimension::GeneralUnit&> 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
Expand All @@ -241,12 +251,12 @@ struct from_general_unit

//create the T container
new (storage) D( gen_unit.scaleFactor() );

data->convertible = storage;
}
}
}
};
};

template<class D>
struct to_general_unit
Expand All @@ -262,7 +272,7 @@ void register_dimension()
{
bp::to_python_converter< D, to_general_unit<D> >();

bp::converter::registry::push_back(
bp::converter::registry::push_back(
&from_general_unit<D>::convertible,
&from_general_unit<D>::construct,
bp::type_id<D>() );
Expand Down

0 comments on commit 858d72f

Please sign in to comment.