GCC Code Coverage Report


Directory: ./
File: include/multicontact-api/bindings/python/serialization/archive.hpp
Date: 2025-03-10 16:17:01
Exec Total Coverage
Lines: 22 22 100.0%
Branches: 26 52 50.0%

Line Branch Exec Source
1 // Copyright (c) 2015-2018, CNRS
2 // Authors: Justin Carpentier <jcarpent@laas.fr>
3
4 #ifndef __multicontact_api_python_serialization_archive_hpp__
5 #define __multicontact_api_python_serialization_archive_hpp__
6
7 #include <boost/archive/text_iarchive.hpp>
8 #include <boost/archive/text_oarchive.hpp>
9
10 #include "multicontact-api/bindings/python/fwd.hpp"
11
12 namespace multicontact_api {
13 namespace python {
14
15 namespace bp = boost::python;
16
17 template <typename Derived>
18 struct cs_pickle_suite : bp::pickle_suite {
19 7 static bp::object getstate(const Derived& cs) {
20
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 std::ostringstream os;
21
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 boost::archive::text_oarchive oa(os);
22
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 oa << cs;
23
3/6
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
21 return bp::str(os.str());
24 7 }
25
26 7 static void setstate(Derived& cs, bp::object entries) {
27
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 bp::str s = bp::extract<bp::str>(entries)();
28
3/6
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
7 std::string st = bp::extract<std::string>(s)();
29
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 std::istringstream is(st);
30
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 boost::archive::text_iarchive ia(is);
31
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 ia >> cs;
32 7 }
33 };
34
35 template <typename Derived>
36 struct SerializableVisitor
37 : public boost::python::def_visitor<SerializableVisitor<Derived> > {
38 template <class PyClass>
39 36 void visit(PyClass& cl) const {
40 36 cl.def("saveAsText", &Derived::saveAsText, bp::args("filename"),
41 "Saves *this inside a text file.")
42
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
72 .def("loadFromText", &Derived::loadFromText, bp::args("filename"),
43 "Loads *this from a text file.")
44
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
72 .def("saveAsXML", &Derived::saveAsXML, bp::args("filename", "tag_name"),
45 "Saves *this inside a XML file.")
46
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
72 .def("loadFromXML", &Derived::loadFromXML,
47 bp::args("filename", "tag_name"), "Loads *this from a XML file.")
48
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
72 .def("saveAsBinary", &Derived::saveAsBinary, bp::args("filename"),
49 "Saves *this inside a binary file.")
50
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
72 .def("loadFromBinary", &Derived::loadFromBinary, bp::args("filename"),
51 "Loads *this from a binary file.")
52
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
36 .def_pickle(cs_pickle_suite<Derived>());
53 36 }
54 };
55 } // namespace python
56 } // namespace multicontact_api
57
58 #endif // ifndef __multicontact_api_python_serialization_archive_hpp__
59