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 #include <boost/optional.hpp>
13 #include <boost/python.hpp>
16 namespace boost
{ namespace python
{
19 struct optional_
: private boost::noncopyable
{
21 public boost::python::converter::expected_from_python_type
<T
>
23 static PyObject
* convert(boost::optional
<T
> const& value
)
25 using namespace boost::python
;
26 return incref((value
? object(*value
) : object()).ptr());
30 static void* convertible(PyObject
* obj
) {
31 using namespace boost::python
;
32 return obj
== Py_None
|| extract
<T
>(obj
).check() ? obj
: NULL
;
35 static void constructor(
37 boost::python::converter::rvalue_from_python_stage1_data
* data
39 using namespace boost::python
;
40 void* const storage
= reinterpret_cast<
41 converter::rvalue_from_python_storage
<optional
<T
> >*
42 >(data
)->storage
.bytes
;
44 new (storage
) boost::optional
<T
>();
46 new (storage
) boost::optional
<T
>(extract
<T
>(obj
));
48 data
->convertible
= storage
;
51 explicit optional_() {
52 using namespace boost::python
;
53 if (!extract
<boost::optional
<T
> >(object()).check()) {
54 to_python_converter
<boost::optional
<T
>, conversion
, true>();
55 converter::registry::push_back(
58 type_id
<boost::optional
<T
> >(),
59 &conversion::get_pytype
65 } } // namespace boost::python