/* -*-c++-*- osgPython - Copyright (C) 2006 Miguel Escriva Gregori * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include #include "Base.h" #include "Value.h" #include "Type.h" #include "Namespace.h" #include "Method.h" #include #include #include using namespace boost::python; namespace osgPython { boost::python::object Base::getAttr(const std::string& a) { throw std::runtime_error("Attribute '" + a + "' not found!"); } Base* Base::call(osgIntrospection::ValueList&) { throw std::runtime_error("Object is not callable"); } boost::python::object Base::boostCall0() { osgIntrospection::ValueList vl; return Base::toPython(call(vl)); } boost::python::object Base::boostCall1(boost::python::object &o) { osgIntrospection::ValueList vl; extract e_list(o); if (e_list.check()){ boost::python::list l = e_list(); int len = extract(l.attr("__len__")()); for (int i=0; i< len; ++i){ object oo = l[i]; vl.push_back(Base::fromPython(oo)); } } else{ vl.push_back(Base::fromPython(o)); } return Base::toPython(call(vl)); } boost::python::object Base::boostCall2(boost::python::object &o1, boost::python::object &o2) { osgIntrospection::ValueList vl; vl.push_back(Base::fromPython(o1)); vl.push_back(Base::fromPython(o2)); return Base::toPython(call(vl)); } boost::python::object Base::boostCall3(boost::python::object &o1, boost::python::object &o2, boost::python::object &o3) { osgIntrospection::ValueList vl; vl.push_back(Base::fromPython(o1)); vl.push_back(Base::fromPython(o2)); vl.push_back(Base::fromPython(o3)); return Base::toPython(call(vl)); } boost::python::object Base::boostCall4(boost::python::object &o1, boost::python::object &o2, boost::python::object &o3, boost::python::object &o4) { osgIntrospection::ValueList vl; vl.push_back(Base::fromPython(o1)); vl.push_back(Base::fromPython(o2)); vl.push_back(Base::fromPython(o3)); vl.push_back(Base::fromPython(o4)); return Base::toPython(call(vl)); } boost::python::object Base::boostCall5(boost::python::object &o1, boost::python::object &o2, boost::python::object &o3, boost::python::object &o4, boost::python::object &o5) { osgIntrospection::ValueList vl; vl.push_back(Base::fromPython(o1)); vl.push_back(Base::fromPython(o2)); vl.push_back(Base::fromPython(o3)); vl.push_back(Base::fromPython(o4)); vl.push_back(Base::fromPython(o5)); return Base::toPython(call(vl)); } boost::python::object Base::boostGetAttr(const std::string& name) { return getAttr(name); } boost::python::object Base::toPython(Base *b) { #define to_basic_type(TYPE, NAME) \ static const osgIntrospection::Type& NAME = \ osgIntrospection::Reflection::getType( osgIntrospection::ExtendedTypeInfo(typeid(TYPE),false,false) ); \ if ( v->getValue().getType() == NAME ) \ return object(osgIntrospection::getInstance(v->getValue())); \ osg::ref_ptr pb = b; if (!b) return object(); if (Namespace *ns = dynamic_cast(b)) return object(ns); if (Type *t = dynamic_cast(b)) return object(t); if (Method *m = dynamic_cast(b)) return object(m); if (Value *v = dynamic_cast(b)){ to_basic_type(bool, tbool) to_basic_type(int, tint) to_basic_type(unsigned int, tuint) to_basic_type(float, tfloat) to_basic_type(double, tdouble) to_basic_type(const char*, tcchar) to_basic_type(const std::string, tstd_string) to_basic_type(osg::Vec2, tosg_Vec2) // to_basic_type(osg::Vec3, tosg_Vec3) // to_basic_type(osg::Vec4, tosg_Vec4) // to_basic_type(osg::Quat, tosg_Quat) return object(v); } throw std::runtime_error("No conversion to Python available"); } osgIntrospection::Value Base::fromPython(object &o) { #define from_basic_type(TYPE, NAME) \ extract NAME(o); \ if (NAME.check() == true) \ return osgIntrospection::Value(NAME()); from_basic_type(const std::string, e_std_string) from_basic_type(double, e_double) from_basic_type(float, e_float) from_basic_type(int, e_int) from_basic_type(bool, e_bool) // from_basic_type(osg::Vec2, e_osg_Vec2) // from_basic_type(osg::Vec3, e_osg_Vec3) // from_basic_type(osg::Vec4, e_osg_Vec4) // from_basic_type(osg::Quat, e_osg_Quat) return extract(o)()->getValue(); } } //end of osgPython namespace