15.10.2012 - version 0.2.0 - PIIODevice - base of file, ethernet, serial and packets extractor. PIEthernet now support also TCP (client and server). PIConsole now can align labels in each column individually.
This commit is contained in:
247
picontainers.h
Normal file → Executable file
247
picontainers.h
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Generic containers, based on STL
|
||||
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -124,7 +124,7 @@ template <typename T> inline _PIForeachC<T> * _PIForeachCastC(_PIForeachBase & c
|
||||
|
||||
#define piForeachRC piForeachCR
|
||||
|
||||
#define piForTimes(c) for(int i = 0; i < c; ++i)
|
||||
#define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c)
|
||||
|
||||
template<typename Enum>
|
||||
class PIFlags {
|
||||
@@ -133,27 +133,243 @@ public:
|
||||
PIFlags(Enum e): flags(e) {;}
|
||||
PIFlags(const PIFlags & f): flags(f.flags) {;}
|
||||
PIFlags(const int i): flags(i) {;}
|
||||
PIFlags & setFlag(const PIFlags & f, bool on = true) {if (on) flags |= f.flags; else flags &= ~f.flags; return *this;}
|
||||
PIFlags & setFlag(const Enum & e, bool on = true) {if (on) flags |= e; else flags &= ~e; return *this;}
|
||||
PIFlags & setFlag(const int & i, bool on = true) {if (on) flags |= i; else flags &= ~i; return *this;}
|
||||
void operator =(const PIFlags & f) {flags = f.flags;}
|
||||
void operator =(const Enum & e) {flags = e;}
|
||||
void operator =(const int & i) {flags = i;}
|
||||
void operator |=(const PIFlags & f) {flags = flags | f.flags;}
|
||||
void operator |=(const Enum & e) {flags = flags | e;}
|
||||
void operator |=(const int i) {flags = flags | i;}
|
||||
void operator &=(const PIFlags & f) {flags = flags & f.flags;}
|
||||
void operator &=(const Enum & e) {flags = flags & e;}
|
||||
void operator &=(const int i) {flags = flags & i;}
|
||||
void operator |=(const PIFlags & f) {flags |= f.flags;}
|
||||
void operator |=(const Enum & e) {flags |= e;}
|
||||
void operator |=(const int i) {flags |= i;}
|
||||
void operator &=(const PIFlags & f) {flags &= f.flags;}
|
||||
void operator &=(const Enum & e) {flags &= e;}
|
||||
void operator &=(const int i) {flags &= i;}
|
||||
void operator ^=(const PIFlags & f) {flags ^= f.flags;}
|
||||
void operator ^=(const Enum & e) {flags ^= e;}
|
||||
void operator ^=(const int i) {flags ^= i;}
|
||||
PIFlags operator |(PIFlags f) const {PIFlags tf(flags | f.flags); return tf;}
|
||||
PIFlags operator |(Enum e) const {PIFlags tf(flags | e); return tf;}
|
||||
PIFlags operator |(int i) const {PIFlags tf(flags | i); return tf;}
|
||||
PIFlags operator &(PIFlags f) const {PIFlags tf(flags & f.flags); return tf;}
|
||||
PIFlags operator &(Enum e) const {PIFlags tf(flags & e); return tf;}
|
||||
PIFlags operator &(int i) const {PIFlags tf(flags & i); return tf;}
|
||||
PIFlags operator ^(PIFlags f) const {PIFlags tf(flags ^ f.flags); return tf;}
|
||||
PIFlags operator ^(Enum e) const {PIFlags tf(flags ^ e); return tf;}
|
||||
PIFlags operator ^(int i) const {PIFlags tf(flags ^ i); return tf;}
|
||||
bool operator [](Enum e) {return (flags & e) == e;}
|
||||
operator int() const {return flags;}
|
||||
private:
|
||||
int flags;
|
||||
};
|
||||
/*
|
||||
template <typename T>
|
||||
class PIVector {
|
||||
public:
|
||||
PIVector(uint size = 0, const T & f = T()): data_(0), size_(0), rsize_(0) {resize(size, f);}
|
||||
PIVector(T * d, uint size): data_(0), size_(0), rsize_(0) {alloc(size); for (uint i = 0; i < size; ++i) data_[i] = d[i];}
|
||||
~PIVector() {dealloc();}
|
||||
|
||||
typedef T value_type;
|
||||
|
||||
class iterator {
|
||||
friend class PIVector<T>;
|
||||
private:
|
||||
iterator(PIVector<T> * v, int p): parent(v), pos(p) {}
|
||||
PIVector<T> * parent;
|
||||
int pos;
|
||||
public:
|
||||
iterator(): parent(0) {}
|
||||
T & operator *() {return (*parent)[pos];}
|
||||
const T & operator *() const {return (*parent)[pos];}
|
||||
void operator ++() {++pos;}
|
||||
void operator ++(int) {++pos;}
|
||||
void operator --() {--pos;}
|
||||
void operator --(int) {--pos;}
|
||||
bool operator ==(const iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const iterator & it) const {return (pos != it.pos);}
|
||||
};
|
||||
|
||||
class const_iterator {
|
||||
friend class PIVector<T>;
|
||||
private:
|
||||
const_iterator(const PIVector<T> * v, int p): parent(v), pos(p) {}
|
||||
const PIVector<T> * parent;
|
||||
int pos;
|
||||
public:
|
||||
const_iterator(): parent(0) {}
|
||||
//T & operator *() {return (*parent)[pos];}
|
||||
const T & operator *() const {return (*parent)[pos];}
|
||||
void operator ++() {++pos;}
|
||||
void operator ++(int) {++pos;}
|
||||
void operator --() {--pos;}
|
||||
void operator --(int) {--pos;}
|
||||
bool operator ==(const const_iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const const_iterator & it) const {return (pos != it.pos);}
|
||||
};
|
||||
|
||||
class reverse_iterator {
|
||||
friend class PIVector<T>;
|
||||
private:
|
||||
reverse_iterator(PIVector<T> * v, int p): parent(v), pos(p) {}
|
||||
PIVector<T> * parent;
|
||||
int pos;
|
||||
public:
|
||||
reverse_iterator(): parent(0) {}
|
||||
T & operator *() {return (*parent)[pos];}
|
||||
const T & operator *() const {return (*parent)[pos];}
|
||||
void operator ++() {--pos;}
|
||||
void operator ++(int) {--pos;}
|
||||
void operator --() {++pos;}
|
||||
void operator --(int) {++pos;}
|
||||
bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);}
|
||||
};
|
||||
|
||||
class const_reverse_iterator {
|
||||
friend class PIVector<T>;
|
||||
private:
|
||||
const_reverse_iterator(const PIVector<T> * v, int p): parent(v), pos(p) {}
|
||||
const PIVector<T> * parent;
|
||||
int pos;
|
||||
public:
|
||||
const_reverse_iterator(): parent(0) {}
|
||||
//T & operator *() {return (*parent)[pos];}
|
||||
const T & operator *() const {return (*parent)[pos];}
|
||||
void operator ++() {--pos;}
|
||||
void operator ++(int) {--pos;}
|
||||
void operator --() {++pos;}
|
||||
void operator --(int) {++pos;}
|
||||
bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);}
|
||||
};
|
||||
|
||||
iterator begin() {return iterator(this, 0);}
|
||||
iterator end() {return iterator(this, size_);}
|
||||
const_iterator begin() const {return const_iterator(this, 0);}
|
||||
const_iterator end() const {return const_iterator(this, size_);}
|
||||
reverse_iterator rbegin() {return reverse_iterator(this, size_ - 1);}
|
||||
reverse_iterator rend() {return reverse_iterator(this, -1);}
|
||||
const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size_ - 1);}
|
||||
const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
|
||||
|
||||
uint size() const {return size_;}
|
||||
int size_s() const {return size_;}
|
||||
int length() const {return size_;}
|
||||
bool isEmpty() const {return (size_ == 0);}
|
||||
|
||||
T & operator [](int index) {return data_[index];}
|
||||
T & at(uint index) {return data_[index];}
|
||||
const T & operator [](int index) const {return data_[index];}
|
||||
const T & at(uint index) const {return data_[index];}
|
||||
T & back() {return data_[size_ - 1];}
|
||||
const T & back() const {return data_[size_ - 1];}
|
||||
T & front() {return data_[0];}
|
||||
const T & front() const {return data_[0];}
|
||||
|
||||
bool operator ==(const PIVector<T> & t) const {if (size_ != t.size_) return false; for (uint i = 0; i < size_; ++i) if (t[i] != data_[i]) return false; return true;}
|
||||
bool operator !=(const PIVector<T> & t) const {if (size_ != t.size_) return true; for (uint i = 0; i < size_; ++i) if (t[i] != data_[i]) return true; return false;}
|
||||
bool contain(const T & v) const {for (uint i = 0; i < size_; ++i) if (v == data_[i]) return true; return false;}
|
||||
|
||||
T * data(int index = 0) {return &(data_[index]);}
|
||||
const T * data(int index = 0) const {return &(data_[index]);}
|
||||
PIVector<T> & clear() {resize(0); return *this;}
|
||||
PIVector<T> & fill(const T & f = T()) {
|
||||
if (sizeof(T) == 1) memset(data_, f, size_);
|
||||
else for (uint i = 0; i < size_; ++i) memcpy(&(data_[i]), &f, sizeof(T));
|
||||
return *this;
|
||||
}
|
||||
PIVector<T> & assign(const T & f = T()) {return fill(f);}
|
||||
PIVector<T> & resize(uint new_size, const T & f = T()) {
|
||||
if (new_size < size_) {
|
||||
size_ = new_size;
|
||||
return *this;
|
||||
}
|
||||
if (new_size > size_) {
|
||||
os = size_;
|
||||
alloc(new_size);
|
||||
uint ds = size_ - os;
|
||||
//if (sizeof(T) == 1) memset(&(data_[os]), f, ds);
|
||||
for (uint i = 0; i < ds; ++i) data_[os + i] = f;
|
||||
return *this;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIVector<T> & insert(int index, const T & v = T()) {
|
||||
push_back(v);
|
||||
if (index >= int(size_ - 1)) return *this;
|
||||
os = size_ - index;
|
||||
T * pd = new T[os * sizeof(T)];
|
||||
memcpy(pd, &(data_[index]), os * sizeof(T));
|
||||
memcpy(&(data_[index + 1]), pd, os * sizeof(T));
|
||||
delete[] pd;
|
||||
data_[index] = v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIVector<T> & remove(int index, int count = 1) {
|
||||
if (index + count >= int(size_)) {
|
||||
resize(index);
|
||||
return *this;
|
||||
}
|
||||
os = size_ - index - count;
|
||||
T * pd = new T[os * sizeof(T)];
|
||||
memcpy(pd, &(data_[index + count]), os * sizeof(T));
|
||||
memcpy(&(data_[index]), pd, os * sizeof(T));
|
||||
delete[] pd;
|
||||
resize(size_ - count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
typedef int (*CompareFunc)(const T * , const T * );
|
||||
static int compare_func(const T * t0, const T * t1) {return (*t0) == (*t1) ? 0 : ((*t0) < (*t1) ? -1 : 1);}
|
||||
PIVector<T> & sort(CompareFunc compare = compare_func) {qsort(data_, size_, sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
|
||||
|
||||
PIVector<T> & removeOne(const T & v) {for (uint i = 0; i < size_; ++i) if (data_[i] == v) {remove(i); return *this;} return *this;}
|
||||
PIVector<T> & removeAll(const T & v) {for (uint i = 0; i < size_; ++i) if (data_[i] == v) {remove(i); --i;} return *this;}
|
||||
|
||||
PIVector<T> & push_back(const T & v) {alloc(size_ + 1); data_[size_ - 1] = v; return *this;}
|
||||
PIVector<T> & append(const T & v) {return push_back(v);}
|
||||
PIVector<T> & operator <<(const T & v) {return push_back(v);}
|
||||
|
||||
PIVector<T> & push_front(const T & v) {insert(0, v); return *this;}
|
||||
PIVector<T> & prepend(const T & v) {return push_front(v);}
|
||||
//PIVector<T> & operator <<(const T & v) {return push_back(v);}
|
||||
|
||||
PIVector<T> & pop_back() {if (size_ == 0) return *this; resize(size_ - 1); return *this;}
|
||||
PIVector<T> & pop_front() {if (size_ == 0) return *this; remove(0); return *this;}
|
||||
|
||||
private:
|
||||
uint asize(uint s) {if (s == 0) return 0; if (rsize_ + rsize_ >= s && rsize_ < s) return rsize_ + rsize_; uint t = 0, s_ = s - 1; while (s_ >> t) ++t; return (1 << t);}
|
||||
void dealloc() {if (data_ != 0) delete[] data_; data_ = 0;}
|
||||
void alloc(uint new_size) {
|
||||
if (new_size <= rsize_) {
|
||||
size_ = new_size;
|
||||
return;
|
||||
}
|
||||
os = size_;
|
||||
size_ = new_size;
|
||||
uint as = asize(new_size);
|
||||
if (as == rsize_) return;
|
||||
T * pd = 0;
|
||||
if (os > 0) {
|
||||
pd = new T[os * sizeof(T)];
|
||||
memcpy(pd, data_, os * sizeof(T));
|
||||
}
|
||||
rsize_ = as;
|
||||
dealloc();
|
||||
data_ = new T[rsize_ * sizeof(T)];
|
||||
if (os > 0) {
|
||||
memcpy(data_, pd, size_ * sizeof(T));
|
||||
delete[] pd;
|
||||
}
|
||||
}
|
||||
|
||||
T * data_;
|
||||
uint size_, rsize_, os;
|
||||
};
|
||||
*/
|
||||
template<typename Type, typename Allocator = std::allocator<Type> >
|
||||
class PIVector: public vector<Type, Allocator> {
|
||||
typedef PIVector<Type, Allocator> _CVector;
|
||||
@@ -172,15 +388,20 @@ public:
|
||||
Type * data(uint index = 0) {return &(*this)[index];}
|
||||
int size_s() const {return static_cast<int>(_stlc::size());}
|
||||
bool isEmpty() const {return _stlc::empty();}
|
||||
typedef int (*CompareFunc)(const Type * , const Type * );
|
||||
static int compare_func(const Type * t0, const Type * t1) {return (*t0) == (*t1) ? 0 : ((*t0) < (*t1) ? -1 : 1);}
|
||||
_CVector & sort(CompareFunc compare = compare_func) {qsort(&at(0), _stlc::size(), sizeof(Type), (int(*)(const void * , const void * ))compare); return *this;}
|
||||
_CVector & fill(const Type & t) {_stlc::assign(_stlc::size(), t); return *this;}
|
||||
_CVector & pop_front() {_stlc::erase(_stlc::begin()); return *this;}
|
||||
_CVector & push_front(const Type & t) {_stlc::insert(_stlc::begin(), t); return *this;}
|
||||
_CVector & remove(uint num) {_stlc::erase(_stlc::begin() + num); return *this;}
|
||||
_CVector & remove(uint num, uint count) {_stlc::erase(_stlc::begin() + num, _stlc::begin() + num + count); return *this;}
|
||||
_CVector & remove(const Type & t) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) {_stlc::erase(i); --i;} return *this;}
|
||||
//_CVector & remove(const Type & t) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) {_stlc::erase(i); --i;} return *this;}
|
||||
_CVector & removeOne(const Type & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); return *this;} return *this;}
|
||||
_CVector & removeAll(const Type & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); --i;} return *this;}
|
||||
_CVector & insert(uint pos, const Type & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;}
|
||||
_CVector & operator <<(const Type & t) {_stlc::push_back(t); return *this;}
|
||||
_CVector & operator <<(const _CVector & t) {for (typename _stlc::iterator i = t.begin(); i != t.end(); i++) _stlc::push_back(*i); return *this;}
|
||||
_CVector & operator <<(const _CVector & t) {for (typename _stlc::const_iterator i = t.begin(); i != t.end(); i++) _stlc::push_back(*i); return *this;}
|
||||
bool operator ==(const _CVector & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return false; return true;}
|
||||
bool operator !=(const _CVector & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return true; return false;}
|
||||
bool contain(const Type & v) const {for (uint i = 0; i < _stlc::size(); ++i) if (v == at(i)) return true; return false;}
|
||||
@@ -212,7 +433,7 @@ public:
|
||||
_CList & remove(uint num, uint count) {_stlc::erase(_stlc::begin() + num, _stlc::begin() + num + count); return *this;}
|
||||
_CList & insert(uint pos, const Type & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;}
|
||||
_CList & operator <<(const Type & t) {_stlc::push_back(t); return *this;}
|
||||
PIVector<Type, Allocator> toVector() {PIVector<Type, Allocator> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
PIVector<Type> toVector() {PIVector<Type> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
};
|
||||
|
||||
template<typename Type, typename Compare = std::less<Type>, typename Allocator = std::allocator<Type> >
|
||||
@@ -232,7 +453,7 @@ public:
|
||||
_CSet & remove(uint num, uint count) {_stlc::erase(_stlc::begin() + num, _stlc::begin() + num + count); return *this;}
|
||||
_CSet & operator <<(const Type & t) {_stlc::insert(t); return *this;}
|
||||
bool operator [](const Type & t) {return _stlc::find(t);}
|
||||
PIVector<Type, Allocator> toVector() {PIVector<Type, Allocator> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
PIVector<Type> toVector() {PIVector<Type> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
@@ -265,7 +486,7 @@ public:
|
||||
int size_s() const {return static_cast<int>(_stlc::size());}
|
||||
bool isEmpty() const {return _stlc::empty();}
|
||||
_CDeque & operator <<(const Type & t) {_CDeque::push_back(t); return *this;}
|
||||
PIVector<Type, Allocator> toVector() {PIVector<Type, Allocator> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
PIVector<Type> toVector() {PIVector<Type> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
|
||||
Reference in New Issue
Block a user