/* -*-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 "Value.h" #include "Method.h" #include #include #include #include #include namespace osgPython { Value::Value(const osgIntrospection::Value& value) : _value(value) { static const osgIntrospection::Type &t_referenced = osgIntrospection::Reflection::getType( osgIntrospection::ExtendedTypeInfo(typeid(osg::Referenced), false, false) ); const osgIntrospection::Type &type = value.getType(); if (type.isDefined()){ if (type.isNonConstPointer()) { const osgIntrospection::Type &p_type = type.getPointedType(); if (p_type.isDefined()) if (p_type.isSubclassOf(t_referenced)) { _referenced = osgIntrospection::variant_cast(_value); } } } } boost::python::object Value::getAttr(const std::string& name) { return Base::toPython(new Method(this, name)); } std::string Value::__str__() const { const osgIntrospection::Type& type = _value.getType(); const osgIntrospection::ReaderWriter *rw = type.getReaderWriter(); if (rw != NULL){ std::stringstream ss; rw->writeTextValue(ss, _value); return ss.str(); } return ""; } std::string Value::__repr__() const { const osgIntrospection::Type& type = _value.getInstanceType(); return type.getQualifiedName() + "("+__str__()+")"; } void Value::setTextValue(Value* v, const std::string& str) { try{ const osgIntrospection::Type& type = v->_value.getInstanceType(); const osgIntrospection::ReaderWriter *rw = type.getReaderWriter(); if (rw != NULL){ std::stringstream ss(str); rw->readTextValue(ss, v->_value); } } catch(osgIntrospection::Exception& e) { throw std::runtime_error(e.what()); } } std::string Value::getTextValue(boost::python::object &o) { Value v = Base::fromPython(o); return v.__str__(); } boost::python::object Value::__getitem__(const std::string& name) { const osgIntrospection::Type& type = _value.getInstanceType(); osgIntrospection::PropertyInfoList plist; osgIntrospection::PropertyInfoList::iterator it; type.getAllProperties(plist); for(it=plist.begin(); it != plist.end(); ++it){ if ((*it)->getName() == name){ if ((*it)->canGet()){ if ((*it)->isSimple()){ return Base::toPython(new Value((*it)->getValue(_value))); } else if ((*it)->isArray()){ // TODO: Array } else if ((*it)->isIndexed()){ // TODO: Indexed } } std::cout << "Property found, but has not a getter!" << std::endl; } } std::cout << "Property NOT found!" << std::endl; return boost::python::object(); } void Value::__setitem__(const std::string& name, boost::python::object &val) { const osgIntrospection::Type& type = _value.getInstanceType(); osgIntrospection::PropertyInfoList plist; osgIntrospection::PropertyInfoList::iterator it; type.getAllProperties(plist); for(it=plist.begin(); it != plist.end(); ++it){ if ((*it)->getName() == name){ if ((*it)->canSet()){ if ((*it)->isSimple()){ (*it)->setValue(_value, Base::fromPython(val)); } // TODO: Array and Indexed: throw a exception. } else{ std::cout << "Property found, but has not a setter!" << std::endl; } } } } } //end of osgPython namespace