00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <utils/eoParam.h>
00022 #include <stdexcept>
00023
00024
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
00043 std::string getValue() const
00044 {
00045 return call_method<std::string>(self, "getValueAsString");
00046 }
00047
00048 void setValue(const std::string& s)
00049 {
00050 call_method<void>(self, "setValueAsString", s);
00051 }
00052
00053 };
00054
00055 template <typename T>
00056 struct ValueParam_pickle_suite : boost::python::pickle_suite
00057 {
00058 static
00059 boost::python::tuple getstate(const eoValueParam<T>& _param)
00060 {
00061 str v(_param.getValue());
00062 str d(_param.description());
00063 str def(_param.defValue());
00064 str l(_param.longName());
00065 object s(_param.shortName());
00066 object r(_param.required());
00067 return make_tuple(v,d,def,l,s,r);
00068 }
00069 static
00070 void setstate(eoValueParam<T>& _param, boost::python::tuple pickled)
00071 {
00072 std::string v = extract<std::string>(pickled[0]);
00073 std::string d = extract<std::string>(pickled[1]);
00074 std::string def = extract<std::string>(pickled[2]);
00075 std::string l = extract<std::string>(pickled[3]);
00076 char s = extract<char>(pickled[4]);
00077 bool r = extract<bool>(pickled[5]);
00078
00079 _param = eoValueParam<T>(T(), l, d, s, r);
00080 _param.defValue(d);
00081 _param.setValue(v);
00082 }
00083 };
00084
00085 template <class T, class U>
00086 U getv(const eoValueParam<T>& v) { return v.value(); }
00087
00088 template <class T, class U>
00089 void setv(eoValueParam<T>& v, U val) { v.value() = val; }
00090
00091 template <>
00092 numeric::array getv< std::vector<double>, numeric::array >
00093 (const eoValueParam< std::vector<double> >& param)
00094 {
00095 const std::vector<double>& v = param.value();
00096 list result;
00097
00098 for (unsigned i =0; i < v.size(); ++i)
00099 result.append(v[i]);
00100
00101 return numeric::array(result);
00102 }
00103
00104 template <>
00105 void setv< std::vector<double>, numeric::array >
00106 (eoValueParam< std::vector<double> >& param, numeric::array val)
00107 {
00108 std::vector<double>& v = param.value();
00109 v.resize( boost::python::len(val) );
00110 for (unsigned i = 0; i < v.size(); ++i)
00111 {
00112 extract<double> x(val[i]);
00113 if (!x.check())
00114 throw std::runtime_error("double expected");
00115
00116 v[i] = x();
00117 }
00118 }
00119
00120 template <>
00121 tuple getv<std::pair<double, double>, tuple >
00122 (const eoValueParam< std::pair<double,double> >& p)
00123 {
00124 return make_tuple(p.value().first, p.value().second);
00125 }
00126
00127 template <>
00128 void setv< std::pair<double, double>, tuple >
00129 (eoValueParam< std::pair<double,double> >& p, tuple val)
00130 {
00131 extract<double> first(val[0]);
00132 extract<double> second(val[1]);
00133
00134 if (!first.check())
00135 throw std::runtime_error("doubles expected");
00136 if (!second.check())
00137 throw std::runtime_error("doubles expected");
00138
00139 p.value().first = first();
00140 p.value().second = second();
00141 }
00142
00143 template <class T, class U>
00144 void define_valueParam(std::string prefix)
00145 {
00146 std::string name = "eoValueParam";
00147 name += prefix;
00148
00149 class_<eoValueParam<T>, bases<eoParam> >(name.c_str(), init<>())
00150 .def(init<T, std::string, std::string, char, bool>())
00151 .def(init<T, std::string, std::string, char>())
00152 .def(init<T, std::string, std::string>())
00153 .def(init<T, std::string>())
00154 .def("getValueAsString", &eoValueParam<T>::getValue)
00155 .def("__str__", &eoValueParam<T>::getValue)
00156 .def("setValueAsString", &eoValueParam<T>::setValue)
00157 .def("getValue", getv<T, U>)
00158 .def("setValue", setv<T, U>)
00159 .add_property("value", getv<T, U>, setv<T, U>)
00160 .def_pickle(ValueParam_pickle_suite<T>())
00161 ;
00162 }
00163
00164 void valueParam()
00165 {
00166 class_<eoParam, ParamWrapper, boost::noncopyable>("eoParam", init<>())
00167 .def(init< std::string, std::string, std::string, char, bool>())
00168 .def("getValueAsString", &ParamWrapper::getValue)
00169 .def("setValueAsString", &ParamWrapper::setValue)
00170 .def("longName", &eoParam::longName, return_value_policy<copy_const_reference>())
00171
00172 .def("description", &eoParam::description, return_value_policy<copy_const_reference>())
00173 .def("shortName", &eoParam::shortName)
00174 .def("required", &eoParam::required)
00175 ;
00176
00177 define_valueParam<int, int>("Int");
00178 define_valueParam<double, double>("Float");
00179 define_valueParam<std::vector<double>, numeric::array >("Vec");
00180 define_valueParam< std::pair<double, double>, tuple >("Pair");
00181
00182
00183 class_<ValueParam, bases<eoParam> >("eoValueParam", init<>())
00184
00185
00186
00187 .def(init<object, std::string>())
00188 .def("getValueAsString", &ValueParam::getValue)
00189 .def("__str__", &ValueParam::getValue)
00190 .def("setValueAsString", &ValueParam::setValue)
00191 .add_property("object", &ValueParam::getObj, &ValueParam::setObj)
00192 ;
00193 }
00194