bindings/python/serialization/serialization.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021-2023 INRIA
3 //
4 
5 #include <boost/asio.hpp>
6 
10 
11 namespace pinocchio
12 {
13  namespace python
14  {
15 
16  static void buffer_copy(boost::asio::streambuf & dest, const boost::asio::streambuf & source)
17  {
18  std::size_t bytes_copied =
19  boost::asio::buffer_copy(dest.prepare(source.size()), source.data());
20  dest.commit(bytes_copied);
21  }
22 
23  static boost::asio::streambuf &
24  prepare_proxy(boost::asio::streambuf & self, const std::size_t n)
25  {
26  self.prepare(n);
27  return self;
28  }
29 
30  static PyObject * view(boost::asio::streambuf & self)
31  {
32  boost::asio::streambuf::const_buffers_type bufs = self.data();
33  PyObject * py_buffer = PyMemoryView_FromMemory(
34  const_cast<char *>(static_cast<const char *>(bufs.data())), self.size(), PyBUF_READ);
35  return py_buffer;
36  }
37 
38  static PyObject * tobytes(boost::asio::streambuf & self)
39  {
40  return PyBytes_FromObject(view(self));
41  }
42 
44  {
45  namespace bp = boost::python;
46 
47  bp::scope current_scope = getOrCreatePythonNamespace("serialization");
48 
49  typedef boost::asio::streambuf StreamBuffer;
50  if (!eigenpy::register_symbolic_link_to_registered_type<StreamBuffer>())
51  {
52  bp::class_<StreamBuffer, boost::noncopyable>(
53  "StreamBuffer", "Stream buffer to save/load serialized objects in binary mode.",
54  bp::init<>(bp::arg("self"), "Default constructor."))
55  // .def("capacity",&StreamBuffer::capacity,"Get the current capacity of the
56  // StreamBuffer.")
57  .def("size", &StreamBuffer::size, "Get the size of the input sequence.")
58  .def("max_size", &StreamBuffer::max_size, "Get the maximum size of the StreamBuffer.")
59  .def("prepare", prepare_proxy, "Reserve data.", bp::return_self<>())
60  .def(
61  "view", view, "Returns the content of *this as a memory view.",
62  bp::with_custodian_and_ward_postcall<0, 1>())
63  .def("tobytes", tobytes, "Returns the content of *this as a byte sequence.");
64  }
65 
66  typedef pinocchio::serialization::StaticBuffer StaticBuffer;
67  if (!eigenpy::register_symbolic_link_to_registered_type<StaticBuffer>())
68  {
69  bp::class_<StaticBuffer>(
70  "StaticBuffer",
71  "Static buffer to save/load serialized objects in binary mode with pre-allocated memory.",
72  bp::init<size_t>(
73  bp::args("self", "size"), "Default constructor from a given size capacity."))
74  .def("size", &StaticBuffer::size, bp::arg("self"), "Get the size of the input sequence.")
75  .def(
76  "reserve", &StaticBuffer::resize, bp::arg("new_size"),
77  "Increase the capacity of the vector to a value that's greater or equal to new_size.");
78  }
79 
80  bp::def(
81  "buffer_copy", buffer_copy, bp::args("dest", "source"),
82  "Copy bytes from a source buffer to a target buffer.");
83  }
84 
85  } // namespace python
86 } // namespace pinocchio
boost::python
pinocchio::python::buffer_copy
static void buffer_copy(boost::asio::streambuf &dest, const boost::asio::streambuf &source)
Definition: bindings/python/serialization/serialization.cpp:16
pinocchio::python::view
static PyObject * view(boost::asio::streambuf &self)
Definition: bindings/python/serialization/serialization.cpp:30
pinocchio::python::tobytes
static PyObject * tobytes(boost::asio::streambuf &self)
Definition: bindings/python/serialization/serialization.cpp:38
pinocchio::serialization::StaticBuffer
Static buffer with pre-allocated memory.
Definition: static-buffer.hpp:16
serialization.hpp
pinocchio::python::getOrCreatePythonNamespace
boost::python::object getOrCreatePythonNamespace(const std::string &submodule_name)
Helper to create or simply return an existing namespace in Python.
Definition: namespace.hpp:34
pinocchio::python::prepare_proxy
static boost::asio::streambuf & prepare_proxy(boost::asio::streambuf &self, const std::size_t n)
Definition: bindings/python/serialization/serialization.cpp:24
python
size
FCL_REAL size() const
fwd.hpp
pinocchio::python::exposeSerialization
void exposeSerialization()
Definition: bindings/python/serialization/serialization.cpp:43
n
Vec3f n
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
namespace.hpp


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:12