MeVisLabToolboxReference
|
00001 // **InsertLicense** code 00002 //=========================================================================== 00004 00009 //=========================================================================== 00010 00011 #ifndef __mlVariant_H 00012 #define __mlVariant_H 00013 00014 #include "mlVariantSystem.h" 00015 00016 #include <string> 00017 #include <mlVector2.h> 00018 #include <mlVector3.h> 00019 #include <mlVector4.h> 00020 #include <mlVector6.h> 00021 #include <mlMatrix3.h> 00022 #include <mlMatrix4.h> 00023 00024 ML_UTILS_START_NAMESPACE 00025 00028 class MLVARIANT_EXPORT Variant 00029 { 00030 public: 00031 enum Type {Invalid, Bool, String, 00032 Int, UInt, Int64, UInt64, Float, Double, 00033 Vec2, Vec3, Vec4, Vec6, Mat3, Mat4, 00034 FilePath, PngImageData, JpgImageData, 00035 TypeCount 00036 }; 00037 00038 Variant(); 00039 Variant(const Variant& value); 00040 Variant(const std::string& value); 00041 Variant(Type type, const std::string& value); 00042 Variant(bool value); 00043 Variant(MLint32 value); 00044 Variant(MLuint32 value); 00045 Variant(MLint64 value); 00046 Variant(MLuint64 value); 00047 Variant(MLfloat value); 00048 Variant(MLdouble value); 00049 Variant(const Vector2& value); 00050 Variant(const Vector3& value); 00051 Variant(const Vector4& value); 00052 Variant(const Vector6& value); 00053 Variant(const Matrix3& value); 00054 Variant(const Matrix4& value); 00055 00057 Variant(Type type, const void* valuePtr); 00058 00060 Type type() const { return _type; } 00061 00063 bool isValid() const { return _type != Invalid; } 00064 00066 bool canConvert(Type t) const; 00067 00069 template<typename T> 00070 const T& asType() const { return *static_cast<T*>(_data); } 00071 00073 void clear(); 00074 00075 std::string toString() const; 00076 bool toBool() const; 00077 MLint32 toInt(bool* ok = 0) const; 00078 MLuint32 toUInt(bool* ok = 0) const; 00079 MLint64 toInt64(bool* ok = 0) const; 00080 MLuint64 toUInt64(bool* ok = 0) const; 00081 MLfloat toFloat(bool* ok = 0) const; 00082 MLdouble toDouble(bool* ok = 0) const; 00083 Vector2 toVec2(bool* ok = 0) const; 00084 Vector3 toVec3(bool* ok = 0) const; 00085 Vector4 toVec4(bool* ok = 0) const; 00086 Vector6 toVec6(bool* ok = 0) const; 00087 Matrix3 toMat3(bool* ok = 0) const; 00088 Matrix4 toMat4(bool* ok = 0) const; 00089 00090 Variant& operator= (const Variant& v); 00091 bool operator== (const Variant& v) const; 00092 bool operator!= (const Variant& v) const; 00093 00095 std::ostream& write(std::ostream& out, bool asBinary, bool writeType = true) const; 00096 std::istream& read(std::istream& in, bool asBinary); 00097 00099 00101 static std::ostream& writeEscapedString(std::ostream& out, const std::string& s); 00102 static std::string readEscapedString(std::istream& in); 00103 00106 template<typename T> 00107 static void writeValue(std::ostream& out, const T& value, bool binary, bool last = true); 00108 template<typename T> 00109 static void readValue(std::istream& in, T& value, bool binary, bool last = true); 00110 00113 template<typename T> 00114 static void writeValueAsText(std::ostream& out, const T& value); 00115 00117 static MLuint32 getCodeForType(Type t); 00118 static Type getTypeForCode(MLuint32); 00120 00121 private: 00123 void copyValue(Type type, const void* valuePtr); 00124 00125 template<typename T> 00126 void copyValueTyped(const void* valuePtr) { _data = new T(*(static_cast<const T*>(valuePtr))); } 00127 00128 template<typename T> 00129 void clearValueTyped() { delete static_cast<T*>(_data); } 00130 00131 template<typename T> 00132 T asNumericType(bool* ok) const; 00133 00135 static MLuint32 _typeCodes[]; 00136 00138 Type _type; 00139 00141 void* _data; 00142 }; 00143 00144 00145 template<typename T> 00146 inline void Variant::writeValueAsText(std::ostream& out, const T& value) 00147 { 00148 out << value; 00149 } 00150 00151 template<> 00152 inline void Variant::writeValueAsText<float>(std::ostream& out, const float& value) 00153 { 00154 // set sufficient precision 00155 std::streamsize oldPrecision = out.precision(8); 00156 out << value; 00157 out.precision(oldPrecision); 00158 } 00159 00160 template<> 00161 inline void Variant::writeValueAsText<double>(std::ostream& out, const double& value) 00162 { 00163 // set sufficient precision 00164 std::streamsize oldPrecision = out.precision(16); 00165 out << value; 00166 out.precision(oldPrecision); 00167 } 00168 00169 template<typename T> 00170 void Variant::writeValue(std::ostream& out, const T& value, bool binary, bool last) 00171 { 00172 if (binary) { 00173 out.write(reinterpret_cast<const char*>(&value), sizeof(T)); 00174 } else { 00175 writeValueAsText (out, value); 00176 if (!last) { 00177 out << ","; 00178 } 00179 } 00180 } 00181 00182 template<typename T> 00183 void Variant::readValue(std::istream& in, T& value, bool binary, bool last) 00184 { 00185 if (binary) { 00186 in.read(reinterpret_cast<char*>(&value), sizeof(T)); 00187 } else if (last) { 00188 in >> value; 00189 } else { 00190 std::string component; 00191 std::getline(in, component, ','); 00192 std::stringstream strstream; 00193 strstream << component; 00194 strstream >> value; 00195 } 00196 } 00197 00198 00199 ML_UTILS_END_NAMESPACE 00200 00201 00202 inline std::ostream& operator<<(std::ostream& out, const ML_UTILS_NAMESPACE::Variant& variant) 00203 { 00204 return variant.write(out, /*asBinary=*/false); 00205 } 00206 00207 inline std::istream& operator>>(std::istream& in, ML_UTILS_NAMESPACE::Variant& variant) 00208 { 00209 return variant.read(in, /*asBinary=*/false); 00210 } 00211 00212 00213 00214 #endif // __mlVariant_H 00215 00216