diff options
author | mgsergio <mgsergio@yandex.ru> | 2017-04-04 17:54:02 +0300 |
---|---|---|
committer | Sergey Yershov <syershov@maps.me> | 2017-04-04 17:54:02 +0300 |
commit | b5b35c2f3ca0e575506c3cba2b2879d5a16276b6 (patch) | |
tree | 8783b05e2490ed83b9ac346c4cea53ef8bd1986d /pyhelpers | |
parent | cec1817da3845ae94665dfe294d824766802972d (diff) |
Add Campaign serialization. Add python bindings.
Diffstat (limited to 'pyhelpers')
-rw-r--r-- | pyhelpers/vector_list_conversion.hpp | 15 | ||||
-rw-r--r-- | pyhelpers/vector_uint8.hpp | 21 |
2 files changed, 23 insertions, 13 deletions
diff --git a/pyhelpers/vector_list_conversion.hpp b/pyhelpers/vector_list_conversion.hpp index 2aa12a9c61..52ef1f1080 100644 --- a/pyhelpers/vector_list_conversion.hpp +++ b/pyhelpers/vector_list_conversion.hpp @@ -1,6 +1,6 @@ #pragma once -#include "std/vector.hpp" +#include <vector> #include <boost/python.hpp> #include <boost/python/stl_iterator.hpp> @@ -8,16 +8,19 @@ namespace { template <typename T> -vector<T> python_list_to_std_vector(boost::python::object const & iterable) +std::vector<T> python_list_to_std_vector(boost::python::object const & iterable) { - return vector<T>(boost::python::stl_input_iterator<T>(iterable), - boost::python::stl_input_iterator<T>()); + return std::vector<T>(boost::python::stl_input_iterator<T>(iterable), + boost::python::stl_input_iterator<T>()); } +// For this to work one should define +// class_<std::vector<YourClass>>("YourClassList") +// .def(vector_indexing_suite<std::vector<YourClass>>()); template <typename T> -boost::python::list std_vector_to_python_list(vector<T> const & v) +boost::python::list std_vector_to_python_list(std::vector<T> const & v) { - boost::python::object get_iter = boost::python::iterator<vector<T>>(); + boost::python::object get_iter = boost::python::iterator<std::vector<T>>(); return boost::python::list(get_iter(v)); } } // namespace diff --git a/pyhelpers/vector_uint8.hpp b/pyhelpers/vector_uint8.hpp index f1bebc5ff3..3f74e97445 100644 --- a/pyhelpers/vector_uint8.hpp +++ b/pyhelpers/vector_uint8.hpp @@ -1,6 +1,13 @@ #pragma once -#include "std/vector.hpp" +#include <vector> + +// These headers are necessary for cross-python compilation. +// Python3 does not have PyString_* methods. One should use PyBytes_* instead. +// bytesobject.h contains a mapping from PyBytes_* to PyString_*. +// See https://docs.python.org/2/howto/cporting.html for more. +#include "Python.h" +#include "bytesobject.h" #include <boost/python.hpp> #include <boost/python/suite/indexing/map_indexing_suite.hpp> @@ -12,7 +19,7 @@ using namespace boost::python; // Converts a vector<uint8_t> to/from Python str. struct vector_uint8t_to_str { - static PyObject * convert(vector<uint8_t> const & v) + static PyObject * convert(std::vector<uint8_t> const & v) { str s(reinterpret_cast<char const *>(v.data()), v.size()); return incref(s.ptr()); @@ -23,24 +30,24 @@ struct vector_uint8t_from_python_str { vector_uint8t_from_python_str() { - converter::registry::push_back(&convertible, &construct, type_id<vector<uint8_t>>()); + converter::registry::push_back(&convertible, &construct, type_id<std::vector<uint8_t>>()); } static void * convertible(PyObject * obj_ptr) { - if (!PyString_Check(obj_ptr)) + if (!PyBytes_Check(obj_ptr)) return nullptr; return obj_ptr; } static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data) { - const char * value = PyString_AsString(obj_ptr); + const char * value = PyBytes_AsString(obj_ptr); if (value == nullptr) throw_error_already_set(); void * storage = - ((converter::rvalue_from_python_storage<vector<uint8_t>> *)data)->storage.bytes; - new (storage) vector<uint8_t>(value, value + PyString_Size(obj_ptr)); + ((converter::rvalue_from_python_storage<std::vector<uint8_t>> *)data)->storage.bytes; + new (storage) std::vector<uint8_t>(value, value + PyBytes_Size(obj_ptr)); data->convertible = storage; } }; |