Commit 58a8c176 authored by Pierre Fernbach's avatar Pierre Fernbach Committed by Pierre Fernbach
Browse files

[Cleaning] remove unused headers in bindings

parent da1929be
......@@ -39,10 +39,6 @@ SET(${PROJECT_NAME}_CONTAINER_HEADERS
)
SET(${PROJECT_NAME}_PYTHON_HEADERS
bindings/python/container/array.hpp
bindings/python/container/reference-wrapper.hpp
bindings/python/container/visitor.hpp
bindings/python/fwd.hpp
bindings/python/geometry/ellipsoid.hpp
bindings/python/geometry/expose-geometry.hpp
bindings/python/geometry/linear-cone.hpp
......@@ -53,7 +49,6 @@ SET(${PROJECT_NAME}_PYTHON_HEADERS
bindings/python/scenario/contact-phase.hpp
bindings/python/scenario/contact-sequence.hpp
bindings/python/scenario/expose-scenario.hpp
bindings/python/utils/base.hpp
bindings/python/utils/printable.hpp
)
......
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>
#ifndef __multicontact_api_python_container_array_hpp__
#define __multicontact_api_python_container_array_hpp__
/// Credit : code adapted from http://stackoverflow.com/questions/18882089/wrapping-arrays-in-boost-python
#include <string>
#include <typeinfo>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
namespace boost {
namespace python {
namespace bp = boost::python;
namespace details {
template <typename>
struct array_trait;
/// @brief Type that proxies to an array.
template <typename T>
class array_proxy {
public:
// Types
typedef T value_type;
typedef T* iterator;
typedef T& reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
/// @brief Empty constructor.
array_proxy() : ptr_(0), length_(0) {}
/// @brief Construct with iterators.
template <typename Iterator>
array_proxy(Iterator begin, Iterator end) : ptr_(&*begin), length_(std::distance(begin, end)) {}
/// @brief Construct with start and size.
array_proxy(reference begin, std::size_t length) : ptr_(&begin), length_(length) {}
// Iterator support.
iterator begin() { return ptr_; }
iterator end() { return ptr_ + length_; }
// Element access.
reference operator[](size_t i) { return ptr_[i]; }
// Capacity.
size_type size() { return length_; }
private:
T* ptr_;
std::size_t length_;
};
/// @brief Make an array_proxy.
template <typename T>
array_proxy<typename array_trait<T>::element_type> make_array_proxy(T& array) {
return array_proxy<typename array_trait<T>::element_type>(array[0], array_trait<T>::static_size);
}
/// @brief Policy type for referenced indexing, meeting the DerivedPolicies
/// requirement of bp::index_suite.
///
/// @note Requires Container to support:
/// - value_type and size_type types,
/// - value_type is default constructable and copyable,
/// - element access via operator[],
/// - Default constructable, iterator constructable,
/// - begin(), end(), and size() member functions
template <typename Container>
class ref_index_suite : public bp::indexing_suite<Container, ref_index_suite<Container> > {
public:
typedef typename Container::value_type data_type;
typedef typename Container::size_type index_type;
typedef typename Container::size_type size_type;
typedef typename Container::difference_type difference_type;
// Element access and manipulation.
/// @brief Get element from container.
static data_type& get_item(Container& container, index_type index) { return container[index]; }
/// @brief Set element from container.
static void set_item(Container& container, index_type index, const data_type& value) { container[index] = value; }
/// @brief Reset index to default value.
static void delete_item(Container& container, index_type index) { set_item(container, index, data_type()); };
// Slice support.
/// @brief Get slice from container.
///
/// @return Python object containing
static bp::object get_slice(Container& container, index_type from, index_type to) {
using bp::list;
if (from > to) return list();
// Return copy, as container only references its elements.
list list;
while (from != to) list.append(container[from++]);
return list;
};
/// @brief Set a slice in container with a given value.
static void set_slice(Container& container, index_type from, index_type to, const data_type& value) {
// If range is invalid, return early.
if (from > to) return;
// Populate range with value.
while (from < to) container[from++] = value;
}
/// @brief Set a slice in container with another range.
template <class Iterator>
static void set_slice(Container& container, index_type from, index_type to, Iterator first, Iterator /*last*/
) {
// If range is invalid, return early.
if (from > to) return;
// Populate range with other range.
while (from < to) container[from++] = *first++;
}
/// @brief Reset slice to default values.
static void delete_slice(Container& container, index_type from, index_type to) {
set_slice(container, from, to, data_type());
}
// Capacity.
/// @brief Get size of container.
static std::size_t size(Container& container) { return container.size(); }
/// @brief Check if a value is within the container.
template <class T>
static bool contains(Container& container, const T& value) {
return std::find(container.begin(), container.end(), value) != container.end();
}
/// @brief Compare two indexes
static bool compare_index(Container& /*container*/, index_type a, index_type b) { return a < b; }
/// @brief Minimum index supported for container.
static index_type get_min_index(Container& /*container*/) { return 0; }
/// @brief Maximum index supported for container.
static index_type get_max_index(Container& container) { return size(container); }
// Misc.
/// @brief Convert python index (could be negative) to a valid container
/// index with proper boundary checks.
static index_type convert_index(Container& container, PyObject* object) {
bp::extract<long> py_index(object);
// If py_index cannot extract a long, then type the type is wrong so
// set error and return early.
if (!py_index.check()) {
PyErr_SetString(PyExc_TypeError, "Invalid index type");
bp::throw_error_already_set();
return index_type();
}
// Extract index.
long index = py_index();
// Adjust negative index.
if (index < 0) index += container.size();
// Boundary check.
if (index >= long(container.size()) || index < 0) {
PyErr_SetString(PyExc_IndexError, "Index out of range");
bp::throw_error_already_set();
}
return (index_type)index;
}
};
/// @brief Trait for arrays.
template <typename T>
struct array_trait_impl;
// Specialize for native array.
template <typename T, std::size_t N>
struct array_trait_impl<T[N]> {
typedef T element_type;
enum { static_size = N };
typedef array_proxy<element_type> proxy_type;
typedef bp::default_call_policies policy;
typedef boost::mpl::vector<array_proxy<element_type> > signature;
};
// Specialize boost::array to use the native array trait.
template <typename T, std::size_t N>
struct array_trait_impl<boost::array<T, N> > : public array_trait_impl<T[N]> {};
// @brief Specialize for member objects to use and modify non member traits.
template <typename T, typename C>
struct array_trait_impl<T(C::*)> : public array_trait_impl<T> {
typedef bp::with_custodian_and_ward_postcall<0, // return object (custodian)
1 // self or this (ward)
>
policy;
// Append the class to the signature.
typedef typename boost::mpl::push_back<typename array_trait_impl<T>::signature, C&>::type signature;
};
/// @brief Trait class used to deduce array information, policies, and
/// signatures
template <typename T>
struct array_trait : public array_trait_impl<typename boost::remove_pointer<T>::type> {
typedef T native_type;
};
/// @brief Functor used used convert an array to an array_proxy for
/// non-member objects.
template <typename Trait>
struct array_proxy_getter {
public:
typedef typename Trait::native_type native_type;
typedef typename Trait::proxy_type proxy_type;
/// @brief Constructor.
array_proxy_getter(native_type array) : array_(array) {}
/// @brief Return an array_proxy for a member array object.
template <typename C>
proxy_type operator()(C& c) {
return make_array_proxy(c.*array_);
}
/// @brief Return an array_proxy for a non-member array object.
proxy_type operator()() { return make_array_proxy(*array_); }
private:
native_type array_;
};
/// @brief Conditionally register a type with Boost.Python.
template <typename Trait>
void register_array_proxy() {
typedef typename Trait::element_type element_type;
typedef typename Trait::proxy_type proxy_type;
// If type is already registered, then return early.
bool is_registered = (0 != bp::converter::registry::query(bp::type_id<proxy_type>())->to_python_target_type());
if (is_registered) return;
// Otherwise, register the type as an internal type.
const std::string type_name = std::string("_") + typeid(element_type).name();
bp::class_<proxy_type>(type_name.c_str(), bp::no_init).def(ref_index_suite<proxy_type>());
}
/// @brief Create a callable Boost.Python object that will return an
/// array_proxy type when called.
///
/// @note This function will conditionally register array_proxy types
/// for conversion within Boost.Python. The array_proxy will
/// extend the life of the object from which it was called.
/// For example, if `foo` is an object, and `vars` is an array,
/// then the object returned from `foo.vars` will extend the life
/// of `foo`.
template <typename Array>
bp::object make_array_aux(Array array) {
typedef array_trait<Array> trait_type;
// Register an array proxy.
register_array_proxy<trait_type>();
// Create function.
return bp::make_function(array_proxy_getter<trait_type>(array), typename trait_type::policy(),
typename trait_type::signature());
}
} // namespace details
/// @brief Create a callable Boost.Python object from an array.
template <typename T>
bp::object make_array(T array) {
return details::make_array_aux(array);
}
} // namespace python
} // namespace boost
#endif // __multicontact_api_python_container_array_hpp__
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>
//
#ifndef __multicontact_api_python_container_reference_wrapper_hpp__
#define __multicontact_api_python_container_reference_wrapper_hpp__
#include <boost/python.hpp>
#include <boost/ref.hpp>
#include <boost/python/to_python_indirect.hpp>
#include <typeinfo>
namespace multicontact_api {
namespace python {
namespace bp = boost::python;
template <typename reference_wrapper_type>
struct reference_wrapper_converter {
typedef reference_wrapper_type ref_type;
typedef typename reference_wrapper_type::type type;
static PyObject* convert(ref_type const& ref) {
ref_type* const p = &const_cast<ref_type&>(ref);
if (p == 0) return bp::detail::none();
return bp::detail::make_reference_holder::execute(&p->get());
}
static void expose() { bp::to_python_converter<ref_type, reference_wrapper_converter>(); }
};
} // namespace python
} // namespace multicontact_api
#endif // ifndef __multicontact_api_python_container_reference_wrapper_hpp__
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>
#ifndef __multcontact_api_python_container_visitor_hpp__
#define __multcontact_api_python_container_visitor_hpp__
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <string>
namespace multicontact_api {
namespace python {
namespace bp = boost::python;
template <typename Vector, bool NoProxy = false>
struct VectorPythonVisitor : public bp::def_visitor<VectorPythonVisitor<Vector> > {
static void expose(const std::string& class_name, const std::string& doc = "") {
bp::class_<Vector>(class_name.c_str(), doc.c_str()).def(bp::vector_indexing_suite<Vector, NoProxy>());
}
};
} // namespace python
} // namespace multicontact_api
#endif // ifndef __multcontact_api_python_container_visitor_hpp__
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>
#ifndef __multicontact_api_python_geometry_ellipsoid_hpp__
#define __multicontact_api_python_geometry_ellipsoid_hpp__
namespace multicontact_api {
namespace python {
namespace internal {
template <typename T>
struct build_type_name {
static const char* name();
static const char* shortname();
};
template <>
const char* build_type_name<double>::shortname() {
return "d";
}
} // namespace internal
} // namespace python
} // namespace multicontact_api
#endif // ifndef __multicontact_api_python_geometry_ellipsoid_hpp__
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>
#ifndef __multicontact_api_python_utils_base_hpp__
#define __multicontact_api_python_utils_base_hpp__
#include <boost/python.hpp>
#include <string>
namespace multicontact_api {
namespace python {
namespace bp = boost::python;
template <typename Derived>
struct BasePythonVisitor : public bp::def_visitor<BasePythonVisitor<Derived> > {
static void expose(const std::string& derived_class_name) {
const std::string base_class_name = derived_class_name + "Base";
const std::string base_doc = "Base class of " + derived_class_name;
bp::class_<typename Derived::Base>(base_class_name.c_str(), base_doc.c_str(), bp::no_init);
}
};
} // namespace python
} // namespace multicontact_api
#endif // ifndef __multicontact_api_python_utils_base_hpp__
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