hpp-python  6.1.0
python bindings for HPP, based on boost python
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 
66 namespace pyhpp {
67 template <typename ObjectWithPrintMethod>
68 std::string to_str(const ObjectWithPrintMethod& obj) {
69  std::ostringstream oss;
70  obj.print(oss);
71  return oss.str();
72 }
73 template <typename ObjectWithPrintMethod>
74 std::string to_str_from_operator(const ObjectWithPrintMethod& obj) {
75  std::ostringstream oss;
76  oss << obj;
77  return oss.str();
78 }
79 
80 template <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 };
89 
90 template <typename T>
91 std::vector<T> extract_vector(boost::python::list py_list) {
92  return std::vector<T>(boost::python::stl_input_iterator<T>(py_list),
93  boost::python::stl_input_iterator<T>());
94 }
95 template <typename T>
96 boost::python::list to_python_list(const std::vector<T>& vec) {
97  boost::python::list py_list;
98  for (const auto& item : vec) {
99  py_list.append(item);
100  }
101  return py_list;
102 }
103 
104 } // namespace pyhpp
105 
106 #endif // PYHPP_FWD_HH
Definition: fwd.hh:35
std::string to_str(const ObjectWithPrintMethod &obj)
Definition: util.hh:68
std::string to_str_from_operator(const ObjectWithPrintMethod &obj)
Definition: util.hh:74
boost::python::list to_python_list(const std::vector< T > &vec)
Definition: util.hh:96
std::vector< T > extract_vector(boost::python::list py_list)
Definition: util.hh:91
Definition: util.hh:81
_Vector Vector
Definition: util.hh:82
static T & get_item(Vector &v, std::size_t i)
Definition: util.hh:83