util.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018 - 2023 CNRS
3 // Authors: Joseph Mirabel
4 //
5 //
6 // This file is part of hpp-python
7 // hpp-python is free software: you can redistribute it
8 // and/or modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation, either version
10 // 3 of the License, or (at your option) any later version.
11 //
12 // hpp-python is distributed in the hope that it will be
13 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Lesser Public License for more details. You should have
16 // received a copy of the GNU Lesser General Public License along with
17 // hpp-python If not, see
18 // <http://www.gnu.org/licenses/>.
19 
20 #ifndef PYHPP_FWD_HH
21 #define PYHPP_FWD_HH
22 
23 #include <boost/python.hpp>
24 #include <eigenpy/eigenpy.hpp>
25 #include <hpp/util/pointer.hh>
26 #include <vector>
27 
28 #define INIT_PYHPP_MODULE \
29  boost::python::docstring_options local_docstring_options(true, true, false)
30 
31 #define PYHPP_DEFINE_METHOD(CLASS, METHOD) def(#METHOD, &CLASS::METHOD)
32 #define PYHPP_DEFINE_METHOD1(CLASS, METHOD, ARG1) \
33  def(#METHOD, &CLASS::METHOD, ARG1)
34 #define PYHPP_DEFINE_METHOD2(CLASS, METHOD, ARG1, ARG2) \
35  def(#METHOD, &CLASS::METHOD, ARG1, ARG2)
36 #define PYHPP_DEFINE_METHOD_INTERNAL_REF(CLASS, METHOD) \
37  def(#METHOD, &CLASS::METHOD, return_internal_reference<>())
38 #define PYHPP_DEFINE_GETTER_SETTER(CLASS, METHOD, TYPE) \
39  def(#METHOD, static_cast<TYPE (CLASS::*)() const>(&CLASS::METHOD)) \
40  .def(#METHOD, static_cast<void (CLASS::*)(TYPE)>(&CLASS::METHOD))
41 #define PYHPP_DEFINE_GETTER_SETTER_INTERNAL_REF(CLASS, METHOD, TYPE) \
42  def(#METHOD, static_cast<TYPE (CLASS::*)() const>(&CLASS::METHOD), \
43  return_internal_reference<>()) \
44  .def(#METHOD, static_cast<void (CLASS::*)(TYPE)>(&CLASS::METHOD))
45 
46 namespace pyhpp {
47 template <typename ObjectWithPrintMethod>
48 std::string to_str(const ObjectWithPrintMethod& obj) {
49  std::ostringstream oss;
50  obj.print(oss);
51  return oss.str();
52 }
53 template <typename ObjectWithPrintMethod>
54 std::string to_str_from_operator(const ObjectWithPrintMethod& obj) {
55  std::ostringstream oss;
56  oss << obj;
57  return oss.str();
58 }
59 
60 template <typename T, typename _Vector = std::vector<hpp::shared_ptr<T> > >
61 struct VectorOfPtr {
62  typedef _Vector Vector;
63  static T& get_item(Vector& v, std::size_t i) {
64  if (i > v.size()) throw std::invalid_argument("Out of range");
65  if (!v[i]) throw std::runtime_error("Null pointer");
66  return *v[i];
67  }
68 };
69 } // namespace pyhpp
70 
71 #endif // PYHPP_FWD_HH
pyhpp::VectorOfPtr
Definition: util.hh:61
pyhpp::VectorOfPtr::Vector
_Vector Vector
Definition: util.hh:62
pyhpp
Definition: fwd.hh:25
pyhpp::VectorOfPtr::get_item
static T & get_item(Vector &v, std::size_t i)
Definition: util.hh:63
pyhpp::to_str_from_operator
std::string to_str_from_operator(const ObjectWithPrintMethod &obj)
Definition: util.hh:54
pyhpp::to_str
std::string to_str(const ObjectWithPrintMethod &obj)
Definition: util.hh:48