1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** python-optional.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
9 * http://stackoverflow.com/questions/6274822/boost-python-no-to-python-converter-found-for-stdstring
12 #ifndef PYTHON_OPTIONAL_H
13 #define PYTHON_OPTIONAL_H
15 #include <boost/optional.hpp>
16 #include <boost/python.hpp>
19 namespace boost
{ namespace python
{
22 struct optional_
: private boost::noncopyable
{
24 public boost::python::converter::expected_from_python_type
<T
>
26 static PyObject
* convert(boost::optional
<T
> const& value
)
28 using namespace boost::python
;
29 return incref((value
? object(*value
) : object()).ptr());
33 static void* convertible(PyObject
* obj
) {
34 using namespace boost::python
;
35 return obj
== Py_None
|| extract
<T
>(obj
).check() ? obj
: NULL
;
38 static void constructor(
40 boost::python::converter::rvalue_from_python_stage1_data
* data
42 using namespace boost::python
;
43 void* const storage
= reinterpret_cast<
44 converter::rvalue_from_python_storage
<optional
<T
> >*
45 >(data
)->storage
.bytes
;
47 new (storage
) boost::optional
<T
>();
49 new (storage
) boost::optional
<T
>(extract
<T
>(obj
));
51 data
->convertible
= storage
;
54 explicit optional_() {
55 using namespace boost::python
;
56 if (!extract
<boost::optional
<T
> >(object()).check()) {
57 to_python_converter
<boost::optional
<T
>, conversion
, true>();
58 converter::registry::push_back(
61 type_id
<boost::optional
<T
> >(),
62 &conversion::get_pytype
68 } } // namespace boost::python