MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlVariant.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2011, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_VARIANT_H
14#define ML_VARIANT_H
15
16
18
19#include "mlVariantSystem.h"
20
21#include <ThirdPartyWarningsDisable.h>
22#include <string>
23#include <ThirdPartyWarningsRestore.h>
24#include <mlVector2.h>
25#include <mlVector3.h>
26#include <mlVector4.h>
27#include <mlVector6.h>
28#include <mlMatrix3.h>
29#include <mlMatrix4.h>
30
32
36{
37public:
59
62 Variant(const Variant& value);
63 Variant(const std::string& value);
64 Variant(Type type, const std::string& value);
65 Variant(bool value);
72 Variant(const Vector2& value);
73 Variant(const Vector3& value);
74 Variant(const Vector4& value);
75 Variant(const Vector6& value);
76 Variant(const Matrix3& value);
77 Variant(const Matrix4& value);
78
80 Variant(Type type, const void* valuePtr);
81
83 Type type() const
84 {
85 return _type;
86 }
87
89 bool isValid() const
90 {
91 return _type != Invalid;
92 }
93
95 bool canConvert(Type t) const;
96
98 template <typename T> const T& asType() const
99 {
100 return *static_cast<T*>(_data);
101 }
102
104 void clear();
105
106 std::string toString() const;
107 bool toBool() const;
108 MLint32 toInt(bool* ok = nullptr) const;
109 MLuint32 toUInt(bool* ok = nullptr) const;
110 MLint64 toInt64(bool* ok = nullptr) const;
111 MLuint64 toUInt64(bool* ok = nullptr) const;
112 MLfloat toFloat(bool* ok = nullptr) const;
113 MLdouble toDouble(bool* ok = nullptr) const;
114 Vector2 toVec2(bool* ok = nullptr) const;
115 Vector3 toVec3(bool* ok = nullptr) const;
116 Vector4 toVec4(bool* ok = nullptr) const;
117 Vector6 toVec6(bool* ok = nullptr) const;
118 Matrix3 toMat3(bool* ok = nullptr) const;
119 Matrix4 toMat4(bool* ok = nullptr) const;
120
122 bool operator==(const Variant& v) const;
123 bool operator!=(const Variant& v) const;
124
126 std::ostream& write(std::ostream& out, bool asBinary, bool writeType = true) const;
127 std::istream& read(std::istream& in, bool asBinary);
128
130
132 static std::ostream& writeEscapedString(std::ostream& out, const std::string& s);
133 static std::string readEscapedString(std::istream& in);
134
137 template <typename T> static void writeValue(std::ostream& out, const T& value, bool binary, bool last = true);
138 template <typename T> static void readValue(std::istream& in, T& value, bool binary, bool last = true);
139
142 template <typename T> static void writeValueAsText(std::ostream& out, const T& value);
143
148
149private:
151 void copyValue(Type type, const void* valuePtr);
152
153 template <typename T> void copyValueTyped(const void* valuePtr)
154 {
155 _data = new T(*(static_cast<const T*>(valuePtr)));
156 }
157
158 template <typename T> void clearValueTyped()
159 {
160 delete static_cast<T*>(_data);
161 }
162
163 template <typename T> T asNumericType(bool* ok) const;
164
166 static MLuint32 _typeCodes[];
167
169 Type _type;
170
172 void* _data;
173};
174
175
176template <typename T> inline void Variant::writeValueAsText(std::ostream& out, const T& value)
177{
178 out << value;
179}
180
181template <> inline void Variant::writeValueAsText<float>(std::ostream& out, const float& value)
182{
183 // set sufficient precision
184 std::streamsize oldPrecision = out.precision(8);
185 out << value;
186 out.precision(oldPrecision);
187}
188
189template <> inline void Variant::writeValueAsText<double>(std::ostream& out, const double& value)
190{
191 // set sufficient precision
192 std::streamsize oldPrecision = out.precision(16);
193 out << value;
194 out.precision(oldPrecision);
195}
196
197template <typename T> void Variant::writeValue(std::ostream& out, const T& value, bool binary, bool last)
198{
199 if (binary) {
200 out.write(reinterpret_cast<const char*>(&value), sizeof(T));
201 }
202 else {
203 writeValueAsText(out, value);
204 if (!last) {
205 out << ",";
206 }
207 }
208}
209
210template <typename T> void Variant::readValue(std::istream& in, T& value, bool binary, bool last)
211{
212 if (binary) {
213 in.read(reinterpret_cast<char*>(&value), sizeof(T));
214 }
215 else if (last) {
216 in >> value;
217 }
218 else {
219 std::string component;
220 std::getline(in, component, ',');
221 std::stringstream strstream;
223 strstream >> value;
224 }
225}
226
227
229
230
231inline std::ostream& operator<<(std::ostream& out, const ML_UTILS_NAMESPACE::Variant& variant)
232{
233 return variant.write(out, /*asBinary=*/false);
234}
235
236inline std::istream& operator>>(std::istream& in, ML_UTILS_NAMESPACE::Variant& variant)
237{
238 return variant.read(in, /*asBinary=*/false);
239}
240
241
242#endif // __mlVariant_H
@ T
The Variant class stores different data types.
Definition mlVariant.h:36
std::string toString() const
Variant(MLuint32 value)
Variant(const Vector2 &value)
MLint64 toInt64(bool *ok=nullptr) const
bool operator!=(const Variant &v) const
MLdouble toDouble(bool *ok=nullptr) const
void clear()
free memory currently occupied by Variant and set type to Invalid
Variant(MLuint64 value)
Variant(const std::string &value)
MLuint32 toUInt(bool *ok=nullptr) const
static std::string readEscapedString(std::istream &in)
std::ostream & write(std::ostream &out, bool asBinary, bool writeType=true) const
write variant to/read variant from stream, either as text or in binary form
Variant(const Vector6 &value)
Variant(const Variant &value)
bool toBool() const
Type type() const
get type of variant
Definition mlVariant.h:83
Vector3 toVec3(bool *ok=nullptr) const
Matrix4 toMat4(bool *ok=nullptr) const
bool canConvert(Type t) const
check if the Variant can be converted to the given type
Variant(MLint32 value)
Variant & operator=(const Variant &v)
Vector2 toVec2(bool *ok=nullptr) const
Variant(const Vector4 &value)
MLfloat toFloat(bool *ok=nullptr) const
Vector6 toVec6(bool *ok=nullptr) const
MLint32 toInt(bool *ok=nullptr) const
Matrix3 toMat3(bool *ok=nullptr) const
Variant(const Matrix4 &value)
static MLuint32 getCodeForType(Type t)
translation between variant type and 4-char-codes (used in both persistence types)
std::istream & read(std::istream &in, bool asBinary)
static Type getTypeForCode(MLuint32)
bool operator==(const Variant &v) const
static std::ostream & writeEscapedString(std::ostream &out, const std::string &s)
static public utility methods
Variant(MLdouble value)
Variant(MLint64 value)
bool isValid() const
check if the Variants type is not Invalid
Definition mlVariant.h:89
Variant(Type type, const std::string &value)
use this for FilePath, PngImageData, JpgImageData
Variant(bool value)
const T & asType() const
access content of variant directly, NOTE: use with care
Definition mlVariant.h:98
Variant(const Matrix3 &value)
MLuint64 toUInt64(bool *ok=nullptr) const
Variant(Type type, const void *valuePtr)
for internal use, copies data from valuePtr, with length according to type
Variant(const Vector3 &value)
Vector4 toVec4(bool *ok=nullptr) const
Variant(MLfloat value)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition mlTypeDefs.h:424
unsigned int MLuint32
Definition mlTypeDefs.h:185
double MLdouble
Definition mlTypeDefs.h:217
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
Definition mlTypeDefs.h:411
signed int MLint32
Definition mlTypeDefs.h:161
float MLfloat
Definition mlTypeDefs.h:201
#define MLVARIANT_EXPORT
defined Header file mlVariantSystem.h
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
Definition mlVariant.h:231
std::istream & operator>>(std::istream &in, ml::Variant &variant)
Definition mlVariant.h:236