Debian Codegraph @

avogadro/core/variant.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_H
7 #define AVOGADRO_CORE_VARIANT_H
8
9 #include "avogadrocoreexport.h"
10
11 #include "avogadrocore.h"
12 #include "matrix.h"
13 #include "vector.h"
14
15 #include <string>
16
17 namespace Avogadro::Core {
18
19 /**
20 * @class Variant variant.h <avogadro/core/variant.h>
21 * @brief The Variant class represents a union of data values.
22 *
23 * Variant objects allow for the storage of and conversion between a variety of
24 * different data types.
25 */
26
27 class AVOGADROCORE_EXPORT Variant
28 {
29 public:
30 // enumerations
31 enum Type
32 {
33 Null,
34 Bool,
35 Int,
36 Long,
37 Float,
38 Double,
39 Pointer,
40 String,
41 Vector,
42 Matrix
43 };
44
45 /** Creates a null variant. */
46 inline Variant();
47
48 /** Creates a variant to store @p value. */
49 template <typename T>
50 Variant(T value);
51
52 /** Creates a new copy of @p variant. */
53 inline Variant(const Variant& variant);
54
55 /** Creates a variant to store a 3D vector */
56 Variant(double x, double y, double z);
57
58 /** Destroys the variant object. */
59 inline ~Variant();
60
61 /** @return variant's type. */
62 inline Type type() const;
63
64 /** @return \c true if the variant is null. */
65 inline bool isNull() const;
66
67 /** Sets the value of the variant to @p value. */
68 template <typename T>
69 bool setValue(T value);
70
71 /** Sets the value of the variant to a 3D vector */
72 bool setValue(double x, double y, double z);
73
74 /** Sets the value of the variant to a vector<double> */
75 bool setValue(const std::vector<double>& v);
76
77 /** @return the value of the variant in the type given by \c T. */
78 template <typename T>
79 T value() const;
80
81 /** Clears the variant's data and sets the variant to null. */
82 inline void clear();
83
84 /** @return the value of the variant as a \c bool. */
85 inline bool toBool() const;
86
87 /** @return the value of the variant as a \c char. */
88 inline char toChar() const;
89
90 /** @return the value of the variant as an \c unsigned \c char. */
91 inline unsigned char toUChar() const;
92
93 /** @return the value of the variant as a \c short. */
94 inline short toShort() const;
95
96 /** @return the value of the variant as an \c unsigned \c short. */
97 inline unsigned short toUShort() const;
98
99 /** @return the value of the variant as an \c int. */
100 inline int toInt() const;
101
102 /** @return the value of the variant as an \c unsigned \c int. */
103 inline unsigned int toUInt() const;
104
105 /** @return the value of the variant as a \c long. */
106 inline long toLong() const;
107
108 /** @return the value of the variant as an \c unsigned \c long. */
109 inline unsigned long toULong() const;
110
111 /** @return the value of the variant as a \c float. */
112 inline float toFloat() const;
113
114 /** @return the value of the variant as a \c double. */
115 inline double toDouble() const;
116
117 /** @return the value of the variant as a \c Real. */
118 inline Real toReal() const;
119
120 /** @return the value of the variant as a pointer. */
121 inline void* toPointer() const;
122
123 /** @return the value of the variant as a string. */
124 inline std::string toString() const;
125
126 /** @return the value of the variant as a MatrixX. */
127 inline MatrixX toMatrix() const;
128
129 /** @return the value of the variant as a Vector3 */
130 inline Vector3 toVector3() const;
131
132 /** @return the value as a vector<double> */
133 inline std::vector<double> toList() const;
134
135 /**
136 * @return a reference to the value of the variant as a MatrixX.
137 * This method will not perform any casting -- if type() is not exactly
138 * MatrixX, the function will fail and return a reference to an empty MatrixX.
139 */
140 inline const MatrixX& toMatrixRef() const;
141
142 // operators
143 inline Variant& operator=(const Variant& variant);
144
145 private:
146 template <typename T>
147 static T lexical_cast(const std::string& string);
148
149 private:
150 Type m_type;
151 union
152 {
153 bool _bool;
154 int _int;
155 long _long;
156 float _float;
157 double _double;
158 void* pointer;
159 std::string* string;
160 Vector3* vector;
161 MatrixX* matrix;
162 } m_value;
163 };
164
165 } // namespace Avogadro::Core
166
167 #include "variant-inline.h"
168
169 #endif // AVOGADRO_CORE_VARIANT_H