/*
	PIP - Platform Independent Primitives
	Variant type
	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#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
 * */

#ifdef CUSTOM_PIVARIANT
PIMap<PIString, __PIVariantInfo__ * > * __PIVariantInfoStorage__::map = 0;
#endif


PIVariant::PIVariant() {
	_type = PIVariant::pivInvalid;
	_info = 0;
}


PIVariant::PIVariant(const PIVariant & v) {
	_type = v._type;
	_content = v._content;
#ifdef CUSTOM_PIVARIANT
	_info = v._info;
#endif
}


PIVariant::PIVariant(PIVariant && v) {
	swap(v);
}


void PIVariant::setValueFromString(const PIString & v) {
	switch (_type) {
	case PIVariant::pivBool: {setValue(v.toBool());} break;
	case PIVariant::pivChar: {setValue(v.toChar());} break;
	case PIVariant::pivUChar: {setValue((uchar)v.toChar());} break;
	case PIVariant::pivShort: {setValue(v.toShort());} break;
	case PIVariant::pivUShort: {setValue(v.toUShort());} break;
	case PIVariant::pivInt: {setValue(v.toInt());} break;
	case PIVariant::pivUInt: {setValue(v.toUInt());} break;
	case PIVariant::pivLLong: {setValue(v.toLLong());} break;
	case PIVariant::pivULLong: {setValue(v.toULLong());} break;
	case PIVariant::pivFloat: {setValue(v.toFloat());} break;
	case PIVariant::pivDouble: {setValue(v.toDouble());} break;
	case PIVariant::pivLDouble: {setValue(v.toLDouble());} break;
	case PIVariant::pivTime: {} break; // TODO
	case PIVariant::pivDate: {} break; // TODO
	case PIVariant::pivDateTime: {} break; // TODO
	case PIVariant::pivString: {setValue(v);} break;
	case PIVariant::pivStringList: {setValue(v.split("%|%"));} break;
	case PIVariant::pivEnum: {PIVariantTypes::Enum r = toEnum(); r.selectName(v); setValue(r);} break;
	case PIVariant::pivFile: {PIVariantTypes::File r = toFile(); r.file = v; setValue(r);} break;
	case PIVariant::pivDir: {PIVariantTypes::Dir r = toDir();  r.dir = v; setValue(r);} break;
	case PIVariant::pivColor: {setValue(PIVariantTypes::Color(v.mid(1).toUInt(16)));} break;
	case PIVariant::pivIODevice: {setValue(PIVariantTypes::IODevice());} break; // TODO
	case PIVariant::pivCustom: {} break; // TODO;
	default: break;
	}
}


PIVariant & PIVariant::operator =(const PIVariant & v) {
	_type = v._type;
	_content = v._content;
#ifdef CUSTOM_PIVARIANT
	_info = v._info;
#endif
	return *this;
}


PIVariant & PIVariant::operator =(PIVariant && v) {
	swap(v);
	return *this;
}


bool PIVariant::operator ==(const PIVariant & v) const {
	return (_type == v._type) && (_content == v._content);
}


PIVariant::Type PIVariant::typeFromName(const PIString & tname) {
	PIString s = tname.trimmed().toLowerCase().replaceAll(" ", "");
	if (s == "bool" || s == "boolean") return PIVariant::pivBool;
	if (s == "char" || s == "sbyte") return PIVariant::pivChar;
	if (s == "short" || s == "shortint" || s == "signedshort" || s == "signedshortint" || s == "sword") return PIVariant::pivShort;
	if (s == "int" || s == "signed" || s == "signedint") return PIVariant::pivInt;
	if (s == "long" || s == "longint" || s == "signedlong" || s == "signedlongint" || s == "sdword") return PIVariant::pivInt;
	if (s == "llong" || s == "longlong" || s == "longlongint" || s == "signedlonglong" || s == "signedlonglongint" || s == "sqword") return PIVariant::pivLLong;
	if (s == "uchar" || s == "byte") return PIVariant::pivUChar;
	if (s == "ushort" || s == "unsignedshort" || s == "unsignedshortint" || s == "word") return PIVariant::pivUShort;
	if (s == "uint" || s == "unsigned" || s == "unsignedint") return PIVariant::pivUInt;
	if (s == "ulong" || s == "unsignedlong" || s == "unsignedlongint" || s == "dword") return PIVariant::pivUInt;
	if (s == "ullong" || s == "unsignedlonglong" || s == "unsignedlonglongint" || s == "qword") return PIVariant::pivULLong;
	if (s == "float") return PIVariant::pivFloat;
	if (s == "double" || s == "real") return PIVariant::pivDouble;
	if (s == "ldouble" || s == "longdouble") return PIVariant::pivLDouble;
	if (s == "complexd" || s == "complex<double>") return PIVariant::pivComplexd;
	if (s == "complexld" || s == "complex<ldouble>" || s == "complex<longdouble>") return PIVariant::pivComplexld;
	if (s == "pibitarray" || s == "bitarray") return PIVariant::pivBitArray;
	if (s == "pibytearray" || s == "bytearray" || s == "vector<uchar>" || s == "pivector<uchar>" || s == "vector<unsignedchar>" || s == "pivector<unsignedchar>" ||
		s == "vector<char>" || s == "pivector<char>") return PIVariant::pivByteArray;
	if (s == "pistring" || s == "string") return PIVariant::pivString;
	if (s == "pistringlist" || s == "stringlist" || s == "vector<string>" || s == "vector<pistring>" || s == "pivector<string>" || s == "pivector<pistring>") return PIVariant::pivStringList;
	if (s == "pitime" || s == "time") return PIVariant::pivTime;
	if (s == "pidate" || s == "date") return PIVariant::pivDate;
	if (s == "pidatetime" || s == "datetime") return PIVariant::pivDateTime;
	if (s == "pisystemtime" || s == "systemtime") return PIVariant::pivSystemTime;
	if (s == "enum") return PIVariant::pivEnum;
	if (s == "file" || s == "path") return PIVariant::pivFile;
	if (s == "dir" || s == "directory") return PIVariant::pivDir;
	if (s == "color") return PIVariant::pivColor;
	if (s == "point") return PIVariant::pivPoint;
	if (s == "rect") return PIVariant::pivRect;
	if (s == "vector") return PIVariant::pivMathVector;
	if (s == "matrix") return PIVariant::pivMathMatrix;
	return PIVariant::pivInvalid;
}


PIString PIVariant::typeName() const {
#ifdef CUSTOM_PIVARIANT
	if ((_type == pivCustom) && _info)
		return _info->typeName;
#endif
	return typeName(_type);
}


void PIVariant::swap(PIVariant & v) {
	piSwap(_type, v._type);
	_content.swap(v._content);
#ifdef CUSTOM_PIVARIANT
	piSwap(_info, v._info);
#endif
}


PIString PIVariant::typeName(PIVariant::Type type) {
	switch (type) {
	case PIVariant::pivBool: return "Bool";
	case PIVariant::pivChar: return "Char";
	case PIVariant::pivUChar: return "UChar";
	case PIVariant::pivShort: return "Short";
	case PIVariant::pivUShort: return "UShort";
	case PIVariant::pivInt: return "Int";
	case PIVariant::pivUInt: return "UInt";
	case PIVariant::pivLLong: return "LLong";
	case PIVariant::pivULLong: return "ULLong";
	case PIVariant::pivFloat: return "Float";
	case PIVariant::pivDouble: return "Double";
	case PIVariant::pivLDouble: return "LDouble";
	case PIVariant::pivComplexd: return "Complexd";
	case PIVariant::pivComplexld: return "Complexld";
	case PIVariant::pivBitArray: return "BitArray";
	case PIVariant::pivByteArray: return "ByteArray";
	case PIVariant::pivString: return "String";
	case PIVariant::pivStringList: return "StringList";
	case PIVariant::pivTime: return "Time";
	case PIVariant::pivDate: return "Date";
	case PIVariant::pivDateTime: return "DateTime";
	case PIVariant::pivSystemTime: return "SystemTime";
	case PIVariant::pivEnum: return "Enum";
	case PIVariant::pivFile: return "File";
	case PIVariant::pivDir: return "Dir";
	case PIVariant::pivColor: return "Color";
	case PIVariant::pivPoint: return "Point";
	case PIVariant::pivRect: return "Rect";
	case PIVariant::pivMathVector: return "Vector";
	case PIVariant::pivMathMatrix: return "Matrix";
	case PIVariant::pivCustom: return "Custom";
	default: break;
	}
	return "Invalid";
}


/** \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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return r;}
	case PIVariant::pivChar: {char r; ba >> r; return r != 0;}
	case PIVariant::pivUChar: {uchar r; ba >> r; return r != 0;}
	case PIVariant::pivShort: {short r; ba >> r; return r != 0;}
	case PIVariant::pivUShort: {ushort r; ba >> r; return r != 0;}
	case PIVariant::pivInt: {int r; ba >> r; return r != 0;}
	case PIVariant::pivUInt: {uint r; ba >> r; return r != 0;}
	case PIVariant::pivLLong: {llong r; ba >> r; return r != 0;}
	case PIVariant::pivULLong: {ullong r; ba >> r; return r != 0;}
	case PIVariant::pivFloat: {float r; ba >> r; return r > 0.f;}
	case PIVariant::pivDouble: {double r; ba >> r; return r > 0.;}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return r > 0.;}
	case PIVariant::pivString: {PIString r; ba >> r; return r.toBool();}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return false; return r.front().toBool();}
	case PIVariant::pivCustom: return getAsValue<bool>(*this);
	default: break;
	}
	return false;
}



/** \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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return r;}
	case PIVariant::pivChar: {char r; ba >> r; return r;}
	case PIVariant::pivUChar: {uchar r; ba >> r; return r;}
	case PIVariant::pivShort: {short r; ba >> r; return r;}
	case PIVariant::pivUShort: {ushort r; ba >> r; return r;}
	case PIVariant::pivInt: {int r; ba >> r; return r;}
	case PIVariant::pivUInt: {uint r; ba >> r; return r;}
	case PIVariant::pivLLong: {llong r; ba >> r; return r;}
	case PIVariant::pivULLong: {ullong r; ba >> r; return r;}
	case PIVariant::pivFloat: {float r; ba >> r; return r;}
	case PIVariant::pivDouble: {double r; ba >> r; return r;}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return r;}
	case PIVariant::pivString: {PIString r; ba >> r; return r.toInt();}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return 0; return r.front().toInt();}
	case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return r.selectedValue();}
	case PIVariant::pivColor: {PIVariantTypes::Color r; ba >> r; return (int)r.rgba;}
	case PIVariant::pivCustom: return getAsValue<int>(*this);
	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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return r;}
	case PIVariant::pivChar: {char r; ba >> r; return r;}
	case PIVariant::pivUChar: {uchar r; ba >> r; return r;}
	case PIVariant::pivShort: {short r; ba >> r; return r;}
	case PIVariant::pivUShort: {ushort r; ba >> r; return r;}
	case PIVariant::pivInt: {int r; ba >> r; return r;}
	case PIVariant::pivUInt: {uint r; ba >> r; return r;}
	case PIVariant::pivLLong: {llong r; ba >> r; return r;}
	case PIVariant::pivULLong: {ullong r; ba >> r; return r;}
	case PIVariant::pivFloat: {float r; ba >> r; return r;}
	case PIVariant::pivDouble: {double r; ba >> r; return r;}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return r;}
	case PIVariant::pivString: {PIString r; ba >> r; return r.toLLong();}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return 0L; return r.front().toLLong();}
	case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return llong(r.selectedValue());}
	case PIVariant::pivCustom: return getAsValue<llong>(*this);
	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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return r;}
	case PIVariant::pivChar: {char r; ba >> r; return r;}
	case PIVariant::pivUChar: {uchar r; ba >> r; return r;}
	case PIVariant::pivShort: {short r; ba >> r; return r;}
	case PIVariant::pivUShort: {ushort r; ba >> r; return r;}
	case PIVariant::pivInt: {int r; ba >> r; return r;}
	case PIVariant::pivUInt: {uint r; ba >> r; return r;}
	case PIVariant::pivLLong: {llong r; ba >> r; return r;}
	case PIVariant::pivULLong: {ullong r; ba >> r; return r;}
	case PIVariant::pivFloat: {float r; ba >> r; return r;}
	case PIVariant::pivDouble: {double r; ba >> r; return r;}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return r;}
	case PIVariant::pivString: {PIString r; ba >> r; return r.toFloat();}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return 0.f; return r.front().toFloat();}
	case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return float(r.selectedValue());}
	case PIVariant::pivCustom: return getAsValue<float>(*this);
	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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return r;}
	case PIVariant::pivChar: {char r; ba >> r; return r;}
	case PIVariant::pivUChar: {uchar r; ba >> r; return r;}
	case PIVariant::pivShort: {short r; ba >> r; return r;}
	case PIVariant::pivUShort: {ushort r; ba >> r; return r;}
	case PIVariant::pivInt: {int r; ba >> r; return r;}
	case PIVariant::pivUInt: {uint r; ba >> r; return r;}
	case PIVariant::pivLLong: {llong r; ba >> r; return r;}
	case PIVariant::pivULLong: {ullong r; ba >> r; return r;}
	case PIVariant::pivFloat: {float r; ba >> r; return r;}
	case PIVariant::pivDouble: {double r; ba >> r; return r;}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return r;}
	case PIVariant::pivString: {PIString r; ba >> r; return r.toDouble();}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return 0.; return r.front().toDouble();}
	case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return double(r.selectedValue());}
	case PIVariant::pivCustom: return getAsValue<double>(*this);
	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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return r;}
	case PIVariant::pivChar: {char r; ba >> r; return r;}
	case PIVariant::pivUChar: {uchar r; ba >> r; return r;}
	case PIVariant::pivShort: {short r; ba >> r; return r;}
	case PIVariant::pivUShort: {ushort r; ba >> r; return r;}
	case PIVariant::pivInt: {int r; ba >> r; return r;}
	case PIVariant::pivUInt: {uint r; ba >> r; return r;}
	case PIVariant::pivLLong: {llong r; ba >> r; return r;}
	case PIVariant::pivULLong: {ullong r; ba >> r; return r;}
	case PIVariant::pivFloat: {float r; ba >> r; return r;}
	case PIVariant::pivDouble: {double r; ba >> r; return r;}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return r;}
	case PIVariant::pivString: {PIString r; ba >> r; return r.toLDouble();}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return 0.; return r.front().toLDouble();}
	case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return ldouble(r.selectedValue());}
	case PIVariant::pivCustom: return getAsValue<float>(*this);
	default: break;
	}
	return 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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivTime) {PITime r; ba >> r; return r;}
	if (_type == PIVariant::pivDateTime) {PIDateTime r; ba >> r; return r.time();}
	if (_type == PIVariant::pivCustom) {return getAsValue<PITime>(*this);}
	return PITime();
}


/** \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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivDate) {PIDate r; ba >> r; return r;}
	if (_type == PIVariant::pivDateTime) {PIDateTime r; ba >> r; return r.date();}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIDate>(*this);}
	return PIDate();
}


/** \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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivTime) {PITime r; ba >> r; return PIDateTime(r);}
	if (_type == PIVariant::pivDate) {PIDate r; ba >> r; return PIDateTime(r);}
	if (_type == PIVariant::pivDateTime) {PIDateTime r; ba >> r; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIDateTime>(*this);}
	return PIDateTime();
}


/** \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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivSystemTime) {PISystemTime r; ba >> r; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PISystemTime>(*this);}
	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 {
	PIByteArray ba(_content);
	switch (_type) {
	case PIVariant::pivBool: {bool r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivChar: {char r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivUChar: {uchar r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivShort: {short r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivUShort: {ushort r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivInt: {int r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivUInt: {uint r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivLLong: {llong r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivULLong: {ullong r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivFloat: {float r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivDouble: {double r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivLDouble: {ldouble r; ba >> r; return PIString::fromNumber(r);}
	case PIVariant::pivTime: {PITime r; ba >> r; return r.toString();}
	case PIVariant::pivDate: {PIDate r; ba >> r; return r.toString();}
	case PIVariant::pivDateTime: {PIDateTime r; ba >> r; return r.toString();}
	case PIVariant::pivString: {PIString r; ba >> r; return r;}
	case PIVariant::pivStringList: {PIStringList r; ba >> r; if (r.isEmpty()) return PIString(); return r.join(";");}
	case PIVariant::pivEnum: {PIVariantTypes::Enum r; ba >> r; return r.selectedName();}
	case PIVariant::pivFile: {PIVariantTypes::File r; ba >> r; return r.file;}
	case PIVariant::pivDir: {PIVariantTypes::Dir r; ba >> r; return r.dir;}
	case PIVariant::pivColor: {PIVariantTypes::Color r; ba >> r; return "#" + PIString::fromNumber(r.rgba, 16);}
	case PIVariant::pivIODevice: {PIVariantTypes::IODevice r; ba >> r; return "IODevice";} // TODO
	case PIVariant::pivCustom: return getAsValue<PIString>(*this);
	default: break;
	}
	return PIString();
}


/** \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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivStringList) {PIStringList r; ba >> r; return r;}
	if (_type == PIVariant::pivEnum) {PIVariantTypes::Enum r; ba >> r; return r.names();}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIStringList>(*this);}
	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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivBitArray) {PIBitArray r; ba >> r; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIBitArray>(*this);}
	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 {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivByteArray) {PIByteArray r; ba >> r; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIByteArray>(*this);}
	return PIByteArray();
}


/** \brief Returns variant content as enum
 * \details In case of Enum type returns enum value. \n
 * In case of String returns Enum with one member. \n
 * In case of StringList returns Enum with corresponding members. \n
 * In case of other types returns empty Enum. */
PIVariantTypes::Enum PIVariant::toEnum() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivEnum) {PIVariantTypes::Enum r; ba >> r; return r;}
	if (_type == PIVariant::pivString) {PIString v; ba >> v; PIVariantTypes::Enum r; r << v; return r;}
	if (_type == PIVariant::pivStringList) {PIStringList v; ba >> v; PIVariantTypes::Enum r; r << v; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIVariantTypes::Enum>(*this);}
	return PIVariantTypes::Enum();
}


/** \brief Returns variant content as file
 * \details In case of File type returns file value. \n
 * In case of String returns File with string value path. \n
 * In case of other types returns empty File. */
PIVariantTypes::File PIVariant::toFile() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivFile) {PIVariantTypes::File r; ba >> r; return r;}
	if (_type == PIVariant::pivString) {PIString v; ba >> v; PIVariantTypes::File r; r.file = v; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIVariantTypes::File>(*this);}
	return PIVariantTypes::File();
}


/** \brief Returns variant content as dir
 * \details In case of Dir type returns dir value. \n
 * In case of String returns Dir with string value path. \n
 * In case of other types returns empty Dir. */
PIVariantTypes::Dir PIVariant::toDir() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivDir) {PIVariantTypes::Dir r; ba >> r; return r;}
	if (_type == PIVariant::pivString) {PIString v; ba >> v; PIVariantTypes::Dir r; r.dir = v; return r;}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIVariantTypes::Dir>(*this);}
	return PIVariantTypes::Dir();
}


/** \brief Returns variant content as color
 * \details In case of Color type returns color value. \n
 * In case of int returns color with int value. \n
 * In case of other types returns empty Color. */
PIVariantTypes::Color PIVariant::toColor() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivColor) {PIVariantTypes::Color r; ba >> r; return r;}
	if (_type == PIVariant::pivInt) {int v; ba >> v; return PIVariantTypes::Color(v);}
	if (_type == PIVariant::pivCustom) {return getAsValue<PIVariantTypes::Color>(*this);}
	return PIVariantTypes::Color();
}


/** \brief Returns variant content as IODevice
 * \details In case of IODevice type returns IODevice value. \n
 * In case of other types returns empty IODevice. */
PIVariantTypes::IODevice PIVariant::toIODevice() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivIODevice) {PIVariantTypes::IODevice r; ba >> r; return r;}
	return PIVariantTypes::IODevice();
}


/** \brief Returns variant content as point
 * \details In case of PIPointd type returns point value. \n
 * In case of other types returns empty PIPointd. */
PIPointd PIVariant::toPoint() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivPoint) {PIPointd r; ba >> r; return r;}
	return PIPointd();
}


/** \brief Returns variant content as rect
 * \details In case of PIRectd type returns rect value. \n
 * In case of other types returns empty PIRectd. */
PIRectd PIVariant::toRect() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivRect) {PIRectd r; ba >> r; return r;}
	return PIRectd();
}


/** \brief Returns variant content as math vector
 * \details In case of PIMathVectord type returns rect value. \n
 * In case of other types returns empty PIMathVectord. */
PIMathVectord PIVariant::toMathVector() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivMathVector) {PIMathVectord r; ba >> r; return r;}
	return PIMathVectord();
}


/** \brief Returns variant content as math matrix
 * \details In case of PIMathMatrixd type returns rect value. \n
 * In case of other types returns empty PIMathMatrixd. */
PIMathMatrixd PIVariant::toMathMatrix() const {
	PIByteArray ba(_content);
	if (_type == PIVariant::pivMathMatrix) {PIMathMatrixd r; ba >> r; return r;}
	return PIMathMatrixd();
}

