Files
pip/pivariant.h

216 lines
7.5 KiB
C++

/*! \file pivariant.h
* \brief Variant type
*
* This file declares PIVariant
*/
/*
PIP - Platform Independent Primitives
Variant type
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIVARIANT_H
#define PIVARIANT_H
#include "pistring.h"
#include "pibitarray.h"
#include "pitime.h"
#include "pimath.h"
#define __PIVARIANT_UNION_SIZE__ 32
#define _vcomplexd (*((complexd*)_vraw))
#define _vcomplexld (*((complexld*)_vraw))
#define _vtime (*((PITime*)_vraw))
#define _vdate (*((PIDate*)_vraw))
#define _vdatetime (*((PIDateTime*)_vraw))
#define _vsystime (*((PISystemTime*)_vraw))
#define _vvcomplexd(v) (*((complexd*)v._vraw))
#define _vvcomplexld(v) (*((complexld*)v._vraw))
#define _vvtime(v) (*((PITime*)v._vraw))
#define _vvdate(v) (*((PIDate*)v._vraw))
#define _vvdatetime(v) (*((PIDateTime*)v._vraw))
#define _vvsystime(v) (*((PISystemTime*)v._vraw))
class PIP_EXPORT PIVariant {
public:
enum Type {
Invalid = 0,
Bool,
Char,
UChar,
Short,
UShort,
Int,
UInt,
Long,
ULong,
LLong,
ULLong,
Float,
Double,
LDouble,
Complexd,
Complexld,
BitArray,
ByteArray,
String,
StringList,
Time,
Date,
DateTime,
SystemTime/*,
Custom = 0xFF*/
};
PIVariant();
PIVariant(const char * v) {setValue(PIString(v));}
PIVariant(const bool v) {setValue(v);}
PIVariant(const char v) {setValue(v);}
PIVariant(const uchar v) {setValue(v);}
PIVariant(const short v) {setValue(v);}
PIVariant(const ushort v) {setValue(v);}
PIVariant(const int & v) {setValue(v);}
PIVariant(const uint & v) {setValue(v);}
PIVariant(const long & v) {setValue(v);}
PIVariant(const ulong & v) {setValue(v);}
PIVariant(const llong & v) {setValue(v);}
PIVariant(const ullong & v) {setValue(v);}
PIVariant(const float & v) {setValue(v);}
PIVariant(const double & v) {setValue(v);}
PIVariant(const ldouble & v) {setValue(v);}
PIVariant(const complexd & v) {setValue(v);}
PIVariant(const complexld & v) {setValue(v);}
PIVariant(const PIBitArray & v) {setValue(v);}
PIVariant(const PIByteArray & v) {setValue(v);}
PIVariant(const PIString & v) {setValue(v);}
PIVariant(const PIStringList & v) {setValue(v);}
PIVariant(const PITime & v) {setValue(v);}
PIVariant(const PIDate & v) {setValue(v);}
PIVariant(const PIDateTime & v) {setValue(v);}
PIVariant(const PISystemTime & v) {setValue(v);}
void setValue(const char * v) {setValue(PIString(v));}
void setValue(const bool v) {type_ = PIVariant::Bool; _vint = (v ? 1 : 0);}
void setValue(const char v) {type_ = PIVariant::Char; _vint = v;}
void setValue(const uchar v) {type_ = PIVariant::UChar; _vint = v;}
void setValue(const short v) {type_ = PIVariant::Short; _vint = v;}
void setValue(const ushort v) {type_ = PIVariant::UShort; _vint = v;}
void setValue(const int & v) {type_ = PIVariant::Int; _vint = v;}
void setValue(const uint & v) {type_ = PIVariant::UInt; _vint = v;}
void setValue(const long & v) {type_ = PIVariant::Long; _vint = v;}
void setValue(const ulong & v) {type_ = PIVariant::ULong; _vint = v;}
void setValue(const llong & v) {type_ = PIVariant::LLong; _vllong = v;}
void setValue(const ullong & v) {type_ = PIVariant::ULLong; _vllong = v;}
void setValue(const float & v) {type_ = PIVariant::Float; _vfloat = v;}
void setValue(const double & v) {type_ = PIVariant::Double; _vdouble = v;}
void setValue(const ldouble & v) {type_ = PIVariant::LDouble; _vldouble = v;}
void setValue(const complexd & v) {type_ = PIVariant::Complexd; _vcomplexd = v;}
void setValue(const complexld & v) {type_ = PIVariant::Complexld; _vcomplexld = v;}
void setValue(const PIBitArray & v) {type_ = PIVariant::BitArray; _vbitarray = v;}
void setValue(const PIByteArray & v) {type_ = PIVariant::ByteArray; _vbytearray = v;}
void setValue(const PIString & v) {type_ = PIVariant::String; _vstring = v;}
void setValue(const PIStringList & v) {type_ = PIVariant::StringList; _vstringlist = v;}
void setValue(const PITime & v) {type_ = PIVariant::Time; _vtime = v;}
void setValue(const PIDate & v) {type_ = PIVariant::Date; _vdate = v;}
void setValue(const PIDateTime & v) {type_ = PIVariant::DateTime; _vdatetime = v;}
void setValue(const PISystemTime & v) {type_ = PIVariant::SystemTime; _vsystime = v;}
bool toBool() const;
int toInt() const;
llong toLLong() const;
float toFloat() const;
double toDouble() const;
ldouble toLDouble() const;
complexd toComplexd() const;
complexld toComplexld() const;
PITime toTime() const;
PIDate toDate() const;
PIDateTime toDateTime() const;
PISystemTime toSystemTime() const;
PIString toString() const;
PIStringList toStringList() const;
PIBitArray toBitArray() const;
PIByteArray toByteArray() const;
void operator =(const PIVariant & v);
void operator =(const char * v) {setValue(PIString(v));}
void operator =(const bool v) {setValue(v);}
void operator =(const char v) {setValue(v);}
void operator =(const uchar v) {setValue(v);}
void operator =(const short v) {setValue(v);}
void operator =(const ushort v) {setValue(v);}
void operator =(const int & v) {setValue(v);}
void operator =(const uint & v) {setValue(v);}
void operator =(const long & v) {setValue(v);}
void operator =(const ulong & v) {setValue(v);}
void operator =(const llong & v) {setValue(v);}
void operator =(const ullong & v) {setValue(v);}
void operator =(const float & v) {setValue(v);}
void operator =(const double & v) {setValue(v);}
void operator =(const ldouble & v) {setValue(v);}
void operator =(const complexd & v) {setValue(v);}
void operator =(const complexld & v) {setValue(v);}
void operator =(const PIBitArray & v) {setValue(v);}
void operator =(const PIByteArray & v) {setValue(v);}
void operator =(const PIString & v) {setValue(v);}
void operator =(const PIStringList & v) {setValue(v);}
void operator =(const PITime & v) {setValue(v);}
void operator =(const PIDate & v) {setValue(v);}
void operator =(const PIDateTime & v) {setValue(v);}
void operator =(const PISystemTime & v) {setValue(v);}
bool operator ==(const PIVariant & v) const;
bool operator !=(const PIVariant & v) const {return !(*this == v);}
PIVariant::Type type() const {return type_;}
PIString typeName() const {return typeName(type_);}
bool isValid() const {return type_ != PIVariant::Invalid;}
static PIVariant::Type typeFromName(const PIString & tname);
static PIString typeName(PIVariant::Type type);
private:
union {
int _vint;
llong _vllong;
float _vfloat;
double _vdouble;
ldouble _vldouble;
uchar _vraw[__PIVARIANT_UNION_SIZE__];
/*complexd _vcomplexd;
complexld _vcomplexld;
PITime _vtime;
PIDate _vdate;
PIDateTime _vdatetime;
PISystemTime _vsystime;*/
};
PIBitArray _vbitarray;
PIByteArray _vbytearray;
PIString _vstring;
PIStringList _vstringlist;
PIVariant::Type type_;
};
inline PICout operator <<(PICout s, const PIVariant & v) {s.space(); s.setControl(0, true); s << "PIVariant(" << PIVariant::typeName(v.type()) << ", " << v.toString() << ")"; s.restoreControl(); return s;}
#endif // PIVARIANT_H