# valueParam.cpp

    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
    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         //.def("defValue", &eoParam::defValue, return_value_policy<copy_const_reference>())
    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     //define_valueParam< object, object >("Py");
    00182
    00183     class_<ValueParam, bases<eoParam> >("eoValueParam", init<>())
    00184         //.def(init<object, std::string, std::string, char, bool>())
    00185         //.def(init<object, std::string, std::string, char>())
    00186         //.def(init<object, std::string, std::string>())
    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