diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-07 10:25:29 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:39 -0400 |
commit | 426a01b3f4163dcb5f0ca4bff6dbdcb3333616c4 (patch) | |
tree | 9e4456bae0efaef43bfa1aa55c178cb99796ea20 /src/py_utils.cc | |
parent | c211335760f2c5883aed34c31aeb6ce7e8e51bf9 (diff) | |
download | ledger-426a01b3f4163dcb5f0ca4bff6dbdcb3333616c4.tar.gz ledger-426a01b3f4163dcb5f0ca4bff6dbdcb3333616c4.tar.bz2 ledger-426a01b3f4163dcb5f0ca4bff6dbdcb3333616c4.zip |
Added by-value conversions to Python for bool. This allowed me to
expose all of the amount_t members now.
Diffstat (limited to 'src/py_utils.cc')
-rw-r--r-- | src/py_utils.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/py_utils.cc b/src/py_utils.cc index 0f82d683..4dfe8d7e 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -9,6 +9,40 @@ namespace ledger { using namespace boost::python; +struct bool_to_python +{ + static PyObject * convert(const bool truth) + { + if (truth) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } +}; + +struct bool_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + if (!PyBool_Check(obj_ptr)) return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, + converter::rvalue_from_python_stage1_data* data) + { + void* storage = ((converter::rvalue_from_python_storage<bool>*) data)->storage.bytes; + if (obj_ptr == Py_True) + new (storage) bool(true); + else + new (storage) bool(false); + data->convertible = storage; + } +}; + +typedef register_python_conversion<bool, bool_to_python, bool_from_python> + bool_python_conversion; + struct string_to_python { static PyObject* convert(const string& str) @@ -40,6 +74,7 @@ typedef register_python_conversion<string, string_to_python, string_from_python> void export_utils() { + bool_python_conversion(); string_python_conversion(); } |