Base: implement number protocol for Vector2dPy

This commit is contained in:
wmayer
2021-11-14 19:25:29 +01:00
parent 6e1874eede
commit 0c0e34b203
2 changed files with 140 additions and 11 deletions

View File

@@ -121,17 +121,6 @@ Vector2dPy::~Vector2dPy()
{
}
void Vector2dPy::init_type(void)
{
behaviors().name( "Vector2d" );
behaviors().doc( "Vector2d class" );
behaviors().supportGetattro();
behaviors().supportSetattro();
behaviors().supportRepr();
// Call to make the type ready for use
behaviors().readyType();
}
Py::Object Vector2dPy::repr()
{
Py::Float x(v.x);
@@ -191,4 +180,121 @@ int Vector2dPy::setattro(const Py::String &name_, const Py::Object &value)
}
}
Py::Object Vector2dPy::number_negative()
{
return create(-v.x, -v.y);
}
Py::Object Vector2dPy::number_positive()
{
return create(v.x, v.y);
}
Py::Object Vector2dPy::number_absolute()
{
return create(fabs(v.x), fabs(v.y));
}
Py::Object Vector2dPy::number_invert()
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_int()
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_float()
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_long()
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_add( const Py::Object & py)
{
Vector2d u(Py::toVector2d(py));
u = v + u;
return create(u);
}
Py::Object Vector2dPy::number_subtract( const Py::Object & py)
{
Vector2d u(Py::toVector2d(py));
u = v - u;
return create(u);
}
Py::Object Vector2dPy::number_multiply( const Py::Object & py)
{
if (PyObject_TypeCheck(py.ptr(), Vector2dPy::type_object())) {
Vector2d u(Py::toVector2d(py));
double d = v * u;
return Py::Float(d);
}
else if (py.isNumeric()) {
double d = static_cast<double>(Py::Float(py));
return create(v * d);
}
else {
throw Py::TypeError("Argument must be Vector2d or Float");
}
}
Py::Object Vector2dPy::number_remainder( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_divmod( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_lshift( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_rshift( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_and( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_xor( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_or( const Py::Object & )
{
throw Py::TypeError("Not defined");
}
Py::Object Vector2dPy::number_power( const Py::Object &, const Py::Object & )
{
throw Py::TypeError("Not defined");
}
void Vector2dPy::init_type(void)
{
behaviors().name( "Vector2d" );
behaviors().doc( "Vector2d class" );
behaviors().supportGetattro();
behaviors().supportSetattro();
behaviors().supportRepr();
behaviors().supportNumberType();
// Call to make the type ready for use
behaviors().readyType();
}
}

View File

@@ -26,6 +26,7 @@
#include <CXX/Objects.hxx>
#include <CXX/Extensions.hxx>
#include <FCGlobal.h>
#include <Base/Vector3D.h>
#include <Base/Matrix.h>
#include <Base/MatrixPy.h>
@@ -70,6 +71,28 @@ public:
v.y = y;
}
/** @name methods for group handling */
//@{
virtual Py::Object number_negative();
virtual Py::Object number_positive();
virtual Py::Object number_absolute();
virtual Py::Object number_invert();
virtual Py::Object number_int();
virtual Py::Object number_float();
virtual Py::Object number_long();
virtual Py::Object number_add( const Py::Object & );
virtual Py::Object number_subtract( const Py::Object & );
virtual Py::Object number_multiply( const Py::Object & );
virtual Py::Object number_remainder( const Py::Object & );
virtual Py::Object number_divmod( const Py::Object & );
virtual Py::Object number_lshift( const Py::Object & );
virtual Py::Object number_rshift( const Py::Object & );
virtual Py::Object number_and( const Py::Object & );
virtual Py::Object number_xor( const Py::Object & );
virtual Py::Object number_or( const Py::Object & );
virtual Py::Object number_power( const Py::Object &, const Py::Object & );
//@}
private:
Vector2d v;
};