21 #include <ThirdPartyWarningsDisable.h>
23 #include <ThirdPartyWarningsRestore.h>
31 ML_UTILS_START_NAMESPACE
39 Int, UInt, Int64, UInt64, Float,
Double,
40 Vec2, Vec3, Vec4, Vec6,
Mat3, Mat4,
71 bool isValid()
const {
return _type != Invalid; }
78 const T&
asType()
const {
return *
static_cast<T*
>(_data); }
103 std::ostream&
write(std::ostream& out,
bool asBinary,
bool writeType =
true)
const;
104 std::istream&
read(std::istream& in,
bool asBinary);
115 static void writeValue(std::ostream& out,
const T& value,
bool binary,
bool last =
true);
117 static void readValue(std::istream& in,
T& value,
bool binary,
bool last =
true);
122 static void writeValueAsText(std::ostream& out,
const T& value);
131 void copyValue(
Type type,
const void* valuePtr);
134 void copyValueTyped(
const void* valuePtr) { _data =
new T(*(
static_cast<const T*
>(valuePtr))); }
137 void clearValueTyped() {
delete static_cast<T*
>(_data); }
140 T asNumericType(
bool* ok)
const;
154 inline void Variant::writeValueAsText(std::ostream& out,
const T& value)
160 inline void Variant::writeValueAsText<float>(std::ostream& out,
const float& value)
163 std::streamsize oldPrecision = out.precision(8);
165 out.precision(oldPrecision);
169 inline void Variant::writeValueAsText<double>(std::ostream& out,
const double& value)
172 std::streamsize oldPrecision = out.precision(16);
174 out.precision(oldPrecision);
178 void Variant::writeValue(std::ostream& out,
const T& value,
bool binary,
bool last)
181 out.write(
reinterpret_cast<const char*
>(&value),
sizeof(
T));
183 writeValueAsText (out, value);
191 void Variant::readValue(std::istream& in,
T& value,
bool binary,
bool last)
194 in.read(
reinterpret_cast<char*
>(&value),
sizeof(
T));
198 std::string component;
199 std::getline(in, component,
',');
200 std::stringstream strstream;
201 strstream << component;
207 ML_UTILS_END_NAMESPACE
210 inline std::ostream&
operator<<(std::ostream& out,
const ML_UTILS_NAMESPACE::Variant& variant)
212 return variant.write(out,
false);
215 inline std::istream&
operator>>(std::istream& in, ML_UTILS_NAMESPACE::Variant& variant)
217 return variant.read(in,
false);
The Variant class stores different data types.
std::string toString() const
const T & asType() const
access content of variant directly, NOTE: use with care
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(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)
Type type() const
get type of variant
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
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
bool isValid() const
check if the Variants type is not Invalid
std::istream & read(std::istream &in, bool asBinary)
Variant(Type type, const std::string &value)
use this for FilePath, PngImageData, JpgImageData
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
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
#define MLVARIANT_EXPORT
defined Header file mlVariantSystem.h
std::ostream & operator<<(std::ostream &out, const ml::Variant &variant)
std::istream & operator>>(std::istream &in, ml::Variant &variant)
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Returns true if yes.
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Returns true if yes.