GCC Code Coverage Report


Directory: ./
File: src/parameter.cc
Date: 2024-08-10 11:29:48
Exec Total Coverage
Lines: 78 138 56.5%
Branches: 23 111 20.7%

Line Branch Exec Source
1 // Copyright (c) 2018, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr), Florent Lamiraux
3 //
4
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // 1. Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28
29 #include <hpp/core/parameter.hh>
30 #include <hpp/util/exception-factory.hh>
31
32 namespace hpp {
33 namespace core {
34
35 static void* copyValue(const Parameter& value);
36
37 /*
38 EitherType::EitherType(const Parameter& value) : value_(new Parameter(value))
39 {
40 }
41
42 EitherType::~EitherType()
43 {
44 delete value_;
45 value_ = NULL;
46 }
47
48 EitherType::operator bool() const
49 {
50 return value_->boolValue();
51 }
52 EitherType::operator unsigned() const
53 {
54 return value_->unsignedValue();
55 }
56 EitherType::operator int() const
57 {
58 return value_->intValue();
59 }
60 EitherType::operator float() const
61 {
62 return value_->floatValue();
63 }
64 EitherType::operator double() const
65 {
66 return value_->doubleValue();
67 }
68 EitherType::operator std::string() const
69 {
70 return value_->stringValue();
71 }
72 EitherType::operator Vector() const
73 {
74 return value_->vectorValue();
75 }
76 EitherType::operator Eigen::MatrixXd() const
77 {
78 return value_->matrixXdValue();
79 }
80
81 EitherType::operator Eigen::Matrix4d() const
82 {
83 return value_->matrix4dValue();
84 }
85 */
86
87 2532 void Parameter::deleteValue() {
88
5/7
✓ Branch 0 taken 446 times.
✓ Branch 1 taken 323 times.
✓ Branch 2 taken 1616 times.
✓ Branch 3 taken 75 times.
✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2532 switch (type_) {
89 446 case BOOL:
90
1/2
✓ Branch 0 taken 446 times.
✗ Branch 1 not taken.
446 delete (const bool*)value_;
91 446 break;
92 323 case INT:
93
1/2
✓ Branch 0 taken 323 times.
✗ Branch 1 not taken.
323 delete (const size_type*)value_;
94 323 break;
95 1616 case FLOAT:
96
1/2
✓ Branch 0 taken 1616 times.
✗ Branch 1 not taken.
1616 delete (const value_type*)value_;
97 1616 break;
98 75 case STRING:
99
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 delete (const std::string*)value_;
100 75 break;
101 72 case VECTOR:
102
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 delete (const vector_t*)value_;
103 72 break;
104 case MATRIX:
105 delete (const matrix_t*)value_;
106 break;
107 2532 default:;
108 }
109 2532 }
110
111 2526 Parameter::~Parameter() { deleteValue(); }
112
113 114 Parameter::Parameter(const bool& value)
114 114 : type_(BOOL), value_(new bool(value)) {}
115
116 83 Parameter::Parameter(const size_type& value)
117 83 : type_(INT), value_(new size_type(value)) {}
118
119 408 Parameter::Parameter(const value_type& value)
120 408 : type_(FLOAT), value_(new value_type(value)) {}
121 19 Parameter::Parameter(const std::string& value)
122
1/2
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
19 : type_(STRING), value_(new std::string(value)) {}
123
124 18 Parameter::Parameter(const vector_t& value)
125
1/2
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 : type_(VECTOR), value_(new vector_t(value)) {}
126
127 Parameter::Parameter(const matrix_t& value)
128 : type_(MATRIX), value_(new matrix_t(value)) {}
129
130 1884 Parameter::Parameter(const Parameter& value)
131 1884 : type_(value.type_), value_(copyValue(value)) {}
132
133 1890 void* copyValue(const Parameter& value) {
134 void* copy;
135
5/8
✗ Branch 1 not taken.
✓ Branch 2 taken 332 times.
✓ Branch 3 taken 240 times.
✓ Branch 4 taken 1208 times.
✓ Branch 5 taken 56 times.
✓ Branch 6 taken 54 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
1890 switch (value.type()) {
136 case Parameter::NONE:
137 copy = NULL;
138 break;
139 332 case Parameter::BOOL:
140 332 copy = new bool(value.boolValue());
141 332 break;
142 240 case Parameter::INT:
143 240 copy = new size_type(value.intValue());
144 240 break;
145 1208 case Parameter::FLOAT:
146 1208 copy = new value_type(value.floatValue());
147 1208 break;
148 56 case Parameter::STRING:
149
1/2
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
56 copy = new std::string(value.stringValue());
150 56 break;
151 54 case Parameter::VECTOR:
152
1/2
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
54 copy = new vector_t(value.vectorValue());
153 54 break;
154 case Parameter::MATRIX:
155 copy = new matrix_t(value.matrixValue());
156 break;
157 default:
158 throw std::invalid_argument("value type is unknown.");
159 break;
160 }
161 1890 return copy;
162 }
163
164 Parameter::Parameter() : type_(NONE), value_(NULL) {}
165
166 6 Parameter Parameter::operator=(const Parameter& value) {
167
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (&value != this) {
168
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (value_ != 0x0) deleteValue();
169 6 type_ = value.type_;
170 6 void** ptValue = const_cast<void**>(&value_);
171 6 *ptValue = copyValue(value);
172 }
173 6 return *this;
174 }
175
176 // const EitherType Parameter::value() const
177 // {
178 // return EitherType(*this);
179 // }
180
181 2207 Parameter::Type Parameter::type() const { return type_; }
182
183 2218 inline void check(Parameter::Type type, Parameter::Type expected) {
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2218 times.
2218 if (type != expected)
185 throw std::invalid_argument("value is not a " +
186 Parameter::typeName(expected));
187 2218 }
188
189 455 bool Parameter::boolValue() const {
190 455 check(type_, BOOL);
191 455 return *((const bool*)value_);
192 }
193
194 270 size_type Parameter::intValue() const {
195 270 check(type_, INT);
196 270 return *((const size_type*)value_);
197 }
198
199 1379 value_type Parameter::floatValue() const {
200 1379 check(type_, FLOAT);
201 1379 return *((const value_type*)value_);
202 }
203
204 60 std::string Parameter::stringValue() const {
205 60 check(type_, STRING);
206 60 return *((const std::string*)value_);
207 }
208
209 54 vector_t Parameter::vectorValue() const {
210 54 check(type_, VECTOR);
211 54 return *((const vector_t*)value_);
212 }
213
214 matrix_t Parameter::matrixValue() const {
215 check(type_, MATRIX);
216 return *((const matrix_t*)value_);
217 }
218
219 std::string Parameter::typeName(Type type) {
220 switch (type) {
221 case BOOL:
222 return std::string("bool");
223 case INT:
224 return std::string("size_type");
225 case FLOAT:
226 return std::string("value_type");
227 case STRING:
228 return std::string("string");
229 case VECTOR:
230 return std::string("vector");
231 case MATRIX:
232 return std::string("matrix");
233 default:
234 return std::string("unknown");
235 }
236 }
237
238 275 const Parameter& ParameterDescription::defaultValue() const {
239
1/2
✓ Branch 1 taken 275 times.
✗ Branch 2 not taken.
275 if (defaultValue_.type() == type_) return defaultValue_;
240 HPP_THROW(std::logic_error,
241 "Parameter " << name_ << " expected a default value of type "
242 << Parameter::typeName(type_)
243 << " but has invalid default value " << defaultValue_);
244 }
245
246 std::ostream& operator<<(std::ostream& os, const Parameter& value) {
247 os << "Type=" << Parameter::typeName(value.type_) << ", value=";
248 switch (value.type_) {
249 case Parameter::BOOL:
250 os << value.boolValue();
251 break;
252 case Parameter::INT:
253 os << value.intValue();
254 break;
255 case Parameter::FLOAT:
256 os << value.floatValue();
257 break;
258 case Parameter::STRING:
259 os << value.stringValue();
260 break;
261 case Parameter::VECTOR:
262 os << value.vectorValue();
263 break;
264 case Parameter::MATRIX:
265 os << value.matrixValue();
266 break;
267 default:
268 return os;
269 }
270 return os;
271 }
272
273 // template< typename T >
274 // struct DYNAMIC_GRAPH_DLLAPI ValueHelper
275 // {
276 // static const Parameter::Type TypeID;
277 // };
278 // template<> const Parameter::Type ValueHelper<bool>::TypeID = Parameter::BOOL;
279 // template<> const Parameter::Type ValueHelper<unsigned>::TypeID =
280 // Parameter::UNSIGNED; template<> const Parameter::Type
281 // ValueHelper<int>::TypeID = Parameter::INT; template<> const Parameter::Type
282 // ValueHelper<float>::TypeID = Parameter::FLOAT; template<> const
283 // Parameter::Type ValueHelper<double>::TypeID = Parameter::DOUBLE; template<>
284 // const Parameter::Type ValueHelper<std::string>::TypeID = Parameter::STRING;
285 // template<> const Parameter::Type ValueHelper<Vector>::TypeID =
286 // Parameter::VECTOR; template<> const Parameter::Type
287 // ValueHelper<Eigen::MatrixXd>::TypeID = Parameter::MATRIX; template<> const
288 // Parameter::Type ValueHelper<Eigen::Matrix4d>::TypeID = Parameter::MATRIX4D;
289
290 } // namespace core
291 } // namespace hpp
292