git-svn-id: svn://db.shs.com.ru/pip@400 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
137
src_main/_unsused/pigeometry.h
Executable file
137
src_main/_unsused/pigeometry.h
Executable file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Geometry
|
||||
Copyright (C) 2015 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 PIGEOMETRY_H
|
||||
#define PIGEOMETRY_H
|
||||
|
||||
#include "pimath.h"
|
||||
|
||||
template<typename Type>
|
||||
class PIP_EXPORT PIPoint {
|
||||
public:
|
||||
Type x;
|
||||
Type y;
|
||||
|
||||
PIPoint() {x = y = 0;};
|
||||
PIPoint(Type x_, Type y_) {set(x_, y_);}
|
||||
|
||||
PIPoint<Type> & set(Type x_, Type y_) {x = x_; y = y_; return *this;}
|
||||
PIPoint<Type> & move(Type x_, Type y_) {x += x_; y += y_; return *this;}
|
||||
PIPoint<Type> & move(const PIPoint<Type> & p) {x += p.x; y += p.y; return *this;}
|
||||
double angleRad() const {return atan2(y, x);}
|
||||
int angleDeg() const {return round(atan2(y, x) * 180. / M_PI);}
|
||||
PIPoint<Type> toPolar(bool isDeg = false) const {return PIPoint<Type>(sqrt(x*x + y*y), isDeg ? angleDeg() : angleRad());}
|
||||
static PIPoint<Type> fromPolar(const PIPoint<Type> & p) {return PIPoint<Type>(p.y * cos(p.x), p.y * sin(p.x));}
|
||||
|
||||
PIPoint<Type> operator +(const PIPoint<Type> & p) {return PIPoint<Type>(x + p.x, y + p.y);}
|
||||
PIPoint<Type> operator +(const Type & p) {return PIPoint<Type>(x + p, y + p);}
|
||||
PIPoint<Type> operator -(const PIPoint<Type> & p) {return PIPoint<Type>(x - p.x, y - p.y);}
|
||||
PIPoint<Type> operator -(const Type & p) {return PIPoint<Type>(x - p, y - p);}
|
||||
PIPoint<Type> operator -() {return PIPoint<Type>(-x, -y);}
|
||||
PIPoint<Type> operator *(const Type & d) {return PIPoint<Type>(x * d, y * d);}
|
||||
PIPoint<Type> operator /(const Type & d) {return PIPoint<Type>(x / d, y / d);}
|
||||
bool operator ==(const PIPoint<Type> & p) const {return (x == p.x && y == p.y);}
|
||||
bool operator !=(const PIPoint<Type> & p) const {return (x != p.x || y != p.y);}
|
||||
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
std::ostream & operator <<(std::ostream & s, const PIPoint<Type> & v) {s << '{' << v.x << ", " << v.y << '}'; return s;}
|
||||
|
||||
template<typename Type>
|
||||
class PIP_EXPORT PIRect {
|
||||
public:
|
||||
Type x0;
|
||||
Type y0;
|
||||
Type x1;
|
||||
Type y1;
|
||||
|
||||
PIRect() {x0 = y0 = x1 = y1 = 0;};
|
||||
PIRect(Type x, Type y, Type w, Type h) {set(x, y, w, h);}
|
||||
PIRect(const PIPoint<Type> & tl, const PIPoint<Type> & br) {set(tl.x, tl.y, br.x, br.y);}
|
||||
PIRect(const PIPoint<Type> & p0, const PIPoint<Type> & p1, const PIPoint<Type> & p2) {set(piMin<Type>(p0.x, p1.x, p2.x), piMin<Type>(p0.y, p1.y, p2.y),
|
||||
piMax<Type>(p0.x, p1.x, p2.x), piMax<Type>(p0.y, p1.y, p2.y));}
|
||||
|
||||
PIRect<Type> & set(Type x, Type y, Type w, Type h) {x0 = x; y0 = y; x1 = x + w; y1 = y + h; return *this;}
|
||||
bool pointIn(Type x, Type y) const {return (x <= x1 && x >= x0 && y <= y1 && y >= y0);}
|
||||
bool pointIn(const PIPoint<Type> & p) const {return pointIn(p.x, p.y);}
|
||||
bool isEmpty() const {return (x1 - x0 == 0 && y1 - y0 == 0);}
|
||||
PIRect<Type> & translate(Type x, Type y) {x0 += x; x1 += x; y0 += y; y1 += y; return *this;}
|
||||
PIRect<Type> & translate(const PIPoint<Type> & p) {x0 += p.x; x1 += p.x; y0 += p.y; y1 += p.y; return *this;}
|
||||
PIRect<Type> translated(Type x, Type y) {PIRect<Type> r(*this); r.translate(x, y); return r;}
|
||||
PIRect<Type> translated(const PIPoint<Type> & p) {PIRect<Type> r(*this); r.translate(p); return r;}
|
||||
PIRect<Type> & scale(Type x, Type y) {setWidth(width() * x); setHeight(height() * y); return *this;}
|
||||
PIRect<Type> & scale(const PIPoint<Type> & p) {setWidth(width() * p.x); setHeight(height() * p.y); return *this;}
|
||||
PIRect<Type> scaled(Type x, Type y) {PIRect<Type> r(*this); r.scale(x, y); return r;}
|
||||
PIRect<Type> scaled(const PIPoint<Type> & p) {PIRect<Type> r(*this); r.scale(p); return r;}
|
||||
PIRect<Type> & normalize() {if (x0 > x1) piSwap<Type>(x0, x1); if (y0 > y1) piSwap<Type>(y0, y1); return *this;}
|
||||
PIRect<Type> normalized() {PIRect<Type> r(*this); r.normalize(); return r;}
|
||||
PIRect<Type> & unite(const PIRect<Type> & r) {x0 = piMin<Type>(x0, r.x0); y0 = piMin<Type>(y0, r.y0); x1 = piMax<Type>(x1, r.x1); y1 = piMax<Type>(y1, r.y1); return *this;}
|
||||
PIRect<Type> united(const PIRect<Type> & rect) {PIRect<Type> r(*this); r.unite(rect); return r;}
|
||||
PIRect<Type> & intersect(const PIRect<Type> & r) {x0 = piMax<Type>(x0, r.x0); y0 = piMax<Type>(y0, r.y0); x1 = piMin<Type>(x1, r.x1); y1 = piMin<Type>(y1, r.y1); if (x0 > x1 || y0 > y1) x0 = x1 = y0 = y1 = Type(0); return *this;}
|
||||
PIRect<Type> intersected(const PIRect<Type> & rect) {PIRect<Type> r(*this); r.intersect(rect); return r;}
|
||||
Type top() const {return y0;}
|
||||
Type left() const {return x0;}
|
||||
Type right() const {return x1;}
|
||||
Type bottom() const {return y1;}
|
||||
Type width() const {return x1 - x0;}
|
||||
Type height() const {return y1 - y0;}
|
||||
PIPoint<Type> topLeft() {return PIPoint<Type>(x0, y0);}
|
||||
PIPoint<Type> topRigth() {return PIPoint<Type>(x1, y0);}
|
||||
PIPoint<Type> bottomLeft() {return PIPoint<Type>(x0, y1);}
|
||||
PIPoint<Type> bottomRight() {return PIPoint<Type>(x1, y1);}
|
||||
void setTop(Type v) {y0 = v;}
|
||||
void setLeft(Type v) {x0 = v;}
|
||||
void setRigth(Type v) {x1 = v;}
|
||||
void setBottom(Type v) {y1 = v;}
|
||||
void setWidth(Type v) {x1 = x0 + v;}
|
||||
void setHeight(Type v) {y1 = y0 + v;}
|
||||
|
||||
PIRect<Type> operator -() {return PIRect<Type>(-x0, -y0, -width(), -height());}
|
||||
void operator +=(Type x) {translate(x, x);}
|
||||
void operator +=(const PIPoint<Type> & p) {translate(p);}
|
||||
void operator -=(Type x) {translate(-x, -x);}
|
||||
void operator -=(const PIPoint<Type> & p) {translate(-p);}
|
||||
void operator *=(Type p) {x0 *= p; x1 *= p; y0 *= p; y1 *= p;}
|
||||
void operator /=(Type p) {x0 /= p; x1 /= p; y0 /= p; y1 /= p;}
|
||||
void operator |=(const PIRect<Type> & r) {unite(r);}
|
||||
void operator &=(const PIRect<Type> & r) {intersect(r);}
|
||||
PIRect<Type> operator +(const PIPoint<Type> & p) {return PIRect<Type>(*this).translated(p);}
|
||||
PIRect<Type> operator -(const PIPoint<Type> & p) {return PIRect<Type>(*this).translated(-p);}
|
||||
PIRect<Type> operator |(const PIRect<Type> & r) {return PIRect<Type>(*this).united(r);}
|
||||
PIRect<Type> operator &(const PIRect<Type> & r) {return PIRect<Type>(*this).intersected(r);}
|
||||
bool operator ==(const PIRect<Type> & r) const {return (x0 == r.x0 && y0 == r.y0 && x1 == r.x1 && y1 == r.y10);}
|
||||
bool operator !=(const PIRect<Type> & r) const {return (x0 != r.x0 || y0 != r.y0 || x1 != r.x1 || y1 != r.y10);}
|
||||
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
std::ostream & operator <<(std::ostream & s, const PIRect<Type> & v) {s << '{' << v.x0 << ", " << v.y0 << "; " << v.x1 - v.x0 << ", " << v.y1 - v.y0 << '}'; return s;}
|
||||
|
||||
typedef PIPoint<int> PIPointi;
|
||||
typedef PIPoint<uint> PIPointu;
|
||||
typedef PIPoint<float> PIPointf;
|
||||
typedef PIPoint<double> PIPointd;
|
||||
|
||||
typedef PIRect<int> PIRecti;
|
||||
typedef PIRect<uint> PIRectu;
|
||||
typedef PIRect<float> PIRectf;
|
||||
typedef PIRect<double> PIRectd;
|
||||
|
||||
#endif // PIGEOMETRY_H
|
||||
249
src_main/_unsused/pivariable.cpp
Executable file
249
src_main/_unsused/pivariable.cpp
Executable file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Variable, Struct (simple serialization)
|
||||
Copyright (C) 2013 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/>.
|
||||
*/
|
||||
|
||||
#include "pivariable.h"
|
||||
|
||||
|
||||
bool PIVariant::operator ==(const PIVariant & v) const {
|
||||
if (type != v.type) return false;
|
||||
switch (type) {
|
||||
case PIVariant::Bool: return vBool == v.vBool;
|
||||
case PIVariant::Char: return vChar == v.vChar;
|
||||
case PIVariant::Short: return vShort == v.vShort;
|
||||
case PIVariant::Int: return vInt == v.vInt;
|
||||
case PIVariant::Long: return vLong == v.vLong;
|
||||
case PIVariant::LLong: return vLLong == v.vLLong;
|
||||
case PIVariant::UChar: return vUChar == v.vUChar;
|
||||
case PIVariant::UShort: return vUShort == v.vUShort;
|
||||
case PIVariant::UInt: return vUInt == v.vUInt;
|
||||
case PIVariant::ULong: return vULong == v.vULong;
|
||||
case PIVariant::ULLong: return vULLong == v.vULLong;
|
||||
case PIVariant::Float: return vFloat == v.vFloat;
|
||||
case PIVariant::Double: return vDouble == v.vDouble;
|
||||
case PIVariant::LDouble: return vLDouble == v.vLDouble;
|
||||
case PIVariant::String: return vString == v.vString;
|
||||
case PIVariant::StringList: return vStringList == v.vStringList;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PIVariant::setValueOnly(const PIString & s) {
|
||||
switch (type) {
|
||||
case PIVariant::Bool: vBool = s.toBool(); break;
|
||||
case PIVariant::Char: vChar = s.toChar(); break;
|
||||
case PIVariant::Short: vShort = s.toShort(); break;
|
||||
case PIVariant::Int: vInt = s.toInt(); break;
|
||||
case PIVariant::Long: vLong = s.toLong(); break;
|
||||
case PIVariant::LLong: vLLong = s.toLLong(); break;
|
||||
case PIVariant::UChar: vUChar = s.toChar(); break;
|
||||
case PIVariant::UShort: vUShort = s.toShort(); break;
|
||||
case PIVariant::UInt: vUInt = s.toInt(); break;
|
||||
case PIVariant::ULong: vULong = s.toLong(); break;
|
||||
case PIVariant::ULLong: vULLong = s.toLLong(); break;
|
||||
case PIVariant::Float: vFloat = s.toFloat(); break;
|
||||
case PIVariant::Double: vDouble = s.toDouble(); break;
|
||||
case PIVariant::LDouble: vLDouble = s.toLDouble(); break;
|
||||
case PIVariant::String: vString = s; break;
|
||||
case PIVariant::StringList: vStringList = s.split("%|%"); break;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
PIString PIVariant::stringValue() const {
|
||||
switch (type) {
|
||||
case PIVariant::Bool: return (vBool ? "true" : "false");
|
||||
case PIVariant::Char: return PIString::fromNumber(vChar);
|
||||
case PIVariant::Short: return PIString::fromNumber(vShort);
|
||||
case PIVariant::Int: return PIString::fromNumber(vInt);
|
||||
case PIVariant::Long: return PIString::fromNumber(vLong);
|
||||
case PIVariant::LLong: return PIString::fromNumber(static_cast<long>(vLLong));
|
||||
case PIVariant::UChar: return PIString::fromNumber(vUChar);
|
||||
case PIVariant::UShort: return PIString::fromNumber(vUShort);
|
||||
case PIVariant::UInt: return PIString::fromNumber(vUInt);
|
||||
case PIVariant::ULong: return PIString::fromNumber(static_cast<long>(vULong));
|
||||
case PIVariant::ULLong: return PIString::fromNumber(static_cast<long>(vULLong));
|
||||
case PIVariant::Float: return PIString::fromNumber(vFloat);
|
||||
case PIVariant::Double: return PIString::fromNumber(vDouble);
|
||||
case PIVariant::LDouble: return PIString::fromNumber(vLDouble);
|
||||
case PIVariant::String: return vString;
|
||||
case PIVariant::StringList: return vStringList.join("%|%");
|
||||
};
|
||||
return vString;
|
||||
}
|
||||
|
||||
|
||||
PIVariant PIVariant::readFromString(const PIString & s) {
|
||||
int i = s.find(':');
|
||||
if (i < 0 || s.length() < 2) return PIVariant(s);
|
||||
PIVariant ret;
|
||||
ret.type = PIVariant::fromString(s.left(i));
|
||||
ret.setValueOnly(s.right(s.length() - i - 1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIVariant::Type PIVariant::fromString(const PIString & str) {
|
||||
PIString s = str.trimmed().toLowerCase().replace(" ", "");
|
||||
if (s == "bool") return PIVariant::Bool;
|
||||
if (s == "char" || s == "sbyte") return PIVariant::Char;
|
||||
if (s == "short" || s == "short int" || s == "signed short" || s == "signed short int" || s == "sword") return PIVariant::Short;
|
||||
if (s == "int" || s == "signed" || s == "signed int") return PIVariant::Int;
|
||||
if (s == "long" || s == "long int" || s == "signed long" || s == "signed long int" || s == "sdword") return PIVariant::Long;
|
||||
if (s == "llong" || s == "long long" || s == "long long int" || s == "signed long long" || s == "signed long long int" || s == "sqword") return PIVariant::LLong;
|
||||
if (s == "uchar" || s == "byte") return PIVariant::UChar;
|
||||
if (s == "ushort" || s == "unsigned short" || s == "unsigned short int" || s == "word") return PIVariant::UShort;
|
||||
if (s == "uint" || s == "unsigned" || s == "unsigned int") return PIVariant::UInt;
|
||||
if (s == "ulong" || s == "unsigned long" || s == "unsigned long int" || s == "dword") return PIVariant::ULong;
|
||||
if (s == "ullong" || s == "unsigned long long" || s == "unsigned long long int" || s == "qword") return PIVariant::ULLong;
|
||||
if (s == "float") return PIVariant::Float;
|
||||
if (s == "double" || s == "real") return PIVariant::Double;
|
||||
if (s == "ldouble" || s == "long double") return PIVariant::LDouble;
|
||||
if (s == "pistring" || s == "string") return PIVariant::String;
|
||||
if (s == "pistringlist" || s == "vector<string>" || s == "vector<pistring>" || s == "pivector<string>" || s == "pivector<pistring>") return PIVariant::StringList;
|
||||
return PIVariant::Double;
|
||||
}
|
||||
|
||||
|
||||
PIString PIVariant::toString(const PIVariant::Type & var) {
|
||||
switch (var) {
|
||||
case PIVariant::Bool: return "bool";
|
||||
case PIVariant::Char: return "char";
|
||||
case PIVariant::Short: return "short";
|
||||
case PIVariant::Int: return "int";
|
||||
case PIVariant::Long: return "long";
|
||||
case PIVariant::LLong: return "llong";
|
||||
case PIVariant::UChar: return "uchar";
|
||||
case PIVariant::UShort: return "ushort";
|
||||
case PIVariant::UInt: return "uint";
|
||||
case PIVariant::ULong: return "ulong";
|
||||
case PIVariant::ULLong: return "ullong";
|
||||
case PIVariant::Float: return "float";
|
||||
case PIVariant::Double: return "double";
|
||||
case PIVariant::LDouble: return "ldouble";
|
||||
case PIVariant::String: return "string";
|
||||
case PIVariant::StringList: return "stringlist";
|
||||
}
|
||||
return "double";
|
||||
}
|
||||
|
||||
|
||||
uint PIVariant::variableSize(const PIVariant::Type & var) {
|
||||
switch (var) {
|
||||
case PIVariant::Bool: return sizeof(bool);
|
||||
case PIVariant::Char: return sizeof(char);
|
||||
case PIVariant::Short: return sizeof(short);
|
||||
case PIVariant::Int: return sizeof(int);
|
||||
case PIVariant::Long: return sizeof(long);
|
||||
case PIVariant::LLong: return sizeof(llong);
|
||||
case PIVariant::UChar: return sizeof(uchar);
|
||||
case PIVariant::UShort: return sizeof(ushort);
|
||||
case PIVariant::UInt: return sizeof(uint);
|
||||
case PIVariant::ULong: return sizeof(ulong);
|
||||
case PIVariant::ULLong: return sizeof(ullong);
|
||||
case PIVariant::Float: return sizeof(float);
|
||||
case PIVariant::Double: return sizeof(double);
|
||||
case PIVariant::LDouble: return sizeof(ldouble);
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
double PIVariant::variableValue(const void * var_ptr, const PIVariant::Type & var) {
|
||||
switch (var) {
|
||||
case PIVariant::Bool: return (double)(*((bool * )var_ptr));
|
||||
case PIVariant::Char: return (double)(*((char * )var_ptr));
|
||||
case PIVariant::Short: return (double)(*((short * )var_ptr));
|
||||
case PIVariant::Int: return (double)(*((int * )var_ptr));
|
||||
case PIVariant::Long: return (double)(*((long * )var_ptr));
|
||||
case PIVariant::LLong: return (double)(*((llong * )var_ptr));
|
||||
case PIVariant::UChar: return (double)(*((uchar * )var_ptr));
|
||||
case PIVariant::UShort: return (double)(*((ushort * )var_ptr));
|
||||
case PIVariant::UInt: return (double)(*((uint * )var_ptr));
|
||||
case PIVariant::ULong: return (double)(*((ulong * )var_ptr));
|
||||
case PIVariant::ULLong: return (double)(*((ullong * )var_ptr));
|
||||
case PIVariant::Float: return (double)(*((float * )var_ptr));
|
||||
case PIVariant::Double: return (double)(*((double * )var_ptr));
|
||||
case PIVariant::LDouble: return (ldouble)(*((ldouble * )var_ptr));
|
||||
default: break;
|
||||
}
|
||||
return 0.;
|
||||
}
|
||||
|
||||
|
||||
void PIVariable::setVariable(const PIString & str) {
|
||||
type_ = PIVariant::fromString(str);
|
||||
size_ = PIVariant::variableSize(type_);
|
||||
}
|
||||
|
||||
|
||||
void PIVariable::writeVariable(void * dest) {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool: *((bool * )((ullong)dest + offset)) = value_ > 0.; return;
|
||||
case PIVariant::Char: *((char * )((ullong)dest + offset)) = char(value_); return;
|
||||
case PIVariant::Short: *((short * )((ullong)dest + offset)) = short(value_); return;
|
||||
case PIVariant::Int: *((int * )((ullong)dest + offset)) = int(value_); return;
|
||||
case PIVariant::Long: *((long * )((ullong)dest + offset)) = long(value_); return;
|
||||
case PIVariant::LLong: *((llong * )((ullong)dest + offset)) = llong(value_); return;
|
||||
case PIVariant::UChar: *((uchar * )((ullong)dest + offset)) = uchar(value_); return;
|
||||
case PIVariant::UShort: *((ushort * )((ullong)dest + offset)) = ushort(value_); return;
|
||||
case PIVariant::UInt: *((uint * )((ullong)dest + offset)) = uint(value_); return;
|
||||
case PIVariant::ULong: *((ulong * )((ullong)dest + offset)) = ulong(value_); return;
|
||||
case PIVariant::ULLong: *((ullong * )((ullong)dest + offset)) = ullong(value_); return;
|
||||
case PIVariant::Float: *((float * )((ullong)dest + offset)) = float(value_); return;
|
||||
case PIVariant::Double: *((double * )((ullong)dest + offset)) = value_; return;
|
||||
case PIVariant::LDouble: *((ldouble * )((ullong)dest + offset)) = ldouble(value_); return;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIStruct::parseFile(const PIString & file) {
|
||||
PIConfig conf(file, PIIODevice::ReadOnly);
|
||||
PIVariable var;
|
||||
PIString ts;
|
||||
uint sz = 0;
|
||||
vars.clear();
|
||||
for (int i = 0; i < conf.entriesCount(); ++i) {
|
||||
var.setVariable(conf.getValue(i));
|
||||
var.setName(conf.getName(i));
|
||||
var.offset = sz;
|
||||
sz += var.size();
|
||||
ts = conf.getComment(i);
|
||||
if (ts.length() > 0)
|
||||
var.setValue(ts.toDouble());
|
||||
else var.setValue(0.);
|
||||
vars.push_back(var);
|
||||
}
|
||||
size_ = sz;
|
||||
}
|
||||
|
||||
|
||||
void PIStruct::readData(const void * data) {
|
||||
for (uint i = 0; i < vars.size(); ++i)
|
||||
vars[i].readVariable(data);
|
||||
}
|
||||
|
||||
|
||||
void PIStruct::writeData(void * data) {
|
||||
for (uint i = 0; i < vars.size(); ++i)
|
||||
vars[i].writeVariable(data);
|
||||
}
|
||||
|
||||
249
src_main/_unsused/pivariable.cpp_
Normal file
249
src_main/_unsused/pivariable.cpp_
Normal file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Variable, Struct (simple serialization)
|
||||
Copyright (C) 2013 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/>.
|
||||
*/
|
||||
|
||||
#include "pivariable.h"
|
||||
|
||||
|
||||
bool PIVariant::operator ==(const PIVariant & v) const {
|
||||
if (type != v.type) return false;
|
||||
switch (type) {
|
||||
case PIVariant::Bool: return vBool == v.vBool;
|
||||
case PIVariant::Char: return vChar == v.vChar;
|
||||
case PIVariant::Short: return vShort == v.vShort;
|
||||
case PIVariant::Int: return vInt == v.vInt;
|
||||
case PIVariant::Long: return vLong == v.vLong;
|
||||
case PIVariant::LLong: return vLLong == v.vLLong;
|
||||
case PIVariant::UChar: return vUChar == v.vUChar;
|
||||
case PIVariant::UShort: return vUShort == v.vUShort;
|
||||
case PIVariant::UInt: return vUInt == v.vUInt;
|
||||
case PIVariant::ULong: return vULong == v.vULong;
|
||||
case PIVariant::ULLong: return vULLong == v.vULLong;
|
||||
case PIVariant::Float: return vFloat == v.vFloat;
|
||||
case PIVariant::Double: return vDouble == v.vDouble;
|
||||
case PIVariant::LDouble: return vLDouble == v.vLDouble;
|
||||
case PIVariant::String: return vString == v.vString;
|
||||
case PIVariant::StringList: return vStringList == v.vStringList;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void PIVariant::setValueOnly(const PIString & s) {
|
||||
switch (type) {
|
||||
case PIVariant::Bool: vBool = s.toBool(); break;
|
||||
case PIVariant::Char: vChar = s.toChar(); break;
|
||||
case PIVariant::Short: vShort = s.toShort(); break;
|
||||
case PIVariant::Int: vInt = s.toInt(); break;
|
||||
case PIVariant::Long: vLong = s.toLong(); break;
|
||||
case PIVariant::LLong: vLLong = s.toLLong(); break;
|
||||
case PIVariant::UChar: vUChar = s.toChar(); break;
|
||||
case PIVariant::UShort: vUShort = s.toShort(); break;
|
||||
case PIVariant::UInt: vUInt = s.toInt(); break;
|
||||
case PIVariant::ULong: vULong = s.toLong(); break;
|
||||
case PIVariant::ULLong: vULLong = s.toLLong(); break;
|
||||
case PIVariant::Float: vFloat = s.toFloat(); break;
|
||||
case PIVariant::Double: vDouble = s.toDouble(); break;
|
||||
case PIVariant::LDouble: vLDouble = s.toLDouble(); break;
|
||||
case PIVariant::String: vString = s; break;
|
||||
case PIVariant::StringList: vStringList = s.split("%|%"); break;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
PIString PIVariant::stringValue() const {
|
||||
switch (type) {
|
||||
case PIVariant::Bool: return (vBool ? "true" : "false");
|
||||
case PIVariant::Char: return PIString::fromNumber(vChar);
|
||||
case PIVariant::Short: return PIString::fromNumber(vShort);
|
||||
case PIVariant::Int: return PIString::fromNumber(vInt);
|
||||
case PIVariant::Long: return PIString::fromNumber(vLong);
|
||||
case PIVariant::LLong: return PIString::fromNumber(static_cast<long>(vLLong));
|
||||
case PIVariant::UChar: return PIString::fromNumber(vUChar);
|
||||
case PIVariant::UShort: return PIString::fromNumber(vUShort);
|
||||
case PIVariant::UInt: return PIString::fromNumber(vUInt);
|
||||
case PIVariant::ULong: return PIString::fromNumber(static_cast<long>(vULong));
|
||||
case PIVariant::ULLong: return PIString::fromNumber(static_cast<long>(vULLong));
|
||||
case PIVariant::Float: return PIString::fromNumber(vFloat);
|
||||
case PIVariant::Double: return PIString::fromNumber(vDouble);
|
||||
case PIVariant::LDouble: return PIString::fromNumber(vLDouble);
|
||||
case PIVariant::String: return vString;
|
||||
case PIVariant::StringList: return vStringList.join("%|%");
|
||||
};
|
||||
return vString;
|
||||
}
|
||||
|
||||
|
||||
PIVariant PIVariant::readFromString(const PIString & s) {
|
||||
int i = s.find(':');
|
||||
if (i < 0 || s.length() < 2) return PIVariant(s);
|
||||
PIVariant ret;
|
||||
ret.type = PIVariant::fromString(s.left(i));
|
||||
ret.setValueOnly(s.right(s.length() - i - 1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIVariant::Type PIVariant::fromString(const PIString & str) {
|
||||
PIString s = str.trimmed().toLowerCase().replace(" ", "");
|
||||
if (s == "bool") return PIVariant::Bool;
|
||||
if (s == "char" || s == "sbyte") return PIVariant::Char;
|
||||
if (s == "short" || s == "short int" || s == "signed short" || s == "signed short int" || s == "sword") return PIVariant::Short;
|
||||
if (s == "int" || s == "signed" || s == "signed int") return PIVariant::Int;
|
||||
if (s == "long" || s == "long int" || s == "signed long" || s == "signed long int" || s == "sdword") return PIVariant::Long;
|
||||
if (s == "llong" || s == "long long" || s == "long long int" || s == "signed long long" || s == "signed long long int" || s == "sqword") return PIVariant::LLong;
|
||||
if (s == "uchar" || s == "byte") return PIVariant::UChar;
|
||||
if (s == "ushort" || s == "unsigned short" || s == "unsigned short int" || s == "word") return PIVariant::UShort;
|
||||
if (s == "uint" || s == "unsigned" || s == "unsigned int") return PIVariant::UInt;
|
||||
if (s == "ulong" || s == "unsigned long" || s == "unsigned long int" || s == "dword") return PIVariant::ULong;
|
||||
if (s == "ullong" || s == "unsigned long long" || s == "unsigned long long int" || s == "qword") return PIVariant::ULLong;
|
||||
if (s == "float") return PIVariant::Float;
|
||||
if (s == "double" || s == "real") return PIVariant::Double;
|
||||
if (s == "ldouble" || s == "long double") return PIVariant::LDouble;
|
||||
if (s == "pistring" || s == "string") return PIVariant::String;
|
||||
if (s == "pistringlist" || s == "vector<string>" || s == "vector<pistring>" || s == "pivector<string>" || s == "pivector<pistring>") return PIVariant::StringList;
|
||||
return PIVariant::Double;
|
||||
}
|
||||
|
||||
|
||||
PIString PIVariant::toString(const PIVariant::Type & var) {
|
||||
switch (var) {
|
||||
case PIVariant::Bool: return "bool";
|
||||
case PIVariant::Char: return "char";
|
||||
case PIVariant::Short: return "short";
|
||||
case PIVariant::Int: return "int";
|
||||
case PIVariant::Long: return "long";
|
||||
case PIVariant::LLong: return "llong";
|
||||
case PIVariant::UChar: return "uchar";
|
||||
case PIVariant::UShort: return "ushort";
|
||||
case PIVariant::UInt: return "uint";
|
||||
case PIVariant::ULong: return "ulong";
|
||||
case PIVariant::ULLong: return "ullong";
|
||||
case PIVariant::Float: return "float";
|
||||
case PIVariant::Double: return "double";
|
||||
case PIVariant::LDouble: return "ldouble";
|
||||
case PIVariant::String: return "string";
|
||||
case PIVariant::StringList: return "stringlist";
|
||||
}
|
||||
return "double";
|
||||
}
|
||||
|
||||
|
||||
uint PIVariant::variableSize(const PIVariant::Type & var) {
|
||||
switch (var) {
|
||||
case PIVariant::Bool: return sizeof(bool);
|
||||
case PIVariant::Char: return sizeof(char);
|
||||
case PIVariant::Short: return sizeof(short);
|
||||
case PIVariant::Int: return sizeof(int);
|
||||
case PIVariant::Long: return sizeof(long);
|
||||
case PIVariant::LLong: return sizeof(llong);
|
||||
case PIVariant::UChar: return sizeof(uchar);
|
||||
case PIVariant::UShort: return sizeof(ushort);
|
||||
case PIVariant::UInt: return sizeof(uint);
|
||||
case PIVariant::ULong: return sizeof(ulong);
|
||||
case PIVariant::ULLong: return sizeof(ullong);
|
||||
case PIVariant::Float: return sizeof(float);
|
||||
case PIVariant::Double: return sizeof(double);
|
||||
case PIVariant::LDouble: return sizeof(ldouble);
|
||||
default: break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
double PIVariant::variableValue(const void * var_ptr, const PIVariant::Type & var) {
|
||||
switch (var) {
|
||||
case PIVariant::Bool: return (double)(*((bool * )var_ptr));
|
||||
case PIVariant::Char: return (double)(*((char * )var_ptr));
|
||||
case PIVariant::Short: return (double)(*((short * )var_ptr));
|
||||
case PIVariant::Int: return (double)(*((int * )var_ptr));
|
||||
case PIVariant::Long: return (double)(*((long * )var_ptr));
|
||||
case PIVariant::LLong: return (double)(*((llong * )var_ptr));
|
||||
case PIVariant::UChar: return (double)(*((uchar * )var_ptr));
|
||||
case PIVariant::UShort: return (double)(*((ushort * )var_ptr));
|
||||
case PIVariant::UInt: return (double)(*((uint * )var_ptr));
|
||||
case PIVariant::ULong: return (double)(*((ulong * )var_ptr));
|
||||
case PIVariant::ULLong: return (double)(*((ullong * )var_ptr));
|
||||
case PIVariant::Float: return (double)(*((float * )var_ptr));
|
||||
case PIVariant::Double: return (double)(*((double * )var_ptr));
|
||||
case PIVariant::LDouble: return (ldouble)(*((ldouble * )var_ptr));
|
||||
default: break;
|
||||
}
|
||||
return 0.;
|
||||
}
|
||||
|
||||
|
||||
void PIVariable::setVariable(const PIString & str) {
|
||||
type_ = PIVariant::fromString(str);
|
||||
size_ = PIVariant::variableSize(type_);
|
||||
}
|
||||
|
||||
|
||||
void PIVariable::writeVariable(void * dest) {
|
||||
switch (type_) {
|
||||
case PIVariant::Bool: *((bool * )((ullong)dest + offset)) = value_ > 0.; return;
|
||||
case PIVariant::Char: *((char * )((ullong)dest + offset)) = char(value_); return;
|
||||
case PIVariant::Short: *((short * )((ullong)dest + offset)) = short(value_); return;
|
||||
case PIVariant::Int: *((int * )((ullong)dest + offset)) = int(value_); return;
|
||||
case PIVariant::Long: *((long * )((ullong)dest + offset)) = long(value_); return;
|
||||
case PIVariant::LLong: *((llong * )((ullong)dest + offset)) = llong(value_); return;
|
||||
case PIVariant::UChar: *((uchar * )((ullong)dest + offset)) = uchar(value_); return;
|
||||
case PIVariant::UShort: *((ushort * )((ullong)dest + offset)) = ushort(value_); return;
|
||||
case PIVariant::UInt: *((uint * )((ullong)dest + offset)) = uint(value_); return;
|
||||
case PIVariant::ULong: *((ulong * )((ullong)dest + offset)) = ulong(value_); return;
|
||||
case PIVariant::ULLong: *((ullong * )((ullong)dest + offset)) = ullong(value_); return;
|
||||
case PIVariant::Float: *((float * )((ullong)dest + offset)) = float(value_); return;
|
||||
case PIVariant::Double: *((double * )((ullong)dest + offset)) = value_; return;
|
||||
case PIVariant::LDouble: *((ldouble * )((ullong)dest + offset)) = ldouble(value_); return;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PIStruct::parseFile(const PIString & file) {
|
||||
PIConfig conf(file, PIIODevice::ReadOnly);
|
||||
PIVariable var;
|
||||
PIString ts;
|
||||
uint sz = 0;
|
||||
vars.clear();
|
||||
for (int i = 0; i < conf.entriesCount(); ++i) {
|
||||
var.setVariable(conf.getValue(i));
|
||||
var.setName(conf.getName(i));
|
||||
var.offset = sz;
|
||||
sz += var.size();
|
||||
ts = conf.getComment(i);
|
||||
if (ts.length() > 0)
|
||||
var.setValue(ts.toDouble());
|
||||
else var.setValue(0.);
|
||||
vars.push_back(var);
|
||||
}
|
||||
size_ = sz;
|
||||
}
|
||||
|
||||
|
||||
void PIStruct::readData(const void * data) {
|
||||
for (uint i = 0; i < vars.size(); ++i)
|
||||
vars[i].readVariable(data);
|
||||
}
|
||||
|
||||
|
||||
void PIStruct::writeData(void * data) {
|
||||
for (uint i = 0; i < vars.size(); ++i)
|
||||
vars[i].writeVariable(data);
|
||||
}
|
||||
|
||||
196
src_main/_unsused/pivariable.h
Executable file
196
src_main/_unsused/pivariable.h
Executable file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Variable, Struct (simple serialization)
|
||||
Copyright (C) 2013 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 PIVARIABLE_H
|
||||
#define PIVARIABLE_H
|
||||
|
||||
#include "piconfig.h"
|
||||
|
||||
class PIP_EXPORT PIVariant {
|
||||
friend class PIVariable;
|
||||
public:
|
||||
enum Type {Bool, Char, Short, Int, Long, LLong, UChar, UShort, UInt, ULong, ULLong, Float, Double, LDouble, String, StringList};
|
||||
|
||||
PIVariant() {setValue(0.);}
|
||||
PIVariant(const char * v) {setValue(v);}
|
||||
PIVariant(const bool & v) {setValue(v);}
|
||||
PIVariant(const char & v) {setValue(v);}
|
||||
PIVariant(const short & v) {setValue(v);}
|
||||
PIVariant(const int & v) {setValue(v);}
|
||||
PIVariant(const long & v) {setValue(v);}
|
||||
PIVariant(const llong & v) {setValue(v);}
|
||||
PIVariant(const uchar & v) {setValue(v);}
|
||||
PIVariant(const ushort & v) {setValue(v);}
|
||||
PIVariant(const uint & v) {setValue(v);}
|
||||
PIVariant(const ulong & 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 PIString & v) {setValue(v);}
|
||||
PIVariant(const PIStringList & v) {setValue(v);}
|
||||
|
||||
void setValue(const char * v) {setValue(PIString(v));}
|
||||
void setValue(const bool & v) {type = PIVariant::Bool; vBool = v;}
|
||||
void setValue(const char & v) {type = PIVariant::Char; vChar = v;}
|
||||
void setValue(const short & v) {type = PIVariant::Short; vShort = v;}
|
||||
void setValue(const int & v) {type = PIVariant::Int; vInt = v;}
|
||||
void setValue(const long & v) {type = PIVariant::Long; vLong = v;}
|
||||
void setValue(const llong & v) {type = PIVariant::LLong; vLLong = v;}
|
||||
void setValue(const uchar & v) {type = PIVariant::UChar; vUChar = v;}
|
||||
void setValue(const ushort & v) {type = PIVariant::UShort; vUShort = v;}
|
||||
void setValue(const uint & v) {type = PIVariant::UInt; vUInt = v;}
|
||||
void setValue(const ulong & v) {type = PIVariant::ULong; vULong = v;}
|
||||
void setValue(const ullong & v) {type = PIVariant::ULLong; vULLong = 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 PIString & v) {type = PIVariant::String; vString = v;}
|
||||
void setValue(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;}
|
||||
void setValueOnly(const PIString & v);
|
||||
PIString typeName() const {return PIVariant::toString(type);}
|
||||
double doubleValue() const {return PIVariant::variableValue(&vChar, type);}
|
||||
PIString stringValue() const;
|
||||
void typeFromString(const PIString & str) {type = PIVariant::fromString(str);}
|
||||
PIString typeToString() const {return PIVariant::toString(type);}
|
||||
uint size() {if (type != PIVariant::String && type != PIVariant::StringList) return PIVariant::variableSize(type); if (type == PIVariant::String) return vString.size(); else return vStringList.contentSize();}
|
||||
PIString writeToString() const {return typeName() + ":" + stringValue();}
|
||||
|
||||
#ifdef QNX
|
||||
void operator =(const PIVariant & v) {type = v.type; vLDouble = v.vLDouble; vString = v.vString; vStringList = v.vStringList;}
|
||||
#endif
|
||||
void operator =(const char * v) {setValue(PIString(v));}
|
||||
void operator =(const bool & v) {type = PIVariant::Bool; vBool = v;}
|
||||
void operator =(const char & v) {type = PIVariant::Char; vChar = v;}
|
||||
void operator =(const short & v) {type = PIVariant::Short; vShort = v;}
|
||||
void operator =(const int & v) {type = PIVariant::Int; vInt = v;}
|
||||
void operator =(const long & v) {type = PIVariant::Long; vLong = v;}
|
||||
void operator =(const llong & v) {type = PIVariant::LLong; vLLong = v;}
|
||||
void operator =(const uchar & v) {type = PIVariant::UChar; vUChar = v;}
|
||||
void operator =(const ushort & v) {type = PIVariant::UShort; vUShort = v;}
|
||||
void operator =(const uint & v) {type = PIVariant::UInt; vUInt = v;}
|
||||
void operator =(const ulong & v) {type = PIVariant::ULong; vULong = v;}
|
||||
void operator =(const ullong & v) {type = PIVariant::ULLong; vULLong = v;}
|
||||
void operator =(const float & v) {type = PIVariant::Float; vFloat = v;}
|
||||
void operator =(const double & v) {type = PIVariant::Double; vDouble = v;}
|
||||
void operator =(const ldouble & v) {type = PIVariant::LDouble; vLDouble = v;}
|
||||
void operator =(const PIString & v) {type = PIVariant::String; vString = v;}
|
||||
void operator =(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;}
|
||||
|
||||
bool operator ==(const PIVariant & v) const;
|
||||
bool operator !=(const PIVariant & v) const {return !(*this == v);}
|
||||
|
||||
PIVariant::Type type;
|
||||
union {
|
||||
bool vBool;
|
||||
char vChar;
|
||||
short vShort;
|
||||
int vInt;
|
||||
long vLong;
|
||||
llong vLLong;
|
||||
uchar vUChar;
|
||||
ushort vUShort;
|
||||
uint vUInt;
|
||||
ulong vULong;
|
||||
ullong vULLong;
|
||||
float vFloat;
|
||||
double vDouble;
|
||||
ldouble vLDouble;
|
||||
};
|
||||
PIString vString;
|
||||
PIStringList vStringList;
|
||||
|
||||
static PIVariant readFromString(const PIString & s);
|
||||
|
||||
private:
|
||||
static PIVariant::Type fromString(const PIString & str);
|
||||
static PIString toString(const PIVariant::Type & var);
|
||||
static uint variableSize(const PIVariant::Type & var);
|
||||
static double variableValue(const void * var_ptr, const PIVariant::Type & var);
|
||||
|
||||
};
|
||||
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIVariant & v) {s << v.typeName() << ": " << v.stringValue(); return s;}
|
||||
|
||||
class PIP_EXPORT PIVariable {
|
||||
public:
|
||||
PIVariable() {;}
|
||||
PIVariable(const PIString & str) {setVariable(str);}
|
||||
~PIVariable() {;}
|
||||
|
||||
void setVariable(const PIString & str);
|
||||
void writeVariable(void * dest);
|
||||
void readVariable(const void * var_ptr) {value_ = PIVariant::variableValue((char * )((long)var_ptr + offset), type_);}
|
||||
PIVariant::Type type() const {return type_;}
|
||||
uint size() const {return size_;}
|
||||
const PIString & name() {return name_;}
|
||||
void setName(const PIString & str) {name_ = str;}
|
||||
double value() const {return value_;}
|
||||
void setValue(const double & val) {value_ = val;}
|
||||
|
||||
int offset;
|
||||
|
||||
private:
|
||||
PIVariant::Type type_;
|
||||
uint size_;
|
||||
PIString name_;
|
||||
double value_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* PIStruct is abstract structure, described by *.conf file with format of each line:
|
||||
* "<name> = <type> #<n|f|b> <start_value>".
|
||||
* e.g. "pi = double #f 3.1418"
|
||||
*
|
||||
* You can write or read binary content of this struct
|
||||
* by functions "writeData" and "readData", e.g.
|
||||
* "char * data = new char[struct.size()];
|
||||
* struct.writeData(data);"
|
||||
*
|
||||
* Access to each variable in struct is looks like
|
||||
* "double value = struct["pi"].value();"
|
||||
*/
|
||||
|
||||
class PIP_EXPORT PIStruct {
|
||||
public:
|
||||
PIStruct() {;}
|
||||
PIStruct(const PIString & str) {parseFile(str);}
|
||||
|
||||
void parseFile(const PIString & file);
|
||||
void readData(const void * data);
|
||||
void writeData(void * data);
|
||||
void clear() {vars.clear(); size_ = 0;}
|
||||
uint count() const {return vars.size();}
|
||||
uint size() const {return size_;}
|
||||
const PIString & name() {return name_;}
|
||||
void setName(const PIString & str) {name_ = str;}
|
||||
PIVariable & operator[](const uint & index) {return vars[index];}
|
||||
PIVariable & operator[](const PIString & name) {for (uint i = 0; i < vars.size(); ++i) if (vars[i].name() == name) return vars[i]; return def;}
|
||||
|
||||
private:
|
||||
uint size_;
|
||||
PIString name_;
|
||||
PIVariable def;
|
||||
PIVector<PIVariable> vars;
|
||||
|
||||
};
|
||||
|
||||
#endif // PIVARIABLE_H
|
||||
196
src_main/_unsused/pivariable.h_
Normal file
196
src_main/_unsused/pivariable.h_
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Variable, Struct (simple serialization)
|
||||
Copyright (C) 2013 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 PIVARIABLE_H
|
||||
#define PIVARIABLE_H
|
||||
|
||||
#include "piconfig.h"
|
||||
|
||||
class PIP_EXPORT PIVariant {
|
||||
friend class PIVariable;
|
||||
public:
|
||||
enum Type {Bool, Char, Short, Int, Long, LLong, UChar, UShort, UInt, ULong, ULLong, Float, Double, LDouble, String, StringList};
|
||||
|
||||
PIVariant() {setValue(0.);}
|
||||
PIVariant(const char * v) {setValue(v);}
|
||||
PIVariant(const bool & v) {setValue(v);}
|
||||
PIVariant(const char & v) {setValue(v);}
|
||||
PIVariant(const short & v) {setValue(v);}
|
||||
PIVariant(const int & v) {setValue(v);}
|
||||
PIVariant(const long & v) {setValue(v);}
|
||||
PIVariant(const llong & v) {setValue(v);}
|
||||
PIVariant(const uchar & v) {setValue(v);}
|
||||
PIVariant(const ushort & v) {setValue(v);}
|
||||
PIVariant(const uint & v) {setValue(v);}
|
||||
PIVariant(const ulong & 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 PIString & v) {setValue(v);}
|
||||
PIVariant(const PIStringList & v) {setValue(v);}
|
||||
|
||||
void setValue(const char * v) {setValue(PIString(v));}
|
||||
void setValue(const bool & v) {type = PIVariant::Bool; vBool = v;}
|
||||
void setValue(const char & v) {type = PIVariant::Char; vChar = v;}
|
||||
void setValue(const short & v) {type = PIVariant::Short; vShort = v;}
|
||||
void setValue(const int & v) {type = PIVariant::Int; vInt = v;}
|
||||
void setValue(const long & v) {type = PIVariant::Long; vLong = v;}
|
||||
void setValue(const llong & v) {type = PIVariant::LLong; vLLong = v;}
|
||||
void setValue(const uchar & v) {type = PIVariant::UChar; vUChar = v;}
|
||||
void setValue(const ushort & v) {type = PIVariant::UShort; vUShort = v;}
|
||||
void setValue(const uint & v) {type = PIVariant::UInt; vUInt = v;}
|
||||
void setValue(const ulong & v) {type = PIVariant::ULong; vULong = v;}
|
||||
void setValue(const ullong & v) {type = PIVariant::ULLong; vULLong = 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 PIString & v) {type = PIVariant::String; vString = v;}
|
||||
void setValue(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;}
|
||||
void setValueOnly(const PIString & v);
|
||||
PIString typeName() const {return PIVariant::toString(type);}
|
||||
double doubleValue() const {return PIVariant::variableValue(&vChar, type);}
|
||||
PIString stringValue() const;
|
||||
void typeFromString(const PIString & str) {type = PIVariant::fromString(str);}
|
||||
PIString typeToString() const {return PIVariant::toString(type);}
|
||||
uint size() {if (type != PIVariant::String && type != PIVariant::StringList) return PIVariant::variableSize(type); if (type == PIVariant::String) return vString.size(); else return vStringList.contentSize();}
|
||||
PIString writeToString() const {return typeName() + ":" + stringValue();}
|
||||
|
||||
#ifdef QNX
|
||||
void operator =(const PIVariant & v) {type = v.type; vLDouble = v.vLDouble; vString = v.vString; vStringList = v.vStringList;}
|
||||
#endif
|
||||
void operator =(const char * v) {setValue(PIString(v));}
|
||||
void operator =(const bool & v) {type = PIVariant::Bool; vBool = v;}
|
||||
void operator =(const char & v) {type = PIVariant::Char; vChar = v;}
|
||||
void operator =(const short & v) {type = PIVariant::Short; vShort = v;}
|
||||
void operator =(const int & v) {type = PIVariant::Int; vInt = v;}
|
||||
void operator =(const long & v) {type = PIVariant::Long; vLong = v;}
|
||||
void operator =(const llong & v) {type = PIVariant::LLong; vLLong = v;}
|
||||
void operator =(const uchar & v) {type = PIVariant::UChar; vUChar = v;}
|
||||
void operator =(const ushort & v) {type = PIVariant::UShort; vUShort = v;}
|
||||
void operator =(const uint & v) {type = PIVariant::UInt; vUInt = v;}
|
||||
void operator =(const ulong & v) {type = PIVariant::ULong; vULong = v;}
|
||||
void operator =(const ullong & v) {type = PIVariant::ULLong; vULLong = v;}
|
||||
void operator =(const float & v) {type = PIVariant::Float; vFloat = v;}
|
||||
void operator =(const double & v) {type = PIVariant::Double; vDouble = v;}
|
||||
void operator =(const ldouble & v) {type = PIVariant::LDouble; vLDouble = v;}
|
||||
void operator =(const PIString & v) {type = PIVariant::String; vString = v;}
|
||||
void operator =(const PIStringList & v) {type = PIVariant::StringList; vStringList = v;}
|
||||
|
||||
bool operator ==(const PIVariant & v) const;
|
||||
bool operator !=(const PIVariant & v) const {return !(*this == v);}
|
||||
|
||||
PIVariant::Type type;
|
||||
union {
|
||||
bool vBool;
|
||||
char vChar;
|
||||
short vShort;
|
||||
int vInt;
|
||||
long vLong;
|
||||
llong vLLong;
|
||||
uchar vUChar;
|
||||
ushort vUShort;
|
||||
uint vUInt;
|
||||
ulong vULong;
|
||||
ullong vULLong;
|
||||
float vFloat;
|
||||
double vDouble;
|
||||
ldouble vLDouble;
|
||||
};
|
||||
PIString vString;
|
||||
PIStringList vStringList;
|
||||
|
||||
static PIVariant readFromString(const PIString & s);
|
||||
|
||||
private:
|
||||
static PIVariant::Type fromString(const PIString & str);
|
||||
static PIString toString(const PIVariant::Type & var);
|
||||
static uint variableSize(const PIVariant::Type & var);
|
||||
static double variableValue(const void * var_ptr, const PIVariant::Type & var);
|
||||
|
||||
};
|
||||
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIVariant & v) {s << v.typeName() << ": " << v.stringValue(); return s;}
|
||||
|
||||
class PIP_EXPORT PIVariable {
|
||||
public:
|
||||
PIVariable() {;}
|
||||
PIVariable(const PIString & str) {setVariable(str);}
|
||||
~PIVariable() {;}
|
||||
|
||||
void setVariable(const PIString & str);
|
||||
void writeVariable(void * dest);
|
||||
void readVariable(const void * var_ptr) {value_ = PIVariant::variableValue((char * )((long)var_ptr + offset), type_);}
|
||||
PIVariant::Type type() const {return type_;}
|
||||
uint size() const {return size_;}
|
||||
const PIString & name() {return name_;}
|
||||
void setName(const PIString & str) {name_ = str;}
|
||||
double value() const {return value_;}
|
||||
void setValue(const double & val) {value_ = val;}
|
||||
|
||||
int offset;
|
||||
|
||||
private:
|
||||
PIVariant::Type type_;
|
||||
uint size_;
|
||||
PIString name_;
|
||||
double value_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* PIStruct is abstract structure, described by *.conf file with format of each line:
|
||||
* "<name> = <type> #<n|f|b> <start_value>".
|
||||
* e.g. "pi = double #f 3.1418"
|
||||
*
|
||||
* You can write or read binary content of this struct
|
||||
* by functions "writeData" and "readData", e.g.
|
||||
* "char * data = new char[struct.size()];
|
||||
* struct.writeData(data);"
|
||||
*
|
||||
* Access to each variable in struct is looks like
|
||||
* "double value = struct["pi"].value();"
|
||||
*/
|
||||
|
||||
class PIP_EXPORT PIStruct {
|
||||
public:
|
||||
PIStruct() {;}
|
||||
PIStruct(const PIString & str) {parseFile(str);}
|
||||
|
||||
void parseFile(const PIString & file);
|
||||
void readData(const void * data);
|
||||
void writeData(void * data);
|
||||
void clear() {vars.clear(); size_ = 0;}
|
||||
uint count() const {return vars.size();}
|
||||
uint size() const {return size_;}
|
||||
const PIString & name() {return name_;}
|
||||
void setName(const PIString & str) {name_ = str;}
|
||||
PIVariable & operator[](const uint & index) {return vars[index];}
|
||||
PIVariable & operator[](const PIString & name) {for (uint i = 0; i < vars.size(); ++i) if (vars[i].name() == name) return vars[i]; return def;}
|
||||
|
||||
private:
|
||||
uint size_;
|
||||
PIString name_;
|
||||
PIVariable def;
|
||||
PIVector<PIVariable> vars;
|
||||
|
||||
};
|
||||
|
||||
#endif // PIVARIABLE_H
|
||||
Reference in New Issue
Block a user