| Directory: | ./ |
|---|---|
| File: | bindings/python/serialization/serialization.cpp |
| Date: | 2025-02-12 21:03:38 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 21 | 34 | 61.8% |
| Branches: | 25 | 52 | 48.1% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2021-2023 INRIA | ||
| 3 | // | ||
| 4 | |||
| 5 | #include <boost/asio.hpp> | ||
| 6 | |||
| 7 | #include "pinocchio/bindings/python/fwd.hpp" | ||
| 8 | #include "pinocchio/bindings/python/utils/namespace.hpp" | ||
| 9 | #include "pinocchio/bindings/python/serialization/serialization.hpp" | ||
| 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 | |||
| 43 | 69 | void exposeSerialization() | |
| 44 | { | ||
| 45 | namespace bp = boost::python; | ||
| 46 | |||
| 47 |
3/6✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 69 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 69 times.
✗ Branch 9 not taken.
|
138 | bp::scope current_scope = getOrCreatePythonNamespace("serialization"); |
| 48 | |||
| 49 | typedef boost::asio::streambuf StreamBuffer; | ||
| 50 |
2/4✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 69 times.
✗ Branch 4 not taken.
|
69 | if (!eigenpy::register_symbolic_link_to_registered_type<StreamBuffer>()) |
| 51 | { | ||
| 52 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | bp::class_<StreamBuffer, boost::noncopyable>( |
| 53 | "StreamBuffer", "Stream buffer to save/load serialized objects in binary mode.", | ||
| 54 |
2/4✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 69 times.
✗ Branch 5 not taken.
|
138 | bp::init<>(bp::arg("self"), "Default constructor.")) |
| 55 | // .def("capacity",&StreamBuffer::capacity,"Get the current capacity of the | ||
| 56 | // StreamBuffer.") | ||
| 57 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | .def("size", &StreamBuffer::size, "Get the size of the input sequence.") |
| 58 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | .def("max_size", &StreamBuffer::max_size, "Get the maximum size of the StreamBuffer.") |
| 59 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | .def("prepare", prepare_proxy, "Reserve data.", bp::return_self<>()) |
| 60 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | .def( |
| 61 | "view", view, "Returns the content of *this as a memory view.", | ||
| 62 | 69 | bp::with_custodian_and_ward_postcall<0, 1>()) | |
| 63 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | .def("tobytes", tobytes, "Returns the content of *this as a byte sequence."); |
| 64 | } | ||
| 65 | |||
| 66 | typedef pinocchio::serialization::StaticBuffer StaticBuffer; | ||
| 67 |
3/4✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
✓ Branch 4 taken 4 times.
|
69 | if (!eigenpy::register_symbolic_link_to_registered_type<StaticBuffer>()) |
| 68 | { | ||
| 69 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | bp::class_<StaticBuffer>( |
| 70 | "StaticBuffer", | ||
| 71 | "Static buffer to save/load serialized objects in binary mode with pre-allocated memory.", | ||
| 72 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | bp::init<size_t>( |
| 73 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
130 | bp::args("self", "size"), "Default constructor from a given size capacity.")) |
| 74 |
2/4✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 65 times.
✗ Branch 5 not taken.
|
130 | .def("size", &StaticBuffer::size, bp::arg("self"), "Get the size of the input sequence.") |
| 75 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | .def( |
| 76 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
130 | "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 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
69 | bp::def( |
| 81 |
1/2✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
|
138 | "buffer_copy", buffer_copy, bp::args("dest", "source"), |
| 82 | "Copy bytes from a source buffer to a target buffer."); | ||
| 83 | 69 | } | |
| 84 | |||
| 85 | } // namespace python | ||
| 86 | } // namespace pinocchio | ||
| 87 |