Skip to content

Commit

Permalink
add to/from specialisation for boost::optional<bool>
Browse files Browse the repository at this point in the history
  • Loading branch information
artemp committed May 17, 2013
1 parent cd2dd66 commit 392561c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
13 changes: 1 addition & 12 deletions bindings/python/mapnik_raster_symbolizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,7 @@
#include <mapnik/raster_colorizer.hpp>
#include <mapnik/image_scaling.hpp>

namespace {

// https://github.com/mapnik/mapnik/issues/1367
PyObject* get_premultiplied_impl(mapnik::raster_symbolizer & sym)
{
boost::optional<bool> premultiplied = sym.premultiplied();
if (premultiplied)
return ::PyBool_FromLong(*premultiplied);
Py_RETURN_NONE;
}

}
using mapnik::raster_symbolizer;

void export_raster_symbolizer()
Expand Down Expand Up @@ -132,7 +121,7 @@ void export_raster_symbolizer()
">>> r.mesh_size = 32\n"
)
.add_property("premultiplied",
&get_premultiplied_impl,
&raster_symbolizer::premultiplied,
&raster_symbolizer::set_premultiplied,
"Get/Set premultiplied status of the source image.\n"
"Can be used to override what the source data reports (when in error)\n"
Expand Down
54 changes: 52 additions & 2 deletions bindings/python/python_optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct python_optional : public mapnik::noncopyable
}
};

// to/from boost::optional<float>
// to/from boost::optional<bool>
template <>
struct python_optional<float> : public mapnik::noncopyable
{
Expand Down Expand Up @@ -130,7 +130,7 @@ struct python_optional<float> : public mapnik::noncopyable
boost::python::converter::rvalue_from_python_stage1_data * data)
{
using namespace boost::python::converter;
void * const storage = ((rvalue_from_python_storage<boost::optional<float> > *)
void * const storage = ((rvalue_from_python_storage<boost::optional<bool> > *)
data)->storage.bytes;
if (source == Py_None) // == None
new (storage) boost::optional<float>(); // A Boost uninitialized value
Expand All @@ -147,6 +147,56 @@ struct python_optional<float> : public mapnik::noncopyable
}
};

// to/from boost::optional<float>
template <>
struct python_optional<bool> : public mapnik::noncopyable
{
struct optional_to_python
{
static PyObject * convert(const boost::optional<bool>& value)
{
if (value)
{
if (*value) Py_RETURN_TRUE;
else Py_RETURN_FALSE;
}
else return boost::python::detail::none();
}
};
struct optional_from_python
{
static void * convertible(PyObject * source)
{
using namespace boost::python::converter;

if (source == Py_None || PyBool_Check(source))
return source;
return 0;
}

static void construct(PyObject * source,
boost::python::converter::rvalue_from_python_stage1_data * data)
{
using namespace boost::python::converter;
void * const storage = ((rvalue_from_python_storage<boost::optional<bool> > *)
data)->storage.bytes;
if (source == Py_None) // == None
new (storage) boost::optional<bool>(); // A Boost uninitialized value
else
{
new (storage) boost::optional<bool>(source == Py_True ? true : false);
}
data->convertible = storage;
}
};

explicit python_optional()
{
register_python_conversion<boost::optional<bool>,
optional_to_python, optional_from_python>();
}
};


// This class works around a feature in boost python.
// See http://osdir.com/ml/python.c++/2003-11/msg00158.html
Expand Down

0 comments on commit 392561c

Please sign in to comment.