MeVisLab Toolbox Reference
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 
31 ML_UTILS_START_NAMESPACE
32 
36 {
37 public:
38  enum Type {Invalid, Bool, String,
39  Int, UInt, Int64, UInt64, Float, Double,
40  Vec2, Vec3, Vec4, Vec6, Mat3, Mat4,
41  FilePath, PngImageData, JpgImageData,
42  TypeCount
43  };
44 
47  Variant(const Variant& value);
48  Variant(const std::string& value);
49  Variant(Type type, const std::string& value);
50  Variant(bool value);
51  Variant(MLint32 value);
52  Variant(MLuint32 value);
53  Variant(MLint64 value);
54  Variant(MLuint64 value);
55  Variant(MLfloat value);
56  Variant(MLdouble value);
57  Variant(const Vector2& value);
58  Variant(const Vector3& value);
59  Variant(const Vector4& value);
60  Variant(const Vector6& value);
61  Variant(const Matrix3& value);
62  Variant(const Matrix4& value);
63 
65  Variant(Type type, const void* valuePtr);
66 
68  Type type() const { return _type; }
69 
71  bool isValid() const { return _type != Invalid; }
72 
74  bool canConvert(Type t) const;
75 
77  template<typename T>
78  const T& asType() const { return *static_cast<T*>(_data); }
79 
81  void clear();
82 
83  std::string toString() const;
84  bool toBool() const;
85  MLint32 toInt(bool* ok = nullptr) const;
86  MLuint32 toUInt(bool* ok = nullptr) const;
87  MLint64 toInt64(bool* ok = nullptr) const;
88  MLuint64 toUInt64(bool* ok = nullptr) const;
89  MLfloat toFloat(bool* ok = nullptr) const;
90  MLdouble toDouble(bool* ok = nullptr) const;
91  Vector2 toVec2(bool* ok = nullptr) const;
92  Vector3 toVec3(bool* ok = nullptr) const;
93  Vector4 toVec4(bool* ok = nullptr) const;
94  Vector6 toVec6(bool* ok = nullptr) const;
95  Matrix3 toMat3(bool* ok = nullptr) const;
96  Matrix4 toMat4(bool* ok = nullptr) const;
97 
98  Variant& operator= (const Variant& v);
99  bool operator== (const Variant& v) const;
100  bool operator!= (const Variant& v) const;
101 
103  std::ostream& write(std::ostream& out, bool asBinary, bool writeType = true) const;
104  std::istream& read(std::istream& in, bool asBinary);
105 
107 
109  static std::ostream& writeEscapedString(std::ostream& out, const std::string& s);
110  static std::string readEscapedString(std::istream& in);
111 
114  template<typename T>
115  static void writeValue(std::ostream& out, const T& value, bool binary, bool last = true);
116  template<typename T>
117  static void readValue(std::istream& in, T& value, bool binary, bool last = true);
118 
121  template<typename T>
122  static void writeValueAsText(std::ostream& out, const T& value);
123 
128 
129 private:
131  void copyValue(Type type, const void* valuePtr);
132 
133  template<typename T>
134  void copyValueTyped(const void* valuePtr) { _data = new T(*(static_cast<const T*>(valuePtr))); }
135 
136  template<typename T>
137  void clearValueTyped() { delete static_cast<T*>(_data); }
138 
139  template<typename T>
140  T asNumericType(bool* ok) const;
141 
143  static MLuint32 _typeCodes[];
144 
146  Type _type;
147 
149  void* _data;
150 };
151 
152 
153 template<typename T>
154 inline void Variant::writeValueAsText(std::ostream& out, const T& value)
155 {
156  out << value;
157 }
158 
159 template<>
160 inline void Variant::writeValueAsText<float>(std::ostream& out, const float& value)
161 {
162  // set sufficient precision
163  std::streamsize oldPrecision = out.precision(8);
164  out << value;
165  out.precision(oldPrecision);
166 }
167 
168 template<>
169 inline void Variant::writeValueAsText<double>(std::ostream& out, const double& value)
170 {
171  // set sufficient precision
172  std::streamsize oldPrecision = out.precision(16);
173  out << value;
174  out.precision(oldPrecision);
175 }
176 
177 template<typename T>
178 void Variant::writeValue(std::ostream& out, const T& value, bool binary, bool last)
179 {
180  if (binary) {
181  out.write(reinterpret_cast<const char*>(&value), sizeof(T));
182  } else {
183  writeValueAsText (out, value);
184  if (!last) {
185  out << ",";
186  }
187  }
188 }
189 
190 template<typename T>
191 void Variant::readValue(std::istream& in, T& value, bool binary, bool last)
192 {
193  if (binary) {
194  in.read(reinterpret_cast<char*>(&value), sizeof(T));
195  } else if (last) {
196  in >> value;
197  } else {
198  std::string component;
199  std::getline(in, component, ',');
200  std::stringstream strstream;
201  strstream << component;
202  strstream >> value;
203  }
204 }
205 
206 
207 ML_UTILS_END_NAMESPACE
208 
209 
210 inline std::ostream& operator<<(std::ostream& out, const ML_UTILS_NAMESPACE::Variant& variant)
211 {
212  return variant.write(out, /*asBinary=*/false);
213 }
214 
215 inline std::istream& operator>>(std::istream& in, ML_UTILS_NAMESPACE::Variant& variant)
216 {
217  return variant.read(in, /*asBinary=*/false);
218 }
219 
220 
221 
222 #endif // __mlVariant_H
223 
224 
@ T
Definition: SoKeyGrabber.h:71
The Variant class stores different data types.
Definition: mlVariant.h:36
std::string toString() const
const T & asType() const
access content of variant directly, NOTE: use with care
Definition: mlVariant.h:78
Variant(MLuint32 value)
Variant(const Vector2 &value)
MLint64 toInt64(bool *ok=nullptr) 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)
Variant(const Vector6 &value)
static std::ostream & writeEscapedString(std::ostream &out, const std::string &s)
static public utility methods
Variant(const Variant &value)
bool toBool() const
Type type() const
get type of variant
Definition: mlVariant.h:68
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)
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)
static Type getTypeForCode(MLuint32)
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(MLdouble value)
Variant(MLint64 value)
bool isValid() const
check if the Variants type is not Invalid
Definition: mlVariant.h:71
std::istream & read(std::istream &in, bool asBinary)
Variant(Type type, const std::string &value)
use this for FilePath, PngImageData, JpgImageData
Variant(bool value)
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)
UINT64 MLuint64
Introduce platform independent 64 bit unsigned integer type.
Definition: mlTypeDefs.h:513
unsigned int MLuint32
Definition: mlTypeDefs.h:191
double MLdouble
Definition: mlTypeDefs.h:223
INT64 MLint64
Include 64 bit integer support for Windows or Unix.
Definition: mlTypeDefs.h:500
signed int MLint32
Definition: mlTypeDefs.h:167
float MLfloat
Definition: mlTypeDefs.h:207
#define MLVARIANT_EXPORT
defined Header file mlVariantSystem.h
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
Definition: mlVariant.h:210
std::istream & operator>>(std::istream &in, ml::Variant &variant)
Definition: mlVariant.h:215
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition: mlMatrix2.h:425
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Return true if yes.
Definition: mlMatrix2.h:433