Commit 30726c1b authored by Carlos Mastalli's avatar Carlos Mastalli
Browse files

[cleanup] Used bp (instead of boost::python) in converters

parent 888ae833
///////////////////////////////////////////////////////////////////////////////
// BSD 3-Clause License
//
// Copyright (C) 2019-2020, LAAS-CNRS, University of Edinburgh,
// Copyright (C) 2020, INRIA
// Copyright (C) 2019-2022, LAAS-CNRS, University of Edinburgh, INRIA
// Copyright note valid unless otherwise stated in individual files.
// All rights reserved.
///////////////////////////////////////////////////////////////////////////////
......@@ -42,8 +41,7 @@ struct PickleMap : public PickleVector<Container> {
template <typename Container>
struct dict_to_map {
static void register_converter() {
boost::python::converter::registry::push_back(&dict_to_map::convertible, &dict_to_map::construct,
boost::python::type_id<Container>());
bp::converter::registry::push_back(&dict_to_map::convertible, &dict_to_map::construct, bp::type_id<Container>());
}
/// Check if conversion is possible
......@@ -54,8 +52,8 @@ struct dict_to_map {
}
/// Perform the conversion
static void construct(PyObject* object, boost::python::converter::rvalue_from_python_stage1_data* data) {
// convert the PyObject pointed to by `object` to a boost::python::dict
static void construct(PyObject* object, bp::converter::rvalue_from_python_stage1_data* data) {
// convert the PyObject pointed to by `object` to a bp::dict
bp::handle<> handle(bp::borrowed(object)); // "smart ptr"
bp::dict dict(handle);
......@@ -96,7 +94,7 @@ struct dict_to_map {
data->convertible = storage;
}
static boost::python::dict todict(Container& self) {
static bp::dict todict(Container& self) {
bp::dict dict;
typename Container::const_iterator it;
for (it = self.begin(); it != self.end(); ++it) {
......@@ -116,14 +114,13 @@ struct dict_to_map {
*/
template <class Key, class T, class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >, bool NoProxy = false>
struct StdMapPythonVisitor
: public boost::python::map_indexing_suite<typename std::map<Key, T, Compare, Allocator>, NoProxy>,
public dict_to_map<std::map<Key, T, Compare, Allocator> > {
struct StdMapPythonVisitor : public bp::map_indexing_suite<typename std::map<Key, T, Compare, Allocator>, NoProxy>,
public dict_to_map<std::map<Key, T, Compare, Allocator> > {
typedef std::map<Key, T, Compare, Allocator> Container;
typedef dict_to_map<Container> FromPythonDictConverter;
static void expose(const std::string& class_name, const std::string& doc_string = "") {
namespace bp = boost::python;
namespace bp = bp;
bp::class_<Container>(class_name.c_str(), doc_string.c_str())
.def(StdMapPythonVisitor())
......
///////////////////////////////////////////////////////////////////////////////
// BSD 3-Clause License
//
// Copyright (C) 2022, University of Oxford
// Copyright (C) 2019-2020, LAAS-CNRS, University of Edinburgh
// Copyright (C) 2020, INRIA
// Copyright (C) 2019-2022, LAAS-CNRS, University of Edinburgh, INRIA
// University of Oxford
// Copyright note valid unless otherwise stated in individual files.
// All rights reserved.
///////////////////////////////////////////////////////////////////////////////
......@@ -30,19 +29,21 @@ namespace bp = boost::python;
* \sa Pickle
*/
template <typename Container>
struct PickleSet : boost::python::pickle_suite {
static boost::python::tuple getinitargs(const Container&) { return boost::python::make_tuple(); }
static boost::python::tuple getstate(boost::python::object op) {
struct PickleSet : bp::pickle_suite {
static bp::tuple getinitargs(const Container&) { return bp::make_tuple(); }
static bp::tuple getstate(bp::object op) {
bp::list list;
const Container& ret = boost::python::extract<const Container&>(op);
const Container& ret = bp::extract<const Container&>(op);
for (const auto& it : ret) {
list.append(it);
}
return boost::python::make_tuple(list);
return bp::make_tuple(list);
}
static void setstate(boost::python::object op, boost::python::tuple tup) {
Container& o = boost::python::extract<Container&>(op)();
boost::python::stl_input_iterator<typename Container::value_type> begin(tup[0]), end;
static void setstate(bp::object op, bp::tuple tup) {
Container& o = bp::extract<Container&>(op)();
bp::stl_input_iterator<typename Container::value_type> begin(tup[0]), end;
o.insert(begin, end);
}
};
......@@ -52,8 +53,7 @@ template <typename Container>
struct set_to_set {
/** @note Registers converter from a python iterable type to the provided type. */
static void register_converter() {
boost::python::converter::registry::push_back(&set_to_set::convertible, &set_to_set::construct,
boost::python::type_id<Container>());
bp::converter::registry::push_back(&set_to_set::convertible, &set_to_set::construct, bp::type_id<Container>());
}
/** @brief Check if PyObject is iterable. */
......@@ -78,7 +78,7 @@ struct set_to_set {
* Container Concept requirements:
* * Container::value_type is CopyConstructable.
*/
static void construct(PyObject* object, boost::python::converter::rvalue_from_python_stage1_data* data) {
static void construct(PyObject* object, bp::converter::rvalue_from_python_stage1_data* data) {
// Object is a borrowed reference, so create a handle indicting it is
// borrowed for proper reference counting.
bp::handle<> handle(bp::borrowed(object));
......@@ -99,7 +99,7 @@ struct set_to_set {
data->convertible = storage;
}
static boost::python::object toset(Container& self) {
static bp::object toset(Container& self) {
PyObject* set = PySet_New(NULL);
for (auto it = self.begin(); it != self.end(); ++it) {
PySet_Add(set, bp::object(*it).ptr());
......@@ -125,8 +125,6 @@ struct StdSetPythonVisitor : public set_to_set<std::set<T, Compare, Allocator>>
typedef set_to_set<Container> FromPythonSetConverter;
static void expose(const std::string& class_name, const std::string& doc_string = "") {
namespace bp = boost::python;
bp::class_<Container>(class_name.c_str(), doc_string.c_str())
// .def(StdSetPythonVisitor()); // TODO: Needs an indexing_suite for
// set
......
///////////////////////////////////////////////////////////////////////////////
// BSD 3-Clause License
//
// Copyright (C) 2019-2020, LAAS-CNRS, University of Edinburgh
// Copyright (C) 2020, INRIA
// Copyright (C) 2019-2022, LAAS-CNRS, University of Edinburgh, INRIA
// Copyright note valid unless otherwise stated in individual files.
// All rights reserved.
///////////////////////////////////////////////////////////////////////////////
......@@ -27,14 +26,12 @@ namespace bp = boost::python;
* \sa Pickle
*/
template <typename Container>
struct PickleVector : boost::python::pickle_suite {
static boost::python::tuple getinitargs(const Container&) { return boost::python::make_tuple(); }
static boost::python::tuple getstate(boost::python::object op) {
return boost::python::make_tuple(boost::python::list(boost::python::extract<const Container&>(op)()));
}
static void setstate(boost::python::object op, boost::python::tuple tup) {
Container& o = boost::python::extract<Container&>(op)();
boost::python::stl_input_iterator<typename Container::value_type> begin(tup[0]), end;
struct PickleVector : bp::pickle_suite {
static bp::tuple getinitargs(const Container&) { return bp::make_tuple(); }
static bp::tuple getstate(bp::object op) { return bp::make_tuple(bp::list(bp::extract<const Container&>(op)())); }
static void setstate(bp::object op, bp::tuple tup) {
Container& o = bp::extract<Container&>(op)();
bp::stl_input_iterator<typename Container::value_type> begin(tup[0]), end;
o.insert(o.begin(), begin, end);
}
};
......@@ -44,8 +41,8 @@ template <typename Container>
struct list_to_vector {
/** @note Registers converter from a python iterable type to the provided type. */
static void register_converter() {
boost::python::converter::registry::push_back(&list_to_vector::convertible, &list_to_vector::construct,
boost::python::type_id<Container>());
bp::converter::registry::push_back(&list_to_vector::convertible, &list_to_vector::construct,
bp::type_id<Container>());
}
/** @brief Check if PyObject is iterable. */
......@@ -73,7 +70,7 @@ struct list_to_vector {
* * Container can be constructed and populated with two iterators.
* i.e. Container(begin, end)
*/
static void construct(PyObject* object, boost::python::converter::rvalue_from_python_stage1_data* data) {
static void construct(PyObject* object, bp::converter::rvalue_from_python_stage1_data* data) {
// Object is a borrowed reference, so create a handle indicting it is
// borrowed for proper reference counting.
bp::handle<> handle(bp::borrowed(object));
......@@ -94,7 +91,7 @@ struct list_to_vector {
data->convertible = storage;
}
static boost::python::list tolist(Container& self) {
static bp::list tolist(Container& self) {
typedef bp::iterator<Container> iterator;
bp::list list(iterator()(self));
return list;
......@@ -109,14 +106,13 @@ struct list_to_vector {
* @param[in] NoProxy When set to false, the elements will be copied when returned to Python.
*/
template <class T, class Allocator = std::allocator<T>, bool NoProxy = false>
struct StdVectorPythonVisitor
: public boost::python::vector_indexing_suite<typename std::vector<T, Allocator>, NoProxy>,
public list_to_vector<std::vector<T, Allocator> > {
struct StdVectorPythonVisitor : public bp::vector_indexing_suite<typename std::vector<T, Allocator>, NoProxy>,
public list_to_vector<std::vector<T, Allocator> > {
typedef std::vector<T, Allocator> Container;
typedef list_to_vector<Container> FromPythonListConverter;
static void expose(const std::string& class_name, const std::string& doc_string = "") {
namespace bp = boost::python;
namespace bp = bp;
bp::class_<Container>(class_name.c_str(), doc_string.c_str())
.def(StdVectorPythonVisitor())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment