/* PyEO Copyright (C) 2003 Maarten Keijzer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include // Here's 'len'. Why? dunno #include "valueParam.h" #include using namespace boost::python; class ParamWrapper : public eoParam { public: PyObject* self; ParamWrapper(PyObject* p) : self(p) {} ParamWrapper(PyObject* p, std::string a, std::string b, std::string c, char d, bool e) : eoParam(a,b,c,d,e), self(p) {} std::string getValue() const { return call_method(self, "getValueAsString"); } void setValue(const std::string& s) { call_method(self, "setValueAsString", s); } }; template struct ValueParam_pickle_suite : boost::python::pickle_suite { static boost::python::tuple getstate(const eoValueParam& _param) { str v(_param.getValue()); str d(_param.description()); str def(_param.defValue()); str l(_param.longName()); object s(_param.shortName()); object r(_param.required()); return make_tuple(v,d,def,l,s,r); } static void setstate(eoValueParam& _param, boost::python::tuple pickled) { std::string v = extract(pickled[0]); std::string d = extract(pickled[1]); std::string def = extract(pickled[2]); std::string l = extract(pickled[3]); char s = extract(pickled[4]); bool r = extract(pickled[5]); _param = eoValueParam(T(), l, d, s, r); _param.defValue(d); _param.setValue(v); } }; template U getv(const eoValueParam& v) { return v.value(); } template void setv(eoValueParam& v, U val) { v.value() = val; } template <> numeric::array getv< std::vector, numeric::array > (const eoValueParam< std::vector >& param) { const std::vector& v = param.value(); list result; for (unsigned i =0; i < v.size(); ++i) result.append(v[i]); return numeric::array(result); } template <> void setv< std::vector, numeric::array > (eoValueParam< std::vector >& param, numeric::array val) { std::vector& v = param.value(); v.resize( boost::python::len(val) ); for (unsigned i = 0; i < v.size(); ++i) { extract x(val[i]); if (!x.check()) throw std::runtime_error("double expected"); v[i] = x(); } } template <> tuple getv, tuple > (const eoValueParam< std::pair >& p) { return make_tuple(p.value().first, p.value().second); } template <> void setv< std::pair, tuple > (eoValueParam< std::pair >& p, tuple val) { extract first(val[0]); extract second(val[1]); if (!first.check()) throw std::runtime_error("doubles expected"); if (!second.check()) throw std::runtime_error("doubles expected"); p.value().first = first(); p.value().second = second(); } template void define_valueParam(std::string prefix) { std::string name = "eoValueParam"; name += prefix; class_, bases >(name.c_str(), init<>()) .def(init()) .def(init()) .def(init()) .def(init()) .def("getValueAsString", &eoValueParam::getValue) .def("__str__", &eoValueParam::getValue) .def("setValueAsString", &eoValueParam::setValue) .def("getValue", getv) .def("setValue", setv) .add_property("value", getv, setv) .def_pickle(ValueParam_pickle_suite()) ; } void valueParam() { class_("eoParam", init<>()) .def(init< std::string, std::string, std::string, char, bool>()) .def("getValueAsString", &ParamWrapper::getValue) .def("setValueAsString", &ParamWrapper::setValue) .def("longName", &eoParam::longName, return_value_policy()) //.def("defValue", &eoParam::defValue, return_value_policy()) .def("description", &eoParam::description, return_value_policy()) .def("shortName", &eoParam::shortName) .def("required", &eoParam::required) ; define_valueParam("Int"); define_valueParam("Float"); define_valueParam, numeric::array >("Vec"); define_valueParam< std::pair, tuple >("Pair"); //define_valueParam< object, object >("Py"); class_ >("eoValueParam", init<>()) //.def(init()) //.def(init()) //.def(init()) .def(init()) .def("getValueAsString", &ValueParam::getValue) .def("__str__", &ValueParam::getValue) .def("setValueAsString", &ValueParam::setValue) .add_property("object", &ValueParam::getObj, &ValueParam::setObj) ; }