add simple Python wrapper for Vector2d class

This commit is contained in:
wmayer
2016-11-21 22:01:34 +01:00
parent d63d7aebb9
commit 14594dde32
4 changed files with 98 additions and 0 deletions
+61
View File
@@ -86,3 +86,64 @@ Base::Vector3d Py::Vector::toVector() const
return Base::getVectorFromTuple<double>(ptr());
}
}
namespace Base {
Vector2dPy::Vector2dPy(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds)
: Py::PythonClass<Vector2dPy>::PythonClass(self, args, kwds)
{
}
Vector2dPy::~Vector2dPy()
{
}
void Vector2dPy::init_type(void)
{
behaviors().name( "Vector2dPy" );
behaviors().doc( "Vector2d class" );
behaviors().supportGetattro();
behaviors().supportSetattro();
// Call to make the type ready for use
behaviors().readyType();
}
Py::Object Vector2dPy::getattro(const Py::String &name_)
{
std::string name( name_.as_std_string( "utf-8" ) );
if (name == "__members__") {
Py::List attr;
attr.append(Py::String("x"));
attr.append(Py::String("y"));
return attr;
}
else if (name == "x") {
return Py::Float(v.x);
}
else if (name == "y") {
return Py::Float(v.y);
}
else {
return genericGetAttro( name_ );
}
}
int Vector2dPy::setattro(const Py::String &name_, const Py::Object &value)
{
std::string name( name_.as_std_string( "utf-8" ) );
if (name == "x" && !value.isNull()) {
v.x = static_cast<double>(Py::Float(value));
return 0;
}
else if (name == "y" && !value.isNull()) {
v.y = static_cast<double>(Py::Float(value));
return 0;
}
else {
return genericSetAttro( name_, value );
}
}
}