mapnik/bindings/python/mapnik_parameters.cpp

192 lines
5.2 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
2006-02-01 00:09:52 +01:00
*
2006-03-31 12:32:02 +02:00
* Copyright (C) 2006 Artem Pavlenko, Jean-Francois Doyon
2006-02-01 00:09:52 +01:00
*
2006-03-31 12:32:02 +02: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-02-01 00:09:52 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02: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-02-01 00:09:52 +01:00
*
2006-03-31 12:32:02 +02:00
*****************************************************************************/
//$Id: mapnik_parameters.cpp 17 2005-03-08 23:58:43Z pavlenko $
2006-02-01 00:09:52 +01:00
// boost
2006-02-01 00:09:52 +01:00
#include <boost/python.hpp>
// mapnik
#include <mapnik/params.hpp>
2006-02-01 00:09:52 +01:00
using mapnik::parameter;
using mapnik::parameters;
struct pickle_value : public boost::static_visitor<>
{
2010-06-02 13:03:30 +02:00
public:
pickle_value( boost::python::list vals):
vals_(vals) {}
2010-06-02 13:03:30 +02:00
void operator () ( int val )
{
vals_.append(val);
}
2010-06-02 13:03:30 +02:00
void operator () ( double val )
{
vals_.append(val);
}
2010-06-02 13:03:30 +02:00
void operator () ( std::string val )
{
vals_.append(val);
}
2010-06-02 13:03:30 +02:00
private:
boost::python::list vals_;
};
2006-02-01 00:09:52 +01:00
struct parameter_pickle_suite : boost::python::pickle_suite
{
static boost::python::tuple
getinitargs(const parameter& p)
{
using namespace boost::python;
return boost::python::make_tuple(p.first,boost::get<std::string>(p.second));
2006-02-01 00:09:52 +01: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())
{
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-02-01 00:09:52 +01: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,
2010-06-02 13:03:30 +02:00
("expected 1-item tuple in call to __setstate__; got %s"
% state).ptr()
);
2006-02-01 00:09:52 +01:00
throw_error_already_set();
}
2006-02-01 00:09:52 +01:00
dict d = extract<dict>(state[0]);
boost::python::list keys = d.keys();
for (int i=0; i<len(keys); ++i)
2006-02-01 00:09:52 +01: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())
{
2010-06-02 13:03:30 +02:00
p[key] = ex0();
}
else if (ex1.check())
{
2010-06-02 13:03:30 +02:00
p[key] = ex1();
}
else if (ex2.check())
{
2010-06-02 13:03:30 +02:00
p[key] = ex2();
}
/*
2010-06-02 13:03:30 +02:00
extract_value serializer( p, key );
mapnik::value_holder val = extract<mapnik::value_holder>(d[key]);
boost::apply_visitor( serializer, val );
*/
}
2006-02-01 00:09:52 +01: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-02-01 00:09:52 +01:00
void export_parameters()
{
using namespace boost::python;
class_<parameter>("Parameter",init<std::string,std::string>())
2006-02-01 00:09:52 +01:00
.def_pickle(parameter_pickle_suite())
.def("as_dict",dict_param)
.def("as_tuple",tuple_param)
2006-02-01 00:09:52 +01:00
;
class_<parameters>("Parameters",init<>())
2006-02-01 00:09:52 +01:00
.def_pickle(parameters_pickle_suite())
.def("as_dict",dict_params)
.def("as_list",list_params)
2006-02-01 00:09:52 +01:00
;
2010-06-02 13:03:30 +02:00
}