+expose parameters as python list and dict, and add pickle support by taking advantage of boost visitation - needs further review - see #345
This commit is contained in:
parent
5aa1fda60e
commit
92ca0a8ac9
1 changed files with 111 additions and 10 deletions
|
@ -21,20 +21,48 @@
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
//$Id: mapnik_parameters.cpp 17 2005-03-08 23:58:43Z pavlenko $
|
//$Id: mapnik_parameters.cpp 17 2005-03-08 23:58:43Z pavlenko $
|
||||||
|
|
||||||
|
// boost
|
||||||
#include <boost/python.hpp>
|
#include <boost/python.hpp>
|
||||||
#include <boost/python/detail/api_placeholder.hpp>
|
|
||||||
|
// mapnik
|
||||||
#include <mapnik/params.hpp>
|
#include <mapnik/params.hpp>
|
||||||
|
|
||||||
using mapnik::parameter;
|
using mapnik::parameter;
|
||||||
using mapnik::parameters;
|
using mapnik::parameters;
|
||||||
|
|
||||||
|
struct pickle_value : public boost::static_visitor<>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
pickle_value( boost::python::list vals):
|
||||||
|
vals_(vals) {}
|
||||||
|
|
||||||
|
void operator () ( int val )
|
||||||
|
{
|
||||||
|
vals_.append(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator () ( double val )
|
||||||
|
{
|
||||||
|
vals_.append(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator () ( std::string val )
|
||||||
|
{
|
||||||
|
vals_.append(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
boost::python::list vals_;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
struct parameter_pickle_suite : boost::python::pickle_suite
|
struct parameter_pickle_suite : boost::python::pickle_suite
|
||||||
{
|
{
|
||||||
static boost::python::tuple
|
static boost::python::tuple
|
||||||
getinitargs(const parameter& p)
|
getinitargs(const parameter& p)
|
||||||
{
|
{
|
||||||
using namespace boost::python;
|
using namespace boost::python;
|
||||||
return boost::python::make_tuple(p.first,p.second);
|
return boost::python::make_tuple(p.first,boost::get<std::string>(p.second));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,7 +76,11 @@ struct parameters_pickle_suite : boost::python::pickle_suite
|
||||||
parameters::const_iterator pos=p.begin();
|
parameters::const_iterator pos=p.begin();
|
||||||
while(pos!=p.end())
|
while(pos!=p.end())
|
||||||
{
|
{
|
||||||
d[pos->first]=pos->second;
|
boost::python::list vals;
|
||||||
|
pickle_value serializer( vals );
|
||||||
|
mapnik::value_holder val = pos->second;
|
||||||
|
boost::apply_visitor( serializer, val );
|
||||||
|
d[pos->first] = vals[0];
|
||||||
++pos;
|
++pos;
|
||||||
}
|
}
|
||||||
return boost::python::make_tuple(d);
|
return boost::python::make_tuple(d);
|
||||||
|
@ -65,26 +97,95 @@ struct parameters_pickle_suite : boost::python::pickle_suite
|
||||||
);
|
);
|
||||||
throw_error_already_set();
|
throw_error_already_set();
|
||||||
}
|
}
|
||||||
|
|
||||||
dict d = extract<dict>(state[0]);
|
dict d = extract<dict>(state[0]);
|
||||||
boost::python::list keys = d.keys();
|
boost::python::list keys = d.keys();
|
||||||
for (int i=0; i<len(keys); ++i)
|
for (int i=0; i<len(keys); ++i)
|
||||||
{
|
{
|
||||||
std::string key = extract<std::string>(keys[i]);
|
std::string key = extract<std::string>(keys[i]);
|
||||||
std::string value=extract<std::string>(d[key]); // FIXME: use boost::variant as a value object. Add Py_Int/Py_Float/Py_String extractors.
|
object obj = d[key];
|
||||||
p[key] = value;
|
extract<std::string> ex0(obj);
|
||||||
|
extract<int> ex1(obj);
|
||||||
|
extract<double> ex2(obj);
|
||||||
|
|
||||||
|
if (ex0.check())
|
||||||
|
{
|
||||||
|
p[key] = ex0();
|
||||||
|
}
|
||||||
|
else if (ex1.check())
|
||||||
|
{
|
||||||
|
p[key] = ex1();
|
||||||
|
}
|
||||||
|
else if (ex2.check())
|
||||||
|
{
|
||||||
|
p[key] = ex2();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
extract_value serializer( p, key );
|
||||||
|
mapnik::value_holder val = extract<mapnik::value_holder>(d[key]);
|
||||||
|
boost::apply_visitor( serializer, val );
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
boost::python::dict dict_params(parameters& p)
|
||||||
|
{
|
||||||
|
boost::python::dict d;
|
||||||
|
parameters::const_iterator pos=p.begin();
|
||||||
|
while(pos!=p.end())
|
||||||
|
{
|
||||||
|
boost::python::list vals;
|
||||||
|
pickle_value serializer( vals );
|
||||||
|
mapnik::value_holder val = pos->second;
|
||||||
|
boost::apply_visitor( serializer, val );
|
||||||
|
d[pos->first] = vals[0];
|
||||||
|
++pos;
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::python::list list_params(parameters& p)
|
||||||
|
{
|
||||||
|
boost::python::list l;
|
||||||
|
parameters::const_iterator pos=p.begin();
|
||||||
|
while(pos!=p.end())
|
||||||
|
{
|
||||||
|
boost::python::list vals;
|
||||||
|
pickle_value serializer( vals );
|
||||||
|
mapnik::value_holder val = pos->second;
|
||||||
|
boost::apply_visitor( serializer, val );
|
||||||
|
l.append(boost::python::make_tuple(pos->first,vals[0]));
|
||||||
|
++pos;
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::python::dict dict_param(parameter& p)
|
||||||
|
{
|
||||||
|
boost::python::dict d;
|
||||||
|
d[p.first] = boost::get<std::string>(p.second);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::python::tuple tuple_param(parameter& p)
|
||||||
|
{
|
||||||
|
return boost::python::make_tuple(p.first,boost::get<std::string>(p.second));
|
||||||
|
}
|
||||||
|
|
||||||
void export_parameters()
|
void export_parameters()
|
||||||
{
|
{
|
||||||
using namespace boost::python;
|
using namespace boost::python;
|
||||||
class_<parameter>("Parameter",init<std::string,std::string>())
|
class_<parameter>("Parameter",init<std::string,std::string>())
|
||||||
.def_pickle(parameter_pickle_suite())
|
.def_pickle(parameter_pickle_suite())
|
||||||
|
.def("as_dict",dict_param)
|
||||||
|
.def("as_tuple",tuple_param)
|
||||||
;
|
;
|
||||||
|
|
||||||
class_<parameters>("Parameters",init<>())
|
class_<parameters>("Parameters",init<>())
|
||||||
.def_pickle(parameters_pickle_suite())
|
.def_pickle(parameters_pickle_suite())
|
||||||
|
.def("as_dict",dict_params)
|
||||||
|
.def("as_list",list_params)
|
||||||
;
|
;
|
||||||
}
|
}
|
Loading…
Reference in a new issue