Debian Codegraph @

avogadro/core/variant-inline.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_VARIANT_INLINE_H
7 #define AVOGADRO_CORE_VARIANT_INLINE_H
8
9 #include "variant.h"
10
11 #include <iostream>
12 #include <sstream>
13
14 namespace Avogadro::Core {
15
16 inline Variant::Variant() : m_type(Null) {}
17
18 inline Variant::Variant(double x, double y, double z) : m_type(Vector)
19 {
20 Vector3* v = new Vector3(x, y, z);
21 m_value.vector = v;
22 }
23
24 template <typename T>
25 inline Variant::Variant(T v) : m_type(Null)
26 {
27 setValue(v);
28 }
29
30 template <>
31 inline Variant::Variant(char v) : m_type(Int)
32 {
33 m_value._int = static_cast<unsigned char>(v);
34 }
35
36 template <>
37 inline Variant::Variant(const char* v) : m_type(String)
38 {
39 m_value.string = new std::string(v);
40 }
41
42 template <>
43 inline Variant::Variant(const MatrixXf& v) : m_type(Matrix)
44 {
45 MatrixX* m = new MatrixX(v.rows(), v.cols());
46 *m = v.cast<double>();
47 m_value.matrix = m;
48 }
49
50 template <>
51 inline Variant::Variant(const Vector3& v) : m_type(Vector)
52 {
53 Vector3* _v = new Vector3(v);
54 m_value.vector = _v;
55 }
56
57 template <>
58 inline Variant::Variant(const Vector3f& v) : m_type(Vector)
59 {
60 Vector3* _v = new Vector3(v.x(), v.y(), v.z());
61 m_value.vector = _v;
62 }
63
64 template <>
65 inline Variant::Variant(const std::vector<double>& v) : m_type(Matrix)
66 {
67 MatrixX* m = new MatrixX(v.size(), 1);
68 for (size_t i = 0; i < v.size(); ++i)
69 m->coeffRef(i, 0) = v[i];
70 m_value.matrix = m;
71 }
72
73 inline Variant::Variant(const Variant& variant) : m_type(variant.type())
74 {
75 if (m_type == String)
76 m_value.string = new std::string(variant.toString());
77 else if (m_type == Matrix)
78 m_value.matrix = new MatrixX(*variant.m_value.matrix);
79 else if (m_type == Vector)
80 m_value.vector = new Vector3(*variant.m_value.vector);
81 else if (m_type != Null)
82 m_value = variant.m_value;
83 }
84
85 inline Variant::~Variant()
86 {
87 clear();
88 }
89
90 inline Variant::Type Variant::type() const
91 {
92 return m_type;
93 }
94
95 inline bool Variant::isNull() const
96 {
97 return m_type == Null;
98 }
99
100 inline bool Variant::setValue(double x, double y, double z)
101 {
102 clear();
103
104 m_type = Vector;
105 m_value.vector = new Vector3(x, y, z);
106
107 return true;
108 }
109
110 inline bool Variant::setValue(const std::vector<double>& v)
111 {
112 clear();
113
114 m_type = Matrix;
115 m_value.matrix = new MatrixX(v.size(), 1);
116 for (size_t i = 0; i < v.size(); ++i)
117 m_value.matrix->coeffRef(i, 0) = v[i];
118
119 return true;
120 }
121
122 template <typename T>
123 inline bool Variant::setValue(T v)
124 {
125 AVO_UNUSED(v);
126
127 #ifndef NDEBUG
128 #if defined(_MSC_VER)
129 std::cerr << " Variant::setValue() not implemented for " << __FUNCSIG__
130 << std::endl;
131 #else
132 std::cerr << " Variant::setValue() not implemented for "
133 << __PRETTY_FUNCTION__ << std::endl;
134 #endif
135 #endif
136
137 clear();
138
139 return false;
140 }
141
142 template <>
143 inline bool Variant::setValue(bool v)
144 {
145 clear();
146
147 m_type = Bool;
148 m_value._bool = v;
149
150 return true;
151 }
152
153 template <>
154 inline bool Variant::setValue(char v)
155 {
156 clear();
157
158 m_type = Int;
159 m_value._int = static_cast<unsigned char>(v);
160
161 return true;
162 }
163
164 template <>
165 inline bool Variant::setValue(short v)
166 {
167 clear();
168
169 m_type = Int;
170 m_value._int = v;
171
172 return true;
173 }
174
175 template <>
176 inline bool Variant::setValue(int v)
177 {
178 clear();
179
180 m_type = Int;
181 m_value._int = v;
182
183 return true;
184 }
185
186 template <>
187 inline bool Variant::setValue(long v)
188 {
189 clear();
190
191 m_type = Long;
192 m_value._long = v;
193
194 return true;
195 }
196
197 template <>
198 inline bool Variant::setValue(float v)
199 {
200 clear();
201
202 m_type = Float;
203 m_value._float = v;
204
205 return true;
206 }
207
208 template <>
209 inline bool Variant::setValue(double v)
210 {
211 clear();
212
213 m_type = Double;
214 m_value._double = v;
215
216 return true;
217 }
218
219 template <>
220 inline bool Variant::setValue(std::string string)
221 {
222 clear();
223
224 m_type = String;
225 m_value.string = new std::string(string);
226
227 return true;
228 }
229
230 template <>
231 inline bool Variant::setValue(const char* string)
232 {
233 return setValue(std::string(string));
234 }
235
236 template <>
237 inline bool Variant::setValue(void* pointer)
238 {
239 clear();
240
241 m_type = Pointer;
242 m_value.pointer = pointer;
243
244 return true;
245 }
246
247 template <>
248 inline bool Variant::setValue(MatrixX matrix)
249 {
250 clear();
251
252 m_type = Matrix;
253 m_value.matrix = new MatrixX(matrix);
254
255 return true;
256 }
257
258 template <>
259 inline bool Variant::setValue(Vector3 vector)
260 {
261 clear();
262
263 m_type = Vector;
264 m_value.vector = new Vector3(vector);
265
266 return true;
267 }
268
269 template <>
270 inline bool Variant::setValue(Vector3f vector)
271 {
272 clear();
273
274 m_type = Vector;
275 m_value.vector = new Vector3(vector.x(), vector.y(), vector.z());
276
277 return true;
278 }
279
280 template <typename T>
281 inline T Variant::value() const
282 {
283 return 0;
284 }
285
286 template <>
287 inline bool Variant::value() const
288 {
289 if (m_type == Bool)
290 return m_value._bool;
291 else if (m_type == Int)
292 return m_value._int != 0;
293
294 return false;
295 }
296
297 template <>
298 inline char Variant::value() const
299 {
300 if (m_type == Int)
301 return static_cast<char>(m_value._int);
302 else if (m_type == String && !m_value.string->empty())
303 return m_value.string->at(0);
304
305 return '\0';
306 }
307
308 template <>
309 inline short Variant::value() const
310 {
311 if (m_type == Int)
312 return static_cast<short>(m_value._int);
313 else if (m_type == String)
314 return lexical_cast<short>(*m_value.string);
315
316 return 0;
317 }
318
319 template <>
320 inline int Variant::value() const
321 {
322 if (m_type == Int)
323 return m_value._int;
324 else if (m_type == Bool)
325 return static_cast<int>(m_value._bool);
326 else if (m_type == Float)
327 return static_cast<int>(m_value._float);
328 else if (m_type == Double)
329 return static_cast<int>(m_value._double);
330 else if (m_type == String)
331 return lexical_cast<int>(*m_value.string);
332
333 return 0;
334 }
335
336 template <>
337 inline long Variant::value() const
338 {
339 if (m_type == Long)
340 return m_value._long;
341 else if (m_type == Int)
342 return static_cast<long>(m_value._int);
343 else if (m_type == String)
344 return lexical_cast<long>(*m_value.string);
345
346 return 0;
347 }
348
349 template <>
350 inline float Variant::value() const
351 {
352 if (m_type == Float)
353 return m_value._float;
354 else if (m_type == Double)
355 return static_cast<float>(m_value._double);
356 else if (m_type == Int)
357 return static_cast<float>(m_value._int);
358 else if (m_type == String)
359 return lexical_cast<float>(*m_value.string);
360
361 return 0;
362 }
363
364 template <>
365 inline double Variant::value() const
366 {
367 if (m_type == Double)
368 return m_value._double;
369 else if (m_type == Float)
370 return static_cast<double>(m_value._float);
371 else if (m_type == Int)
372 return static_cast<double>(m_value._int);
373 else if (m_type == String)
374 return lexical_cast<double>(*m_value.string);
375
376 return 0;
377 }
378
379 template <>
380 inline void* Variant::value() const
381 {
382 if (m_type == Pointer)
383 return m_value.pointer;
384
385 return nullptr;
386 }
387
388 template <>
389 inline std::string Variant::value() const
390 {
391 if (m_type == String)
392 return *m_value.string;
393
394 std::stringstream string;
395
396 if (m_type == Int)
397 string << m_value._int;
398 else if (m_type == Float)
399 string << m_value._float;
400 else if (m_type == Double)
401 string << m_value._double;
402
403 return string.str();
404 }
405
406 template <>
407 inline MatrixX Variant::value() const
408 {
409 if (m_type == Matrix)
410 return *m_value.matrix;
411
412 return MatrixX();
413 }
414
415 template <>
416 inline const MatrixX& Variant::value() const
417 {
418 if (m_type == Matrix)
419 return *m_value.matrix;
420
421 // Use a static null matrix for the reference.
422 static MatrixX nullMatrix(0, 0);
423 return nullMatrix;
424 }
425
426 template <>
427 inline Vector3 Variant::value() const
428 {
429 if (m_type == Vector)
430 return *m_value.vector;
431
432 return Vector3();
433 }
434
435 template <>
436 inline const Vector3& Variant::value() const
437 {
438 if (m_type == Vector)
439 return *m_value.vector;
440
441 static Vector3 nullVector(0, 0, 0);
442 return nullVector;
443 }
444
445 template <>
446 inline std::vector<double> Variant::value() const
447 {
448 if (m_type == Matrix && m_value.matrix->cols() == 1) {
449 std::vector<double> list(m_value.matrix->rows());
450 for (int i = 0; i < m_value.matrix->rows(); ++i)
451 list[i] = m_value.matrix->coeff(i, 0);
452 return list;
453 }
454
455 return std::vector<double>();
456 }
457
458 inline void Variant::clear()
459 {
460 if (m_type == String) {
461 delete m_value.string;
462 m_value.string = nullptr;
463 } else if (m_type == Matrix) {
464 delete m_value.matrix;
465 m_value.matrix = nullptr;
466 } else if (m_type == Vector) {
467 delete m_value.vector;
468 m_value.vector = nullptr;
469 }
470
471 m_type = Null;
472 }
473
474 inline bool Variant::toBool() const
475 {
476 return value<bool>();
477 }
478
479 inline char Variant::toChar() const
480 {
481 return value<char>();
482 }
483
484 inline unsigned char Variant::toUChar() const
485 {
486 return value<unsigned char>();
487 }
488
489 inline short Variant::toShort() const
490 {
491 return value<short>();
492 }
493
494 inline unsigned short Variant::toUShort() const
495 {
496 return value<unsigned short>();
497 }
498
499 inline int Variant::toInt() const
500 {
501 return value<int>();
502 }
503
504 inline unsigned int Variant::toUInt() const
505 {
506 return value<unsigned int>();
507 }
508
509 inline long Variant::toLong() const
510 {
511 return value<long>();
512 }
513
514 inline unsigned long Variant::toULong() const
515 {
516 return value<unsigned long>();
517 }
518
519 inline float Variant::toFloat() const
520 {
521 return value<float>();
522 }
523
524 inline double Variant::toDouble() const
525 {
526 return value<double>();
527 }
528
529 inline Real Variant::toReal() const
530 {
531 return value<Real>();
532 }
533
534 inline void* Variant::toPointer() const
535 {
536 return value<void*>();
537 }
538
539 inline std::string Variant::toString() const
540 {
541 return value<std::string>();
542 }
543
544 inline MatrixX Variant::toMatrix() const
545 {
546 return value<MatrixX>();
547 }
548
549 inline const MatrixX& Variant::toMatrixRef() const
550 {
551 return value<const MatrixX&>();
552 }
553
554 inline Vector3 Variant::toVector3() const
555 {
556 return value<Vector3>();
557 }
558
559 inline std::vector<double> Variant::toList() const
560 {
561 return value<std::vector<double>>();
562 }
563
564 // --- Operators ----------------------------------------------------------- //
565 inline Variant& Variant::operator=(const Variant& variant)
566 {
567 if (this != &variant) {
568 // Clear previous data,
569 clear();
570
571 // Set the new type.
572 m_type = variant.m_type;
573
574 // Set the new value,
575 if (m_type == String)
576 m_value.string = new std::string(variant.toString());
577 else if (m_type == Matrix)
578 m_value.matrix = new MatrixX(*variant.m_value.matrix);
579 else if (m_type == Vector)
580 m_value.vector = new Vector3(*variant.m_value.vector);
581 else if (m_type != Null)
582 m_value = variant.m_value;
583 }
584
585 return *this;
586 }
587
588 // --- Internal Methods ---------------------------------------------------- //
589 template <typename T>
590 inline T Variant::lexical_cast(const std::string& str)
591 {
592 T value;
593 std::istringstream(str) >> value;
594 return value;
595 }
596
597 } // namespace Avogadro::Core
598
599 #endif // AVOGADRO_CORE_VARIANT_INLINE_H