hpp-python: include/pyhpp/util.hh Source File

hpp-python 9.0.2 python bindings for HPP, based on boost python |

Loading...
Searching...
No Matches
util.hh
Go to the documentation of this file.
1//
2// Copyright (c) 2018 - 2023 CNRS
3// Authors: Joseph Mirabel
4//
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
9
10// 1. Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12
13// 2. Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following
15// disclaimer in the documentation and/or other materials provided
16// with the distribution.
17
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29// OF THE POSSIBILITY OF SUCH DAMAGE.
30
31#ifndef PYHPP_FWD_HH
32#define PYHPP_FWD_HH
33
34#include <boost/python.hpp>
35#include <eigenpy/eigenpy.hpp>
36#include <hpp/util/pointer.hh>
37#include <vector>
38
39#define INIT_PYHPP_MODULE \
40 boost::python::docstring_options local_docstring_options(true, true, false)
41
42#define PYHPP_DEFINE_METHOD(CLASS, METHOD) def(#METHOD, &CLASS::METHOD)
43#define PYHPP_DEFINE_METHOD1(CLASS, METHOD, ARG1) \
44 def(#METHOD, &CLASS::METHOD, ARG1)
45#define PYHPP_DEFINE_METHOD2(CLASS, METHOD, ARG1, ARG2) \
46 def(#METHOD, &CLASS::METHOD, ARG1, ARG2)
47#define PYHPP_DEFINE_METHOD_INTERNAL_REF(CLASS, METHOD) \
48 def(#METHOD, &CLASS::METHOD, return_internal_reference<>())
49#define PYHPP_DEFINE_METHOD_CONST_REF(CLASS, METHOD) \
50 def(#METHOD, &CLASS::METHOD, return_value_policy<copy_const_reference>())
51#define PYHPP_DEFINE_METHOD_CONST_REF_BY_VALUE(CLASS, METHOD) \
52 def(#METHOD, &CLASS::METHOD, return_value_policy<return_by_value>())
53#define PYHPP_DEFINE_GETTER_SETTER(CLASS, METHOD, TYPE) \
54 def(#METHOD, static_cast<TYPE (CLASS::*)() const>(&CLASS::METHOD)) \
55 .def(#METHOD, static_cast<void (CLASS::*)(TYPE)>(&CLASS::METHOD))
56#define PYHPP_DEFINE_GETTER_SETTER_INTERNAL_REF(CLASS, METHOD, TYPE) \
57 def(#METHOD, static_cast<TYPE (CLASS::*)() const>(&CLASS::METHOD), \
58 return_internal_reference<>()) \
59 .def(#METHOD, static_cast<void (CLASS::*)(TYPE)>(&CLASS::METHOD))
60#define PYHPP_DEFINE_GETTER_SETTER_CONST_REF(CLASS, METHOD, TYPE) \
61 def(#METHOD, static_cast<TYPE (CLASS::*)() const>(&CLASS::METHOD)) \
62 .def(#METHOD, static_cast<void (CLASS::*)(const TYPE&)>(&CLASS::METHOD))
63#define PYHPP_DEFINE_METHOD_STATIC(CLASS, METHOD) \
64 def(#METHOD, &CLASS::METHOD).staticmethod(#METHOD)
65
66namespace pyhpp {
67template <typename ObjectWithPrintMethod>
68std::string to_str(const ObjectWithPrintMethod& obj) {
69 std::ostringstream oss;
70 obj.print(oss);
71 return oss.str();
72}
73template <typename ObjectWithPrintMethod>
74std::string to_str_from_operator(const ObjectWithPrintMethod& obj) {
75 std::ostringstream oss;
76 oss << obj;
77 return oss.str();
78}
79
80template <typename T, typename _Vector = std::vector<hpp::shared_ptr<T> > >
81 struct VectorOfPtr {
82 typedef _Vector Vector;
83 static T& get_item(Vector& v, std::size_t i) {
84 if (i > v.size()) throw std::invalid_argument("Out of range");
85 if (!v[i]) throw std::runtime_error("Null pointer");
86 return *v[i];
87 }
88};
89template <typename T>
90std::vector<T> extract_vector(boost::python::list py_list) {
91 std::vector<T> result;
92 result.reserve(boost::python::len(py_list));
93
94 for (int i = 0; i < boost::python::len(py_list); ++i) {
95 boost::python::extract<T> extractor(py_list[i]);
96 if (extractor.check()) {
97 result.push_back(extractor());
98 } else {
99 throw std::runtime_error("Failed to extract element");
100 }
101 }
102 return result;
103}
104template <typename T>
105boost::python::list to_python_list(const std::vector<T>& vec) {
106 boost::python::list py_list;
107 for (const auto& item : vec) {
108 py_list.append(item);
109 }
110 return py_list;
111}
112
113} // namespace pyhpp
114
115#endif // PYHPP_FWD_HH
pyhpp
Definition fwd.hh:35
pyhpp::to_str
std::string to_str(const ObjectWithPrintMethod &obj)
Definition util.hh:68
pyhpp::to_str_from_operator
std::string to_str_from_operator(const ObjectWithPrintMethod &obj)
Definition util.hh:74
pyhpp::to_python_list
boost::python::list to_python_list(const std::vector< T > &vec)
Definition util.hh:105
pyhpp::extract_vector
std::vector< T > extract_vector(boost::python::list py_list)
Definition util.hh:90
pointer.hh
pyhpp::VectorOfPtr
Definition util.hh:81
pyhpp::VectorOfPtr::Vector
_Vector Vector
Definition util.hh:82
pyhpp::VectorOfPtr::get_item
static T & get_item(Vector &v, std::size_t i)
Definition util.hh:83