23.06.2014 - PICodeParser, PICodeInfo, PIConnection, new binary "pip_cmg"
This commit is contained in:
132
pivariant.cpp
132
pivariant.cpp
@@ -20,13 +20,42 @@
|
||||
#include "pivariant.h"
|
||||
|
||||
|
||||
/** \class PIVariant
|
||||
* \brief Variant type
|
||||
* \details
|
||||
* \section PIVariant_sec0 Synopsis
|
||||
* This class provides general type that can contains all standard types, some
|
||||
* PIP types or custom type. In case of standard types this class also provides
|
||||
* convertions between them.
|
||||
*
|
||||
* \section PIVariant_sec1 Usage
|
||||
* %PIVariant useful if you want pass many variables with different types in
|
||||
* single array, e.g.:
|
||||
* \code{cpp}
|
||||
* PIVector<PIVariant> array;
|
||||
* array << PIVariant(10) << PIVariant(1.61) << PIVariant(true) << PIVariant("0xFF");
|
||||
* piCout << array;
|
||||
* piForeachC (PIVariant & i, array)
|
||||
* piCout << i.toInt();
|
||||
* \endcode
|
||||
* Result:
|
||||
* \code{cpp}
|
||||
* {PIVariant(Int, 10), PIVariant(Double, 1,61), PIVariant(Bool, true), PIVariant(String, 0xFF)}
|
||||
* 10
|
||||
* 1
|
||||
* 1
|
||||
* 255
|
||||
* \endcode
|
||||
* */
|
||||
|
||||
|
||||
PIVariant::PIVariant() {
|
||||
type_ = PIVariant::Invalid;
|
||||
memset(_vraw, 0, __PIVARIANT_UNION_SIZE__);
|
||||
}
|
||||
|
||||
|
||||
void PIVariant::operator =(const PIVariant & v) {
|
||||
PIVariant & PIVariant::operator =(const PIVariant & v) {
|
||||
type_ = v.type_;
|
||||
memcpy(_vraw, v._vraw, __PIVARIANT_UNION_SIZE__);
|
||||
_vbytearray = v._vbytearray;
|
||||
@@ -34,6 +63,7 @@ void PIVariant::operator =(const PIVariant & v) {
|
||||
_vstring = v._vstring;
|
||||
_vstringlist = v._vstringlist;
|
||||
_vcustom = v._vcustom;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +164,12 @@ PIString PIVariant::typeName(PIVariant::Type type) {
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as boolean
|
||||
* \details In case of numeric types returns \b true if value != 0. \n
|
||||
* In case of String type returns \a PIString::toBool(). \n
|
||||
* In case of StringList type returns \b false if string list is empty,
|
||||
* otherwise returns \a PIString::toBool() of first string. \n
|
||||
* In case of other types returns \b false. */
|
||||
bool PIVariant::toBool() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -160,6 +196,13 @@ bool PIVariant::toBool() const {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** \brief Returns variant content as int
|
||||
* \details In case of numeric types returns integer value. \n
|
||||
* In case of String type returns \a PIString::toInt(). \n
|
||||
* In case of StringList type returns \b 0 if string list is empty,
|
||||
* otherwise returns \a PIString::toInt() of first string. \n
|
||||
* In case of other types returns \b 0. */
|
||||
int PIVariant::toInt() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -179,13 +222,19 @@ int PIVariant::toInt() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toInt();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toInt();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return 0; return _vstringlist.front().toInt();
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as long long
|
||||
* \details In case of numeric types returns integer value. \n
|
||||
* In case of String type returns \a PIString::toLLong(). \n
|
||||
* In case of StringList type returns \b 0L if string list is empty,
|
||||
* otherwise returns \a PIString::toLLong() of first string. \n
|
||||
* In case of other types returns \b 0L. */
|
||||
llong PIVariant::toLLong() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -205,13 +254,19 @@ llong PIVariant::toLLong() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toLLong();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toLLong();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return 0L; return _vstringlist.front().toLLong();
|
||||
default: break;
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as float
|
||||
* \details In case of numeric types returns float value. \n
|
||||
* In case of String type returns \a PIString::toFloat(). \n
|
||||
* In case of StringList type returns \b 0.f if string list is empty,
|
||||
* otherwise returns \a PIString::toFloat() of first string. \n
|
||||
* In case of other types returns \b 0.f. */
|
||||
float PIVariant::toFloat() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -231,13 +286,19 @@ float PIVariant::toFloat() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toFloat();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toFloat();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return 0.f; return _vstringlist.front().toFloat();
|
||||
default: break;
|
||||
}
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as double
|
||||
* \details In case of numeric types returns double value. \n
|
||||
* In case of String type returns \a PIString::toDouble(). \n
|
||||
* In case of StringList type returns \b 0. if string list is empty,
|
||||
* otherwise returns \a PIString::toDouble() of first string. \n
|
||||
* In case of other types returns \b 0.. */
|
||||
double PIVariant::toDouble() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -257,13 +318,19 @@ double PIVariant::toDouble() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return 0.; return _vstringlist.front().toDouble();
|
||||
default: break;
|
||||
}
|
||||
return 0.;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as long double
|
||||
* \details In case of numeric types returns long double value. \n
|
||||
* In case of String type returns \a PIString::toLDouble(). \n
|
||||
* In case of StringList type returns \b 0. if string list is empty,
|
||||
* otherwise returns \a PIString::toLDouble() of first string. \n
|
||||
* In case of other types returns \b 0.. */
|
||||
ldouble PIVariant::toLDouble() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -283,13 +350,19 @@ ldouble PIVariant::toLDouble() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toLDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toLDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return 0.; return _vstringlist.front().toLDouble();
|
||||
default: break;
|
||||
}
|
||||
return 0.;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as complex
|
||||
* \details In case of numeric types returns complex value. \n
|
||||
* In case of String type returns \a PIString::toDouble(). \n
|
||||
* In case of StringList type returns \b 0. if string list is empty,
|
||||
* otherwise returns \a PIString::toDouble() of first string. \n
|
||||
* In case of other types returns \b 0.. */
|
||||
complexd PIVariant::toComplexd() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -309,13 +382,19 @@ complexd PIVariant::toComplexd() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return complexd_0; return _vstringlist.front().toDouble();
|
||||
default: break;
|
||||
}
|
||||
return complexd_0;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as long complex
|
||||
* \details In case of numeric types returns long complex value. \n
|
||||
* In case of String type returns \a PIString::toLDouble(). \n
|
||||
* In case of StringList type returns \b 0. if string list is empty,
|
||||
* otherwise returns \a PIString::toLDouble() of first string. \n
|
||||
* In case of other types returns \b 0.. */
|
||||
complexld PIVariant::toComplexld() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool:
|
||||
@@ -335,13 +414,17 @@ complexld PIVariant::toComplexld() const {
|
||||
case PIVariant::Complexd: return _vcomplexd.real();
|
||||
case PIVariant::Complexld: return _vcomplexld.real();
|
||||
case PIVariant::String: return _vstring.toLDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return false; return _vstringlist.front().toLDouble();
|
||||
case PIVariant::StringList: if (_vstringlist.isEmpty()) return complexld_0; return _vstringlist.front().toLDouble();
|
||||
default: break;
|
||||
}
|
||||
return complexld_0;
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as time
|
||||
* \details In case of Time type returns time value. \n
|
||||
* In case of DateTime type returns time part of value. \n
|
||||
* In case of other types returns \a PITime(). */
|
||||
PITime PIVariant::toTime() const {
|
||||
if (type_ == PIVariant::Time) return _vtime;
|
||||
if (type_ == PIVariant::DateTime) return _vtime;
|
||||
@@ -349,6 +432,10 @@ PITime PIVariant::toTime() const {
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as date
|
||||
* \details In case of Date type returns date value. \n
|
||||
* In case of DateTime type returns date part of value. \n
|
||||
* In case of other types returns \a PIDate(). */
|
||||
PIDate PIVariant::toDate() const {
|
||||
if (type_ == PIVariant::Date) return _vdate;
|
||||
if (type_ == PIVariant::DateTime) return *((PIDate*)(&(_vdatetime.day)));
|
||||
@@ -356,6 +443,11 @@ PIDate PIVariant::toDate() const {
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as date and time
|
||||
* \details In case of Time type returns time value with null date. \n
|
||||
* In case of Date type returns date value with null time. \n
|
||||
* In case of DateTime type returns date and time. \n
|
||||
* In case of other types returns \a PIDateTime(). */
|
||||
PIDateTime PIVariant::toDateTime() const {
|
||||
if (type_ == PIVariant::DateTime) return _vdatetime;
|
||||
if (type_ == PIVariant::Time) return PIDateTime(_vtime);
|
||||
@@ -364,12 +456,25 @@ PIDateTime PIVariant::toDateTime() const {
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as system time
|
||||
* \details In case of SystemTime type returns system time. \n
|
||||
* In case of other types returns \a PISystemTime::fromSeconds() from
|
||||
* double value of variant content. */
|
||||
PISystemTime PIVariant::toSystemTime() const {
|
||||
if (type_ == PIVariant::SystemTime) return _vsystime;
|
||||
return PISystemTime::fromSeconds(toDouble());
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as string
|
||||
* \details In case of numeric types returns \a PIString::fromNumber(). \n
|
||||
* In case of String type returns string value. \n
|
||||
* In case of StringList type returns joined string ("(" + PIStringList::join("; ") + ")"). \n
|
||||
* In case of BitArray or ByteArray types returns number of bits/bytes. \n
|
||||
* In case of Time, Date or DateTime types returns toString() of this values. \n
|
||||
* In case of SystemTime types returns second and nanoseconds of time
|
||||
* ("(PISystemTime::seconds s, PISystemTime::nanoseconds ns)"). \n
|
||||
* In case of other types returns \b "". */
|
||||
PIString PIVariant::toString() const {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool: return _vint == 0 ? "false" : "true";
|
||||
@@ -389,7 +494,7 @@ PIString PIVariant::toString() const {
|
||||
case PIVariant::Complexd: return "(" + PIString::fromNumber(_vcomplexd.real()) + "; " + PIString::fromNumber(_vcomplexd.imag()) + ")";
|
||||
case PIVariant::Complexld: return "(" + PIString::fromNumber(_vcomplexld.real()) + "; " + PIString::fromNumber(_vcomplexld.imag()) + ")";
|
||||
case PIVariant::BitArray: return PIString::fromNumber(_vbitarray.bitSize()) + " bits";
|
||||
case PIVariant::ByteArray: return PIString::fromNumber(_vbytearray.size()) + " bytes";
|
||||
case PIVariant::ByteArray: return _vbytearray.toString();
|
||||
case PIVariant::String: return _vstring;
|
||||
case PIVariant::StringList: return "(" + _vstringlist.join("; ") + ")";
|
||||
case PIVariant::Time: return _vtime.toString();
|
||||
@@ -402,18 +507,27 @@ PIString PIVariant::toString() const {
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as strings list
|
||||
* \details In case of StringList type returns strings list value. \n
|
||||
* In case of other types returns \a PIStringList with one string value of variant content. */
|
||||
PIStringList PIVariant::toStringList() const {
|
||||
if (type_ == PIVariant::StringList) return _vstringlist;
|
||||
return PIStringList(toString());
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as bit array
|
||||
* \details In case of BitArray type returns bit array value. \n
|
||||
* In case of other types returns \a PIBitArray from \a toLLong() value. */
|
||||
PIBitArray PIVariant::toBitArray() const {
|
||||
if (type_ == PIVariant::BitArray) return _vbitarray;
|
||||
return PIBitArray(ullong(toLLong()));
|
||||
}
|
||||
|
||||
|
||||
/** \brief Returns variant content as byte array
|
||||
* \details In case of ByteArray type returns byte array value. \n
|
||||
* In case of other types returns empty \a PIByteArray. */
|
||||
PIByteArray PIVariant::toByteArray() const {
|
||||
if (type_ == PIVariant::ByteArray) return _vbytearray;
|
||||
return PIByteArray();
|
||||
|
||||
Reference in New Issue
Block a user