This commit is contained in:
2022-11-25 21:35:39 +03:00
parent a2256a3872
commit dc6fbbf7ec
37 changed files with 1498 additions and 1321 deletions

View File

@@ -0,0 +1,44 @@
/*
PIP - Platform Independent Primitives
Bit array
Ivan Pelipenko peri4ko@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 "pibitarray.h"
#include "picout.h"
PICout operator <<(PICout s, const PIBitArray & ba) {
s.space();
s.saveAndSetControls(0);
for (uint i = 0; i < ba.bitSize(); ++i) {
s << int(ba[i]);
if (i % 8 == 7) s << ' ';
}
s.restoreControls();
return s;
}
#ifdef PIP_STD_IOSTREAM
std::ostream &operator <<(std::ostream & s, const PIBitArray & ba) {
for (uint i = 0; i < ba.bitSize(); ++i) {
s << ba[i];
if (i % 8 == 7) s << ' ';
}
return s;
}
#endif

View File

@@ -0,0 +1,229 @@
/*! \file pibitarray.h
* \~\brief
* \~english Bit array
* \~russian Битовый массив
*/
/*
PIP - Platform Independent Primitives
Bit array
Ivan Pelipenko peri4ko@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/>.
*/
#ifndef PIBITARRAY_H
#define PIBITARRAY_H
#include "pivector.h"
//! \ingroup Types
//! \~\brief
//! \~english The %PIBitArray class provides an space-efficient array of bits.
//! \~russian Класс %PIBitArray представляет собой компактный массив битов.
class PIP_EXPORT PIBitArray {
template <typename P>
friend PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIBitArray & v);
template <typename P>
friend PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIBitArray & v);
public:
//! \~english Constructs bit array with "size" size.
//! \~russian Создает битовый массив с размером "size".
PIBitArray(const int & size = 0) {resize(size);}
//! \~english Constructs bit array with size 8 and values from "val".
//! \~russian Создает битовый массив с размером 8 и значениями из "val".
PIBitArray(uchar val) {resize(sizeof(val) * 8); data_[0] = val;}
//! \~english Constructs bit array with size 16 and values from "val".
//! \~russian Создает битовый массив с размером 16 и значениями из "val".
PIBitArray(ushort val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));}
//! \~english Constructs bit array with size 32 and values from "val".
//! \~russian Создает битовый массив с размером 32 и значениями из "val".
PIBitArray(uint val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));}
//! \~english Constructs bit array with size 64 and values from "val".
//! \~russian Создает битовый массив с размером 64 и значениями из "val".
PIBitArray(ullong val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));}
//! \~english Constructs bit array with size "bytes * 8" and values from "data".
//! \~russian Создает битовый массив с размером "bytes * 8" и значениями из "data".
PIBitArray(const uchar * data, uint bytes) {resize(bytes * 8); memcpy(this->data(), data, bytes);}
//! \~english Returns a size in bits.
//! \~russian Возвращает размер в битах.
uint bitSize() const {return size_;}
//! \~english Returns a size in bytes.
//! \~russian Возвращает размер в байтах.
uint byteSize() const {return bytesInBits(size_);}
//! \~english Resize this array to "size" bits.
//! \~russian Изменяет размер массива до "size" битов.
PIBitArray & resize(const uint & size) {size_ = size; data_.resize(bytesInBits(size_)); return *this;}
//! \~english Set bit with index "index" to 0.
//! \~russian Устанавливает бит с индексом "index" в 0.
PIBitArray & clearBit(const uint & index) {data_[index / 8] &= ~(1 << (index % 8)); return *this;}
//! \~english Set bit with index "index" to 1.
//! \~russian Устанавливает бит с индексом "index" в 1.
PIBitArray & setBit(const uint & index) {data_[index / 8] |= (1 << (index % 8)); return *this;}
//! \~english Set bit with index "index" to "value".
//! \~russian Устанавливает бит с индексом "index" в "value".
PIBitArray & writeBit(const uint & index, const bool & value) {if (value) setBit(index); else clearBit(index); return *this;}
//! \~english Set bit with index "index" to "value".
//! \~russian Устанавливает бит с индексом "index" в "value".
PIBitArray & writeBit(const uint & index, const uchar & value) {return writeBit(index, value > 0);}
//! \~english Add bit with value "value" to the end of array.
//! \~russian Добавляет бит со значением "value" в конец массива.
PIBitArray & push_back(const bool & value) {resize(size_ + 1); writeBit(size_ - 1, value); return *this;}
//! \~english Add bit with value "value" to the end of array.
//! \~russian Добавляет бит со значением "value" в конец массива.
PIBitArray & push_back(const uchar & value) {return push_back(value > 0);}
//! \~english Insert bit with value "value" after "index" position.
//! \~russian Вставляет бит со значением "value" после положения "index".
PIBitArray & insert(const uint & index, const bool & value) {
resize(size_ + 1);
uint fi = byteSize() - 1, si = index / 8, ti = index % 8;
uchar c = data_[si];
for (uint i = fi; i > si; --i) {
data_[i] <<= 1;
if ((0x80 & data_[i - 1]) == 0x80) data_[i] |= 1;
else data_[i] &= 0xFE;}
data_[si] &= (0xFF >> (7 - ti));
data_[si] |= ((c << 1) & (0xFF << (ti)));
if (value) data_[si] |= (1 << ti);
else data_[si] &= ~(1 << ti);
return *this;}
//! \~english Insert bit with value "value" after "index" position.
//! \~russian Вставляет бит со значением "value" после положения "index".
PIBitArray & insert(const uint & index, const uchar & value) {return insert(index, value > 0);}
//! \~english Add bit with value "value" to the beginning of array.
//! \~russian Добавляет бит со значением "value" в начало массива.
PIBitArray & push_front(const bool & value) {return insert(0, value);}
//! \~english Add bit with value "value" to the beginning of array.
//! \~russian Добавляет бит со значением "value" в начало массива.
PIBitArray & push_front(const uchar & value) {return push_front(value > 0);}
//! \~english Remove one bit from the end of array.
//! \~russian Удаляет один бит с конца массива.
PIBitArray & pop_back() {return resize(size_ - 1);}
//! \~english Remove one bit from the beginning of array.
//! \~russian Удаляет один бит с начала массива.
PIBitArray & pop_front() {
if (size_ == 0) return *this;
uint fi = byteSize() - 1;
for (uint i = 0; i < fi; ++i) {
data_[i] >>= 1;
if ((1 & data_[i + 1]) == 1) data_[i] |= 0x80;
else data_[i] &= 0x7F;}
data_[fi] >>= 1;
resize(size_ - 1);
return *this;}
//! \~english Add bits "ba" to the end of array.
//! \~russian Добавляет биты "ba" в конец массива.
PIBitArray & append(const PIBitArray & ba) {for (uint i = 0; i < ba.bitSize(); ++i) push_back(ba[i]); return *this;}
//! \~english Returns pointer to bytes data of array.
//! \~russian Возвращает указатель на байтовое содержимое массива.
uchar * data() {return data_.data();}
//! \~english Returns data of array as \b uchar.
//! \~russian Возвращает байтовое содержимое массива как \b uchar.
uchar toUChar() {if (size_ == 0) return 0; return data_[0];}
//! \~english Returns data of array as \b ushort.
//! \~russian Возвращает байтовое содержимое массива как \b ushort.
ushort toUShort() {ushort t = 0; memcpy(&t, data(), piMin<uint>(byteSize(), sizeof(t))); return t;}
//! \~english Returns data of array as \b uint.
//! \~russian Возвращает байтовое содержимое массива как \b uint.
uint toUInt() {uint t = 0; memcpy(&t, data(), piMin<uint>(byteSize(), sizeof(t))); return t;}
//! \~english Returns data of array as \b ullong.
//! \~russian Возвращает байтовое содержимое массива как \b ullong.
ullong toULLong() {ullong t = 0; memcpy(&t, data(), piMin<uint>(byteSize(), sizeof(t))); return t;}
//! \~english Returns bit value at index "index".
//! \~russian Возвращает значение бита с индексом "index".
bool at(const uint & index) const {return (1 & (data_[index / 8] >> (index % 8))) == 1 ? true : false;}
//! \~english Returns bit value at index "index".
//! \~russian Возвращает значение бита с индексом "index".
bool operator [](const uint & index) const {return at(index);}
//! \~english Similar to \a append().
//! \~russian Аналог \a append().
void operator +=(const PIBitArray & ba) {append(ba);}
//! \~english Returns if two bit arrays are similar.
//! \~russian Возвращает равны ли для битовых массива.
bool operator ==(const PIBitArray & ba) const {if (bitSize() != ba.bitSize()) return false; for (uint i = 0; i < bitSize(); ++i) if (at(i) != ba[i]) return false; return true;}
//! \~english Returns if two bit arrays are not similar.
//! \~russian Возвращает не равны ли для битовых массива.
bool operator !=(const PIBitArray & ba) const {return !(*this == ba);}
//! \~english Resize and set values from "val".
//! \~russian Изменяет размер и устанавливает значения из "val".
void operator =(const uchar & val) {resize(sizeof(val) * 8); data_[0] = val;}
//! \~english Resize and set values from "val".
//! \~russian Изменяет размер и устанавливает значения из "val".
void operator =(const ushort & val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));}
//! \~english Resize and set values from "val".
//! \~russian Изменяет размер и устанавливает значения из "val".
void operator =(const uint & val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));}
//! \~english Resize and set values from "val".
//! \~russian Изменяет размер и устанавливает значения из "val".
void operator =(const ullong & val) {resize(sizeof(val) * 8); memcpy(data(), &val, sizeof(val));}
private:
static uint bytesInBits(const uint & bits) {return (bits + 7) / 8;}
PIVector<uchar> data_;
uint size_;
};
#ifdef PIP_STD_IOSTREAM
//std::ostream & operator <<(std::ostream & s, const PIBitArray & ba);
#endif
//! \relatesalso PIByteArray
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
PICout operator <<(PICout s, const PIBitArray & ba);
#endif // PIBITARRAY_H

View File

@@ -0,0 +1,431 @@
/*
PIP - Platform Independent Primitives
Byte array
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 "pibytearray.h"
#include "pistringlist.h"
#include <iostream>
#include "picrc.h"
//! \class PIByteArray pibytearray.h
//! \~\details
//! \~english
//! %PIByteArray used to store raw bytes.
//! It can be constructed from any data and size.
//! You can use %PIByteArray as binary stream
//! to serialize/deserialize any objects and data.
//! See details \ref iostream.
//! This class use PIDeque<uchar> and provide some handle function
//! to manipulate it.
//! \~russian
//! %PIByteArray используется для хранения байтов.
//! Он может быть сконструирован из любых даных.
//! Можно использовать %PIByteArray как потоковый объект
//! для сериализации/десериализации любых типов и данных.
//! Подробнее \ref iostream.
//! Этот класс использует PIDeque<uchar> и предоставляет набор
//! удобных методов для работы с байтами.
//!
//! \~english \section PIByteArray_sec0 Usage
//! \~russian \section PIByteArray_sec0 Использование
//! \~english
//! %PIByteArray subclass PIBinaryStream and can be used to store custom data and manipulate it.
//! Store operators places data at the end of array, restore operators takes data from the beginning
//! of array.
//! In addition there are Hex and Base64 convertions.
//! \~russian
//! %PIByteArray наследован от PIBinaryStream и может быть использован для сохранения любых данных и работы с ними.
//! Операторы сохранения добавляют данные в конец массива, а операторы извлечения
//! берут данные из его начала.
//! Также есть методы для преобразования в Hex и Base64.
//!
//! \~english \section PIByteArray_sec1 Attention
//! \~russian \section PIByteArray_sec1 Внимание
//! \~english
//! Stream operator of %PIByteArray store byte array as vector, not simply append
//! content of byte array. This operators useful to transmit custom data as %PIByteArray
//! packed into parent byte array, e.g. to form packet from %PIByteArray.
//! To append one byte array to another use funtion \a append().
//! \~russian
//! Потоковый оператор для типа %PIByteArray сохраняет его как контейнер,
//! а не просто добавляет его содержимое в конец. Этот оператор полезен для управляемой
//! упаковки произвольных данных в виде %PIByteArray.
//! Для добавления содержимого одного байтового массива к другому используется
//! метод \a append().
//! \~\snippet pibytearray.cpp 3
//!
static const uchar base64Table[64] = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f};
static const uchar base64InvTable[256] = {
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x3E, 0x0, 0x0, 0x0, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6,
0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE,
0xF, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
0x31, 0x32, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
struct base64HelpStruct {
base64HelpStruct() {v = 0;}
inline void setBytes(const uchar * r, int size = 3) {
v = 0;
switch (size) {
case 3: v |= r[2];
case 2: v |= r[1] << 8;
case 1: v |= r[0] << 16;
}
}
inline void getBytes(uchar * r) {
r[0] = (v >> 16) & 0xFF;
r[1] = (v >> 8) & 0xFF;
r[2] = v & 0xFF;
}
inline void setAscii(const uchar * r, int size = 4) {
v = 0;
switch (size) {
case 4: v |= (base64InvTable[r[3]] & 0x3F);
case 3: v |= (base64InvTable[r[2]] & 0x3F) << 6;
case 2: v |= (base64InvTable[r[1]] & 0x3F) << 12;
case 1: v |= (base64InvTable[r[0]] & 0x3F) << 18;
}
}
inline void getAscii(uchar * r) {
r[0] = base64Table[(v >> 18) & 0x3F];
r[1] = base64Table[(v >> 12) & 0x3F];
r[2] = base64Table[(v >> 6) & 0x3F];
r[3] = base64Table[ v & 0x3F];
}
uint v;
};
PIByteArray &PIByteArray::convertToBase64() {
return *this = toBase64();
}
PIByteArray &PIByteArray::convertFromBase64() {
return *this = fromBase64(*this);
}
PIByteArray PIByteArray::toBase64() const {
if (isEmpty()) return PIByteArray();
base64HelpStruct hs;
PIByteArray ret;
int sz = (size_s() / 3) * 3, ri = -1;
uchar t[4];
ret.resize(((size_s() - 1) / 3 + 1) * 4);
for (int i = 0; i < sz; i += 3) {
hs.setBytes(data(i));
hs.getAscii(t);
ret[++ri] = (t[0]);
ret[++ri] = (t[1]);
ret[++ri] = (t[2]);
ret[++ri] = (t[3]);
}
int der = size_s() % 3;
switch (der) {
case 1:
hs.setBytes(data(sz), 1);
hs.getAscii(t);
ret[++ri] = (t[0]);
ret[++ri] = (t[1]);
ret[++ri] = ('=');
ret[++ri] = ('=');
break;
case 2:
hs.setBytes(data(sz), 2);
hs.getAscii(t);
ret[++ri] = (t[0]);
ret[++ri] = (t[1]);
ret[++ri] = (t[2]);
ret[++ri] = ('=');
break;
default: break;
}
return ret;
}
PIByteArray PIByteArray::fromBase64(const PIByteArray & base64) {
if (base64.isEmpty()) return PIByteArray();
base64HelpStruct hs;
PIByteArray ret;
int sz = base64.size_s(), ind = -1;
uchar t[4];
ret.resize(sz / 4 * 3);
for (int i = 0; i < sz; i += 4) {
hs.setAscii(base64.data(i));
hs.getBytes(t);
ret[++ind] = (t[0]);
ret[++ind] = (t[1]);
ret[++ind] = (t[2]);
}
if (base64.back() == '=') ret.pop_back();
if (sz > 1) if (base64[sz - 2] == '=') ret.pop_back();
return ret;
}
PIByteArray PIByteArray::fromBase64(const PIString & base64) {
return fromBase64(base64.toByteArray());
}
PIByteArray & PIByteArray::compressRLE(uchar threshold) {
PIByteArray t;
uchar fb, clen, mlen = 255 - threshold;
for (uint i = 0; i < size();) {
fb = at(i);
clen = 1;
while (at(++i) == fb) {
++clen;
if (clen == mlen)
break;
}
if (clen > 1) {
t.push_back(threshold + clen);
t.push_back(fb);
continue;
}
if (fb >= threshold) {
t.push_back(threshold + 1);
t.push_back(fb);
} else
t.push_back(fb);
}
*this = t;
return *this;
}
PIByteArray & PIByteArray::decompressRLE(uchar threshold) {
PIByteArray t;
uchar fb, clen;
for (uint i = 0; i < size(); ++i) {
fb = at(i);
if (fb >= threshold) {
clen = fb - threshold;
fb = at(++i);
for (uint j = 0; j < clen; ++j)
t.push_back(fb);
continue;
} else
t.push_back(fb);
}
*this = t;
return *this;
}
//! \~\details
//! \~english
//! This is simple sum of all bytes, if "inverse" then add 1 and inverse.
//! Pseudocode:
//! \~russian
//! Это простая сумма всех байтов, если "inverse", то ещё добавляется 1 и инвертируется результат.
//! Псевдокод:
//! \~\code
//! for (i)
//! sum += at(i);
//! if (inverse) return ~(sum + 1);
//! else return sum;
//! \endcode
uchar PIByteArray::checksumPlain8(bool inverse) const {
uchar c = 0;
int sz = size_s();
for (int i = 0; i < sz; ++i)
c += at(i);
if (inverse) c = ~(c + 1);
return c;
}
uchar PIByteArray::checksumCRC8() const {
return standardCRC_8().calculate(data(), size());
}
ushort PIByteArray::checksumCRC16() const {
return standardCRC_16().calculate(data(), size());
}
uint PIByteArray::checksumCRC32() const {
return standardCRC_32().calculate(data(), size());
}
//! \~\details
//! \~english
//! This is sum of all bytes multiplied by index+1, if inverse then add 1 and inverse.
//! Pseudocode:
//! \~russian
//! Это простая сумма всех байтов, умноженных на индекс+1, если "inverse", то ещё добавляется 1 и инвертируется результат.
//! Псевдокод:
//! \~\code
//! for (i)
//! sum += at(i) * (i + 1);
//! if (inverse) return ~(sum + 1);
//! else return sum;
//! \endcode
uint PIByteArray::checksumPlain32(bool inverse) const {
uint c = 0;
int sz = size_s();
for (int i = 0; i < sz; ++i)
c += at(i) * (i + 1);
if (inverse) c = ~(c + 1);
return c;
}
uint PIByteArray::hash() const {
return piHashData(data(), size_s());
}
PIString PIByteArray::toString(int base) const {
PIString ret;
int sz = size_s();
for (int i = 0; i < sz; ++i) {
if (i > 0) ret += " ";
if (base == 2) ret += "b";
if (base == 8) ret += "0";
if (base == 16) ret += "0x";
ret += PIString::fromNumber(at(i), base);
}
return ret;
}
PIString PIByteArray::toHex() const {
PIByteArray hex(size() * 2);
uchar *hexData = hex.data();
const uchar *d = data();
for (int i = 0; i < size_s(); ++i) {
int j = (d[i] >> 4) & 0xf;
if (j <= 9) hexData[i*2] = (j + '0');
else hexData[i*2] = (j + 'a' - 10);
j = d[i] & 0xf;
if (j <= 9) hexData[i*2+1] = (j + '0');
else hexData[i*2+1] = (j + 'a' - 10);
}
return PIString(hex);
}
PIByteArray PIByteArray::fromUserInput(PIString str) {
PIByteArray ret;
if (str.trim().isEmpty()) return ret;
str.replaceAll("\n", " ").replaceAll("\t", " ").replaceAll(" ", " ");
PIStringList bl(str.split(" "));
bool ok(false);
piForeachC (PIString & b, bl) {
int bv = b.toInt(-1, &ok);
if (ok) ret << uchar(bv);
}
return ret;
}
PIByteArray PIByteArray::fromHex(PIString str) {
PIByteArray hexEncoded = str.toByteArray();
PIByteArray res((hexEncoded.size() + 1)/ 2);
uchar *result = res.data() + res.size();
bool odd_digit = true;
for (int i = hexEncoded.size() - 1; i >= 0; --i) {
int ch = hexEncoded.at(i);
int tmp;
if (ch >= '0' && ch <= '9') tmp = ch - '0';
else if (ch >= 'a' && ch <= 'f') tmp = ch - 'a' + 10;
else if (ch >= 'A' && ch <= 'F') tmp = ch - 'A' + 10;
else continue;
if (odd_digit) {
--result;
*result = tmp;
odd_digit = false;
} else {
*result |= tmp << 4;
odd_digit = true;
}
}
res.remove(0, result - res.data());
return res;
}
PICout operator <<(PICout s, const PIByteArray & ba) {
s.space();
s.saveAndSetControls(0);
s << "{";
for (uint i = 0; i < ba.size(); ++i) {
s << ba[i];
if (i < ba.size() - 1) s << ", ";
}
s << "}";
s.restoreControls();
return s;
}
#ifdef PIP_STD_IOSTREAM
std::ostream &operator <<(std::ostream & s, const PIByteArray & ba) {
s << "{";
for (uint i = 0; i < ba.size(); ++i) {
s << ba[i];
if (i < ba.size() - 1) s << ", ";
}
s << "}";
return s;
}
#endif

View File

@@ -0,0 +1,1272 @@
/*! \file pibytearray.h
* \ingroup Types
* \~\brief
* \~english Byte array
* \~russian Байтовый массив
*/
/*
PIP - Platform Independent Primitives
Byte array
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/>.
*/
#ifndef PIBYTEARRAY_H
#define PIBYTEARRAY_H
#include "pichar.h"
#include "pibinarystream.h"
#include <stdio.h>
class PIString;
class PIByteArray;
//! \ingroup Types
//! \~\brief
//! \~english The %PIByteArray class provides an array of bytes.
//! \~russian Класс %PIByteArray представляет собой массив байтов.
class PIP_EXPORT PIByteArray: public PIBinaryStream<PIByteArray>
{
public:
typedef ::PIMemoryBlock RawData DEPRECATEDM("use PIMemoryBlock instead");
//! \~english Constructs an empty byte array
//! \~russian Создает пустой байтовый массив
PIByteArray() {}
//! \~english Constructs copy of byte array "o"
//! \~russian Создает копию байтового массива "o"
PIByteArray(const PIByteArray & o): d(o.d) {}
//! \~english Constructs copy of byte array "o"
//! \~russian Создает копию байтового массива "o"
PIByteArray(const PIDeque<uchar> & o): d(o) {}
PIByteArray(PIByteArray && o): d(std::move(o.d)) {}
//! \~english Constructs 0-filled byte array with size "size"
//! \~russian Создает заполненный "0" байтовый массив размером "size"
PIByteArray(const uint size) {resize(size);}
//! \~english Constructs byte array from data "data" and size "size"
//! \~russian Создает байтовый массив из данных по указателю "data" размером "size"
PIByteArray(const void * data, const uint size): d((const uchar*)data, size_t(size)) {}
//! \~english Constructs byte array with size "size" filled by "t"
//! \~russian Создает заполненный "t" байтовый массив размером "size"
PIByteArray(const uint size, uchar t): d(size, t) {}
//! \~english Contructs array from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Создает массив из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\details
//! \~\code
//! PIByteArray v{1,2,3};
//! piCout << v; // {1, 2, 3}
//! \endcode
PIByteArray(std::initializer_list<uchar> init_list) : d(init_list) {}
//! \~english Swaps array `v` other with this array.
//! \~russian Меняет местами массив `v` с этим массивом.
//! \~\details
//! \~english This operation is very fast and never fails.
//! \~russian Эта операция выполняется мгновенно без копирования памяти и никогда не дает сбоев.
inline void swap(PIByteArray & other) {
d.swap(other.d);
}
//! \~english Iterator to the first element.
//! \~russian Итератор на первый элемент.
//! \~\details ![begin, end](doc/images/pivector_begin.png)
//!
//! \~english If the array is empty, the returned iterator is equal to \a end().
//! \~russian Если массив - пуст, возвращаемый итератор будет равен \a end().
//! \~\return \ref stl_iterators
//! \~\sa \a end(), \a rbegin(), \a rend()
inline PIDeque<uchar>::iterator begin() {return d.begin();}
//! \~english Iterator to the element following the last element.
//! \~russian Итератор на элемент, следующий за последним элементом.
//! \~\details ![begin, end](doc/images/pivector_begin.png)
//!
//! \~english This element acts as a placeholder;
//! attempting to access it results in undefined behavior.
//! \~russian Этот элемент существует лишь условно,
//! попытка доступа к нему приведёт к выходу за разрешенную память.
//! \~\return \ref stl_iterators
//! \~\sa \a begin(), \a rbegin(), \a rend()
inline PIDeque<uchar>::iterator end() {return d.end();}
inline PIDeque<uchar>::const_iterator begin() const {return d.begin();}
inline PIDeque<uchar>::const_iterator end() const {return d.end();}
//! \~english Returns a reverse iterator to the first element of the reversed array.
//! \~russian Обратный итератор на первый элемент.
//! \~\details ![rbegin, rend](doc/images/pivector_rbegin.png)
//!
//! \~english It corresponds to the last element of the non-reversed array.
//! If the array is empty, the returned iterator is equal to \a rend().
//! \~russian Итератор для прохода массива в обратном порядке.
//! Указывает на последний элемент.
//! Если массив пустой, то совпадает с итератором \a rend().
//! \~\return \ref stl_iterators
//! \~\sa \a rend(), \a begin(), \a end()
inline PIDeque<uchar>::reverse_iterator rbegin() {return d.rbegin();}
//! \~english Returns a reverse iterator to the element.
//! following the last element of the reversed array.
//! \~russian Обратный итератор на элемент, следующий за последним элементом.
//! \~\details ![rbegin, rend](doc/images/pivector_rbegin.png)
//!
//! \~english It corresponds to the element preceding the first element of the non-reversed array.
//! This element acts as a placeholder, attempting to access it results in undefined behavior.
//! \~russian Итератор для прохода массива в обратном порядке.
//! Указывает на элемент, предшествующий первому элементу.
//! Этот элемент существует лишь условно,
//! попытка доступа к нему приведёт к выходу за разрешенную память.
//! \~\return \ref stl_iterators
//! \~\sa \a rbegin(), \a begin(), \a end()
inline PIDeque<uchar>::reverse_iterator rend() {return d.rend();}
inline PIDeque<uchar>::const_reverse_iterator rbegin() const {return d.rbegin();}
inline PIDeque<uchar>::const_reverse_iterator rend() const {return d.rend();}
//! \~english Number of elements in the container.
//! \~russian Количество элементов массива.
//! \~\sa \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline size_t size() const {return d.size();}
//! \~english Number of elements in the container as signed value.
//! \~russian Количество элементов массива в виде знакового числа.
//! \~\sa \a size(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline ssize_t size_s() const {return d.size_s();}
//! \~english Same as \a size().
//! \~russian Синоним \a size().
//! \~\sa \a size(), \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline size_t length() const {return d.length();}
//! \~english Number of elements that the container has currently allocated space for.
//! \~russian Количество элементов, для которого сейчас выделена память массивом.
//! \~\details
//! \~english To find out the actual number of items, use the function \a size().
//! \~russian Чтобы узнать фактическое количество элементов используйте функцию \a size().
//! \~\sa \a reserve(), \a size(), \a size_s()
inline size_t capacity() const {return d.capacity();}
//! \~english Checks if the container has no elements.
//! \~russian Проверяет пуст ли массив.
//! \~\return
//! \~english **true** if the container is empty, **false** otherwise
//! \~russian **true** если массив пуст, **false** иначе.
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline bool isEmpty() const {return d.isEmpty();}
//! \~english Checks if the container has elements.
//! \~russian Проверяет не пуст ли массив.
//! \~\return
//! \~english **true** if the container is not empty, **false** otherwise
//! \~russian **true** если массив не пуст, **false** иначе.
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
inline bool isNotEmpty() const {return d.isNotEmpty();}
//! \~english Tests whether at least one element in the array
//! passes the test implemented by the provided function `test`.
//! \~russian Проверяет, удовлетворяет ли какой-либо элемент массива условию,
//! заданному в передаваемой функции `test`.
//! \~\return
//! \~english **true** if, in the array,
//! it finds an element for which the provided function returns **true**;
//! otherwise it returns **false**. Always returns **false** if is empty.
//! \~russian **true** если хотя бы для одного элемента
//! передаваемая функция возвращает **true**, в остальных случаях **false**.
//! Метод возвращает **false** при любом условии для пустого массива.
//! \~\details
//! \~\sa \a every(), \a contains(), \a entries(), \a forEach()
inline bool any(std::function<bool(uchar e)> test) const {
return d.any(test);
}
//! \~english Tests whether all elements in the array passes the test
//! implemented by the provided function `test`.
//! \~russian Проверяет, удовлетворяют ли все элементы массива условию,
//! заданному в передаваемой функции `test`.
//! \~\return
//! \~english **true** if, in the array,
//! it finds an element for which the provided function returns **true**;
//! otherwise it returns **false**. Always returns **true** if is empty.
//! \~russian **true** если для всех элементов передаваемая функция возвращает **true**,
//! в остальных случаях **false**.
//! Метод возвращает **true** при любом условии для пустого массива.
//! \~\details
//! \~\sa \a any(), \a contains(), \a entries(), \a forEach()
inline bool every(std::function<bool(uchar e)> test) const {
return d.every(test);
}
//! \~english Full access to element by `index`.
//! \~russian Полный доступ к элементу по индексу `index`.
//! \~\details
//! \~english Element index starts from `0`.
//! Element index must be in range from `0` to `size()-1`.
//! Otherwise will be undefined behavior.
//! \~russian Индекс элемента считается от `0`.
//! Индекс элемента должен лежать в пределах от `0` до `size()-1`.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
//! \~\sa \a at()
inline uchar & operator [](size_t index) {return d[index];}
inline uchar operator [](size_t index) const {return d[index];}
//! \~english Read only access to element by `index`.
//! \~russian Доступ исключительно на чтение к элементу по индексу `index`.
//! \~\details
//! \~english Element index starts from `0`.
//! Element index must be in range from `0` to `size()-1`.
//! Otherwise will be undefined behavior.
//! \~russian Индекс элемента считается от `0`.
//! Индекс элемента должен лежать в пределах от `0` до `size()-1`.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
inline uchar at(size_t index) const {return d.at(index);}
//! \~english Last element.
//! \~russian Последний элемент массива.
//! \~\details
//! \~english Returns a reference to the last item in the array.
//! This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Возвращает ссылку на последний элемент в массиве.
//! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
inline uchar & back() {return d.back();}
inline uchar back() const {return d.back();}
//! \~english Last element.
//! \~russian Первый элемент массива.
//! \~\details
//! \~english Returns a reference to the last item in the array.
//! This function assumes that the array isn't empty.
//! Otherwise will be undefined behavior.
//! \~russian Возвращает ссылку на пенрвый элемент в массиве.
//! Эта функция предполагает, что массив не пустой.
//! Иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
inline uchar & front() {return d.front();}
inline uchar front() const {return d.front();}
//! \~english Tests if element `e` exists in the array.
//! \~russian Проверяет наличие элемента `e` в массиве.
//! \~\details
//! \~english Optional argument `start` - the position in this array at which to begin searching.
//! If the index is greater than or equal to the array's size,
//! **false** is returned, which means the array will not be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Note: if the provided index is negative,
//! the array is still searched from front to back.
//! Default: 0 (entire array is searched).
//! \~russian Опциональный аргумент `start` указывает на индекс в массиве, откуда будет начинаться поиск.
//! Если индекс больше или равен длине массива,
//! возвращается **false**, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code
//! PIByteArray v{1, 2, 3, 4};
//! piCout << v.contains(3); // true
//! piCout << v.contains(5); // false
//! piCout << v.contains(3, 3); // false
//! piCout << v.contains(3, -2); // true
//! piCout << v.contains(3, -99); // true
//! \endcode
//! \~\return
//! \~english **true** if the array contains an occurrence of element `e`,
//! otherwise it returns **false**.
//! \~russian **true** если элемент `e` присутствует в массиве,
//! в остальных случаях **false**.
//! \~\sa \a every(), \a any(), \a entries()
inline bool contains(uchar e, ssize_t start = 0) const {
return d.contains(e, start);
}
//! \~english Count elements equal `e` in the array.
//! \~russian Подсчитывает количество элементов, совпадающих с элементом `e` в массиве.
//! \~\details
//! \~english Optional argument `start` - the position in this array at which to begin searching.
//! If the index is greater than or equal to the array's size,
//! 0 is returned, which means the array will not be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Note: if the provided index is negative,
//! the array is still searched from front to back.
//! Default: 0 (entire array is searched).
//! \~russian Опциональный аргумент `start` указывает на индекс в массиве, откуда будет начинаться поиск.
//! Если индекс больше или равен длине массива,
//! возвращается 0, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\sa \a every(), \a any(), \a contains(), \a indexOf()
inline int entries(uchar e, ssize_t start = 0) const {
return d.entries(e, start);
}
//! \~english Count elements in the array passes the test implemented by the provided function `test`.
//! \~russian Подсчитывает количество элементов в массиве,
//! проходящих по условию, заданному в передаваемой функции `test`.
//! \~\details
//! \~english Overloaded function.
//! Optional argument `start` - the position in this array at which to begin searching.
//! If the index is greater than or equal to the array's size,
//! 0 is returned, which means the array will not be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Note: if the provided index is negative,
//! the array is still searched from front to back.
//! Default: 0 (entire array is searched).
//! \~russian Перегруженная функция.
//! Опциональный аргумент `start` указывает на индекс в массиве, откуда будет начинаться поиск.
//! Если индекс больше или равен длине массива,
//! возвращается 0, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\sa \a every(), \a any(), \a contains(), \a indexWhere()
inline int entries(std::function<bool(uchar e)> test, ssize_t start = 0) const {
return d.entries(test, start);
}
//! \~english Returns the first index at which a given element `e`
//! can be found in the array, or `-1` if it is not present.
//! \~russian Возвращает первый индекс, по которому данный элемент `e`
//! может быть найден в массиве или `-1`, если такого индекса нет.
//! \~\details
//! \~english Optional argument `start` - the position in this array at which to begin searching.
//! If the index is greater than or equal to the array's size,
//! `-1` is returned, which means the array will not be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Note: if the provided index is negative,
//! the array is still searched from front to back.
//! Default: 0 (entire array is searched).
//! \~russian Опциональный аргумент `start` указывает на индекс в массиве, откуда будет начинаться поиск.
//! Если индекс больше или равен длине массива,
//! возвращается `-1`, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code
//! PIByteArray v{2, 5, 9};
//! piCout << v.indexOf(2); // 0
//! piCout << v.indexOf(7); // -1
//! piCout << v.indexOf(9, 2); // 2
//! piCout << v.indexOf(2, -1); // -1
//! piCout << v.indexOf(2, -3); // 0
//! \endcode
//! \~\sa \a indexWhere(), \a lastIndexOf(), \a lastIndexWhere(), \a contains()
inline ssize_t indexOf(const uchar & e, ssize_t start = 0) const {
return d.indexOf(e, start);
}
//! \~english Returns the first index passes the test implemented by the provided function `test`,
//! or `-1` if it is not present.
//! can be found in the array, or `-1` if it is not present.
//! \~russian Возвращает первый индекс элемента проходящего по условию,
//! заданному в передаваемой функции `test`, или `-1`, если таких элементов нет.
//! \~\details
//! \~english Optional argument `start` - the position in this array at which to begin searching.
//! If the index is greater than or equal to the array's size,
//! `-1` is returned, which means the array will not be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Note: if the provided index is negative,
//! the array is still searched from front to back.
//! Default: 0 (entire array is searched).
//! \~russian Опциональный аргумент `start` указывает на индекс в массиве, откуда будет начинаться поиск.
//! Если индекс больше или равен длине массива,
//! возвращается `-1`, что означает, что массив даже не просматривается.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Если рассчитанный индекс все равно оказывается меньше 0, просматривается весь массив.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от начала к концу.
//! Значение по умолчанию равно 0, что означает, что просматривается весь массив.
//! \~\code
//! PIByteArray v{2, 5, 9};
//! piCout << v.indexWhere([](const uchar & s){return s > 3;}); // 1
//! piCout << v.indexWhere([](const uchar & s){return s > 3;}, 2); // 2
//! piCout << v.indexWhere([](const uchar & s){return s > 10;}); // -1
//! \endcode
//! \~\sa \a indexOf(), \a lastIndexOf(), \a lastIndexWhere(), \a contains()
inline ssize_t indexWhere(std::function<bool(const uchar & e)> test, ssize_t start = 0) const {
return d.indexWhere(test, start);
}
//! \~english Returns the last index at which a given element `e`
//! can be found in the array, or `-1` if it is not present.
//! \~russian Возвращает последний индекс, по которому данный элемент `e`
//! может быть найден в массиве или `-1`, если такого индекса нет.
//! \~\details
//! \~english Optional argument `start` - the position in this array
//! at which to start searching backwards.
//! If the index is greater than or equal to the array's size,
//! causes the whole array to be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Therefore, if calculated index less than 0,
//! the array is not searched, and the method returns `-1`.
//! Note: if the provided index is negative,
//! the array is still searched from back to front.
//! Default: -1 (entire array is searched).
//! \~russian Опциональный аргумент `start` указывает на индекс
//! c которого начинать поиск в обратном направлении.
//! Если индекс больше или равен длине массива, просматривается весь массив.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от конца к началу.
//! Если рассчитанный индекс оказывается меньше 0, массив даже не просматривается.
//! Значение по умолчанию равно `-1`, что равно индексу последнего элемента
//! и означает, что просматривается весь массив.
//! \~\code
//! PIByteArray v{2, 5, 9, 2};
//! piCout << v.lastIndexOf(2); // 3
//! piCout << v.lastIndexOf(7); // -1
//! piCout << v.lastIndexOf(2, 2); // 0
//! piCout << v.lastIndexOf(2, -3); // 0
//! piCout << v.lastIndexOf(2, -300); // -1
//! piCout << v.lastIndexOf(2, 300); // 3
//! \endcode
//! \~\sa \a indexOf(), \a indexWhere(), \a lastIndexWhere(), \a contains()
inline ssize_t lastIndexOf(const uchar & e, ssize_t start = -1) const {
return d.lastIndexOf(e, start);
}
//! \~english Returns the last index passes the test implemented by the provided function `test`,
//! or `-1` if it is not present.
//! \~russian Возвращает последний индекс элемента проходящего по условию,
//! заданному в передаваемой функции `test`, или `-1`, если таких элементов нет.
//! \~\details
//! \~english Optional argument `start` - the position in this array
//! at which to start searching backwards.
//! If the index is greater than or equal to the array's size,
//! causes the whole array to be searched.
//! If the provided index value is a negative number,
//! it is taken as the offset from the end of the array.
//! Therefore, if calculated index less than 0,
//! the array is not searched, and the method returns `-1`.
//! Note: if the provided index is negative,
//! the array is still searched from back to front.
//! Default: -1 (entire array is searched).
//! \~russian Опциональный аргумент `start` указывает на индекс
//! c которого начинать поиск в обратном направлении.
//! Если индекс больше или равен длине массива, просматривается весь массив.
//! Если индекс является отрицательным числом, он трактуется как смещение с конца массива.
//! Обратите внимание: если индекс отрицателен, массив всё равно просматривается от конца к началу.
//! Если рассчитанный индекс оказывается меньше 0, массив даже не просматривается.
//! Значение по умолчанию равно `-1`, что равно индексу последнего элемента
//! и означает, что просматривается весь массив.
//! \~\sa \a indexOf(), \a lastIndexOf(), \a indexWhere(), \a contains()
inline ssize_t lastIndexWhere(std::function<bool(const uchar & e)> test, ssize_t start = -1) const {
return d.lastIndexWhere(test, start);
}
//! \~english Pointer to array
//! \~russian Указатель на память массива
//! \~\details
//! \~english Optional argument `index` the position in this array,
//! where is pointer. Default: start of array.
//! \~russian Опциональный аргумент `index` указывает на индекс c которого брать указатель.
//! По умолчанию указывает на начало массива.
inline uchar * data(size_t index = 0) {return d.data(index);}
//! \~english Read only pointer to array
//! \~russian Указатель на память массива только для чтения.
//! \~\details
//! \~english The pointer can be used to access and modify the items in the array.
//! The pointer remains valid as long as the array isn't reallocated.
//! Optional argument `index` the position in this array,
//! where is pointer. Default: start of array.
//! \~russian Указатель можно использовать для доступа и изменения элементов в массиве.
//! Указатель остается действительным только до тех пор, пока массив не будет перераспределен.
//! Опциональный аргумент `index` указывает на индекс c которого брать указатель.
//! По умолчанию указывает на начало массива.
inline const uchar * data(size_t index = 0) const {return d.data(index);}
//! \~english Clear array, remove all elements.
//! \~russian Очищает массив, удаляет все элементы.
//! \~\details
//! \~\note
//! \~english Reserved memory will not be released.
//! \~russian Зарезервированная память не освободится.
//! \~\sa \a resize()
inline PIByteArray & clear() {
resize(0);
return *this;
}
//! \~english Assigns element 'e' to all items in the array.
//! \~russian Заполняет весь массив копиями элемента 'e'.
//! \~\details
//! \~\sa \a resize()
inline PIByteArray & fill(uchar e = 0) {
d.fill(e);
return *this;
}
//! \~english Assigns result of function 'f(size_t i)' to all items in the array.
//! \~russian Заполняет весь массив результатом вызова функции 'f(size_t i)'.
//! \~\details
//! \~\sa \a resize()
inline PIByteArray & fill(std::function<uchar(size_t i)> f) {
d.fill(f);
return *this;
}
//! \~english Same as \a fill().
//! \~russian Тоже самое что и \a fill().
//! \~\sa \a fill(), \a resize()
inline PIByteArray & assign(uchar e = 0) {return fill(e);}
//! \~english First does `resize(new_size)` then `fill(e)`.
//! \~russian Сначала делает `resize(new_size)`, затем `fill(e)`.
//! \~\sa \a fill(), \a resize()
inline PIByteArray & assign(size_t new_size, uchar e) {
resize(new_size);
return fill(e);
}
//! \~english Sets size of the array, new elements are copied from `e`.
//! \~russian Устанавливает размер массива, новые элементы копируются из `e`.
//! \~\details
//! \~english If `new_size` is greater than the current \a size(),
//! elements are added to the end; the new elements are initialized from `e`.
//! If `new_size` is less than the current \a size(), elements are removed from the end.
//! \~russian Если `new_size` больше чем текущий размер массива \a size(),
//! новые элементы добавляются в конец массива и создаются из `e`.
//! Если `new_size` меньше чем текущий размер массива \a size(),
//! лишние элементы удаляются с конца массива.
//! \~\sa \a size(), \a clear()
inline PIByteArray & resize(size_t new_size, uchar e = 0) {
d.resize(new_size, e);
return *this;
}
//! \~english Sets size of the array, new elements created by function `f(size_t i)`.
//! \~russian Устанавливает размер массива, новые элементы создаются функцией `f(size_t i)`.
//! \~\details
//! \~english If `new_size` is greater than the current \a size(),
//! elements are added to the end; the new elements created by function `f(size_t i)`.
//! If `new_size` is less than the current \a size(), elements are removed from the end.
//! \~russian Если `new_size` больше чем текущий размер массива \a size(),
//! новые элементы добавляются в конец массива и функцией `f(size_t i)`.
//! Если `new_size` меньше чем текущий размер массива \a size(),
//! лишние элементы удаляются с конца массива.
//! \~\sa \a size(), \a clear()
inline PIByteArray & resize(size_t new_size, std::function<uchar(size_t i)> f) {
d.resize(new_size, f);
return *this;
}
//! \~english Return resized byte array
//! \~russian Возвращает копию байтового массива с измененным размером
PIByteArray resized(uint new_size) const {
PIByteArray ret(new_size);
memcpy(ret.data(), data(), new_size);
return ret;
}
//! \~english Attempts to allocate memory for at least `new_size` elements.
//! \~russian Резервируется память под как минимум `new_size` элементов.
//! \~\details
//! \~english If you know in advance how large the array will be,
//! you should call this function to prevent reallocations and memory fragmentation.
//! If `new_size` is greater than the current \a capacity(),
//! new storage is allocated, otherwise the function does nothing.
//! This function does not change the \a size() of the array.
//! \~russian Если вы заранее знаете, насколько велик будет массив,
//! вы можете вызвать эту функцию, чтобы предотвратить перераспределение и фрагментацию памяти.
//! Если размер `new_size` больше чем выделенная память \a capacity(),
//! то произойдёт выделение новой памяти и перераспределение массива.
//! Эта функция не изменяет количество элементов в массиве \a size().
//! \~\sa \a size(), \a capacity(), \a resize()
inline PIByteArray & reserve(size_t new_size) {
d.reserve(new_size);
return *this;
}
//! \~english Inserts value `e` at `index` position in the array.
//! \~russian Вставляет значение `e` в позицию `index` в массиве.
//! \~\details
//! \~english The index must be greater than 0 and less than or equal to \a size().
//! \~russian Индекс должен быть больше 0 и меньше или равен \a size().
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIByteArray & insert(size_t index, uchar e = 0) {
d.insert(index, e);
return *this;
}
//! \~english Inserts array `v` at `index` position in the array.
//! \~russian Вставляет массив `v` в позицию `index` в массиве.
//! \~\details
//! \~english The index must be greater than or equal to 0 and less than or equal to \a size().
//! \~russian Индекс должен быть больше или равен 0 и меньше или равен \a size().
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIByteArray & insert(size_t index, const PIByteArray & v) {
d.insert(index, v.d);
return *this;
}
//! \~english Inserts the given elements at `index` position in the array.
//! \~russian Вставляет элементы в позицию `index` в массиве.
//! \~\details
//! \~english The index must be greater than or equal to 0 and less than or equal to \a size().
//! Inserts the given elements from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Индекс должен быть больше или равен 0 и меньше или равен \a size().
//! Вставляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIByteArray & insert(size_t index, std::initializer_list<uchar> init_list) {
d.insert(index, init_list);
return *this;
}
//! \~english Removes `count` elements from the middle of the array, starting at `index` position.
//! \~russian Удаляет элементы из массива, начиная с позиции `index` в количестве `count`.
//! \~\details
//! \~\sa \a resize(), \a insert(), \a removeOne(), \a removeAll(), \a removeWhere()
inline PIByteArray & remove(size_t index, size_t count = 1) {
d.remove(index, count);
return *this;
}
//! \~english Return sub-array starts from "index" and has "count" or less bytes
//! \~russian Возвращает подмассив с данными от индекса "index" и размером не более "count"
PIByteArray getRange(size_t index, size_t count) const {
return d.getRange(index, count);
}
//! \~english Reverses this array.
//! \~russian Обращает порядок следования элементов этого массива.
//! \~\details
//! \~english This method reverses an array [in place](https://en.wikipedia.org/wiki/In-place_algorithm).
//! The first array element becomes the last, and the last array element becomes the first.
//! The reverse method transposes the elements of the calling array object in place,
//! mutating the array, and returning a reference to the array.
//! \~russian Метод reverse() на месте переставляет элементы массива,
//! на котором он был вызван, изменяет массив и возвращает ссылку на него.
//! Первый элемент массива становится последним, а последний — первым.
//! \~\sa \a reversed()
inline PIByteArray & reverse() {
d.reverse();
return *this;
}
//! \~english Returns reversed array.
//! \~russian Возвращает перевернутый массив.
//! \~\details
//! \~english Returns a copy of the array with elements in reverse order.
//! The first array element becomes the last, and the last array element becomes the first.
//! \~russian Возвращает копию массива с элементами в обратном порядке.
//! Первый элемент массива становится последним, а последний — первым.
//! \~\sa \a reverse()
inline PIByteArray reversed() const {
PIByteArray ret(*this);
return ret.reverse();
}
//! \~english Increases or decreases the size of the array by `add_size` elements.
//! \~russian Увеличивает или уменьшает размер массива на `add_size` элементов.
//! \~\details
//! \~english If `add_size > 0` then elements are added to the end of the array.
//! If `add_size < 0` then elements are removed from the end of the array.
//! If `add_size < 0` and there are fewer elements in the array than specified, then the array becomes empty.
//! \~russian Если `add_size > 0`, то в конец массива добавляются элементы.
//! Если `add_size < 0`, то с конца массива удаляются элементы.
//! Если `add_size < 0` и в массиве меньше элементов чем указано, то массив становится пустым.
//! \~\sa \a resize()
inline PIByteArray & enlarge(ssize_t add_size, uchar e = 0) {
d.enlarge(add_size, e);
return *this;
}
//! \~english Remove no more than one element equal `e`.
//! \~russian Удаляет первый элемент, который равен элементу `e`.
//! \~\details
//! \~\sa \a remove(), \a removeAll(), \a removeWhere()
inline PIByteArray & removeOne(uchar e) {
d.removeOne(e);
return *this;
}
//! \~english Remove all elements equal `e`.
//! \~russian Удаляет все элементы, равные элементу `e`.
//! \~\details
//! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIByteArray & removeAll(uchar e) {
d.removeAll(e);
return *this;
}
//! \~english Remove all elements in the array
//! passes the test implemented by the provided function `test`.
//! \~russian Удаляет все элементы, удовлетворяющие условию,
//! заданному в передаваемой функции `test`.
//! \~\details
//! \~\sa \a remove(), \a removeOne(), \a removeWhere()
inline PIByteArray & removeWhere(std::function<bool(uchar e)> test) {
d.removeWhere(test);
return *this;
}
//! \~english Appends the given element `e` to the end of the array.
//! \~russian Добавляет элемент `e` в конец массива.
//! \~\details
//! \~english If size() is less than capacity(), which is most often
//! then the addition will be very fast.
//! In any case, the addition is fast and does not depend on the size of the array.
//! If the new size() is greater than capacity()
//! then all iterators and references
//! (including the past-the-end iterator) are invalidated.
//! Otherwise only the past-the-end iterator is invalidated.
//! \~russian Если size() меньше capacity(), что часто бывает,
//! то добавление будет очень быстрым.
//! В любом случае добавление быстрое и не зависит от размера массива.
//! Если новый size() больше, чем capacity(),
//! то все итераторы и указатели становятся нерабочими.
//! В противном случае все, кроме итераторов, указывающих на конец массива,
//! остаются в рабочем состоянии.
//! \~\sa \a push_front(), \a append(), \a prepend(), \a insert()
inline PIByteArray & push_back(uchar e) {
d.push_back(e);
return *this;
}
//! \~english Appends the given elements to the end of the array.
//! \~russian Добавляет элементы в конец массива.
//! \~\details
//! \~english Overloaded function.
//! Appends the given elements from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Перегруженая функция.
//! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a push_back()
inline PIByteArray & push_back(std::initializer_list<uchar> init_list) {
d.push_back(init_list);
return *this;
}
//! \~english Appends the given array `v` to the end of the array.
//! \~russian Добавляет массив `v` в конец массива.
//! \~\details
//! \~english Overloaded function.
//! \~russian Перегруженая функция.
//! \~\sa \a push_back()
inline PIByteArray & push_back(const PIByteArray & v) {
d.push_back(v.d);
return *this;
}
//! \~english Add to the end data "data" with size "size"
//! \~russian Добавляет в конец массива данные по указателю "data" размером "size"
PIByteArray & push_back(const void * data_, int size_) {uint ps = size(); enlarge(size_); memcpy(data(ps), data_, size_); return *this;}
//! \~english Appends the given element `e` to the begin of the array.
//! \~russian Добавляет элемент `e` в начало массива.
//! \~\details
//! \~english If there is free space at the beginning of the array,
//! which is most often, then the addition will be very fast.
//! In any case, the addition is fast and does not depend on the size of the array.
//! If there is no free space at the beginning of the array
//! then all iterators and references
//! (including the past-the-begin iterator) are invalidated.
//! Otherwise only the past-the-begin iterator is invalidated.
//! \~russian Если в начале массива имеется свободное место,
//! что часто бывает, то добавление будет очень быстрым.
//! В любом случае добавление быстрое и не зависит от размера массива.
//! Если в начале массива нет свободного места,
//! то все итераторы и указатели становятся нерабочими.
//! В противном случае все, кроме итераторов указывающих, на начало массива,
//! остаются в рабочем состоянии.
//! \~\sa \a push_back(), \a append(), \a prepend(), \a insert()
inline PIByteArray & push_front(uchar e) {
d.push_front(e);
return *this;
}
//! \~english Appends the given array `v` to the begin of the array.
//! \~russian Добавляет массив `v` в начало массива.
//! \~\details
//! \~english Overloaded function.
//! \~russian Перегруженая функция.
//! \~\sa \a push_front()
inline PIByteArray & push_front(const PIByteArray & v) {
d.push_front(v.d);
return *this;
}
//! \~english Appends the given elements to the begin of the array.
//! \~russian Добавляет элементы в начало массива.
//! \~\details
//! \~english Overloaded function.
//! Appends the given elements from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Перегруженая функция.
//! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append()
inline PIByteArray & push_front(std::initializer_list<uchar> init_list) {
d.push_front(init_list);
return *this;
}
//! \~english Appends the given element `e` to the begin of the array.
//! \~russian Добавляет элемент `e` в начало массива.
//! \~\details
//! \~english If there is free space at the beginning of the array,
//! which is most often, then the addition will be very fast.
//! In any case, the addition is fast and does not depend on the size of the array.
//! If there is no free space at the beginning of the array
//! then all iterators and references
//! (including the past-the-begin iterator) are invalidated.
//! Otherwise only the past-the-begin iterator is invalidated.
//! \~russian Если в начале массива имеется свободное место,
//! что часто бывает, то добавление будет очень быстрым.
//! В любом случае добавление быстрое и не зависит от размера массива.
//! Если в начале массива нет свободного места,
//! то все итераторы и указатели становятся нерабочими.
//! В противном случае все, кроме итераторов указывающих, на начало массива,
//! остаются в рабочем состоянии.
//! \~\sa \a push_back(), \a append(), \a prepend(), \a insert()
inline PIByteArray & prepend(uchar e) {
d.prepend(e);
return *this;
}
//! \~english Appends the given array `v` to the begin of the array.
//! \~russian Добавляет массив `v` в начало массива.
//! \~\details
//! \~english Overloaded function.
//! \~russian Перегруженая функция.
//! \~\sa \a prepend()
inline PIByteArray & prepend(const PIByteArray & v) {
d.prepend(v.d);
return *this;
}
//! \~english Appends the given elements to the begin of the array.
//! \~russian Добавляет элементы в начало массива.
//! \~\details
//! \~english Overloaded function.
//! Appends the given elements from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Перегруженая функция.
//! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a append()
inline PIByteArray & prepend(std::initializer_list<uchar> init_list) {
d.prepend(init_list);
return *this;
}
//! \~english Remove one element from the end of the array.
//! \~russian Удаляет один элемент с конца массива.
//! \~\details
//! \~english Deleting an element from the end is very fast
//! and does not depend on the size of the array.
//! \~russian Удаление элемента с конца выполняется очень быстро
//! и не зависит от размера массива.
//! \~\sa \a pop_front(), \a take_back(), \a take_front()
inline PIByteArray & pop_back() {
d.pop_back();
return *this;
}
//! \~english Remove one element from the begining of the array.
//! \~russian Удаляет один элемент с начала массива.
//! \~\details
//! \~english Removing an element from the beginning takes longer than from the end.
//! This time is directly proportional to the size of the array.
//! All iterators and references are invalidated.
//! \~russian Удаление элемента с начала выполняется дольше, чем с конца.
//! Это время прямопропорционально размеру массива.
//! При удалении элемента все итераторы и указатели становятся нерабочими.
//! \~\sa \a pop_back(), \a take_back(), \a take_front()
inline PIByteArray & pop_front() {
d.pop_front();
return *this;
}
//! \~english Remove one element from the end of the array and return it.
//! \~russian Удаляет один элемент с начала массива и возвращает его.
//! \~\details
//! \~\sa \a take_front(), \a pop_back(), \a pop_front()
inline uchar take_back() {
return d.take_back();
}
//! \~english Remove one element from the begining of the array and return it.
//! \~russian Удаляет один элемент с конца массива и возвращает его.
//! \~\details
//! \~\sa \a take_front(), \a pop_back(), \a pop_front()
inline uchar take_front() {
return d.take_front();
}
//! \~english Returns a new array with all elements
//! that pass the test implemented by the provided function `test`.
//! \~russian Возвращает новый массив со всеми элементами,
//! прошедшими проверку, задаваемую в передаваемой функции `test`.
//! \~\details
//! \~\code
//! PIByteArray v{3, 2, 5, 2, 7};
//! PIByteArray v2 = v.filter([](const uchar & i){return i > 2;});
//! piCout << v2; // {3, 5, 7}
//! \endcode
//! \~\sa \a map(), \a any(), \a every()
inline PIByteArray filter(std::function<bool(const uchar & e)> test) const {
return PIByteArray(d.filter(test));
}
//! \~english Execute function `void f(const uchar & e)` for every element in array.
//! \~russian Выполняет функцию `void f(const uchar & e)` для каждого элемента массива.
//! \~\details
//! \~russian Не позволяет изменять элементы массива.
//! Для редактирования элементов используйте функцию вида `void f(uchar & e)`.
//! \~english Does not allow changing array elements.
//! To edit elements, use the function like `void f(T & e)`
//! \~\code
//! PIByteArray v{1, 2, 3, 4, 5};
//! int s = 0;
//! v.forEach([&s](const uchar & e){s += e;});
//! piCout << s; // 15
//! \endcode
//! \~\sa \a filter(), \a map(), \a reduce(), \a any(), \a every()
inline void forEach(std::function<void(const uchar & e)> f) const {
d.forEach(f);
}
//! \~english Execute function `void f(uchar & e)` for every element in array.
//! \~russian Выполняет функцию `void f(uchar & e)` для каждого элемента массива.
//! \~\details
//! \~english Overloaded function.
//! Allows you to change the elements of the array.
//! \~russian Перегруженая функция.
//! Позволяет изменять элементы массива.
//! \~\code
//! PIByteArray v{1, 2, 3, 4, 5};
//! v.forEach([](uchar & e){e++;});
//! piCout << v; // {2, 3, 4, 5, 6}
//! \endcode
//! \~\sa \a filter(), \a map(), \a reduce(), \a any(), \a every()
inline PIByteArray & forEach(std::function<void(uchar & e)> f) {
d.forEach(f);
return *this;
}
//! \~english Сreates a new array populated with the results
//! of calling a provided function `ST f(const uchar & e)` on every element in the calling array.
//! \~russian Создаёт новый массив с результатом вызова указанной функции
//! `ST f(const T & e)` для каждого элемента массива.
//! \~\details
//! \~english Calls a provided function`ST f(const uchar & e)`
//! once for each element in an array, in order,
//! and constructs a new array from the results.
//! \~russian Метод `map` вызывает переданную функцию `ST f(const uchar & e)`
//! один раз для каждого элемента в порядке их появления
//! и конструирует новый массив из результатов её вызова.
//! \~\code
//! PIByteArray v{0x31, 0x0A, 0xFF};
//! PIStringList sl = v.map<PIString>([](const uchar & i){return PIString::fromNumber(i, 16);});
//! piCout << sl; {"31", "A", "FF"}
//! \endcode
//! \~\sa \a forEach(), \a reduce()
template <typename ST>
inline PIDeque<ST> map(std::function<ST(const uchar & e)> f) const {
return d.map<ST>(f);
}
//! \~english Applies the function `ST f(const uchar & e, const ST & acc)`
//! to each element of the array (from left to right), returns one value.
//! \~russian Применяет функцию `ST f(const uchar & e, const ST & acc)`
//! к каждому элементу массива (слева-направо), возвращает одно значение.
//! \~\details
//! \~english The reduce() method performs the `f` function
//! once for each element in the array.
//! If the `initial` argument is passed when calling reduce(),
//! then when the function `f` is called for the first time,
//! the value of `acc` will be assigned to `initial`.
//! If the array is empty, the value `initial` will be returned.
//! \param f is a function like `ST f(const uchar & e, const ST & acc)`,
//! executed for each element of the array. It takes two arguments:
//! * **e** - current element of the array
//! * **acc** - accumulator accumulating the value
//! which this function returns after visiting the next element
//!
//! \param initial _optional_ Object used as the second argument
//! when the `f` function is first called.
//! \~russian Метод reduce() выполняет функцию `f`
//! один раз для каждого элемента, присутствующего в массиве.
//! Если при вызове reduce() передан аргумент `initial`,
//! то при первом вызове функции `f` значение `acc`
//! будет равным значению `initial`.
//! Если массив пустой то будет возвращено значение `initial`.
//! \param f Функция, вида `ST f(const uchar & e, const ST & acc)`,
//! выполняющаяся для каждого элемента массива.
//! Она принимает два аргумента:
//! * **e** - текущий элемент массива
//! * **acc** - аккумулятор, аккумулирующий значение
//! которое возвращает эта функция после посещения очередного элемента
//!
//! \param initial _опциональный_ Объект,
//! используемый в качестве второго аргумента при первом вызове функции `f`.
//!
//! \~\code
//! PIByteArray v{1, 2, 3, 4, 5};
//! PIString s = v.reduce<PIString>([](const uchar & e, const PIString & acc){return acc + PIString::fromNumber(e);});
//! piCout << s; // "12345"
//! \endcode
//! \~\sa \a forEach(), \a map()
template <typename ST>
inline ST reduce(std::function<ST(const uchar & e, const ST & acc)> f, const ST & initial = ST()) const {
return d.reduce<ST>(f, initial);
}
//! \~english Convert data to Base 64 and return this byte array
//! \~russian Преобразует данные в Base 64 и возвращает текущий массив
PIByteArray & convertToBase64();
//! \~english Convert data from Base 64 and return this byte array
//! \~russian Преобразует данные из Base 64 и возвращает текущий массив
PIByteArray & convertFromBase64();
//! \~english Return converted to Base 64 data
//! \~russian Возвращает копию байтового массива, преобразованного в Base 64
PIByteArray toBase64() const;
PIByteArray & compressRLE(uchar threshold = 192);
PIByteArray & decompressRLE(uchar threshold = 192);
PIByteArray compressedRLE(uchar threshold = 192) {PIByteArray ba(*this); ba.compressRLE(threshold); return ba;}
PIByteArray decompressedRLE(uchar threshold = 192) {PIByteArray ba(*this); ba.decompressRLE(threshold); return ba;}
//! \~english Return string representation of data, each byte in "base" base, separated by spaces
//! \~russian Возвращает текстовое представление байтового массива, каждый байт в "base" системе, с пробелами
PIString toString(int base = 16) const;
//! \~english
//! Returns a hex encoded copy of the byte array, without spaces.
//! The hex encoding uses the numbers 0-9 and the letters a-f.
//! \~russian
//! Возвращает шестнадцатеричное представление массива, без пробелов.
//! Оно использует цифры 0-9 и буквы a-f.
PIString toHex() const;
//! \~english Add to the end data "data" with size "size"
//! \~russian Добавляет в конец массива данные по указателю "data" размером "size"
PIByteArray & append(const void * data_, int size_) {uint ps = size(); enlarge(size_); memcpy(data(ps), data_, size_); return *this;}
//! \~english Add to the end byte array "data"
//! \~russian Добавляет в конец массива содержимое массива "data"
PIByteArray & append(const PIByteArray & data_) {uint ps = size(); enlarge(data_.size_s()); memcpy(data(ps), data_.data(), data_.size()); return *this;}
//! \~english Add to the end "t"
//! \~russian Добавляет в конец массива байт "t"
PIByteArray & append(uchar t) {push_back(t); return *this;}
//! \~english Appends the given elements to the end of the array.
//! \~russian Добавляет элементы в конец массива.
//! \~\details
//! \~english Overloaded function.
//! Appends the given elements from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
//! \~russian Перегруженая функция.
//! Добавляет элементы из
//! [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
//! \~\sa \a push_back()
inline PIByteArray & append(std::initializer_list<uchar> init_list) {
d.append(init_list);
return *this;
}
//! \~english Returns 8-bit checksum
//! \~russian Возвращает 8-битную контрольную сумму
uchar checksumPlain8(bool inverse = true) const;
//! \~english Returns 32-bit checksum
//! \~russian Возвращает 32-битную контрольную сумму
uint checksumPlain32(bool inverse = true) const;
//! \~english Returns 8-bit checksum CRC-8
//! \~russian Возвращает 8-битную контрольную сумму CRC-8
uchar checksumCRC8() const;
//! \~english Returns 16-bit checksum CRC-16
//! \~russian Возвращает 16-битную контрольную сумму CRC-16
ushort checksumCRC16() const;
//! \~english Returns 32-bit checksum CRC-32
//! \~russian Возвращает 32-битную контрольную сумму CRC-32
uint checksumCRC32() const;
//! \~english Returns hash of content
//! \~russian Возвращает хэш содержимого
uint hash() const;
void operator =(const PIDeque<uchar> & o) {resize(o.size()); memcpy(data(), o.data(), o.size());}
PIByteArray & operator =(const PIByteArray & o) {if (this == &o) return *this; clear(); append(o); return *this;}
PIByteArray & operator =(PIByteArray && o) {swap(o); return *this;}
static PIByteArray fromUserInput(PIString str);
static PIByteArray fromHex(PIString str);
//! \~english Return converted from Base 64 data
//! \~russian Возвращает массив из Base 64 представления
static PIByteArray fromBase64(const PIByteArray & base64);
static PIByteArray fromBase64(const PIString & base64);
bool binaryStreamAppendImp(const void * d_, size_t s) {
append(d_, s);
return true;
}
bool binaryStreamTakeImp(void * d_, size_t s) {
size_t rs = size();
if (rs > s) rs = s;
memcpy(d_, data(), rs);
remove(0, rs);
return rs == s;
}
ssize_t binaryStreamSizeImp() const {return size();}
private:
PIDeque<uchar> d;
};
//! \relatesalso PIByteArray
//! \~english Byte arrays compare operator
//! \~russian Оператор сравнения
inline bool operator <(const PIByteArray & v0, const PIByteArray & v1) {
if (v0.size() == v1.size()) {
if (v0.isEmpty()) return false;
return memcmp(v0.data(), v1.data(), v0.size()) < 0;
}
return v0.size() < v1.size();
}
//! \relatesalso PIByteArray
//! \~english Byte arrays compare operator
//! \~russian Оператор сравнения
inline bool operator >(const PIByteArray & v0, const PIByteArray & v1) {
if (v0.size() == v1.size()) {
if (v0.isEmpty()) return false;
return memcmp(v0.data(), v1.data(), v0.size()) > 0;
}
return v0.size() > v1.size();
}
//! \relatesalso PIByteArray
//! \~english Byte arrays compare operator
//! \~russian Оператор сравнения
inline bool operator ==(const PIByteArray & v0, const PIByteArray & v1) {
if (v0.size() == v1.size()) {
if (v0.isEmpty()) return true;
return memcmp(v0.data(), v1.data(), v0.size()) == 0;
}
return false;
}
//! \relatesalso PIByteArray
//! \~english Byte arrays compare operator
//! \~russian Оператор сравнения
inline bool operator !=(const PIByteArray & v0, const PIByteArray & v1) {
if (v0.size() == v1.size()) {
if (v0.isEmpty()) return false;
return memcmp(v0.data(), v1.data(), v0.size()) != 0;
}
return true;
}
#ifdef PIP_STD_IOSTREAM
//! \relatesalso PIByteArray \brief Output to std::ostream operator
inline std::ostream & operator <<(std::ostream & s, const PIByteArray & ba);
#endif
//! \relatesalso PIByteArray
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
PIP_EXPORT PICout operator <<(PICout s, const PIByteArray & ba);
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIByteArray) {
s.binaryStreamAppend((int)v.size_s());
s.binaryStreamAppend(v.data(), v.size());
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ(PIByteArray) {
v.resize(s.binaryStreamTakeInt());
s.binaryStreamTake(v.data(), v.size());
return s;
}
//! \relatesalso PIByteArray
//! \~english Returns PIByteArray::hash() of "ba"
//! \~russian Возвращает PIByteArray::hash() от "ba"
template<> inline uint piHash(const PIByteArray & ba) {return ba.hash();}
//! \relatesalso PIByteArray
//! \~english Swap contents betwee "f" and "s"
//! \~russian Меняет содержимое массивов "f" и "s"
template<> inline void piSwap(PIByteArray & f, PIByteArray & s) {f.swap(s);}
//! \relatesalso PIByteArray
//! \~english Store "value" to bytearray and returns it
//! \~russian Сохраняет "value" в байтовый массив и возвращает его
template <typename T> PIByteArray piSerialize(const T & value) {
PIByteArray ret;
ret << value;
return ret;
}
//! \relatesalso PIByteArray
//! \~english Restore type "T" from bytearray "data" and returns it
//! \~russian Извлекает тип "T" из байтового массива "data" и возвращает его
template <typename T> T piDeserialize(const PIByteArray & data) {
T ret;
if (!data.isEmpty()) {
PIByteArray ba(data);
ba >> ret;
}
return ret;
}
#endif // PIBYTEARRAY_H

View File

@@ -0,0 +1,350 @@
/*
PIP - Platform Independent Primitives
Timer
Ivan Pelipenko peri4ko@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 "piincludes_p.h"
#include "pidatetime.h"
#include <ctime>
#ifdef ARDUINO
# include <Arduino.h>
#endif
#ifdef MAC_OS
//# include <mach/mach_traps.h>
//# include <mach/mach.h>
# include <mach/clock.h>
//# include <crt_externs.h>
#endif
#ifdef MICRO_PIP
# include <sys/time.h>
#endif
//! \addtogroup Types
//! \{
//!
//! \~\class PITime pidatetime.h
//!
//! \~\class PIDate pidatetime.h
//!
//! \~\class PIDateTime pidatetime.h
//!
//! \}
bool operator ==(const PITime & t0, const PITime & t1) {
return (t0.hours == t1.hours && t0.minutes == t1.minutes && t0.seconds == t1.seconds);
}
bool operator <(const PITime & t0, const PITime & t1) {
if (t0.hours == t1.hours) {
if (t0.minutes == t1.minutes) {
return t0.seconds < t1.seconds;
} else return t0.minutes < t1.minutes;
} else return t0.hours < t1.hours;
}
bool operator >(const PITime & t0, const PITime & t1) {
if (t0.hours == t1.hours) {
if (t0.minutes == t1.minutes) {
return t0.seconds > t1.seconds;
} else return t0.minutes > t1.minutes;
} else return t0.hours > t1.hours;
}
bool operator ==(const PIDate & t0, const PIDate & t1) {
return (t0.year == t1.year && t0.month == t1.month && t0.day == t1.day);
}
bool operator <(const PIDate & t0, const PIDate & t1) {
if (t0.year == t1.year) {
if (t0.month == t1.month) {
return t0.day < t1.day;
} else return t0.month < t1.month;
} else return t0.year < t1.year;
}
bool operator >(const PIDate & t0, const PIDate & t1) {
if (t0.year == t1.year) {
if (t0.month == t1.month) {
return t0.day > t1.day;
} else return t0.month > t1.month;
} else return t0.year > t1.year;
}
bool operator ==(const PIDateTime & t0, const PIDateTime & t1) {
return (t0.year == t1.year && t0.month == t1.month && t0.day == t1.day &&
t0.hours == t1.hours && t0.minutes == t1.minutes && t0.seconds == t1.seconds);
}
bool operator <(const PIDateTime & t0, const PIDateTime & t1) {
if (t0.year == t1.year) {
if (t0.month == t1.month) {
if (t0.day == t1.day) {
if (t0.hours == t1.hours) {
if (t0.minutes == t1.minutes) {
return t0.seconds < t1.seconds;
} else return t0.minutes < t1.minutes;
} else return t0.hours < t1.hours;
} else return t0.day < t1.day;
} else return t0.month < t1.month;
} else return t0.year < t1.year;
}
bool operator >(const PIDateTime & t0, const PIDateTime & t1) {
if (t0.year == t1.year) {
if (t0.month == t1.month) {
if (t0.day == t1.day) {
if (t0.hours == t1.hours) {
if (t0.minutes == t1.minutes) {
return t0.seconds > t1.seconds;
} else return t0.minutes > t1.minutes;
} else return t0.hours > t1.hours;
} else return t0.day > t1.day;
} else return t0.month > t1.month;
} else return t0.year > t1.year;
}
PISystemTime PITime::toSystemTime() const {
return PISystemTime((hours * 60. + minutes) * 60. + seconds, milliseconds * 1000.);
}
PITime PITime::current() {
time_t rt = ::time(0);
tm * pt = localtime(&rt);
PITime t;
t.seconds = pt->tm_sec;
t.minutes = pt->tm_min;
t.hours = pt->tm_hour;
return t;
}
PITime PITime::fromSystemTime(const PISystemTime & st) {
double s = st.toSeconds();
int v = s;
PITime ret;
ret.milliseconds = (s - v) * 1000;
ret.seconds = v % 60; v = (v - ret.seconds) / 60;
ret.minutes = v % 60; v = (v - ret.minutes) / 60;
ret.hours = v;
return ret;
}
PIDate PIDate::current() {
time_t rt = ::time(0);
tm * pt = localtime(&rt);
PIDate d;
d.day = pt->tm_mday;
d.month = pt->tm_mon + 1;
d.year = pt->tm_year + 1900;
return d;
}
PIDateTime PIDateTime::current() {
time_t rt = ::time(0);
tm * pt = localtime(&rt);
PIDateTime dt;
dt.milliseconds = 0;
dt.seconds = pt->tm_sec;
dt.minutes = pt->tm_min;
dt.hours = pt->tm_hour;
dt.day = pt->tm_mday;
dt.month = pt->tm_mon + 1;
dt.year = pt->tm_year + 1900;
return dt;
}
PIString PITime::toString(const PIString & format) const {
PIString ts = format;
ts.replace("hh", PIString::fromNumber(hours).expandLeftTo(2, '0'));
ts.replace("h", PIString::fromNumber(hours));
ts.replace("mm", PIString::fromNumber(minutes).expandLeftTo(2, '0'));
ts.replace("m", PIString::fromNumber(minutes));
ts.replace("ss", PIString::fromNumber(seconds).expandLeftTo(2, '0'));
ts.replace("s", PIString::fromNumber(seconds));
ts.replace("zzz", PIString::fromNumber(milliseconds).expandLeftTo(3, '0'));
ts.replace("zz", PIString::fromNumber(milliseconds).expandLeftTo(2, '0'));
ts.replace("z", PIString::fromNumber(milliseconds));
return ts;
}
PIString PIDate::toString(const PIString & format) const {
PIString ts = format;
ts.replace("yyyy", PIString::fromNumber(year).expandLeftTo(4, '0'));
ts.replace("yy", PIString::fromNumber(year).right(2));
ts.replace("y", PIString::fromNumber(year).right(1));
ts.replace("MM", PIString::fromNumber(month).expandLeftTo(2, '0'));
ts.replace("M", PIString::fromNumber(month));
ts.replace("dd", PIString::fromNumber(day).expandLeftTo(2, '0'));
ts.replace("d", PIString::fromNumber(day));
return ts;
}
PIString PIDateTime::toString(const PIString & format) const {
PIString ts = format;
ts.replace("yyyy", PIString::fromNumber(year).expandLeftTo(4, '0'));
ts.replace("yy", PIString::fromNumber(year).right(2));
ts.replace("y", PIString::fromNumber(year).right(1));
ts.replace("MM", PIString::fromNumber(month).expandLeftTo(2, '0'));
ts.replace("M", PIString::fromNumber(month));
ts.replace("dd", PIString::fromNumber(day).expandLeftTo(2, '0'));
ts.replace("d", PIString::fromNumber(day));
ts.replace("hh", PIString::fromNumber(hours).expandLeftTo(2, '0'));
ts.replace("h", PIString::fromNumber(hours));
ts.replace("mm", PIString::fromNumber(minutes).expandLeftTo(2, '0'));
ts.replace("m", PIString::fromNumber(minutes));
ts.replace("ss", PIString::fromNumber(seconds).expandLeftTo(2, '0'));
ts.replace("s", PIString::fromNumber(seconds));
ts.replace("zzz", PIString::fromNumber(milliseconds).expandLeftTo(3, '0'));
ts.replace("zz", PIString::fromNumber(milliseconds).expandLeftTo(2, '0'));
ts.replace("z", PIString::fromNumber(milliseconds));
return ts;
}
time_t PIDateTime::toSecondSinceEpoch() const {
tm pt;
memset(&pt, 0, sizeof(pt));
pt.tm_sec = seconds;
pt.tm_min = minutes;
pt.tm_hour = hours;
pt.tm_mday = day;
pt.tm_mon = month - 1;
#ifdef WINDOWS
pt.tm_year = piMaxi(year - 1900, 70);
#else
pt.tm_year = piMaxi(year - 1900, 0);
#endif
return mktime(&pt);
}
PIDateTime PIDateTime::fromSecondSinceEpoch(const time_t sec) {
tm * pt = localtime(&sec);
PIDateTime dt;
dt.seconds = pt->tm_sec;
dt.minutes = pt->tm_min;
dt.hours = pt->tm_hour;
dt.day = pt->tm_mday;
dt.month = pt->tm_mon + 1;
dt.year = pt->tm_year + 1900;
return dt;
}
PIString time2string(const PITime & time, const PIString & format) {
PIString ts = format;
ts.replace("hh", PIString::fromNumber(time.hours).expandLeftTo(2, '0'));
ts.replace("h", PIString::fromNumber(time.hours));
ts.replace("mm", PIString::fromNumber(time.minutes).expandLeftTo(2, '0'));
ts.replace("m", PIString::fromNumber(time.minutes));
ts.replace("ss", PIString::fromNumber(time.seconds).expandLeftTo(2, '0'));
ts.replace("s", PIString::fromNumber(time.seconds));
return ts;
}
PIString date2string(const PIDate & date, const PIString & format) {
PIString ts = format;
ts.replace("yyyy", PIString::fromNumber(date.year).expandLeftTo(4, '0'));
ts.replace("yy", PIString::fromNumber(date.year).right(2));
ts.replace("y", PIString::fromNumber(date.year).right(1));
ts.replace("MM", PIString::fromNumber(date.month).expandLeftTo(2, '0'));
ts.replace("M", PIString::fromNumber(date.month));
ts.replace("dd", PIString::fromNumber(date.day).expandLeftTo(2, '0'));
ts.replace("d", PIString::fromNumber(date.day));
return ts;
}
PIString datetime2string(const PIDateTime & date, const PIString & format) {
PIString ts = format;
ts.replace("hh", PIString::fromNumber(date.hours).expandLeftTo(2, '0'));
ts.replace("h", PIString::fromNumber(date.hours));
ts.replace("mm", PIString::fromNumber(date.minutes).expandLeftTo(2, '0'));
ts.replace("m", PIString::fromNumber(date.minutes));
ts.replace("ss", PIString::fromNumber(date.seconds).expandLeftTo(2, '0'));
ts.replace("s", PIString::fromNumber(date.seconds));
ts.replace("yyyy", PIString::fromNumber(date.year).expandLeftTo(4, '0'));
ts.replace("yy", PIString::fromNumber(date.year).right(2));
ts.replace("y", PIString::fromNumber(date.year).right(1));
ts.replace("MM", PIString::fromNumber(date.month).expandLeftTo(2, '0'));
ts.replace("M", PIString::fromNumber(date.month));
ts.replace("dd", PIString::fromNumber(date.day).expandLeftTo(2, '0'));
ts.replace("d", PIString::fromNumber(date.day));
return ts;
}
PICout operator <<(PICout s, const PITime & v) {
s.space();
s.saveAndSetControls(0);
s << "PITime(" << v.hours << ":";
s << PIString::fromNumber(v.minutes).expandLeftTo(2, '0') << ":";
s << PIString::fromNumber(v.seconds).expandLeftTo(2, '0') << ":";
s << PIString::fromNumber(v.milliseconds).expandLeftTo(3, '0') << ")";
s.restoreControls();
return s;
}
PICout operator <<(PICout s, const PIDate & v) {
s.space();
s.saveAndSetControls(0);
s << "PIDate(" << v.day << "-";
s << PIString::fromNumber(v.month).expandLeftTo(2, '0') << "-";
s << v.year << ")";
s.restoreControls();
return s;
}
PICout operator <<(PICout s, const PIDateTime & v) {
s.space();
s.saveAndSetControls(0);
s << "PIDateTime(";
s << v.day << "-";
s << PIString::fromNumber(v.month).expandLeftTo(2, '0') << "-";
s << v.year << " ";
s << v.hours << ":";
s << PIString::fromNumber(v.minutes).expandLeftTo(2, '0') << ":";
s << PIString::fromNumber(v.seconds).expandLeftTo(2, '0') << ":";
s << PIString::fromNumber(v.milliseconds).expandLeftTo(3, '0') << ")";
s.restoreControls();
return s;
}

View File

@@ -0,0 +1,320 @@
/*! \file pidatetime.h
* \ingroup Types
* \~\brief
* \~english Time and date structs
* \~russian Типы времени и даты
*/
/*
PIP - Platform Independent Primitives
Time and date structs
Ivan Pelipenko peri4ko@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/>.
*/
#ifndef PIDATETIME_H
#define PIDATETIME_H
#include "pisystemtime.h"
//! \ingroup Types
//! \~\brief
//! \~english Calendar time.
//! \~russian Календарное время.
class PIP_EXPORT PITime {
public:
//! \~english Construct %PITime from hours, minutes, seconds and milliseconds
//! \~russian Создает %PITime из часов, минут, секунд и миллисекунд
PITime(int hours_ = 0, int minutes_ = 0, int seconds_ = 0, int milliseconds_ = 0): hours(hours_), minutes(minutes_), seconds(seconds_), milliseconds(milliseconds_) {;}
//! \~english Returns string representation
//! \~russian Возвращает строковое представление
PIString toString(const PIString & format = "h:mm:ss") const;
//! \~english Returns time as %PISystemTime
//! \~russian Возвращает время как %PISystemTime
PISystemTime toSystemTime() const;
//! \~english Returns current time
//! \~russian Возвращает текущее время
static PITime current();
//! \~english Construct %PITime from %PISystemTime
//! \~russian Создает %PITime из %PISystemTime
static PITime fromSystemTime(const PISystemTime & st);
//! \~english Hour, 0-23
//! \~russian Час, 0-23
int hours;
//! \~english Minutes, 0-59
//! \~russian Минуты, 0-59
int minutes;
//! \~english Seconds, 0-59
//! \~russian Секунды, 0-59
int seconds;
//! \~english Milliseconds, 0-999
//! \~russian Миллисекунды, 0-999
int milliseconds;
};
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator ==(const PITime & t0, const PITime & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator <(const PITime & t0, const PITime & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator >(const PITime & t0, const PITime & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator !=(const PITime & t0, const PITime & t1) {return !(t0 == t1);}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator <=(const PITime & t0, const PITime & t1) {return !(t0 > t1);}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator >=(const PITime & t0, const PITime & t1) {return !(t0 < t1);}
//! \relatesalso PICout
//! \~english \brief Output operator to PICout
//! \~russian \brief Оператор вывода в PICout
PIP_EXPORT PICout operator <<(PICout s, const PITime & v);
//! \ingroup Types
//! \~\brief
//! \~english Calendar date.
//! \~russian Календарная дата.
class PIP_EXPORT PIDate {
public:
//! \~english Construct %PIDate from year, month and day
//! \~russian Создает %PIDate из года, месяца и дня
PIDate(int year_ = 0, int month_ = 0, int day_ = 0): year(year_), month(month_), day(day_) {;}
//! \~english Returns string representation
//! \~russian Возвращает строковое представление
PIString toString(const PIString & format = "d.MM.yyyy") const;
//! \~english Returns current date
//! \~russian Возвращает текущую дату
static PIDate current();
//! \~english Year
//! \~russian Год
int year;
//! \~english Month, 1-12
//! \~russian Месяц, 1-12
int month;
//! \~english Day, 1-31
//! \~russian День, 1-31
int day;
};
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator ==(const PIDate & t0, const PIDate & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator <(const PIDate & t0, const PIDate & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator >(const PIDate & t0, const PIDate & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator !=(const PIDate & t0, const PIDate & t1) {return !(t0 == t1);}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator <=(const PIDate & t0, const PIDate & t1) {return !(t0 > t1);}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator >=(const PIDate & t0, const PIDate & t1) {return !(t0 < t1);}
//! \relatesalso PICout
//! \~english \brief Output operator to PICout
//! \~russian \brief Оператор вывода в PICout
PIP_EXPORT PICout operator <<(PICout s, const PIDate & v);
//! \ingroup Types
//! \~\brief
//! \~english Calendar date and time.
//! \~russian Календарное дата и время.
class PIP_EXPORT PIDateTime {
public:
//! \~english Construct null %PIDateTime
//! \~russian Создает нулевой %PIDateTime
PIDateTime() {year = month = day = hours = minutes = seconds = milliseconds = 0;}
//! \~english Construct %PIDateTime from %PITime and null %PIDate
//! \~russian Создает %PIDateTime из %PITime и нулевого %PIDate
PIDateTime(const PITime & time) {year = month = day = 0; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
//! \~english Construct %PIDateTime from %PIDate and null %PITime
//! \~russian Создает %PIDateTime из %PIDate и нулевого %PITime
PIDateTime(const PIDate & date) {year = date.year; month = date.month; day = date.day; hours = minutes = seconds = milliseconds = 0;}
//! \~english Construct %PIDateTime from %PIDate and %PITime
//! \~russian Создает %PIDateTime из %PIDate и %PITime
PIDateTime(const PIDate & date, const PITime & time) {year = date.year; month = date.month; day = date.day; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
//! \~english Returns normalized %PIDateTime
//! \~russian Возвращает нормализованный %PIDateTime
PIDateTime normalized() const {return PIDateTime::fromSecondSinceEpoch(toSecondSinceEpoch());}
//! \~english Normalize all fields
//! \~russian Нормализует все поля
void normalize() {*this = normalized();}
//! \~english Returns string representation
//! \~russian Возвращает строковое представление
PIString toString(const PIString & format = "h:mm:ss d.MM.yyyy") const;
//! \~english Returns seconds since 1 Jan 1970
//! \~russian Возвращает секунды от 1 Янв 1970
time_t toSecondSinceEpoch() const;
//! \~english Returns time as %PISystemTime
//! \~russian Возвращает время как %PISystemTime
PISystemTime toSystemTime() const {return PISystemTime(int(toSecondSinceEpoch()), milliseconds * 1000000);}
//! \~english Returns date part
//! \~russian Возвращает дату
PIDate date() const {return PIDate(year, month, day);}
//! \~english Returns time part
//! \~russian Возвращает время
PITime time() const {return PITime(hours, minutes, seconds, milliseconds);}
//! \~english Set date part
//! \~russian Устанавливает дату
void setDate(const PIDate & d) {year = d.year; month = d.month; day = d.day;}
//! \~english Set time part
//! \~russian Устанавливает время
void setTime(const PITime & t) {hours = t.hours; minutes = t.minutes; seconds = t.seconds; milliseconds = t.milliseconds;}
//! \~english Sum operator
//! \~russian Оператор сложения
void operator +=(const PIDateTime & d1) {year += d1.year; month += d1.month; day += d1.day; hours += d1.hours; minutes += d1.minutes; seconds += d1.seconds; normalize();}
//! \~english Subtract operator
//! \~russian Оператор вычитания
void operator -=(const PIDateTime & d1) {year -= d1.year; month -= d1.month; day -= d1.day; hours -= d1.hours; minutes -= d1.minutes; seconds -= d1.seconds; normalize();}
//! \~english Construct %PIDateTime from seconds since 1 Jan 1970
//! \~russian Создает %PIDateTime из секунд от 1 Янв 1970
static PIDateTime fromSecondSinceEpoch(const time_t sec);
//! \~english Construct %PIDateTime from %PISystemTime
//! \~russian Создает %PIDateTime из %PISystemTime
static PIDateTime fromSystemTime(const PISystemTime & st) {PIDateTime dt = fromSecondSinceEpoch(st.seconds); dt.milliseconds = piClampi(st.nanoseconds / 1000000, 0, 999); return dt;}
//! \~english Returns current date and time
//! \~russian Возвращает текущую дату и время
static PIDateTime current();
//! \~english Year
//! \~russian Год
int year;
//! \~english Month, 1-12
//! \~russian Месяц, 1-12
int month;
//! \~english Day, 1-31
//! \~russian День, 1-31
int day;
//! \~english Hour, 0-23
//! \~russian Час, 0-23
int hours;
//! \~english Minutes, 0-59
//! \~russian Минуты, 0-59
int minutes;
//! \~english Seconds, 0-59
//! \~russian Секунды, 0-59
int seconds;
//! \~english Milliseconds, 0-999
//! \~russian Миллисекунды, 0-999
int milliseconds;
};
//! \~english Sum operator
//! \~russian Оператор сложения
inline PIDateTime operator +(const PIDateTime & d0, const PIDateTime & d1) {PIDateTime td = d0; td += d1; return td.normalized();}
//! \~english Subtract operator
//! \~russian Оператор вычитания
inline PIDateTime operator -(const PIDateTime & d0, const PIDateTime & d1) {PIDateTime td = d0; td -= d1; return td.normalized();}
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator ==(const PIDateTime & t0, const PIDateTime & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator <(const PIDateTime & t0, const PIDateTime & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
PIP_EXPORT bool operator >(const PIDateTime & t0, const PIDateTime & t1);
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator !=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 == t1);}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator <=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 > t1);}
//! \~english Compare operator
//! \~russian Оператор сравнения
inline bool operator >=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 < t1);}
//! \relatesalso PICout
//! \~english \brief Output operator to PICout
//! \~russian \brief Оператор вывода в PICout
PIP_EXPORT PICout operator <<(PICout s, const PIDateTime & v);
#endif // PIDATETIME_H

247
libs/main/types/piflags.h Normal file
View File

@@ -0,0 +1,247 @@
/*! \file piflags.h
* \ingroup Types
* \~\brief
* \~english General flags class
* \~russian Универсальные флаги
*/
/*
PIP - Platform Independent Primitives
General flags class
Ivan Pelipenko peri4ko@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/>.
*/
#ifndef PIFLAGS_H
#define PIFLAGS_H
#include "pip_export.h"
//! \addtogroup Types
//! \{
//! \~\class PIFlags piflags.h
//! \~\brief
//! \~english Container for bit flags
//! \~russian Контейнер для битовых полей (флагов)
//!
//! \~\details
//! \~english
//! %PIFlags is wrapper around \c "int". One can use it as native \c "int".
//! There are manybit-wise operators,
//! native conversion to "int" and function
//! to test flag. \n Example:
//!
//! \~russian
//! %PIFlags по сути обертка вокруг \c "int". Можно использовать его как обычный \c "int".
//! Имеет много битовых операторов,
//! неявное преобразование в "int" и методы для проверки
//! флагов. \n Пример:
//!
//! \~\snippet piincludes.cpp flags
//!
//! \}
template<typename Enum>
class PIFlags {
public:
//! \~english Constructor with flags = 0
//! \~russian Создает нулевые флаги
PIFlags(): flags(0) {;}
//! \~english Constructor with flags = Enum "e"
//! \~russian Создает флаги со значением = Enum "e"
PIFlags(Enum e): flags(e) {;}
//! \~english Constructor with flags = int "i"
//! \~russian Создает флаги со значением = int "i"
PIFlags(const int i): flags(i) {;}
//! \~english Set flags on positions "f" to value "on"
//! \~russian Устанавливает флаги по позициям "f" в "on"
PIFlags & setFlag(const PIFlags & f, bool on = true) {if (on) flags |= f.flags; else flags &= ~f.flags; return *this;}
//! \~english Set flag "e" to value "on"
//! \~russian Устанавливает флаг "e" в "on"
PIFlags & setFlag(const Enum & e, bool on = true) {if (on) flags |= e; else flags &= ~e; return *this;}
//! \~english Set flag "i" to value "on"
//! \~russian Устанавливает флаг "i" в "on"
PIFlags & setFlag(const int & i, bool on = true) {if (on) flags |= i; else flags &= ~i; return *this;}
//! \~english Assign operator
//! \~russian Оператор присваивания
void operator =(const Enum & e) {flags = e;}
//! \~english Assign operator
//! \~russian Оператор присваивания
void operator =(const int & i) {flags = i;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator ==(const PIFlags & f) {return flags == f.flags;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator ==(const Enum & e) {return flags == e;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator ==(const int i) {return flags == i;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator !=(const PIFlags & f) {return flags != f.flags;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator !=(const Enum & e) {return flags != e;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator !=(const int i) {return flags != i;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >(const PIFlags & f) {return flags > f.flags;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >(const Enum & e) {return flags > e;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >(const int i) {return flags > i;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <(const PIFlags & f) {return flags < f.flags;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <(const Enum & e) {return flags < e;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <(const int i) {return flags < i;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >=(const PIFlags & f) {return flags >= f.flags;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >=(const Enum & e) {return flags >= e;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >=(const int i) {return flags >= i;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <=(const PIFlags & f) {return flags <= f.flags;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <=(const Enum & e) {return flags <= e;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <=(const int i) {return flags <= i;}
//! \~english Bit-wise AND operator
//! \~russian Оператор побитового И
void operator &=(const PIFlags & f) {flags &= f.flags;}
//! \~english Bit-wise AND operator
//! \~russian Оператор побитового И
void operator &=(const Enum & e) {flags &= e;}
//! \~english Bit-wise AND operator
//! \~russian Оператор побитового И
void operator &=(const int i) {flags &= i;}
//! \~english Bit-wise OR operator
//! \~russian Оператор побитового ИЛИ
void operator |=(const PIFlags & f) {flags |= f.flags;}
//! \~english Bit-wise OR operator
//! \~russian Оператор побитового ИЛИ
void operator |=(const Enum & e) {flags |= e;}
//! \~english Bit-wise OR operator
//! \~russian Оператор побитового ИЛИ
void operator |=(const int i) {flags |= i;}
//! \~english Bit-wise XOR operator
//! \~russian Оператор побитового исключающего ИЛИ
void operator ^=(const PIFlags & f) {flags ^= f.flags;}
//! \~english Bit-wise XOR operator
//! \~russian Оператор побитового исключающего ИЛИ
void operator ^=(const Enum & e) {flags ^= e;}
//! \~english Bit-wise XOR operator
//! \~russian Оператор побитового исключающего ИЛИ
void operator ^=(const int i) {flags ^= i;}
//! \~english Bit-wise AND operator
//! \~russian Оператор побитового И
PIFlags operator &(PIFlags f) const {PIFlags tf(flags & f.flags); return tf;}
//! \~english Bit-wise AND operator
//! \~russian Оператор побитового И
PIFlags operator &(Enum e) const {PIFlags tf(flags & e); return tf;}
//! \~english Bit-wise AND operator
//! \~russian Оператор побитового И
PIFlags operator &(int i) const {PIFlags tf(flags & i); return tf;}
//! \~english Bit-wise OR operator
//! \~russian Оператор побитового ИЛИ
PIFlags operator |(PIFlags f) const {PIFlags tf(flags | f.flags); return tf;}
//! \~english Bit-wise OR operator
//! \~russian Оператор побитового ИЛИ
PIFlags operator |(Enum e) const {PIFlags tf(flags | e); return tf;}
//! \~english Bit-wise OR operator
//! \~russian Оператор побитового ИЛИ
PIFlags operator |(int i) const {PIFlags tf(flags | i); return tf;}
//! \~english Bit-wise XOR operator
//! \~russian Оператор побитового исключающего ИЛИ
PIFlags operator ^(PIFlags f) const {PIFlags tf(flags ^ f.flags); return tf;}
//! \~english Bit-wise XOR operator
//! \~russian Оператор побитового исключающего ИЛИ
PIFlags operator ^(Enum e) const {PIFlags tf(flags ^ e); return tf;}
//! \~english Bit-wise XOR operator
//! \~russian Оператор побитового исключающего ИЛИ
PIFlags operator ^(int i) const {PIFlags tf(flags ^ i); return tf;}
//! \~english Test flag operator
//! \~russian Оператор проверки флага
bool operator [](Enum e) const {return (flags & e) == e;}
//! \~english Implicity conversion to \c int
//! \~russian Оператор неявного преобразования в \c int
operator int() const {return flags;}
private:
int flags;
};
#endif // PIFLAGS_H

View File

@@ -0,0 +1,201 @@
/*
PIP - Platform Independent Primitives
Storage of properties for GUI usage
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 "pipropertystorage.h"
//! \~\class PIPropertyStorage pipropertystorage.h
//! \~\details
//! \~english \section PIPropertyStorage_sec0 Synopsis
//! \~russian \section PIPropertyStorage_sec0 Краткий обзор
//!
//! \~english
//! Key-value storage, based on PIVector with PIPropertyStorage::Property elements. Each element in vector
//! contains unique name. You can access property by name with \a propertyValueByName() or \a propertyByName().
//! You can add or replace property by \a addProperty(const Property&) or \a addProperty(const PIString&, const PIVariant&, const PIString&, int).
//!
//! \~russian
//! Хранилище свойств ключ-значние, основанный на PIVector с элементами PIPropertyStorage::Property.
//! Каждый элемент имеет уникальное имя. Доступ к свойствам через \a propertyValueByName() или \a propertyByName().
//! Добавление и перезапись свойств через \a addProperty(const Property&) или \a addProperty(const PIString&, const PIVariant&, const PIString&, int).
//!
//! \~english Example:
//! \~russian Пример:
//! \~\code{.cpp}
//! \endcode
//!
//! \~\class PIPropertyStorage::Property pipropertystorage.h
//! \~\details
//!
bool PIPropertyStorage::isPropertyExists(const PIString & _name) const {
for (uint i = 0; i < props.size(); ++i)
if (props[i].name == _name)
return true;
return false;
}
//! \details
//! \~english Returns "true" if new property added, else if update existing property return "false"
//! \~russian Возвращает истину если новое свойство добавлено, в случае обновления "ложь"
bool PIPropertyStorage::addProperty(const PIPropertyStorage::Property & p) {
for (uint i = 0; i < props.size(); ++i)
if (props[i].name == p.name) {
props[i] = p;
return false;
}
props << p;
return true;
}
bool PIPropertyStorage::addProperty(PIPropertyStorage::Property && p) {
for (uint i = 0; i < props.size(); ++i)
if (props[i].name == p.name) {
props[i] = std::move(p);
return false;
}
props << std::move(p);
return true;
}
//! \details
//! \~english Returns "true" if new property added, else if update existing property return "false"
//! \~russian Возвращает истину если новое свойство добавлено, в случае обновления "ложь"
bool PIPropertyStorage::addProperty(const PIString & _name, const PIVariant & _def_value, const PIString & _comment, int _flags) {
return addProperty(Property(_name, _comment, _def_value, _flags));
}
bool PIPropertyStorage::removeProperty(const PIString & _name) {
for (uint i = 0; i < props.size(); ++i) {
if (props[i].name == _name) {
props.remove(i);
return true;
}
}
return false;
}
//! \details
//! \~english "flag" checked as bitfield
//! \~russian "flag" проверяется как битовое поле
int PIPropertyStorage::removePropertiesByFlag(int flag) {
int ret = 0;
for (int i = 0; i < props.size_s(); ++i) {
if ((props[i].flags & flag) == flag) {
props.remove(i);
--i;
ret++;
}
}
return ret;
}
//! \details
//! \~english "flag_ignore" is bitfield to ignore property in merge process
//! \~russian "flag_ignore" - битовое поле для исключения свойств из процесса слияния
void PIPropertyStorage::updateProperties(const PIVector<PIPropertyStorage::Property> & properties_, int flag_ignore) {
PIMap<PIString, PIVariant> values;
piForeachC(Property & p, props)
if (((p.flags & flag_ignore) != flag_ignore) || (flag_ignore == 0))
values[p.name] = p.value;
props = properties_;
for (uint i = 0; i < props.size(); ++i) {
Property & p(props[i]);
if (values.contains(p.name)) {
PIVariant pv = values[p.name];
if (pv.type() == p.value.type())
p.value = pv;
}
}
}
PIPropertyStorage::Property PIPropertyStorage::propertyByName(const PIString & name) const {
piForeachC(Property & p, props)
if (p.name == name)
return p;
return Property();
}
PIVariant PIPropertyStorage::propertyValueByName(const PIString & name) const {
piForeachC(Property & p, props)
if (p.name == name)
return p.value;
return PIVariant();
}
bool PIPropertyStorage::setPropertyValue(const PIString & name, const PIVariant & value) {
for (uint i = 0; i < props.size(); ++i) {
if (props[i].name == name) {
props[i].value = value;
return true;
}
}
return false;
}
bool PIPropertyStorage::setPropertyComment(const PIString & name, const PIString & comment) {
for (uint i = 0; i < props.size(); ++i) {
if (props[i].name == name) {
props[i].comment = comment;
return true;
}
}
return false;
}
bool PIPropertyStorage::setPropertyFlags(const PIString & name, int flags) {
for (uint i = 0; i < props.size(); ++i) {
if (props[i].name == name) {
props[i].flags = flags;
return true;
}
}
return false;
}
PIPropertyStorage::Property & PIPropertyStorage::operator[](const PIString & name) {
piForeach (Property & p, props)
if (p.name == name)
return p;
addProperty(name, "");
return props.back();
}
const PIPropertyStorage::Property PIPropertyStorage::operator[](const PIString & name) const {
piForeachC (Property & p, props)
if (p.name == name)
return p;
return Property();
}

View File

@@ -0,0 +1,296 @@
/*! \file pipropertystorage.h
* \ingroup Types
* \~\brief
* \~english Properties array
* \~russian Массив свойств
*/
/*
PIP - Platform Independent Primitives
Storage of properties for GUI usage
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/>.
*/
#ifndef PIPROPERTYSTORAGE_H
#define PIPROPERTYSTORAGE_H
#include "pivariant.h"
//! \ingroup Types
//! \~\brief
//! \~english This class provides key-value properties storage.
//! \~russian Этот класс предоставляет ключ-значение хранение свойств.
class PIP_EXPORT PIPropertyStorage {
public:
//! \~english Contructs an empty %PIPropertyStorage
//! \~russian Создает пустой %PIPropertyStorage
PIPropertyStorage() {}
//! \ingroup Types
//! \~\brief
//! \~english PIPropertyStorage element.
//! \~russian Элемент PIPropertyStorage.
struct PIP_EXPORT Property {
//! \~english Contructs %PIPropertyStorage::Property with name "n", comment "c", value "v" and flags "f"
//! \~russian Создает %PIPropertyStorage::Property с именем "n", комментарием "c", значением "v" и флагами "f"
Property(const PIString & n = PIString(), const PIString & c = PIString(), const PIVariant & v = PIVariant(), int f = 0): name(n), comment(c), value(v), flags(f) {}
//! \~english Contructs copy of another %PIPropertyStorage::Property "o"
//! \~russian Создает копию %PIPropertyStorage::Property "o"
Property(const Property & o): name(o.name), comment(o.comment), value(o.value), flags(o.flags) {}
Property(Property && o) {swap(o);}
//! \~english Assign operator
//! \~russian Оператор присваивания
Property & operator =(const Property & v) {
name = v.name;
comment = v.comment;
value = v.value;
flags = v.flags;
return *this;
}
Property & operator =(Property && v) {swap(v); return *this;}
//! \~english Returns value as boolean
//! \~russian Возвращает значение как логическое
bool toBool() const {return value.toBool();}
//! \~english Returns value as integer
//! \~russian Возвращает значение как целое
int toInt() const {return value.toInt();}
//! \~english Returns value as float
//! \~russian Возвращает значение как float
float toFloat() const {return value.toFloat();}
//! \~english Returns value as double
//! \~russian Возвращает значение как double
double toDouble() const {return value.toDouble();}
//! \~english Returns value as string
//! \~russian Возвращает значение как строку
PIString toString() const {return value.toString();}
void swap(Property & o) {
name.swap(o.name);
comment.swap(o.comment);
value.swap(o.value);
piSwap(flags, o.flags);
}
//! \~english Property name (uniqueue for %PIPropertyStorage)
//! \~russian Имя свойства (уникальное для %PIPropertyStorage)
PIString name;
//! \~english Optional description of property
//! \~russian Опциональный комментарий свойства
PIString comment;
//! \~english Property value
//! \~russian Значение свойства
PIVariant value;
//! \~english Abstract flags which may be used for user needs
//! \~russian Абстрактные флаги, могут быть использованы для своих нужд
int flags;
};
//! \~english Contructs %PIPropertyStorage with "pl" properties
//! \~russian Создает %PIPropertyStorage со свойствами "pl"
PIPropertyStorage(const PIVector<Property> & pl) {props = pl;}
//! \~english Contructs %PIPropertyStorage from another "o"
//! \~russian Создает %PIPropertyStorage из другого "o"
PIPropertyStorage(PIVector<Property> && o): props(std::move(o)) {}
typedef PIVector<Property>::const_iterator const_iterator;
typedef PIVector<Property>::iterator iterator;
typedef Property value_type;
iterator begin() {return props.begin();}
const_iterator begin() const {return props.begin();}
iterator end() {return props.end();}
const_iterator end() const {return props.end();}
//! \~english Returns properties count
//! \~russian Возвращает количество свойств
int length() const {return props.length();}
//! \~english Returns properties count
//! \~russian Возвращает количество свойств
int size() const {return props.size();}
//! \~english Returns if no properties
//! \~russian Возвращает нет ли свойств
bool isEmpty() const {return props.isEmpty();}
//! \~english Returns if properties
//! \~russian Возвращает есть ли свойства
bool isNotEmpty() const {return props.isNotEmpty();}
//! \~english Returns first property
//! \~russian Возвращает первое свойство
Property & front() {return props.front();}
//! \~english Returns first property as const
//! \~russian Возвращает первое свойство как константу
const Property & front() const {return props.front();}
//! \~english Returns last property
//! \~russian Возвращает последнее свойство
Property & back() {return props.back();}
//! \~english Returns last property as const
//! \~russian Возвращает последнее свойство как константу
const Property & back() const {return props.back();}
//! \~english Remove property at index "i"
//! \~russian Удаляет свойство по индексу "i"
void removeAt(int i) {props.remove(i);}
//! \~english Remove all properties
//! \~russian Удаляет все свойства
void clear() {props.clear();}
//! \~english Returns copy of this %PIPropertyStorage
//! \~russian Возвращает копию этого %PIPropertyStorage
PIPropertyStorage copy() const {return PIPropertyStorage(*this);}
//! \~english Returns properties count
//! \~russian Возвращает количество свойств
int propertiesCount() const {return props.size();}
//! \~english Returns properties as PIVector
//! \~russian Возвращает свойства как PIVector
PIVector<Property> & properties() {return props;}
//! \~english Returns properties as const PIVector
//! \~russian Возвращает свойства как константный PIVector
const PIVector<Property> & properties() const {return props;}
const PIPropertyStorage & propertyStorage() const {return *this;}
//! \~english Returns if properties with name "name" exists
//! \~russian Возвращает присутствует ли свойство с именем "name"
bool isPropertyExists(const PIString & name) const;
//! \~english Remove all properties
//! \~russian Удаляет все свойства
void clearProperties() {props.clear();}
//! \~english Add property if name isn't present in storage, otherwrise update existing property with same name
//! \~russian Добавляет новое свойство, если его имени не было в контейнере, иначе обновляет существующее свойство с этим именем
bool addProperty(const Property & p);
bool addProperty(Property && p);
//! \~english Add property if name isn't present in storage, otherwrise update existing property with same name
//! \~russian Добавляет новое свойство, если его имени не было в контейнере, иначе обновляет существующее свойство с этим именем
bool addProperty(const PIString & _name, const PIVariant & _def_value, const PIString & _comment = PIString(), int _flags = 0);
//! \~english Remove property with name "name", returns if property removed
//! \~russian Удаляет свойство с именем "name", возвращает было ли оно удалено
bool removeProperty(const PIString & name);
//! \~english Remove all properties with flag "flag" set, returns removed properties count
//! \~russian Удаляет все свойства с флагом "flag", возвращает количество удаленных свойств
int removePropertiesByFlag(int flag);
//! \~english Merge other "properties" into this
//! \~russian Объединяет "properties" с текущим контейнером
void updateProperties(const PIVector<Property> & properties, int flag_ignore = 0);
//! \~english Returns property with name "name" or default-constructed %PIPropertyStorage::Property
//! \~russian Возвращает свойство с именем "name" или пустое %PIPropertyStorage::Property
Property propertyByName(const PIString & name) const;
//! \~english Returns property value with name "name" or invalid %PIVariant
//! \~russian Возвращает значение свойства с именем "name" или недействительный %PIVariant
PIVariant propertyValueByName(const PIString & name) const;
//! \~english Set value of property with name "name" to "value", returns if property exists
//! \~russian Устанавливает значение "value" свойству с именем "name", возвращает существует ли такое свойство
bool setPropertyValue(const PIString & name, const PIVariant & value);
//! \~english Set comment of property with name "name" to "comment", returns if property exists
//! \~russian Устанавливает комментарий "comment" свойству с именем "name", возвращает существует ли такое свойство
bool setPropertyComment(const PIString & name, const PIString & comment);
//! \~english Set flags of property with name "name" to "flags", returns if property exists
//! \~russian Устанавливает флаги "flags" свойству с именем "name", возвращает существует ли такое свойство
bool setPropertyFlags(const PIString & name, int flags);
//! \~english Add property "p"
//! \~russian Добавляет свойство "p"
PIPropertyStorage & operator <<(const PIPropertyStorage::Property & p) {props << p; return *this;}
//! \~english Add properties "p"
//! \~russian Добавляет свойства "p"
PIPropertyStorage & operator <<(const PIVector<Property> & p) {props << p; return *this;}
//! \~english Add properties "p"
//! \~russian Добавляет свойства "p"
PIPropertyStorage & operator <<(const PIPropertyStorage & p) {props << p.props; return *this;}
//! \~english Returns property with index "i"
//! \~russian Возвращает свойство по индексу "i"
Property & operator[](int i) {return props[i];}
//! \~english Returns property with index "i" as const
//! \~russian Возвращает свойство по индексу "i" как константу
const Property & operator[](int i) const {return props[i];}
//! \~english Returns property with name "name"
//! \~russian Возвращает свойство с именем "name"
Property & operator[](const PIString & name);
//! \~english Returns property with name "name" as const
//! \~russian Возвращает свойство с именем "name" как константу
const Property operator[](const PIString & name) const;
protected:
PIVector<Property> props;
};
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIPropertyStorage::Property) {s << v.name << v.value << v.comment << v.flags; return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIPropertyStorage::Property) {s >> v.name >> v.value >> v.comment >> v.flags; return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIPropertyStorage) {s << v.properties(); return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIPropertyStorage) {s >> v.properties(); return s;}
#endif // PIPROPERTYSTORAGE_H

View File

@@ -0,0 +1,219 @@
/*
PIP - Platform Independent Primitives
Unix time
Ivan Pelipenko peri4ko@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 "piincludes_p.h"
#include "pisystemtime.h"
#include "pitime.h"
#include <ctime>
#ifdef QNX
# include <time.h>
#endif
#ifndef MICRO_PIP
# include "pisystemtests.h"
#elif defined(ARDUINO)
# include <Arduino.h>
#endif
#ifdef WINDOWS
extern FILETIME __pi_ftjan1970;
long long __PIQueryPerformanceCounter() {LARGE_INTEGER li; QueryPerformanceCounter(&li); return li.QuadPart;}
#endif
#ifdef MAC_OS
//# include <mach/mach_traps.h>
//# include <mach/mach.h>
# include <mach/clock.h>
//# include <crt_externs.h>
extern clock_serv_t __pi_mac_clock;
#endif
#ifdef MICRO_PIP
# include <sys/time.h>
#endif
//! \class PISystemTime pisystemtime.h
//! \details
//! \~english \section PISystemTime_sec0 Synopsis
//! \~russian \section PISystemTime_sec0 Краткий обзор
//! \~english
//! This class provide arithmetic functions for POSIX system time.
//! This time represents as seconds and nanosecons in integer formats.
//! You can take current system time with function \a PISystemTime::current(),
//! compare times, sum or subtract two times, convert time to/from
//! seconds, milliseconds, microseconds or nanoseconds.
//!
//! \~russian
//! Этот класс предоставляет арифметику для системного времени в формате POSIX.
//! Это время представлено в виде целочисленных секунд и наносекунд.
//! Можно взять текущее время с помощью метода \a PISystemTime::current(),
//! сравнивать, суммировать и вычитать времена, преобразовывать в/из
//! секунд, миллисекунд, микросекунд и наносекунд.
//!
//! \~english \section PISystemTime_sec1 Example
//! \~russian \section PISystemTime_sec1 Пример
//! \~\snippet pitimer.cpp system_time
//!
//! \class PITimeMeasurer pisystemtime.h
//! \details
//! \~english \section PITimeMeasurer_sec0 Usage
//! \~russian \section PITimeMeasurer_sec0 Использование
//! \~english
//! Function \a reset() set time mark to current
//! system time, then functions "double elapsed_*()" returns time elapsed from this mark.
//! These functions can returns nano-, micro-, milli- and seconds with suffixes "n", "u", "m"
//! and "s"
//!
//! \~russian
//! Метод \a reset() устанавливает текущую метку системного времени. Далее методы
//! "double elapsed_*()" возвращают время, прошедшее от установленной метки.
//! Эти методы возвращают нано, микро, милли и секунды с приставками
//! "n", "u", "m" и "s".
//!
//! \details
//! \warning
//! \~english
//! Use this function to sleep for difference of system times or constructs system time.
//! If you call this function on system time returned with \a PISystemTime::current() thread will be sleep almost forever
//! \~russian
//! Используйте этот метод для ожидания разниц системных времен или своего времени.
//! Если метод будет вызван для системного времени \a PISystemTime::current(), то
//! ожидание будет почти бесконечным
void PISystemTime::sleep() {
piUSleep(piFloord(toMicroseconds()));
}
void PISystemTime::toTimespec(void * ts) {
#ifndef WINDOWS
((timespec*)ts)->tv_sec = seconds;
((timespec*)ts)->tv_nsec = nanoseconds;
#endif
}
PISystemTime PISystemTime::abs() const {
if (seconds < 0)
return PISystemTime(piAbsl(seconds) - 1, 1000000000l - piAbsl(nanoseconds));
else
return PISystemTime(piAbsl(seconds), piAbsl(nanoseconds));
}
PISystemTime PISystemTime::current(bool precise_but_not_system) {
#ifdef WINDOWS
if (precise_but_not_system) {
llong qpc(0);
if (__pi_perf_freq > 0) {
qpc = __PIQueryPerformanceCounter();
return PISystemTime::fromSeconds(qpc / double(__pi_perf_freq));
}
return PISystemTime();
} else {
FILETIME ft, sft;
# if (_WIN32_WINNT >= 0x0602)
GetSystemTimePreciseAsFileTime(&ft);
# else
GetSystemTimeAsFileTime(&ft);
# endif
sft.dwHighDateTime = ft.dwHighDateTime - __pi_ftjan1970.dwHighDateTime;
if (ft.dwLowDateTime < __pi_ftjan1970.dwLowDateTime) {
sft.dwLowDateTime = ft.dwLowDateTime + (0xFFFFFFFF - __pi_ftjan1970.dwLowDateTime);
sft.dwHighDateTime--;
} else
sft.dwLowDateTime = ft.dwLowDateTime - __pi_ftjan1970.dwLowDateTime;
ullong lt = ullong(sft.dwHighDateTime) * 0x100000000U + ullong(sft.dwLowDateTime);
return PISystemTime(lt / 10000000U, (lt % 10000000U) * 100U);
}
#elif defined(MAC_OS)
mach_timespec_t t_cur;
clock_get_time(__pi_mac_clock, &t_cur);
#elif defined(MICRO_PIP)
timespec t_cur;
# ifdef ARDUINO
static const uint32_t offSetSinceEpoch_s = 1581897605UL;
uint32_t mt = millis();
t_cur.tv_sec = offSetSinceEpoch_s + (mt / 1000);
t_cur.tv_nsec = (mt - (mt / 1000)) * 1000000UL;
# else
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
gettimeofday(&tv, NULL);
t_cur.tv_sec = tv.tv_sec;
t_cur.tv_nsec = tv.tv_usec * 1000;
# endif
#else
timespec t_cur;
clock_gettime(precise_but_not_system ? CLOCK_MONOTONIC : 0, &t_cur);
#endif
#ifndef WINDOWS
return PISystemTime(t_cur.tv_sec, t_cur.tv_nsec);
#endif
}
PITimeMeasurer::PITimeMeasurer() {
reset();
}
double PITimeMeasurer::elapsed_n() const {
return (PISystemTime::current(true) - t_st).toNanoseconds()
#ifndef MICRO_PIP
- PISystemTests::time_elapsed_ns
#endif
;
}
double PITimeMeasurer::elapsed_u() const {
return (PISystemTime::current(true) - t_st).toMicroseconds()
#ifndef MICRO_PIP
- PISystemTests::time_elapsed_ns / 1.E+3
#endif
;
}
double PITimeMeasurer::elapsed_m() const {
return (PISystemTime::current(true) - t_st).toMilliseconds()
#ifndef MICRO_PIP
- PISystemTests::time_elapsed_ns / 1.E+6
#endif
;
}
double PITimeMeasurer::elapsed_s() const {
return (PISystemTime::current(true) - t_st).toSeconds()
#ifndef MICRO_PIP
- PISystemTests::time_elapsed_ns / 1.E+9
#endif
;
}
PISystemTime PITimeMeasurer::elapsed() const {
return (PISystemTime::current(true) - t_st);
}

View File

@@ -0,0 +1,253 @@
/*! \file pisystemtime.h
* \ingroup Types
* \~\brief
* \~english System time structs and methods
* \~russian Типы и методы системного времени
*/
/*
PIP - Platform Independent Primitives
Time structs
Ivan Pelipenko peri4ko@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/>.
*/
#ifndef PISYSTEMTIME_H
#define PISYSTEMTIME_H
#include "pistring.h"
//! \ingroup Types
//! \~\brief
//! \~english System time with nanosecond precision.
//! \~russian Системное время с точностью до наносекунд.
class PIP_EXPORT PISystemTime {
public:
//! \~english Contructs time with seconds and nanoseconds = 0
//! \~russian Создает время с секундами и наносекундами = 0
PISystemTime() {seconds = nanoseconds = 0;}
//! \~english Contructs time with "s" seconds and "ns" nanoseconds
//! \~russian Создает время с секундами "s" и наносекундами "ns"
PISystemTime(int s, int ns) {seconds = s; nanoseconds = ns; checkOverflows();}
//! \~english Returns time value in seconds
//! \~russian Возвращает значение времени в секундах
double toSeconds() const {return double(seconds) + nanoseconds / 1.e+9;}
//! \~english Returns time value in milliseconds
//! \~russian Возвращает значение времени в миллисекундах
double toMilliseconds() const {return seconds * 1.e+3 + nanoseconds / 1.e+6;}
//! \~english Returns time value in microseconds
//! \~russian Возвращает значение времени в микросекундах
double toMicroseconds() const {return seconds * 1.e+6 + nanoseconds / 1.e+3;}
//! \~english Returns time value in nanoseconds
//! \~russian Возвращает значение времени в наносекундах
double toNanoseconds() const {return seconds * 1.e+9 + double(nanoseconds);}
//! \~english Add to time "v" seconds
//! \~russian Добавляет ко времени "v" секунд
PISystemTime & addSeconds(double v) {*this += fromSeconds(v); return *this;}
//! \~english Add to time "v" milliseconds
//! \~russian Добавляет ко времени "v" миллисекунд
PISystemTime & addMilliseconds(double v) {*this += fromMilliseconds(v); return *this;}
//! \~english Add to time "v" microseconds
//! \~russian Добавляет ко времени "v" микросекунд
PISystemTime & addMicroseconds(double v) {*this += fromMicroseconds(v); return *this;}
//! \~english Add to time "v" nanoseconds
//! \~russian Добавляет ко времени "v" наносекунд
PISystemTime & addNanoseconds(double v) {*this += fromNanoseconds(v); return *this;}
//! \~english Sleep for this time
//! \~russian Ожидать это время
void sleep();
//! \~english On *nix system assign current value to timespec struct
//! \~russian На *nix системах присваивает время к timespec структуре
void toTimespec(void * ts);
//! \~english Returns copy of this time with absolutely values of s and ns
//! \~russian Возвращает копию времени с модулем значения
PISystemTime abs() const;
//! \~english Returns sum of this time with "t"
//! \~russian Возвращает сумму этого времени с "t"
PISystemTime operator +(const PISystemTime & t) const {PISystemTime tt(*this); tt.seconds += t.seconds; tt.nanoseconds += t.nanoseconds; tt.checkOverflows(); return tt;}
//! \~english Returns difference between this time and "t"
//! \~russian Возвращает разницу между этим временем и "t"
PISystemTime operator -(const PISystemTime & t) const {PISystemTime tt(*this); tt.seconds -= t.seconds; tt.nanoseconds -= t.nanoseconds; tt.checkOverflows(); return tt;}
//! \~english Returns multiplication between this time and "t"
//! \~russian Возвращает это временя умноженное на "t"
PISystemTime operator *(const double & v) const {return fromMilliseconds(toMilliseconds() * v);}
//! \~english Returns division between this time and "t"
//! \~russian Возвращает это временя поделённое на "t"
PISystemTime operator /(const double & v) const {return fromMilliseconds(toMilliseconds() / v);}
//! \~english Add to time "t"
//! \~russian Добавляет ко времени "t"
PISystemTime & operator +=(const PISystemTime & t) {seconds += t.seconds; nanoseconds += t.nanoseconds; checkOverflows(); return *this;}
//! \~english Subtract from time "t"
//! \~russian Вычитает из времени "t"
PISystemTime & operator -=(const PISystemTime & t) {seconds -= t.seconds; nanoseconds -= t.nanoseconds; checkOverflows(); return *this;}
//! \~english Multiply time by "v"
//! \~russian Умножает время на "v"
PISystemTime & operator *=(const double & v) {*this = fromMilliseconds(toMilliseconds() * v); return *this;}
//! \~english Divide time by "v"
//! \~russian Делит время на "v"
PISystemTime & operator /=(const double & v) {*this = fromMilliseconds(toMilliseconds() / v); return *this;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator ==(const PISystemTime & t) const {return ((seconds == t.seconds) && (nanoseconds == t.nanoseconds));}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator !=(const PISystemTime & t) const {return ((seconds != t.seconds) || (nanoseconds != t.nanoseconds));}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >(const PISystemTime & t) const {if (seconds == t.seconds) return nanoseconds > t.nanoseconds; return seconds > t.seconds;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <(const PISystemTime & t) const {if (seconds == t.seconds) return nanoseconds < t.nanoseconds; return seconds < t.seconds;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator >=(const PISystemTime & t) const {if (seconds == t.seconds) return nanoseconds >= t.nanoseconds; return seconds >= t.seconds;}
//! \~english Compare operator
//! \~russian Оператор сравнения
bool operator <=(const PISystemTime & t) const {if (seconds == t.seconds) return nanoseconds <= t.nanoseconds; return seconds <= t.seconds;}
//! \~english Contructs time from seconds "v"
//! \~russian Создает время из "v" секунд
static PISystemTime fromSeconds(double v) {int s = piFloord(v); return PISystemTime(s, int((v - s) * 1000000000.));}
//! \~english Contructs time from milliseconds "v"
//! \~russian Создает время из "v" миллисекунд
static PISystemTime fromMilliseconds(double v) {int s = piFloord(v / 1000.); return PISystemTime(s, int((v / 1000. - s) * 1000000000.));}
//! \~english Contructs time from microseconds "v"
//! \~russian Создает время из "v" микросекунд
static PISystemTime fromMicroseconds(double v) {int s = piFloord(v / 1000000.); return PISystemTime(s, int((v / 1000000. - s) * 1000000000.));}
//! \~english Contructs time from nanoseconds "v"
//! \~russian Создает время из "v" наносекунд
static PISystemTime fromNanoseconds(double v) {int s = piFloord(v / 1000000000.); return PISystemTime(s, int((v / 1000000000. - s) * 1000000000.));}
//! \~english Returns current system time
//! \~russian Возвращает текущее системное время
static PISystemTime current(bool precise_but_not_system = false);
//! \~english Seconds time part
//! \~russian Секунды времени
int seconds;
//! \~english Nanoseconds time part
//! \~russian Наносекунды времени
int nanoseconds;
private:
void checkOverflows() {while (nanoseconds >= 1000000000) {nanoseconds -= 1000000000; seconds++;} while (nanoseconds < 0) {nanoseconds += 1000000000; seconds--;}}
};
//! \relatesalso PICout
//! \~english \brief Output operator to PICout
//! \~russian \brief Оператор вывода в PICout
inline PICout operator <<(PICout s, const PISystemTime & v) {s.space(); s.saveAndSetControls(0); s << "(" << v.seconds << " s, " << v.nanoseconds << " ns)"; s.restoreControls(); return s;}
//! \ingroup Types
//! \~\brief
//! \~english Time measurements.
//! \~russian Измерение времени.
class PIP_EXPORT PITimeMeasurer {
public:
PITimeMeasurer();
/** \brief Set internal time mark to current system time
* \details This function used for set start time mark. Later
* you can find out elapsed time from this time mark to any
* moment of time with \a elapsed_s(), \a elapsed_m(),
* \a elapsed_u() or \a elapsed_n() functions.
* \sa \a elapsed_s(), \a elapsed_m(), \a elapsed_u(), \a elapsed_n() */
void reset() {t_st = PISystemTime::current(true);}
//! \brief Returns nanoseconds elapsed from last \a reset() execution or from timer measurer creation.
double elapsed_n() const;
//! \brief Returns microseconds elapsed from last \a reset() execution or from timer measurer creation.
double elapsed_u() const;
//! \brief Returns milliseconds elapsed from last \a reset() execution or from timer measurer creation.
double elapsed_m() const;
//! \brief Returns seconds elapsed from last \a reset() execution or from timer measurer creation.
double elapsed_s() const;
//! \brief Returns PISystemTime elapsed from last \a reset() execution or from timer measurer creation.
PISystemTime elapsed() const;
double reset_time_n() const {return t_st.toNanoseconds();}
double reset_time_u() const {return t_st.toMicroseconds();}
double reset_time_m() const {return t_st.toMilliseconds();}
double reset_time_s() const {return t_st.toSeconds();}
//! \brief Returns time mark of last \a reset() execution or timer measurer creation.
PISystemTime reset_time() {return t_st;}
//! \brief Returns nanoseconds representation of current system time.
static double elapsed_system_n() {return PISystemTime::current(true).toNanoseconds();}
//! \brief Returns microseconds representation of current system time.
static double elapsed_system_u() {return PISystemTime::current(true).toMicroseconds();}
//! \brief Returns milliseconds representation of current system time.
static double elapsed_system_m() {return PISystemTime::current(true).toMilliseconds();}
//! \brief Returns seconds representation of current system time.
static double elapsed_system_s() {return PISystemTime::current(true).toSeconds();}
//! \brief Returns time mark of current system time.
static PISystemTime elapsed_system() {return PISystemTime::current(true);}
private:
PISystemTime t_st, t_cur;
};
#endif // PITIME_H

View File

@@ -0,0 +1,61 @@
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@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 "piincludes_p.h"
#include "pitime.h"
#ifdef QNX
# include <time.h>
#endif
#ifndef MICRO_PIP
# include "pisystemtests.h"
#elif defined(ARDUINO)
# include <Arduino.h>
#endif
#ifdef MICRO_PIP
# include <sys/time.h>
#endif
//! \details
//! \~english
//! This function consider \c "usleep" offset
//! on QNX/Linux/Mac, which is calculated with
//! \a pip_sys_test program. If this is correct
//! offset value in system config, this function
//! wait \b exactly "usecs" microseconds.
//! \~russian
//! Этот метод учитывает смещение \c "usleep"
//! на QNX/Linux/Mac, которое расчитывается с помощью
//! утилиты \a pip_sys_test. Если это значение в системном
//! конфиге действительно, то этот метод будет ожидать
//! \b точно "usecs" микросекунд.
void piUSleep(int usecs) {
if (usecs <= 0) return;
#ifdef WINDOWS
//printf("Sleep %d\n", usecs / 1000);
if (usecs > 0) Sleep(usecs / 1000);
//printf("Sleep end");
#else
# ifdef FREERTOS
vTaskDelay(usecs / 1000 / portTICK_PERIOD_MS);
# else
usecs -= PISystemTests::usleep_offset_us;
if (usecs > 0) usleep(usecs);
# endif
#endif
}

58
libs/main/types/pitime.h Normal file
View File

@@ -0,0 +1,58 @@
/*! \file pitime.h
* \ingroup Types
* \~\brief
* \~english System time, time and date
* \~russian Системное время, время и дата
*/
/*
PIP - Platform Independent Primitives
Ivan Pelipenko peri4ko@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/>.
*/
#ifndef PITIME_H
#define PITIME_H
#include "pidatetime.h"
//! \ingroup Types
//! \~english Precise sleep for "usecs" microseconds
//! \~russian Точно ожидает "usecs" микросекунд
PIP_EXPORT void piUSleep(int usecs); // on !Windows consider constant "usleep" offset
//! \ingroup Types
//! \brief
//! \~english Precise sleep for "msecs" milliseconds
//! \~russian Точно ожидает "msecs" миллисекунд
//! \~\details
//! \~english This function exec \a piUSleep (msecs * 1000)
//! \~russian Этот метод вызывает \a piUSleep (msecs * 1000)
inline void piMSleep(double msecs) {piUSleep(int(msecs * 1000.));} // on !Windows consider constant "usleep" offset
//! \ingroup Types
//! \brief
//! \~english Precise sleep for "secs" seconds
//! \~russian Точно ожидает "secs" секунд
//! \~\details
//! \~english This function exec \a piUSleep (msecs * 1000000)
//! \~russian Этот метод вызывает \a piUSleep (msecs * 1000000)
inline void piSleep(double secs) {piUSleep(int(secs * 1000000.));} // on !Windows consider constant "usleep" offset
//! \ingroup Types
//! \~english Shortest available on current system sleep
//! \~russian Наименее возможное для данной системы по длительности ожидание
inline void piMinSleep() {piMSleep(PIP_MIN_MSLEEP);}
#endif // PITIME_H

View File

@@ -0,0 +1,88 @@
/*! \file pitime_win.h
* \ingroup Types
* \brief
* \~english PITime conversions for Windows
* \~russian Преобразования PITime для Windows
*/
/*
PIP - Platform Independent Primitives
PITime conversions for Windows
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/>.
*/
#ifndef PITIME_WIN_H
#define PITIME_WIN_H
#include "pibase.h"
#ifdef WINDOWS
#include "pitime.h"
#include <windows.h>
inline PISystemTime FILETIME2PISystemTime(const FILETIME &t) {
PISystemTime st;
ullong lt = ullong(t.dwHighDateTime) * 0x100000000U + ullong(t.dwLowDateTime);
st.seconds = lt / 10000000U;
st.nanoseconds = (lt % 10000000U) * 100U;
return st;
}
inline PIDateTime SYSTEMTIME2PIDateTime(const SYSTEMTIME &t) {
PIDateTime dt;
dt.year = t.wYear;
dt.month = t.wMonth;
dt.day = t.wDay;
dt.hours = t.wHour;
dt.minutes = t.wMinute;
dt.seconds = t.wSecond;
dt.milliseconds = t.wMilliseconds;
return dt;
}
inline PIDateTime FILETIME2PIDateTime(const FILETIME &t) {
FILETIME lt;
SYSTEMTIME st;
FileTimeToLocalFileTime(&t, &lt);
FileTimeToSystemTime(&lt, &st);
return SYSTEMTIME2PIDateTime(st);
}
inline SYSTEMTIME PIDateTime2SYSTEMTIME(const PIDateTime &dt) {
SYSTEMTIME st;
st.wYear = dt.year;
st.wMonth = dt.month;
st.wDay = dt.day;
st.wHour = dt.hours;
st.wMinute = dt.minutes;
st.wSecond = dt.seconds;
st.wMilliseconds = dt.milliseconds;
return st;
}
inline FILETIME PIDateTime2FILETIME(const PIDateTime &dt) {
FILETIME lt, ret;
SYSTEMTIME st = PIDateTime2SYSTEMTIME(dt);
SystemTimeToFileTime(&st, &lt);
LocalFileTimeToFileTime(&lt, &ret);
return ret;
}
#endif // WINDOWS
#endif // PITIME_WIN_H

View File

@@ -0,0 +1,61 @@
/*
PIP - Platform Independent Primitives
Module includes
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/>.
*/
//! \defgroup Types Types
//! \~\brief
//! \~english Basic types.
//! \~russian Базовые типы.
//!
//! \~\details
//! \~english \section cmake_module_Types Building with CMake
//! \~russian \section cmake_module_Types Сборка с использованием CMake
//!
//! \~\code
//! find_package(PIP REQUIRED)
//! target_link_libraries([target] PIP)
//! \endcode
//!
//! \~english \par Common
//! \~russian \par Общее
//!
//! \~english
//!
//!
//! \~russian
//!
//!
//! \~\authors
//! \~english
//! Ivan Pelipenko peri4ko@yandex.ru;
//! Andrey Bychkov work.a.b@yandex.ru;
//! \~russian
//! Иван Пелипенко peri4ko@yandex.ru;
//! Андрей Бычков work.a.b@yandex.ru;
//!
#ifndef PITYPESMODULE_H
#define PITYPESMODULE_H
#include "pibitarray.h"
#include "pibytearray.h"
#include "piflags.h"
#include "pitime.h"
#include "pipropertystorage.h"
#include "pivariantsimple.h"
#endif // PITYPESMODULE_H

View File

@@ -0,0 +1,939 @@
/*
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 pivariant.h
//! \details
//! \~english \section PIVariant_sec0 Synopsis
//! \~russian \section PIVariant_sec0 Краткий обзор
//! \~english
//! 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.
//!
//! \~russian
//! Этот класс предоставляет некий общий тип, который может содержать все стандартные тип,
//! некоторые типы PIP, а также любой свой тип. В случае стандартных типов предоставляется
//! автоматическое преобразование между ними.
//!
//! \~english \section PIVariant_sec1 Usage
//! \~russian \section PIVariant_sec1 Использование
//! \~english
//! %PIVariant useful if you want pass many variables with different types in
//! single array, or type may vary from case to case, e.g.:
//!
//! \~russian
//! %PIVariant полезен когда надо передать несколько разнотипных значений
//! одним массивом, или тип заранее неизвестен, например:
//!
//! \~\code{cpp}
//! PIVector<PIVariant> array;
//! array << PIVariant(10) << PIVariant(1.61) << PIVariant(true) << PIVariant("0xFF");
//! piCout << array;
//! for (auto i: array)
//! piCout << i.toInt();
//! \endcode
//!
//! \~english Result:
//! \~russian Результат:
//! \~\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;
if (s == "line") return PIVariant::pivLine;
if (s == "device" || s == "iodevice") return PIVariant::pivIODevice;
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
//! \~english Returns variant content as boolean
//! \~russian Возвращает содержимое как boolean
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для численных типов возвращает \b true если значение != 0. \n
//! Для типа String возвращает \a PIString::toBool(). \n
//! Для типа StringList возвращает \b false если массив пустой, \n
//! иначе возвращает \a PIString::toBool() первой строки. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as int
//! \~russian Возвращает содержимое как int
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для численных типов возвращает целочисленное значение. \n
//! Для типа String возвращает \a PIString::toInt(). \n
//! Для типа StringList возвращает \b 0 если массив пустой, \n
//! иначе возвращает \a PIString::toInt() первой строки. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as long long
//! \~russian Возвращает содержимое как long long
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для численных типов возвращает целочисленное значение. \n
//! Для типа String возвращает \a PIString::toLLong(). \n
//! Для типа StringList возвращает \b 0L если массив пустой, \n
//! иначе возвращает \a PIString::toLLong() первой строки. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as float
//! \~russian Возвращает содержимое как float
//!
//! \~\details
//! \~english
//! 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 .
//!
//! \~russian
//! Для численных типов возвращает вещественное значение. \n
//! Для типа String возвращает \a PIString::toFloat(). \n
//! Для типа StringList возвращает \b 0.f если массив пустой, \n
//! иначе возвращает \a PIString::toFloat() первой строки. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as double
//! \~russian Возвращает содержимое как double
//!
//! \~\details
//! \~english
//! 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. .
//!
//! \~russian
//! Для численных типов возвращает вещественное значение. \n
//! Для типа String возвращает \a PIString::toDouble(). \n
//! Для типа StringList возвращает \b 0. если массив пустой, \n
//! иначе возвращает \a PIString::toDouble() первой строки. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as long double
//! \~russian Возвращает содержимое как long double
//!
//! \~\details
//! \~english
//! 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. .
//!
//! \~russian
//! Для численных типов возвращает вещественное значение. \n
//! Для типа String возвращает \a PIString::toLDouble(). \n
//! Для типа StringList возвращает \b 0. если массив пустой, \n
//! иначе возвращает \a PIString::toLDouble() первой строки. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as time
//! \~russian Возвращает содержимое как время
//!
//! \~\details
//! \~english
//! 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().
//!
//! \~russian
//! Для типа Time возвращает время. \n
//! Для типа DateTime возвращает часть времени. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as date
//! \~russian Возвращает содержимое как дата
//!
//! \~\details
//! \~english
//! 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().
//!
//! \~russian
//! Для типа Date возвращает дату. \n
//! Для типа DateTime возвращает часть даты. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as date and time
//! \~russian Возвращает содержимое как дата и время
//!
//! \~\details
//! \~english
//! 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().
//!
//! \~russian
//! Для типа Time возвращает время без даты. \n
//! Для типа Date возвращает дату без времени. \n
//! Для типа DateTime возвращает время и дату. \n
//! Для остальных типов возвращает \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
//! \~english Returns variant content as system time
//! \~russian Возвращает содержимое как системное время
//!
//! \~\details
//! \~english
//! In case of SystemTime type returns system time. \n
//! In case of other types returns \a PISystemTime::fromSeconds() from \a toDouble().
//!
//! \~russian
//! Для типа SystemTime возвращает системное время. \n
//! Для остальных типов возвращает \a PISystemTime::fromSeconds() от \a toDouble().
//!
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
//! \~english Returns variant content as string
//! \~russian Возвращает содержимое как строка
//!
//! \~\details
//! \~english
//! 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 "".
//!
//! \~russian
//! Для численных типов возвращает \a PIString::fromNumber(). \n
//! Для типа String возвращает строку. \n
//! Для типа StringList возвращает объединенную строку ("(" + PIStringList::join("; ") + ")"). \n
//! Для типов BitArray или ByteArray возвращает количество бит/байт. \n
//! Для типов Time, Date или DateTime возвращает toString(). \n
//! Для типов SystemTime возвращает секунды и наносекунды в формате "(s, ns)".
//! Для остальных типов возвращает \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
//! \~english Returns variant content as strings list
//! \~russian Возвращает содержимое как массив строк
//!
//! \~\details
//! \~english
//! In case of StringList type returns strings list value. \n
//! In case of other types returns \a PIStringList with one string \a toString().
//!
//! \~russian
//! Для типа StringList возвращает массив строк. \n
//! Для остальных типов возвращает \a PIStringList с одной строкой \a toString().
//!
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
//! \~english Returns variant content as bit array
//! \~russian Возвращает содержимое как массив битов
//!
//! \~\details
//! \~english
//! In case of BitArray type returns bit array value. \n
//! In case of other types returns \a PIBitArray from \a toLLong() value.
//!
//! \~russian
//! Для типа BitArray возвращает массив битов. \n
//! Для остальных типов возвращает \a PIBitArray от значения \a toLLong().
//!
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
//! \~english Returns variant content as byte array
//! \~russian Возвращает содержимое как массив байтов
//!
//! \~\details
//! \~english
//! In case of ByteArray type returns byte array value. \n
//! In case of other types returns empty \a PIByteArray.
//!
//! \~russian
//! Для типа ByteArray возвращает массив байтов. \n
//! Для остальных типов возвращает пустой \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
//! \~english Returns variant content as enum
//! \~russian Возвращает содержимое как перечисление
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для типа Enum возвращает перечисление. \n
//! Для типа String возвращает Enum с одним членом. \n
//! Для типа StringList возвращает Enum с членами по строкам. \n
//! Для остальных типов возвращает пустой 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
//! \~english Returns variant content as file
//! \~russian Возвращает содержимое как файл
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для типа File возвращает файл. \n
//! Для типа String возвращает File с путем значения строки. \n
//! Для остальных типов возвращает пустой 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
//! \~english Returns variant content as dir
//! \~russian Возвращает содержимое как директория
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для типа Dir возвращает директорию. \n
//! Для типа String возвращает Dir с путем значения строки. \n
//! Для остальных типов возвращает пустой 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
//! \~english Returns variant content as color
//! \~russian Возвращает содержимое как цвет
//!
//! \~\details
//! \~english
//! 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.
//!
//! \~russian
//! Для типа Color возвращает цвет. \n
//! Для типа Int возвращает цвет с целочисленным значением. \n
//! Для остальных типов возвращает пустой 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
//! \~english Returns variant content as IODevice
//! \~russian Возвращает содержимое как IODevice
//!
//! \~\details
//! \~english
//! In case of IODevice type returns IODevice value. \n
//! In case of other types returns empty IODevice.
//!
//! \~russian
//! Для типа IODevice возвращает IODevice. \n
//! Для остальных типов возвращает пустой IODevice.
//!
PIVariantTypes::IODevice PIVariant::toIODevice() const {
PIByteArray ba(_content);
if (_type == PIVariant::pivIODevice) {PIVariantTypes::IODevice r; ba >> r; return r;}
return PIVariantTypes::IODevice();
}
//! \~\brief
//! \~english Returns variant content as point
//! \~russian Возвращает содержимое как точка
//!
//! \~\details
//! \~english
//! In case of Point type returns point value. \n
//! In case of other types returns empty PIPointd.
//!
//! \~russian
//! Для типа Point возвращает точку. \n
//! Для остальных типов возвращает пустую PIPointd.
//!
PIPointd PIVariant::toPoint() const {
PIByteArray ba(_content);
if (_type == PIVariant::pivPoint) {PIPointd r; ba >> r; return r;}
return PIPointd();
}
//! \~\brief
//! \~english Returns variant content as rect
//! \~russian Возвращает содержимое как прямоугольник
//!
//! \~\details
//! \~english
//! In case of Rect type returns rect value. \n
//! In case of other types returns empty PIRectd.
//!
//! \~russian
//! Для типа Rect возвращает прямоугольник. \n
//! Для остальных типов возвращает пустой PIRectd.
//!
PIRectd PIVariant::toRect() const {
PIByteArray ba(_content);
if (_type == PIVariant::pivRect) {PIRectd r; ba >> r; return r;}
return PIRectd();
}
//! \~\brief
//! \~english Returns variant content as line
//! \~russian Возвращает содержимое как линия
//!
//! \~\details
//! \~english
//! In case of Line type returns line value. \n
//! In case of other types returns empty PILined.
//!
//! \~russian
//! Для типа Line возвращает линию. \n
//! Для остальных типов возвращает пустую PILined.
//!
PILined PIVariant::toLine() const {
PIByteArray ba(_content);
if (_type == PIVariant::pivLine) {PILined r; ba >> r; return r;}
return PILined();
}
//! \~\brief
//! \~english Returns variant content as math vector
//! \~russian Возвращает содержимое как вектор
//!
//! \~\details
//! \~english
//! In case of MathVector type returns rect value. \n
//! In case of other types returns empty PIMathVectord.
//!
//! \~russian
//! Для типа MathVector возвращает вектор. \n
//! Для остальных типов возвращает пустой PIMathVectord.
//!
PIMathVectord PIVariant::toMathVector() const {
PIByteArray ba(_content);
if (_type == PIVariant::pivMathVector) {PIMathVectord r; ba >> r; return r;}
return PIMathVectord();
}
//! \~\brief
//! \~english Returns variant content as math matrix
//! \~russian Возвращает содержимое как матрица
//!
//! \~\details
//! \~english
//! In case of MathMatrix type returns rect value. \n
//! In case of other types returns empty PIMathMatrixd.
//!
//! \~russian
//! Для типа MathMatrix возвращает матрицу. \n
//! Для остальных типов возвращает пустую PIMathMatrixd.
//!
PIMathMatrixd PIVariant::toMathMatrix() const {
PIByteArray ba(_content);
if (_type == PIVariant::pivMathMatrix) {PIMathMatrixd r; ba >> r; return r;}
return PIMathMatrixd();
}

1001
libs/main/types/pivariant.h Normal file
View File

@@ -0,0 +1,1001 @@
/*! \file pivariant.h
* \ingroup Types
* \brief
* \~english Variant type
* \~russian Вариативный тип
*/
/*
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/>.
*/
#ifndef PIVARIANT_H
#define PIVARIANT_H
#include "pivarianttypes.h"
#include "piconstchars.h"
#include "pitime.h"
#include "pigeometry.h"
#include "pimathmatrix.h"
#ifdef DOXYGEN
//! \relatesalso PIVariant
//! \~\brief
//! \~english Macro to register type for using in %PIVariant
//! \~russian Макрос для регистрации типа для использования в %PIVariant
# define REGISTER_VARIANT(Typename)
//! \relatesalso PIVariant
//! \~\brief
//! \~english Macro to register type with namespace for using in %PIVariant
//! \~russian Макрос для регистрации типа с пространством имен для использования в %PIVariant
# define REGISTER_NS_VARIANT(Namespace, Typename)
#endif
#ifndef QNX
# define CUSTOM_PIVARIANT
#endif
#ifdef CUSTOM_PIVARIANT
template<typename T>
class __PIVariantFunctions__ {
public:
static PIString typeNameHelper() {return PIStringAscii("");}
static bool isSimpleHelper() {return false;}
template<typename C> static PIByteArray castHelper(PIByteArray ba) {return PIByteArray();}
template<typename C> static C castVariant(const T & v) {return C();}
};
struct PIP_EXPORT __PIVariantInfo__ {
__PIVariantInfo__() {
simple = false;
}
typedef PIByteArray(*castHelperFunc)(PIByteArray);
PIMap<PIString, castHelperFunc> cast;
PIString typeName;
bool simple;
};
template<typename T>
struct __PIVariantTypeInfo__ {
typedef T PureType;
typedef const T ConstPureType;
typedef T * PointerType;
typedef const T * ConstPointerType;
typedef T & ReferenceType;
typedef const T & ConstReferenceType;
};
#define __TYPEINFO_SINGLE(PT, T) \
template<> struct __PIVariantTypeInfo__<T> { \
typedef PT PureType; \
typedef const PT ConstPureType; \
typedef PT * PointerType; \
typedef const PT * ConstPointerType; \
typedef PT & ReferenceType; \
typedef const PT & ConstReferenceType; \
};
#define REGISTER_VARIANT_TYPEINFO(T) \
__TYPEINFO_SINGLE(T, T &) \
__TYPEINFO_SINGLE(T, const T) \
__TYPEINFO_SINGLE(T, const T &)
class PIP_EXPORT __PIVariantInfoStorage__ {
public:
__PIVariantInfoStorage__() {if (!map) map = new PIMap<PIString, __PIVariantInfo__ * >();}
static __PIVariantInfoStorage__ * get() {static __PIVariantInfoStorage__ * r = new __PIVariantInfoStorage__(); return r;}
static PIMap<PIString, __PIVariantInfo__ * > * map;
};
#define REGISTER_VARIANT_H(classname) \
template<> inline PIString __PIVariantFunctions__< classname >::typeNameHelper() {static PIString tn = PIStringAscii(#classname); return tn;} \
REGISTER_VARIANT_TYPEINFO(classname)
#define REGISTER_NS_VARIANT_H(ns, classname) \
template<> inline PIString __PIVariantFunctions__< ns::classname >::typeNameHelper() {static PIString tn = PIStringAscii(#ns"::"#classname); return tn;} \
REGISTER_VARIANT_TYPEINFO(ns::classname)
#define REGISTER_VARIANT_CPP(classname) \
template <typename T> \
class __##classname##_PIVariantInitializer__ { \
public: \
__##classname##_PIVariantInitializer__(const PIString & name) { \
if (__PIVariantInfoStorage__::get()->map->contains(name)) \
return; \
__PIVariantInfo__ * vi = new __PIVariantInfo__(); \
vi->typeName = name; \
(*(__PIVariantInfoStorage__::get()->map))[name] = vi; \
} \
};
#define REGISTER_NS_VARIANT_CPP(ns, classname) \
template <typename T> \
class __##ns##classname##_PIVariantInitializer__ { \
public: \
__##ns##classname##_PIVariantInitializer__(const PIString & name) { \
if (__PIVariantInfoStorage__::get()->map->contains(name)) \
return; \
__PIVariantInfo__ * vi = new __PIVariantInfo__(); \
vi->typeName = name; \
(*(__PIVariantInfoStorage__::get()->map))[name] = vi; \
} \
};
#define INIT_VARIANT(classname) \
__##classname##_PIVariantInitializer__< classname > __##classname##_pivariant_initializer__(#classname);
#define INIT_NS_VARIANT(ns, classname) \
__##ns##classname##_PIVariantInitializer__< ns::classname > __##ns##classname##_pivariant_initializer__(#ns"::"#classname);
#define REGISTER_VARIANT(classname) \
REGISTER_VARIANT_H(classname) \
REGISTER_VARIANT_CPP(classname) \
static INIT_VARIANT(classname)
#define REGISTER_NS_VARIANT(ns, classname) \
REGISTER_NS_VARIANT_H(ns, classname) \
REGISTER_NS_VARIANT_CPP(ns, classname) \
static INIT_NS_VARIANT(ns, classname)
#define REGISTER_VARIANT_CAST_H(classname_from, classname_to) \
template<> template<> inline \
classname_to __PIVariantFunctions__<classname_from>::castVariant<classname_to>(const classname_from & v);
#define REGISTER_VARIANT_CAST_CPP(classname_from, classname_to) \
template<> template<> inline \
PIByteArray __PIVariantFunctions__<classname_from>::castHelper<classname_to>(PIByteArray v) { \
classname_from f; v >> f; \
classname_to t = __PIVariantFunctions__<classname_from>::castVariant<classname_to>(f); \
PIByteArray ret; ret << t; \
return ret;} \
template <typename T, typename C> \
class __##classname_from##_##classname_to##_PIVariantCastInitializer__ { \
public: \
__##classname_from##_##classname_to##_PIVariantCastInitializer__(const PIString & name, const PIString & cname) { \
__PIVariantInfo__ * vi(__PIVariantInfoStorage__::get()->map->value(name, 0)); \
if (!vi) { \
piCout << "Warning! Using REGISTER_VARIANT_CAST("#classname_from", "#classname_to") before REGISTER_VARIANT("#classname_from"), ignore."; \
return; \
} \
vi->cast[cname] = __PIVariantFunctions__<classname_from>::castHelper<classname_to>; \
} \
}; \
static __##classname_from##_##classname_to##_PIVariantCastInitializer__< classname_from, classname_to > __##classname_from##_##classname_to##_pivariant_cast_initializer__(#classname_from, #classname_to); \
template<> template<> \
classname_to __PIVariantFunctions__<classname_from>::castVariant<classname_to>(const classname_from & v)
#define REGISTER_VARIANT_CAST(classname_from, classname_to) \
REGISTER_VARIANT_CAST_H(classname_from, classname_to) \
REGISTER_VARIANT_CAST_CPP(classname_from, classname_to)
#define REGISTER_VARIANT_CAST_SIMPLE(classname_from, classname_to) REGISTER_VARIANT_CAST(classname_from, classname_to) {return classname_to(v);}
#define REGISTER_VARIANT_CAST_SIMPLE_H(classname_from, classname_to) REGISTER_VARIANT_CAST_H(classname_from, classname_to)
#define REGISTER_VARIANT_CAST_SIMPLE_CPP(classname_from, classname_to) REGISTER_VARIANT_CAST_CPP(classname_from, classname_to) {return classname_to(v);}
#else
#define REGISTER_VARIANT_TYPEINFO(classname)
#define REGISTER_VARIANT_H(classname)
#define REGISTER_VARIANT_CPP(classname)
#define INIT_VARIANT(classname)
#define REGISTER_VARIANT(classname)
#define REGISTER_NS_VARIANT_H(ns, classname)
#define REGISTER_NS_VARIANT_CPP(ns, classname)
#define INIT_NS_VARIANT(ns, classname)
#define REGISTER_NS_VARIANT(ns, classname)
#define REGISTER_VARIANT_CAST_H(classname_from, classname_to)
#define REGISTER_VARIANT_CAST_CPP(classname_from, classname_to)
#define REGISTER_VARIANT_CAST(classname_from, classname_to)
#define REGISTER_VARIANT_CAST_SIMPLE(classname_from, classname_to)
#define REGISTER_VARIANT_CAST_SIMPLE_H(classname_from, classname_to)
#define REGISTER_VARIANT_CAST_SIMPLE_CPP(classname_from, classname_to)
#endif
//! \ingroup Types
//! \~\brief
//! \~english Variant type.
//! \~russian Вариантный тип.
class PIP_EXPORT PIVariant {
friend PICout operator <<(PICout s, const PIVariant & v);
BINARY_STREAM_FRIEND(PIVariant);
public:
//! \~english Type of %PIVariant content
//! \~russian Тип содержимого %PIVariant
enum Type {
pivInvalid /** \~english Invalid type, default type of empty contructor \~russian Недействительный тип, также конструированный по умолчанию */ = 0 ,
pivBool /** bool */ ,
pivChar /** char */ ,
pivUChar /** uchar */ ,
pivShort /** short */ ,
pivUShort /** ushort */ ,
pivInt /** int */ ,
pivUInt /** uint */ ,
pivLLong /** llong */ ,
pivULLong /** ullong */ ,
pivFloat /** float */ ,
pivDouble /** double */ ,
pivLDouble /** ldouble */ ,
pivComplexd /** complexd */ ,
pivComplexld /** complexld */ ,
pivBitArray /** PIBitArray */ ,
pivByteArray /** PIByteArray */ ,
pivString /** PIString */ ,
pivStringList /** PIStringList */ ,
pivTime /** PITime */ ,
pivDate /** PIDate */ ,
pivDateTime /** PIDateTime */ ,
pivSystemTime /** PISystemTime */ ,
pivEnum /** PIVariantTypes::Enum */ ,
pivFile /** PIVariantTypes::File */ ,
pivDir /** PIVariantTypes::Dir */ ,
pivColor /** PIVariantTypes::Color */ ,
pivPoint /** PIPoint<double> */ ,
pivRect /** PIRect<double> */ ,
pivIODevice /** PIVariantTypes::IODevice */ ,
pivMathVector /** PIMathVector<double> */ ,
pivMathMatrix /** PIMathMatrix<double> */ ,
pivLine /** PILine<double> */ ,
pivCustom /** \~english Custom \~russian Свой тип */ = 0xFF
};
//! \~english Construct \a pivInvalid %PIVariant
//! \~russian Создает \a pivInvalid %PIVariant
PIVariant();
//! \~english Contructs a copy of %PIVariant.
//! \~russian Создает копию %PIVariant.
PIVariant(const PIVariant & v);
//! \~english Move constructor.
//! \~russian Перемещающий конструктор.
PIVariant(PIVariant && v);
//! \~english Constructs %PIVariant from string
//! \~russian Создает %PIVariant из строки.
PIVariant(const char * v) {initType(PIString(v));}
//! \~english Constructs %PIVariant from boolean
//! \~russian Создает %PIVariant из логического значения.
PIVariant(const bool v) {initType(v);}
//! \~english Constructs %PIVariant from char
//! \~russian Создает %PIVariant из символа.
PIVariant(const char v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const uchar v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const short v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const ushort v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const int & v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const uint & v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const llong & v) {initType(v);}
//! \~english Constructs %PIVariant from integer
//! \~russian Создает %PIVariant из целого числа.
PIVariant(const ullong & v) {initType(v);}
//! \~english Constructs %PIVariant from float
//! \~russian Создает %PIVariant из вещественного числа.
PIVariant(const float & v) {initType(v);}
//! \~english Constructs %PIVariant from double
//! \~russian Создает %PIVariant из вещественного числа.
PIVariant(const double & v) {initType(v);}
//! \~english Constructs %PIVariant from long double
//! \~russian Создает %PIVariant из вещественного числа.
PIVariant(const ldouble & v) {initType(v);}
//! \~english Constructs %PIVariant from bit array
//! \~russian Создает %PIVariant из массива битов.
PIVariant(const PIBitArray & v) {initType(v);}
//! \~english Constructs %PIVariant from byte array
//! \~russian Создает %PIVariant из массива байтов.
PIVariant(const PIByteArray & v) {initType(v);}
//! \~english Constructs %PIVariant from string
//! \~russian Создает %PIVariant из строки.
PIVariant(const PIString & v) {initType(v);}
//! \~english Constructs %PIVariant from strings list
//! \~russian Создает %PIVariant из массива строк.
PIVariant(const PIStringList & v) {initType(v);}
//! \~english Constructs %PIVariant from time
//! \~russian Создает %PIVariant из времени.
PIVariant(const PITime & v) {initType(v);}
//! \~english Constructs %PIVariant from date
//! \~russian Создает %PIVariant из даты.
PIVariant(const PIDate & v) {initType(v);}
//! \~english Constructs %PIVariant from date and time
//! \~russian Создает %PIVariant из даты и времени.
PIVariant(const PIDateTime & v) {initType(v);}
//! \~english Constructs %PIVariant from system time
//! \~russian Создает %PIVariant из системного времени.
PIVariant(const PISystemTime & v) {initType(v);}
//! \~english Constructs %PIVariant from enum
//! \~russian Создает %PIVariant из перечисления.
PIVariant(const PIVariantTypes::Enum & v) {initType(v);}
//! \~english Constructs %PIVariant from file
//! \~russian Создает %PIVariant из файла.
PIVariant(const PIVariantTypes::File & v) {initType(v);}
//! \~english Constructs %PIVariant from dir
//! \~russian Создает %PIVariant из директории.
PIVariant(const PIVariantTypes::Dir & v) {initType(v);}
//! \~english Constructs %PIVariant from color
//! \~russian Создает %PIVariant из цвета.
PIVariant(const PIVariantTypes::Color & v) {initType(v);}
//! \~english Constructs %PIVariant from IODevice
//! \~russian Создает %PIVariant из IODevice.
PIVariant(const PIVariantTypes::IODevice & v) {initType(v);}
//! \~english Constructs %PIVariant from point
//! \~russian Создает %PIVariant из точки.
PIVariant(const PIPointd & v) {initType(v);}
//! \~english Constructs %PIVariant from rect
//! \~russian Создает %PIVariant из прямоугольника.
PIVariant(const PIRectd & v) {initType(v);}
//! \~english Constructs %PIVariant from line
//! \~russian Создает %PIVariant из линии.
PIVariant(const PILined & v) {initType(v);}
//! \~english Constructs %PIVariant from MathVector
//! \~russian Создает %PIVariant из MathVector.
PIVariant(const PIMathVectord & v) {initType(v);}
//! \~english Constructs %PIVariant from MathMatrix
//! \~russian Создает %PIVariant из MathMatrix.
PIVariant(const PIMathMatrixd & v) {initType(v);}
//! \~english Set variant content and type to string
//! \~russian Устанавливает значение и тип из строки
void setValue(const char * v) {setValue(PIString(v));}
//! \~english Set variant content and type to boolean
//! \~russian Устанавливает значение и тип из
void setValue(const bool v) {initType(v);}
//! \~english Set variant content and type to char
//! \~russian Устанавливает значение и тип из символа
void setValue(const char v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const uchar v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const short v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const ushort v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const int & v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const uint & v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const llong & v) {initType(v);}
//! \~english Set variant content and type to integer
//! \~russian Устанавливает значение и тип из целого числа
void setValue(const ullong & v) {initType(v);}
//! \~english Set variant content and type to float
//! \~russian Устанавливает значение и тип из вещественного числа
void setValue(const float & v) {initType(v);}
//! \~english Set variant content and type to double
//! \~russian Устанавливает значение и тип из вещественного числа
void setValue(const double & v) {initType(v);}
//! \~english Set variant content and type to long double
//! \~russian Устанавливает значение и тип из вещественного числа
void setValue(const ldouble & v) {initType(v);}
//! \~english Set variant content and type to bit array
//! \~russian Устанавливает значение и тип из массива битов
void setValue(const PIBitArray & v) {initType(v);}
//! \~english Set variant content and type to byte array
//! \~russian Устанавливает значение и тип из массива байтов
void setValue(const PIByteArray & v) {initType(v);}
//! \~english Set variant content and type to string
//! \~russian Устанавливает значение и тип из строки
void setValue(const PIString & v) {initType(v);}
//! \~english Set variant content and type to strings list
//! \~russian Устанавливает значение и тип из массива строк
void setValue(const PIStringList & v) {initType(v);}
//! \~english Set variant content and type to time
//! \~russian Устанавливает значение и тип из времени
void setValue(const PITime & v) {initType(v);}
//! \~english Set variant content and type to date
//! \~russian Устанавливает значение и тип из даты
void setValue(const PIDate & v) {initType(v);}
//!
//! \~english Set variant content and type to date and time
//! \~russian Устанавливает значение и тип из даты и времени
void setValue(const PIDateTime & v) {initType(v);}
//! \~english Set variant content and type to system time
//! \~russian Устанавливает значение и тип из системного времени
void setValue(const PISystemTime & v) {initType(v);}
//! \~english Set variant content and type to enum
//! \~russian Устанавливает значение и тип из перечисления
void setValue(const PIVariantTypes::Enum & v) {initType(v);}
//! \~english Set variant content and type to file
//! \~russian Устанавливает значение и тип из файла
void setValue(const PIVariantTypes::File & v) {initType(v);}
//! \~english Set variant content and type to dir
//! \~russian Устанавливает значение и тип из директории
void setValue(const PIVariantTypes::Dir & v) {initType(v);}
//! \~english Set variant content and type to color
//! \~russian Устанавливает значение и тип из цвета
void setValue(const PIVariantTypes::Color & v) {initType(v);}
//! \~english Set variant content and type to IODevice
//! \~russian Устанавливает значение и тип из IODevice
void setValue(const PIVariantTypes::IODevice & v) {initType(v);}
//! \~english Set variant content and type to point
//! \~russian Устанавливает значение и тип из точки
void setValue(const PIPointd & v) {initType(v);}
//! \~english Set variant content and type to rect
//! \~russian Устанавливает значение и тип из прямоугольника
void setValue(const PIRectd & v) {initType(v);}
//! \~english Set variant content and type to line
//! \~russian Устанавливает значение и тип из линии
void setValue(const PILined & v) {initType(v);}
//! \~english Set variant content and type to math vector
//! \~russian Устанавливает значение и тип из вектора
void setValue(const PIMathVectord & v) {initType(v);}
//! \~english Set variant content and type to math matrix
//! \~russian Устанавливает значение и тип из матрицы
void setValue(const PIMathMatrixd & v) {initType(v);}
//! \~english Set current value from string without change type
//! \~russian Устанавливает текущее значение из строки без изменения типа
void setValueFromString(const PIString & v);
bool toBool() const;
int toInt() const;
llong toLLong() const;
float toFloat() const;
double toDouble() const;
ldouble toLDouble() 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;
PIVariantTypes::Enum toEnum() const;
PIVariantTypes::File toFile() const;
PIVariantTypes::Dir toDir() const;
PIVariantTypes::Color toColor() const;
PIVariantTypes::IODevice toIODevice() const;
PIPointd toPoint() const;
PIRectd toRect() const;
PILined toLine() const;
PIMathVectord toMathVector() const;
PIMathMatrixd toMathMatrix() const;
//! \~\brief
//! \~english Returns variant content as custom type
//! \~russian Возвращает содержимое в качестве своего типа
//! \~\details
//! \~english
//! In case of known types this function equivalent \a to<Type> function. \n
//! Otherwise returns content as type T only if this type was set earlier.
//! \~russian
//! Для стандартных типов эквиваленстно методу \a to<Type>. \n
//! Для других возвращает тип T только если он был установлен ранее.
template<typename T>
T value() const {return getAsValue<T>(*this);}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIVariant & v);
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(PIVariant && v);
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const char * v) {setValue(PIString(v)); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const bool v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const char v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const uchar v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const short v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const ushort v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const int & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const uint & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const llong & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const ullong & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const float & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const double & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const ldouble & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIBitArray & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIByteArray & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIString & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIStringList & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PITime & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIDate & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIDateTime & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PISystemTime & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIVariantTypes::Enum & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIVariantTypes::File & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIVariantTypes::Dir & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIVariantTypes::Color & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIVariantTypes::IODevice & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIPointd & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIRectd & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PILined & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIMathVectord & v) {setValue(v); return *this;}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariant & operator =(const PIMathMatrixd & v) {setValue(v); return *this;}
//! \~english Compare operator. Check type and content.
//! \~russian Оператор сравнения. Сравнивает тип и содержимое.
bool operator ==(const PIVariant & v) const;
//! \~english Compare operator. Check type and content.
//! \~russian Оператор сравнения. Сравнивает тип и содержимое.
bool operator !=(const PIVariant & v) const {return !(*this == v);}
//! \~english Returns type of variant content.
//! \~russian Возвращает тип значения.
PIVariant::Type type() const {return _type;}
//! \~english Returns type name of variant content.
//! \~russian Возвращает имя типа значения.
PIString typeName() const;
//! \~english Returns if type is not \a pivInvalid.
//! \~russian Возвращает не является ли тип \a pivInvalid.
bool isValid() const {return _type != PIVariant::pivInvalid;}
//! \~english Swaps with other %PIVariant.
//! \~russian Меняет с другим %PIVariant.
void swap(PIVariant & v);
//! \~\brief
//! \~english Returns new %PIVariant from custom type value.
//! \~russian Возвращает новый %PIVariant из значения своего типа.
//! \~\details
//! \~english
//! In case of known types this function equivalent \a PIVariant() constructors. \n
//! Otherwise returns variant with content "v" and type \a pivCustom.
//! \~russian
//! Для стандартных типов эквиваленстно конструктору \a PIVariant(). \n
//! Для других устанавливает содержимое из "v" и тип \a pivCustom.
template <typename T>
static PIVariant fromValue(const T & v) {
PIVariant ret;
ret.initType<T>(v);
return ret;
}
static PIVariant fromValue(const PIByteArray & c, const PIString & type) {
PIVariant ret;
PIVariant::Type t = typeFromName(type);
if (t == pivInvalid) {
#ifdef CUSTOM_PIVARIANT
ret._info = __PIVariantInfoStorage__::get()->map->value(type, 0);
if (ret._info) {
t = pivCustom;
} else
#endif
{
piCout << "Can`t initialize PIVariant from unregistered type \"" << type << "\"!";
return ret;
}
}
ret._type = t;
ret._content = c;
return ret;
}
//! \~english Returns type from its name.
//! \~russian Возвращает тип из его названия.
static PIVariant::Type typeFromName(const PIString & tname);
//! \~english Returns type name.
//! \~russian Возвращает имя типа.
static PIString typeName(PIVariant::Type type);
private:
void destroy() {_content.clear();}
template <typename T> inline static Type getType() {return pivCustom;}
template <typename T> inline void initType(const T & v) {
_content.clear();
_content << v;
_type = getType<T>();
#ifdef CUSTOM_PIVARIANT
if (_type == pivCustom) {
_info = __PIVariantInfoStorage__::get()->map->value(__PIVariantFunctions__<T>::typeNameHelper(), 0);
if (!_info)
piCout << "Can`t initialize PIVariant from unregistered type!";
} else
_info = 0;
#endif
}
template<typename T> inline static T getAsValue(const PIVariant & v) {
#ifdef CUSTOM_PIVARIANT
if (v._content.isEmpty() || !v._info) return T();
PIString cn = __PIVariantFunctions__<T>::typeNameHelper();
//piCout << "gav" << cn;
PIByteArray ba;
if (cn == v._info->typeName) {
ba = v._content;
} else {
__PIVariantInfo__::castHelperFunc cf = v._info->cast.value(cn);
//piCout << "gav cast" << cf;
if (!cf) return T();
ba = cf(v._content);
}
T ret; ba >> ret;
return ret;
#else
return T();
#endif
}
PIByteArray _content;
PIVariant::Type _type;
#ifdef CUSTOM_PIVARIANT
__PIVariantInfo__ * _info;
#endif
};
template<> inline bool PIVariant::value() const {return toBool();}
template<> inline char PIVariant::value() const {return (char)toInt();}
template<> inline uchar PIVariant::value() const {return (uchar)toInt();}
template<> inline short PIVariant::value() const {return (short)toInt();}
template<> inline ushort PIVariant::value() const {return (ushort)toInt();}
template<> inline int PIVariant::value() const {return toInt();}
template<> inline uint PIVariant::value() const {return (uint)toInt();}
template<> inline llong PIVariant::value() const {return toLLong();}
template<> inline ullong PIVariant::value() const {return (ullong)toLLong();}
template<> inline float PIVariant::value() const {return toFloat();}
template<> inline double PIVariant::value() const {return toDouble();}
template<> inline ldouble PIVariant::value() const {return toLDouble();}
template<> inline void* PIVariant::value() const {return (void*)toLLong();}
template<> inline const char* PIVariant::value() const {return toString().data();}
template<> inline PITime PIVariant::value() const {return toTime();}
template<> inline PIDate PIVariant::value() const {return toDate();}
template<> inline PIDateTime PIVariant::value() const {return toDateTime();}
template<> inline PIString PIVariant::value() const {return toString();}
template<> inline PIStringList PIVariant::value() const {return toStringList();}
template<> inline PIBitArray PIVariant::value() const {return toBitArray();}
template<> inline PIByteArray PIVariant::value() const {return toByteArray();}
template<> inline PIVariantTypes::Enum PIVariant::value() const {return toEnum();}
template<> inline PIVariantTypes::File PIVariant::value() const {return toFile();}
template<> inline PIVariantTypes::Dir PIVariant::value() const {return toDir();}
template<> inline PIVariantTypes::Color PIVariant::value() const {return toColor();}
template<> inline PIVariantTypes::IODevice PIVariant::value() const {return toIODevice();}
template<> inline PIPointd PIVariant::value() const {return toPoint();}
template<> inline PIRectd PIVariant::value() const {return toRect();}
template<> inline PILined PIVariant::value() const {return toLine();}
template<> inline PIVariant PIVariant::value() const {return *this;}
template<> inline PIVariant PIVariant::fromValue(const bool & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const char & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const uchar & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const short & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const ushort & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const int & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const uint & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const llong & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const ullong & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const float & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const double & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const ldouble & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIBitArray & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIByteArray & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIString & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIStringList & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PITime & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIDate & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIDateTime & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PISystemTime & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::Enum & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::File & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::Dir & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::Color & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIVariantTypes::IODevice & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIPointd & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIRectd & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PILined & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIMathVectord & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIMathMatrixd & v) {return PIVariant(v);}
template<> inline PIVariant PIVariant::fromValue(const PIVariant & v) {return PIVariant(v);}
template<> inline PIVariant::Type PIVariant::getType<bool>() {return PIVariant::pivBool;}
template<> inline PIVariant::Type PIVariant::getType<char>() {return PIVariant::pivChar;}
template<> inline PIVariant::Type PIVariant::getType<uchar>() {return PIVariant::pivUChar;}
template<> inline PIVariant::Type PIVariant::getType<short>() {return PIVariant::pivShort;}
template<> inline PIVariant::Type PIVariant::getType<ushort>() {return PIVariant::pivUShort;}
template<> inline PIVariant::Type PIVariant::getType<int>() {return PIVariant::pivInt;}
template<> inline PIVariant::Type PIVariant::getType<uint>() {return PIVariant::pivUInt;}
template<> inline PIVariant::Type PIVariant::getType<llong>() {return PIVariant::pivLLong;}
template<> inline PIVariant::Type PIVariant::getType<ullong>() {return PIVariant::pivULLong;}
template<> inline PIVariant::Type PIVariant::getType<float>() {return PIVariant::pivFloat;}
template<> inline PIVariant::Type PIVariant::getType<double>() {return PIVariant::pivDouble;}
template<> inline PIVariant::Type PIVariant::getType<ldouble>() {return PIVariant::pivLDouble;}
template<> inline PIVariant::Type PIVariant::getType<PIBitArray>() {return PIVariant::pivBitArray;}
template<> inline PIVariant::Type PIVariant::getType<PIByteArray>() {return PIVariant::pivByteArray;}
template<> inline PIVariant::Type PIVariant::getType<PIString>() {return PIVariant::pivString;}
template<> inline PIVariant::Type PIVariant::getType<PIStringList>() {return PIVariant::pivStringList;}
template<> inline PIVariant::Type PIVariant::getType<PITime>() {return PIVariant::pivTime;}
template<> inline PIVariant::Type PIVariant::getType<PIDate>() {return PIVariant::pivDate;}
template<> inline PIVariant::Type PIVariant::getType<PIDateTime>() {return PIVariant::pivDateTime;}
template<> inline PIVariant::Type PIVariant::getType<PISystemTime>() {return PIVariant::pivSystemTime;}
template<> inline PIVariant::Type PIVariant::getType<PIVariantTypes::Enum>() {return PIVariant::pivEnum;}
template<> inline PIVariant::Type PIVariant::getType<PIVariantTypes::File>() {return PIVariant::pivFile;}
template<> inline PIVariant::Type PIVariant::getType<PIVariantTypes::Dir>() {return PIVariant::pivDir;}
template<> inline PIVariant::Type PIVariant::getType<PIVariantTypes::Color>() {return PIVariant::pivColor;}
template<> inline PIVariant::Type PIVariant::getType<PIVariantTypes::IODevice>() {return PIVariant::pivIODevice;}
template<> inline PIVariant::Type PIVariant::getType<PIPointd>() {return PIVariant::pivPoint;}
template<> inline PIVariant::Type PIVariant::getType<PIRectd>() {return PIVariant::pivRect;}
template<> inline PIVariant::Type PIVariant::getType<PILined>() {return PIVariant::pivLine;}
template<> inline PIVariant::Type PIVariant::getType<PIMathVectord>() {return PIVariant::pivMathVector;}
template<> inline PIVariant::Type PIVariant::getType<PIMathMatrixd>() {return PIVariant::pivMathMatrix;}
REGISTER_VARIANT(bool)
REGISTER_VARIANT(char)
REGISTER_VARIANT(uchar)
REGISTER_VARIANT(short)
REGISTER_VARIANT(ushort)
REGISTER_VARIANT(int)
REGISTER_VARIANT(uint)
REGISTER_VARIANT(llong)
REGISTER_VARIANT(ullong)
REGISTER_VARIANT(float)
REGISTER_VARIANT(double)
REGISTER_VARIANT(ldouble)
REGISTER_VARIANT(PIBitArray)
REGISTER_VARIANT(PIByteArray)
REGISTER_VARIANT(PIString)
REGISTER_VARIANT(PIStringList)
REGISTER_VARIANT(PITime)
REGISTER_VARIANT(PIDate)
REGISTER_VARIANT(PIDateTime)
REGISTER_VARIANT(PISystemTime)
REGISTER_NS_VARIANT(PIVariantTypes, Enum)
REGISTER_NS_VARIANT(PIVariantTypes, File)
REGISTER_NS_VARIANT(PIVariantTypes, Dir)
REGISTER_NS_VARIANT(PIVariantTypes, Color)
REGISTER_NS_VARIANT(PIVariantTypes, IODevice)
REGISTER_VARIANT(PIPointd)
REGISTER_VARIANT(PIRectd)
REGISTER_VARIANT(PILined)
REGISTER_VARIANT(PIMathVectord)
REGISTER_VARIANT(PIMathMatrixd)
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariant) {
s << v._content << v._type;
if (v._type == PIVariant::pivCustom) {
#ifdef CUSTOM_PIVARIANT
if (v._info) {
s << v._info->typeName;
} else {
s << PIStringAscii("");
}
#else
s << PIStringAscii("");
#endif
}
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ(PIVariant) {
s >> v._content >> v._type;
if (v._type == PIVariant::pivCustom) {
PIString tn;
s >> tn;
#ifdef CUSTOM_PIVARIANT
PIByteArray vc = v._content;
v = PIVariant::fromValue(vc, tn);
#endif
}
return s;
}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariant & v) {
s.space(); s.saveAndSetControls(0);
s << "PIVariant(" << v.typeName();
if (v.isValid())
s << ", " << v.toString();
s << ")";
s.restoreControls(); return s;
}
#endif // PIVARIANT_H

View File

@@ -0,0 +1,186 @@
/*! \file pivariantsimple.h
* \ingroup Types
* \brief
* \~english Simple variant type
* \~russian Простой вариативный тип
*/
/*
PIP - Platform Independent Primitives
Variant simple type
Ivan Pelipenko peri4ko@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/>.
*/
#ifndef PIVARIANTSIMPLE_H
#define PIVARIANTSIMPLE_H
#include "pistring.h"
#include <typeinfo>
#ifdef MICRO_PIP
#include "pivariant.h"
#endif
class __VariantFunctionsBase__ {
public:
virtual __VariantFunctionsBase__ * instance() {return 0;}
virtual PIString typeName() const {return PIString();}
virtual uint hash() const {return 0;}
virtual void newT(void *& ptr, const void * value) {;}
virtual void newNullT(void *& ptr) {;}
virtual void assignT(void *& ptr, const void * value) {;}
virtual void deleteT(void *& ptr) {;}
};
template<typename T>
class __VariantFunctions__: public __VariantFunctionsBase__ {
public:
__VariantFunctionsBase__ * instance() final {static __VariantFunctions__<T> ret; return &ret;}
#ifdef MICRO_PIP
PIString typeName() const final {static PIString ret(PIVariant(T()).typeName()); return ret;}
#else
PIString typeName() const final {static PIString ret(typeid(T).name()); return ret;}
#endif
uint hash() const final {static uint ret = typeName().hash(); return ret;}
void newT(void *& ptr, const void * value) final {ptr = (void*)(new T(*(const T*)value));}
void newNullT(void *& ptr) final {ptr = (void*)(new T());}
void assignT(void *& ptr, const void * value) final {*(T*)ptr = *(const T*)value;}
void deleteT(void *& ptr) final {delete (T*)(ptr);}
};
//! \addtogroup Types
//! \{
//! \~\class PIVariantSimple pivariantsimple.h
//! \~\brief
//! \~english Simple variant type.
//! \~russian Простой вариативный тип.
//!
//! \~\details
//! \~english
//!
//! \~russian
//!
//! \}
class PIVariantSimple {
public:
//! \~english Construct null %PIVariantSimple
//! \~russian Создает пустой %PIVariantSimple
PIVariantSimple() {ptr = 0; f = 0;}
//! \~english Contructs a copy of %PIVariantSimple.
//! \~russian Создает копию %PIVariantSimple.
PIVariantSimple(const PIVariantSimple & v) {
ptr = 0;
f = v.f;
if (f && v.ptr)
f->newT(ptr, v.ptr);
}
~PIVariantSimple() {destroy();}
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIVariantSimple & operator=(const PIVariantSimple & v) {
destroy();
f = v.f;
if (f && v.ptr)
f->newT(ptr, v.ptr);
return *this;
}
//! \~english Set value to "v".
//! \~russian Устанавливает значение в "v".
template <typename T>
void setValue(const T & v) {
if (f) {
if (isMyType<T>()) {
f->assignT(ptr, (const void *)&v);
return;
}
}
destroy();
f = __VariantFunctions__<T>().instance();
f->newT(ptr, (const void *)&v);
}
//! \~\brief
//! \~english Returns value as type "T".
//! \~russian Возвращает значение как тип "T".
//! \~\details
//! \~english
//! In contrast of PIVariant this class has strong check of type.
//! Returns value only if this type was set before.
//!
//! \~russian
//! В отличии от PIVariant этот класс строго проверяет типы.
//! Возвращает значение только если этот же тип был установлен ранее.
//!
template <typename T>
T value() const {
if (!f) return T();
if (!isMyType<T>())
return T();
return *(T*)(ptr);
}
//! \~english Returns %PIVariantSimple with value "v".
//! \~russian Возвращает %PIVariantSimple со значением "v".
template <typename T>
static PIVariantSimple fromValue(const T & v) {
PIVariantSimple ret;
ret.setValue(v);
return ret;
}
private:
template <typename T>
bool isMyType() const {
if (!f) return false;
uint mh = f->hash(), th = __VariantFunctions__<T>().instance()->hash();
if (mh == 0 || th == 0) return false;
return mh == th;
}
void destroy() {
if (ptr && f)
f->deleteT(ptr);
ptr = 0;
f = 0;
}
void * ptr;
__VariantFunctionsBase__ * f;
};
#define REGISTER_PIVARIANTSIMPLE(Type) \
template<> \
class __VariantFunctions__<Type>: public __VariantFunctionsBase__ { \
public: \
__VariantFunctionsBase__ * instance() final {static __VariantFunctions__<Type> ret; return &ret;} \
PIString typeName() const final {static PIString ret(#Type); return ret;} \
uint hash() const final {static uint ret = typeName().hash(); return ret;} \
void newT(void *& ptr, const void * value) final {ptr = (void*)(new Type(*(const Type *)value));} \
void newNullT(void *& ptr) final {ptr = (void*)(new Type());} \
void assignT(void *& ptr, const void * value) final {*(Type *)ptr = *(const Type *)value;} \
void deleteT(void *& ptr) final {delete (Type *)(ptr);} \
};
REGISTER_PIVARIANTSIMPLE(std::function<void(void*)>)
#endif // PIVARIANTSIMPLE_H

View File

@@ -0,0 +1,158 @@
/*
PIP - Platform Independent Primitives
Variant types
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 "pivarianttypes.h"
#include "pipropertystorage.h"
#ifndef MICRO_PIP
# include "piiodevice.h"
#endif
int PIVariantTypes::Enum::selectedValue() const {
for (const auto & e: enum_list)
if (e.name == selected)
return e.value;
return 0;
}
bool PIVariantTypes::Enum::selectValue(int v) {
for (const auto & e: enum_list)
if (e.value == v) {
selected = e.name;
return true;
}
return false;
}
bool PIVariantTypes::Enum::selectName(const PIString & n) {
for (const auto & e: enum_list)
if (e.name == n) {
selected = e.name;
return true;
}
return false;
}
int PIVariantTypes::Enum::value(const PIString & n) const {
for (const auto & e: enum_list)
if (e.name == n)
return e.value;
return 0;
}
PIString PIVariantTypes::Enum::name(int v) const {
for (const auto & e: enum_list)
if (e.value == v)
return e.name;
return PIString();
}
PIVector<int> PIVariantTypes::Enum::values() const {
PIVector<int> ret;
for (const auto & e: enum_list)
ret << e.value;
return ret;
}
PIStringList PIVariantTypes::Enum::names() const {
PIStringList ret;
for (const auto & e: enum_list)
ret << e.name;
return ret;
}
PIVariantTypes::IODevice::IODevice() {
#ifndef MICRO_PIP
mode = PIIODevice::ReadWrite;
#else
mode = 0; // TODO: PIIODevice for MICRO PIP
#endif // MICRO_PIP
options = 0;
}
void PIVariantTypes::IODevice::set(const PIPropertyStorage & ps) {
props.clear();
props << ps;
}
PIPropertyStorage PIVariantTypes::IODevice::get() const {
PIPropertyStorage ret;
PIByteArray ba = props;
if (!ba.isEmpty()) ba >> ret;
return ret;
}
PIString PIVariantTypes::IODevice::toPICout() const {
PIString s;
s += "IODevice(" + prefix + ", mode=";
int rwc = 0;
if (mode & 1) {s += "r"; ++rwc;}
if (mode & 2) {s += "w"; ++rwc;}
if (rwc == 1) s += "o";
s += ", flags=";
#ifndef MICRO_PIP // TODO: PIIODevice for MICRO PIP
if (options != 0) {
if (((PIIODevice::DeviceOptions)options)[PIIODevice::BlockingRead])
s += " br";
if (((PIIODevice::DeviceOptions)options)[PIIODevice::BlockingWrite])
s += " bw";
}
#endif // MICRO_PIP
PIPropertyStorage ps = get();
piForeachC (PIPropertyStorage::Property & p, ps) {
s += ", " + p.name + "=\"" + p.value.toString() + "\"";
}
s += ")";
return s;
}
PIVariantTypes::Enum & PIVariantTypes::Enum::operator <<(const PIVariantTypes::Enumerator & v) {
enum_list << v;
return *this;
}
PIVariantTypes::Enum & PIVariantTypes::Enum::operator <<(const PIString & v) {
if (enum_list.isEmpty()) {
enum_list << Enumerator(0, v);
} else {
enum_list << Enumerator(enum_list.back().value + 1, v);
}
return *this;
}
PIVariantTypes::Enum & PIVariantTypes::Enum::operator <<(const PIStringList & v) {
for (const auto & s: v)
(*this) << s;
return *this;
}

View File

@@ -0,0 +1,379 @@
/*! \file pivarianttypes.h
* \ingroup Types
* \brief
* \~english Types for PIVariant
* \~russian Типы для PIVariant
*/
/*
PIP - Platform Independent Primitives
Variant types
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/>.
*/
#ifndef PIVARIANTYPES_H
#define PIVARIANTYPES_H
#include "pistringlist.h"
class PIPropertyStorage;
//! \ingroup Types
//! \relatesalso PIVariant
//! \~english Namespace contains several types for PIVariant
//! \~russian Пространство имен содержит некоторые типы для PIVariant
namespace PIVariantTypes {
//! \addtogroup Types
//! \{
//! \~\struct Enumerator pivarianttypes.h
//! \~\brief
//! \~english Name-value pair.
//! \~russian Пара имя-значение.
//! \}
struct PIP_EXPORT Enumerator {
Enumerator(int v = 0, const PIString & n = PIString()): value(v), name(n) {}
//! \~english Value.
//! \~russian Значение.
int value;
//! \~english Name.
//! \~russian Имя.
PIString name;
};
//! \addtogroup Types
//! \{
//! \~\struct Enum pivarianttypes.h
//! \~\brief
//! \~english Collection of PIVariantTypes::Enumerator.
//! \~russian Набор PIVariantTypes::Enumerator.
//! \~\details
//! \~english
//! This class represents classic c-style enum.
//! Contains elements with unique names and not uniqueue values, also
//! name of enum and selected member.
//!
//! \~russian
//! Этот класс представляет классический c-style enum.
//! Содержит элементы с уникальными именами и неуникальными значениями,
//! а также имя перечисления и выбранного элемента.
//!
//! \}
struct PIP_EXPORT Enum {
Enum(const PIString & n = PIString()): enum_name(n) {}
//! \~english Returns selected value, or 0 if there is no name in members.
//! \~russian Возвращает выбранное значение, или 0, если выбранного имени нет в элементах.
int selectedValue() const;
//! \~english Returns selected name.
//! \~russian Возвращает выбранное имя.
PIString selectedName() const {return selected;}
//! \~\brief
//! \~english Select value if exists in Enum.
//! \~russian Выбирает значение, если оно существует в Enum.
//!
//! \~\details
//! \~english
//! If Enum contains several PIVariantTypes::Enumerator with same values,
//! first PIVariantTypes::Enumerator will selected.
//! Returns if value exists in Enum.
//!
//! \~russian
//! Если Enum содержит несколько PIVariantTypes::Enumerator с этим значением,
//! то будет выбран первый.
//! Возвращает существовал ли такой Enum.
//!
bool selectValue(int v);
//! \~\brief
//! \~english Select name if exists in Enum.
//! \~russian Выбирает имя, если оно существует в Enum.
//!
//! \~\details
//! \~english
//! Returns if name exists in Enum.
//!
//! \~russian
//! Возвращает существовал ли такой Enum.
//!
bool selectName(const PIString & n);
//! \~english Find PIVariantTypes::Enumerator with name "n" and return it value, otherwise 0.
//! \~russian Ищет PIVariantTypes::Enumerator с именем "n" и возвращает его значение, иначе 0.
int value(const PIString & n) const;
//! \~english Find PIVariantTypes::Enumerator with value "v" and return it name, otherwise empty string.
//! \~russian Ищет PIVariantTypes::Enumerator со значением "v" и возвращает его имя, иначе пустую строку.
PIString name(int v) const;
//! \~english Returns all members values.
//! \~russian Возвращает все значения членов.
PIVector<int> values() const;
//! \~english Returns all members names.
//! \~russian Возвращает все имена членов.
PIStringList names() const;
//! \~english Add PIVariantTypes::Enumerator to Enum.
//! \~russian Добавляет в Enum PIVariantTypes::Enumerator.
Enum & operator <<(const Enumerator & v);
//! \~english Add PIVariantTypes::Enumerator with name "v" and value of last element + 1, otherwise 0.
//! \~russian Добавляет PIVariantTypes::Enumerator с именем "v" и значением последнего элемента + 1, иначе 0.
Enum & operator <<(const PIString & v);
//! \~english Add PIVariantTypes::Enumerator element for each name of vector.
//! \~russian Добавляет PIVariantTypes::Enumerator для каждой строки из массива.
Enum & operator <<(const PIStringList & v);
//! \~english Returns if Enum is empty.
//! \~russian Возвращает пустой ли Enum.
bool isEmpty() const {return enum_list.isEmpty();}
//! \~english Name of Enum.
//! \~russian Имя Enum.
PIString enum_name;
//! \~english Name of selected element.
//! \~russian Имя выбранного элемента.
PIString selected;
//! \~english Members.
//! \~russian Элементы.
PIVector<Enumerator> enum_list;
};
//! \addtogroup Types
//! \{
//! \~\struct File pivarianttypes.h
//! \~\brief
//! \~english File location description.
//! \~russian Описание положения файла.
//! \}
struct PIP_EXPORT File {
File(const PIString & p = PIString(), const PIString & f = PIString(), bool abs = false, bool save_mode = false):
file(p), filter(f), is_abs(abs), is_save(save_mode) {}
//! \~english Returns path.
//! \~russian Возвращает путь.
PIString toString() const {return file;}
//! \~english Path.
//! \~russian Путь.
PIString file;
//! \~english Name filter.
//! \~russian Фильтр имени.
PIString filter;
//! \~english If path is absolute.
//! \~russian Абсолютный ли путь.
bool is_abs;
//! \~english If save is available.
//! \~russian Доступно ли сохранение.
bool is_save;
};
//! \addtogroup Types
//! \{
//! \~\struct Dir pivarianttypes.h
//! \~\brief
//! \~english Directory description.
//! \~russian Описание директории.
//! \}
struct PIP_EXPORT Dir {
Dir(const PIString & d = PIString(), bool abs = false):
dir(d), is_abs(abs) {}
//! \~english Returns path.
//! \~russian Возвращает путь.
PIString toString() const {return dir;}
//! \~english Path.
//! \~russian Путь.
PIString dir;
//! \~english If path is absolute.
//! \~russian Абсолютный ли путь.
bool is_abs;
};
//! \addtogroup Types
//! \{
//! \~\struct Color pivarianttypes.h
//! \~\brief
//! \~english 32-bits color.
//! \~russian 32-битный цвет.
//! \}
struct PIP_EXPORT Color {
Color(uint v = 0) {rgba = v;}
//! \~english Integer color.
//! \~russian Целочисленный цвет.
uint rgba;
};
//! \addtogroup Types
//! \{
//! \~\struct IODevice pivarianttypes.h
//! \~\brief
//! \~english Input/output device description.
//! \~russian Описание устройства ввода/вывода.
//! \}
struct PIP_EXPORT IODevice {
IODevice();
//! \~english Serialize "ps" and set it to "props".
//! \~russian Сериализует "ps" и устанавливает "props".
void set(const PIPropertyStorage & ps);
//! \~english Deserialize "props" and returns it.
//! \~russian Десериализует "props" и возвращает свойства.
PIPropertyStorage get() const;
//! \~english Returns string for PICout.
//! \~russian Возвращает строку для PICout.
PIString toPICout() const;
//! \~english PIIODevice prefix, see \ref PIIODevice_sec7.
//! \~russian Префикс PIIODevice, см. \ref PIIODevice_sec7.
PIString prefix;
//! PIIODevice::DeviceMode.
int mode;
//! PIIODevice::DeviceOptions.
int options;
//! \~english Serialized properties.
//! \~russian Сериализованные свойства.
PIByteArray props;
};
}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariantTypes::Enumerator & v) {s << v.name << "(" << v.value << ")"; return s;}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariantTypes::Enum & v) {s << "Enum(" << v.selectedValue() << "=" << v.selectedName() << ")"; return s;}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariantTypes::File & v) {s << "File(\"" << v.file << "\")"; return s;}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariantTypes::Dir & v) {s << "Dir(\"" << v.dir << "\")"; return s;}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariantTypes::Color & v) {s.saveControls(); s << PICoutManipulators::Hex << "Color(#" << v.rgba << ")"; s.restoreControls(); return s;}
//! \relatesalso PICout
//! \~english Output operator to \a PICout
//! \~russian Оператор вывода в \a PICout
inline PICout operator <<(PICout s, const PIVariantTypes::IODevice & v) {s << v.toPICout(); return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariantTypes::Enumerator) {s << v.value << v.name; return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIVariantTypes::Enumerator) {s >> v.value >> v.name; return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariantTypes::Enum) {s << v.enum_name << v.selected << v.enum_list; return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIVariantTypes::Enum) {s >> v.enum_name >> v.selected >> v.enum_list; return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariantTypes::File) {s << v.file << v.filter << uchar((v.is_abs ? 1 : 0) + (v.is_save ? 2 : 0)); return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIVariantTypes::File) {uchar f(0); s >> v.file >> v.filter >> f; v.is_abs = ((f & 1) == 1); v.is_save = ((f & 2) == 2); return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariantTypes::Dir) {s << v.dir << v.is_abs; return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIVariantTypes::Dir) {s >> v.dir >> v.is_abs; return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariantTypes::Color) {s << v.rgba; return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIVariantTypes::Color) {s >> v.rgba; return s;}
//! \relatesalso PIBinaryStream
//! \~english Store operator.
//! \~russian Оператор сохранения.
BINARY_STREAM_WRITE(PIVariantTypes::IODevice) {s << v.prefix << v.mode << v.options << v.props; return s;}
//! \relatesalso PIBinaryStream
//! \~english Restore operator.
//! \~russian Оператор извлечения.
BINARY_STREAM_READ (PIVariantTypes::IODevice) {s >> v.prefix >> v.mode >> v.options >> v.props; return s;}
#endif // PIVARIANTYPES_H