2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2006-01-31 23:09:52 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon
|
2006-01-31 23:09:52 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2006-01-31 23:09:52 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-01-31 23:09:52 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2006-09-11 09:48:27 +00:00
|
|
|
//$Id: mapnik_parameters.cpp 17 2005-03-08 23:58:43Z pavlenko $
|
2006-01-31 23:09:52 +00:00
|
|
|
|
2009-05-24 06:12:32 +00:00
|
|
|
// boost
|
2006-01-31 23:09:52 +00:00
|
|
|
#include <boost/python.hpp>
|
2009-05-24 06:12:32 +00:00
|
|
|
|
|
|
|
// mapnik
|
2006-10-04 11:22:18 +00:00
|
|
|
#include <mapnik/params.hpp>
|
2006-01-31 23:09:52 +00:00
|
|
|
|
|
|
|
using mapnik::parameter;
|
|
|
|
using mapnik::parameters;
|
|
|
|
|
2009-05-24 06:12:32 +00:00
|
|
|
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_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2006-01-31 23:09:52 +00:00
|
|
|
struct parameter_pickle_suite : boost::python::pickle_suite
|
|
|
|
{
|
|
|
|
static boost::python::tuple
|
|
|
|
getinitargs(const parameter& p)
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
2009-05-24 06:12:32 +00:00
|
|
|
return boost::python::make_tuple(p.first,boost::get<std::string>(p.second));
|
2006-01-31 23:09:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct parameters_pickle_suite : boost::python::pickle_suite
|
|
|
|
{
|
|
|
|
static boost::python::tuple
|
|
|
|
getstate(const parameters& p)
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
|
|
|
dict d;
|
|
|
|
parameters::const_iterator pos=p.begin();
|
|
|
|
while(pos!=p.end())
|
|
|
|
{
|
2009-05-24 06:12:32 +00:00
|
|
|
boost::python::list vals;
|
|
|
|
pickle_value serializer( vals );
|
|
|
|
mapnik::value_holder val = pos->second;
|
|
|
|
boost::apply_visitor( serializer, val );
|
|
|
|
d[pos->first] = vals[0];
|
2006-01-31 23:09:52 +00:00
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
return boost::python::make_tuple(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setstate(parameters& p, boost::python::tuple state)
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
|
|
|
if (len(state) != 1)
|
|
|
|
{
|
|
|
|
PyErr_SetObject(PyExc_ValueError,
|
|
|
|
("expected 1-item tuple in call to __setstate__; got %s"
|
|
|
|
% state).ptr()
|
|
|
|
);
|
|
|
|
throw_error_already_set();
|
|
|
|
}
|
2009-05-24 06:12:32 +00:00
|
|
|
|
2006-01-31 23:09:52 +00:00
|
|
|
dict d = extract<dict>(state[0]);
|
2009-05-24 06:12:32 +00:00
|
|
|
boost::python::list keys = d.keys();
|
|
|
|
for (int i=0; i<len(keys); ++i)
|
2006-01-31 23:09:52 +00:00
|
|
|
{
|
2009-05-24 06:12:32 +00:00
|
|
|
std::string key = extract<std::string>(keys[i]);
|
|
|
|
object obj = d[key];
|
|
|
|
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 );
|
|
|
|
*/
|
|
|
|
}
|
2006-01-31 23:09:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-24 06:12:32 +00:00
|
|
|
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));
|
|
|
|
}
|
2006-01-31 23:09:52 +00:00
|
|
|
|
|
|
|
void export_parameters()
|
|
|
|
{
|
|
|
|
using namespace boost::python;
|
2006-02-20 01:34:02 +00:00
|
|
|
class_<parameter>("Parameter",init<std::string,std::string>())
|
2006-01-31 23:09:52 +00:00
|
|
|
.def_pickle(parameter_pickle_suite())
|
2009-05-24 06:12:32 +00:00
|
|
|
.def("as_dict",dict_param)
|
|
|
|
.def("as_tuple",tuple_param)
|
2006-01-31 23:09:52 +00:00
|
|
|
;
|
|
|
|
|
2006-02-20 01:34:02 +00:00
|
|
|
class_<parameters>("Parameters",init<>())
|
2006-01-31 23:09:52 +00:00
|
|
|
.def_pickle(parameters_pickle_suite())
|
2009-05-24 06:12:32 +00:00
|
|
|
.def("as_dict",dict_params)
|
|
|
|
.def("as_list",list_params)
|
2006-01-31 23:09:52 +00:00
|
|
|
;
|
2009-05-24 06:12:32 +00:00
|
|
|
}
|