GCC Code Coverage Report


Directory: ./
File: include/multicontact-api/serialization/archive.hpp
Date: 2025-03-10 16:17:01
Exec Total Coverage
Lines: 46 58 79.3%
Branches: 32 88 36.4%

Line Branch Exec Source
1 // Copyright (c) 2015-2018, CNRS
2 // Authors: Justin Carpentier <jcarpent@laas.fr>
3
4 #ifndef __multicontact_api_serialization_archive_hpp__
5 #define __multicontact_api_serialization_archive_hpp__
6
7 #include <boost/archive/binary_iarchive.hpp>
8 #include <boost/archive/binary_oarchive.hpp>
9 #include <boost/archive/text_iarchive.hpp>
10 #include <boost/archive/text_oarchive.hpp>
11 #include <boost/archive/xml_iarchive.hpp>
12 #include <boost/archive/xml_oarchive.hpp>
13 #include <boost/serialization/version.hpp>
14 #include <fstream>
15 #include <stdexcept>
16 #include <string>
17
18 const unsigned int API_VERSION =
19 2; // must be increased everytime the save() method of a class is modified
20
21 // Macro used to define the serialization version of a templated class
22 #define MULTICONTACT_API_DEFINE_CLASS_TEMPLATE_VERSION(Template, Type) \
23 namespace boost { \
24 namespace serialization { \
25 template <Template> \
26 struct version<Type> { \
27 static constexpr unsigned int value = API_VERSION; \
28 }; \
29 template <Template> \
30 constexpr unsigned int version<Type>::value; \
31 } \
32 }
33
34 namespace multicontact_api {
35 namespace serialization {
36
37 template <class Derived>
38 struct Serializable {
39 private:
40 48 Derived& derived() { return *static_cast<Derived*>(this); }
41 36 const Derived& derived() const { return *static_cast<const Derived*>(this); }
42
43 public:
44 /// \brief Loads a Derived object from a text file.
45 16 void loadFromText(const std::string& filename) {
46
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 std::ifstream ifs(filename.c_str());
47
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
16 if (ifs) {
48
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
16 boost::archive::text_iarchive ia(ifs);
49
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 ia >> derived();
50 16 } else {
51 const std::string exception_message(filename +
52 " does not seem to be a valid file.");
53 throw std::invalid_argument(exception_message);
54 }
55 16 }
56
57 /// \brief Saved a Derived object as a text file.
58 16 void saveAsText(const std::string& filename) const {
59
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 std::ofstream ofs(filename.c_str());
60
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
16 if (ofs) {
61
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
16 boost::archive::text_oarchive oa(ofs);
62
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 oa << derived();
63 16 } else {
64 const std::string exception_message(filename +
65 " does not seem to be a valid file.");
66 throw std::invalid_argument(exception_message);
67 }
68 16 }
69
70 /// \brief Loads a Derived object from an XML file.
71 16 void loadFromXML(const std::string& filename, const std::string& tag_name) {
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
16 assert(!tag_name.empty());
73
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 std::ifstream ifs(filename.c_str());
74
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
16 if (ifs) {
75
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
16 boost::archive::xml_iarchive ia(ifs);
76
1/2
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
16 ia >> boost::serialization::make_nvp(tag_name.c_str(), derived());
77 16 } else {
78 const std::string exception_message(filename +
79 " does not seem to be a valid file.");
80 throw std::invalid_argument(exception_message);
81 }
82 16 }
83
84 /// \brief Saved a Derived object as an XML file.
85 16 void saveAsXML(const std::string& filename,
86 const std::string& tag_name) const {
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
16 assert(!tag_name.empty());
88
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 std::ofstream ofs(filename.c_str());
89
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
16 if (ofs) {
90
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
16 boost::archive::xml_oarchive oa(ofs);
91
1/2
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
16 oa << boost::serialization::make_nvp(tag_name.c_str(), derived());
92 16 } else {
93 const std::string exception_message(filename +
94 " does not seem to be a valid file.");
95 throw std::invalid_argument(exception_message);
96 }
97 16 }
98
99 /// \brief Loads a Derived object from an binary file.
100 16 void loadFromBinary(const std::string& filename) {
101
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 std::ifstream ifs(filename.c_str());
102
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
16 if (ifs) {
103
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
16 boost::archive::binary_iarchive ia(ifs);
104
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 ia >> derived();
105 16 } else {
106 const std::string exception_message(filename +
107 " does not seem to be a valid file.");
108 throw std::invalid_argument(exception_message);
109 }
110 16 }
111
112 /// \brief Saved a Derived object as an binary file.
113 16 void saveAsBinary(const std::string& filename) const {
114
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 std::ofstream ofs(filename.c_str());
115
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
16 if (ofs) {
116
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
16 boost::archive::binary_oarchive oa(ofs);
117
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
16 oa << derived();
118 16 } else {
119 const std::string exception_message(filename +
120 " does not seem to be a valid file.");
121 throw std::invalid_argument(exception_message);
122 }
123 16 }
124 };
125
126 } // namespace serialization
127
128 } // namespace multicontact_api
129
130 #endif // ifndef __multicontact_api_serialization_archive_hpp__
131