FEATURE: Entity "stance" phase (fallback)
[Tsunagari.git] / src / python-optional.h
blob7c3a23cb516e56e2c517209271a688bd2e6b8f6c
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** python-optional.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 /*
8 * Based on
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 {
21 template<typename T>
22 struct optional_ : private boost::noncopyable {
23 struct conversion :
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(
39 PyObject* obj,
40 boost::python::converter::rvalue_from_python_stage1_data* data
41 ) {
42 using namespace boost::python;
43 void* const storage = reinterpret_cast<
44 converter::rvalue_from_python_storage<optional<T> >*
45 >(data)->storage.bytes;
46 if (obj == Py_None) {
47 new (storage) boost::optional<T>();
48 } else {
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(
59 &convertible,
60 &constructor,
61 type_id<boost::optional<T> >(),
62 &conversion::get_pytype
68 } } // namespace boost::python
70 #endif