EvolvingObjects
|
00001 /* 00002 PyEO 00003 00004 Copyright (C) 2003 Maarten Keijzer 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include <utils/eoParam.h> 00022 #include <stdexcept> 00023 00024 // Here's 'len'. Why? dunno 00025 #include "valueParam.h" 00026 #include <boost/python/detail/api_placeholder.hpp> 00027 00028 using namespace boost::python; 00029 00030 class ParamWrapper : public eoParam 00031 { 00032 public: 00033 PyObject* self; 00034 ParamWrapper(PyObject* p) : self(p) {} 00035 ParamWrapper(PyObject* p, 00036 std::string a, 00037 std::string b, 00038 std::string c, 00039 char d, 00040 bool e) : eoParam(a,b,c,d,e), self(p) {} 00041 00042 std::string getValue() const 00043 { 00044 return call_method<std::string>(self, "getValueAsString"); 00045 } 00046 00047 void setValue(const std::string& s) 00048 { 00049 call_method<void>(self, "setValueAsString", s); 00050 } 00051 }; 00052 00053 template <typename T> 00054 struct ValueParam_pickle_suite : boost::python::pickle_suite 00055 { 00056 static 00057 boost::python::tuple getstate(const eoValueParam<T>& _param) 00058 { 00059 str v(_param.getValue()); 00060 str d(_param.description()); 00061 str def(_param.defValue()); 00062 str l(_param.longName()); 00063 object s(_param.shortName()); 00064 object r(_param.required()); 00065 return make_tuple(v,d,def,l,s,r); 00066 } 00067 static 00068 void setstate(eoValueParam<T>& _param, boost::python::tuple pickled) 00069 { 00070 std::string v = extract<std::string>(pickled[0]); 00071 std::string d = extract<std::string>(pickled[1]); 00072 std::string def = extract<std::string>(pickled[2]); 00073 std::string l = extract<std::string>(pickled[3]); 00074 char s = extract<char>(pickled[4]); 00075 bool r = extract<bool>(pickled[5]); 00076 00077 _param = eoValueParam<T>(T(), l, d, s, r); 00078 _param.defValue(d); 00079 _param.setValue(v); 00080 } 00081 }; 00082 00083 template <class T, class U> 00084 U getv(const eoValueParam<T>& v) { return v.value(); } 00085 00086 template <class T, class U> 00087 void setv(eoValueParam<T>& v, U val) { v.value() = val; } 00088 00089 template <> 00090 numeric::array getv< std::vector<double>, numeric::array > 00091 (const eoValueParam< std::vector<double> >& param) 00092 { 00093 const std::vector<double>& v = param.value(); 00094 list result; 00095 00096 for (unsigned i =0; i < v.size(); ++i) 00097 result.append(v[i]); 00098 00099 return numeric::array(result); 00100 } 00101 00102 template <> 00103 void setv< std::vector<double>, numeric::array > 00104 (eoValueParam< std::vector<double> >& param, numeric::array val) 00105 { 00106 std::vector<double>& v = param.value(); 00107 v.resize( boost::python::len(val) ); 00108 for (unsigned i = 0; i < v.size(); ++i) 00109 { 00110 extract<double> x(val[i]); 00111 if (!x.check()) 00112 throw std::runtime_error("double expected"); 00113 00114 v[i] = x(); 00115 } 00116 } 00117 00118 template <> 00119 tuple getv<std::pair<double, double>, tuple > 00120 (const eoValueParam< std::pair<double,double> >& p) 00121 { 00122 return make_tuple(p.value().first, p.value().second); 00123 } 00124 00125 template <> 00126 void setv< std::pair<double, double>, tuple > 00127 (eoValueParam< std::pair<double,double> >& p, tuple val) 00128 { 00129 extract<double> first(val[0]); 00130 extract<double> second(val[1]); 00131 00132 if (!first.check()) 00133 throw std::runtime_error("doubles expected"); 00134 if (!second.check()) 00135 throw std::runtime_error("doubles expected"); 00136 00137 p.value().first = first(); 00138 p.value().second = second(); 00139 } 00140 00141 template <class T, class U> 00142 void define_valueParam(std::string prefix) 00143 { 00144 std::string name = "eoValueParam"; 00145 name += prefix; 00146 00147 class_<eoValueParam<T>, bases<eoParam> >(name.c_str(), init<>()) 00148 .def(init<T, std::string, std::string, char, bool>()) 00149 .def(init<T, std::string, std::string, char>()) 00150 .def(init<T, std::string, std::string>()) 00151 .def(init<T, std::string>()) 00152 .def("getValueAsString", &eoValueParam<T>::getValue) 00153 .def("__str__", &eoValueParam<T>::getValue) 00154 .def("setValueAsString", &eoValueParam<T>::setValue) 00155 .def("getValue", getv<T, U>) 00156 .def("setValue", setv<T, U>) 00157 .add_property("value", getv<T, U>, setv<T, U>) 00158 .def_pickle(ValueParam_pickle_suite<T>()) 00159 ; 00160 } 00161 00162 void valueParam() 00163 { 00164 class_<eoParam, ParamWrapper, boost::noncopyable>("eoParam", init<>()) 00165 .def(init< std::string, std::string, std::string, char, bool>()) 00166 .def("getValueAsString", &ParamWrapper::getValue) 00167 .def("setValueAsString", &ParamWrapper::setValue) 00168 .def("longName", &eoParam::longName, return_value_policy<copy_const_reference>()) 00169 //.def("defValue", &eoParam::defValue, return_value_policy<copy_const_reference>()) 00170 .def("description", &eoParam::description, return_value_policy<copy_const_reference>()) 00171 .def("shortName", &eoParam::shortName) 00172 .def("required", &eoParam::required) 00173 ; 00174 00175 define_valueParam<int, int>("Int"); 00176 define_valueParam<double, double>("Float"); 00177 define_valueParam<std::vector<double>, numeric::array >("Vec"); 00178 define_valueParam< std::pair<double, double>, tuple >("Pair"); 00179 //define_valueParam< object, object >("Py"); 00180 00181 class_<ValueParam, bases<eoParam> >("eoValueParam", init<>()) 00182 //.def(init<object, std::string, std::string, char, bool>()) 00183 //.def(init<object, std::string, std::string, char>()) 00184 //.def(init<object, std::string, std::string>()) 00185 .def(init<object, std::string>()) 00186 .def("getValueAsString", &ValueParam::getValue) 00187 .def("__str__", &ValueParam::getValue) 00188 .def("setValueAsString", &ValueParam::setValue) 00189 .add_property("object", &ValueParam::getObj, &ValueParam::setObj) 00190 ; 00191 }