engine errors thrown as Py error if in Py script
[Tsunagari.git] / src / python-optional.h
blob05da6743bf08d38b93ceedfb87710db6a8c3d5a3
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 #include <boost/optional.hpp>
13 #include <boost/python.hpp>
16 namespace boost { namespace python {
18 template<typename T>
19 struct optional_ : private boost::noncopyable {
20 struct conversion :
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(
36 PyObject* obj,
37 boost::python::converter::rvalue_from_python_stage1_data* data
38 ) {
39 using namespace boost::python;
40 void* const storage = reinterpret_cast<
41 converter::rvalue_from_python_storage<optional<T> >*
42 >(data)->storage.bytes;
43 if (obj == Py_None) {
44 new (storage) boost::optional<T>();
45 } else {
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(
56 &convertible,
57 &constructor,
58 type_id<boost::optional<T> >(),
59 &conversion::get_pytype
65 } } // namespace boost::python