CXX11 support for PITimer
git-svn-id: svn://db.shs.com.ru/pip@882 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
381
main.cpp
381
main.cpp
@@ -1,380 +1,15 @@
|
|||||||
#include "pip.h"
|
#include "pip.h"
|
||||||
|
|
||||||
|
void test() {
|
||||||
template <typename Key, typename T>
|
piCout << "test";
|
||||||
class PIHash {
|
|
||||||
//template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIHash<Key1, T1> & v);
|
|
||||||
//template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIHash<Key1, T1> & v);
|
|
||||||
public:
|
|
||||||
PIHash() {;}
|
|
||||||
PIHash(const PIHash<Key, T> & other) {*this = other;}
|
|
||||||
virtual ~PIHash() {;}
|
|
||||||
|
|
||||||
PIHash<Key, T> & operator =(const PIHash<Key, T> & other) {
|
|
||||||
if (this == &other) return *this;
|
|
||||||
clear();
|
|
||||||
pih_content = other.pih_content;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef T mapped_type;
|
|
||||||
typedef Key key_type;
|
|
||||||
typedef PIPair<Key, T> value_type;
|
|
||||||
/*
|
|
||||||
class iterator {
|
|
||||||
friend class PIHash<Key, T>;
|
|
||||||
private:
|
|
||||||
iterator(const PIHash<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
|
||||||
const PIHash<Key, T> * parent;
|
|
||||||
ssize_t pos;
|
|
||||||
public:
|
|
||||||
iterator(): parent(0), pos(0) {}
|
|
||||||
const Key & key() const {return const_cast<PIHash<Key, T> * >(parent)->_key(pos);}
|
|
||||||
T & value() {return const_cast<PIHash<Key, T> * >(parent)->_value(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 reverse_iterator {
|
|
||||||
friend class PIHash<Key, T>;
|
|
||||||
private:
|
|
||||||
reverse_iterator(const PIHash<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
|
||||||
const PIHash<Key, T> * parent;
|
|
||||||
ssize_t pos;
|
|
||||||
public:
|
|
||||||
reverse_iterator(): parent(0), pos(0) {}
|
|
||||||
const Key & key() const {return const_cast<PIHash<Key, T> * >(parent)->_key(pos);}
|
|
||||||
T & value() const {return const_cast<PIHash<Key, T> * >(parent)->_value(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_iterator {
|
|
||||||
friend class PIHash<Key, T>;
|
|
||||||
private:
|
|
||||||
const_iterator(const PIHash<Key, T> * v, ssize_t p): parent(v), pos(p), bpos(0) {
|
|
||||||
if (pos == 0) {
|
|
||||||
pos = -1;
|
|
||||||
nextPos();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void nextPos() {
|
|
||||||
while (++pos < parent->pih_content.size_s()) {
|
|
||||||
if (!parent->pih_content[pos].isEmpty())
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const PIHash<Key, T> * parent;
|
|
||||||
ssize_t pos, bpos;
|
|
||||||
public:
|
|
||||||
const_iterator(): parent(0), pos(0) {}
|
|
||||||
//const value_type operator *() const {return parent->_pair(pos);}
|
|
||||||
//const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
|
||||||
const Key & key() const {return const_cast<PIHash<Key, T> * >(parent)->pih_content[pos][bpos].key;}
|
|
||||||
const T & value() const {return const_cast<PIHash<Key, T> * >(parent)->pih_content[pos][bpos].value;}
|
|
||||||
void operator ++() {
|
|
||||||
if (pos < parent->pih_content.size_s()) {
|
|
||||||
if (bpos >= parent->pih_content[pos].size_s() - 1) {
|
|
||||||
bpos = 0;
|
|
||||||
nextPos();
|
|
||||||
} else
|
|
||||||
++bpos;
|
|
||||||
} else {
|
|
||||||
bpos = 0;
|
|
||||||
++pos;
|
|
||||||
}
|
|
||||||
printf(" ++: %d %d\n", pos, bpos);
|
|
||||||
}
|
|
||||||
//void operator ++(int) {++pos;}
|
|
||||||
void operator --() {
|
|
||||||
--pos;
|
|
||||||
}
|
|
||||||
//void operator --(int) {--pos;}
|
|
||||||
bool operator ==(const const_iterator & it) const {return (pos == it.pos && bpos == it.bpos);}
|
|
||||||
bool operator !=(const const_iterator & it) const {return !(*this == it);}
|
|
||||||
mutable value_type cval;
|
|
||||||
};
|
|
||||||
/*
|
|
||||||
class const_reverse_iterator {
|
|
||||||
friend class PIHash<Key, T>;
|
|
||||||
private:
|
|
||||||
const_reverse_iterator(const PIHash<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
|
||||||
const PIHash<Key, T> * parent;
|
|
||||||
ssize_t pos;
|
|
||||||
public:
|
|
||||||
const_reverse_iterator(): parent(0), pos(0) {}
|
|
||||||
const value_type operator *() const {return parent->_pair(pos);}
|
|
||||||
const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
|
||||||
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);}
|
|
||||||
mutable value_type cval;
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
//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());}
|
|
||||||
const_iterator constBegin() const {return const_iterator(this, 0);}
|
|
||||||
const_iterator constEnd() 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);}
|
|
||||||
//const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);}
|
|
||||||
//const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);}
|
|
||||||
|
|
||||||
size_t size() const {return pih_content.size();}
|
|
||||||
int size_s() const {return pih_content.size_s();}
|
|
||||||
size_t length() const {return pih_content.size();}
|
|
||||||
size_t capacity() const {return pih_content.size();}
|
|
||||||
bool isEmpty() const {return (pih_content.size() == 0);}
|
|
||||||
|
|
||||||
T & operator [](const Key & key) {
|
|
||||||
if (pih_content.isEmpty()) _rehash(1);
|
|
||||||
uint k = piHash(key);
|
|
||||||
int i = _index(k);
|
|
||||||
if (i < pih_content.size_s()) {
|
|
||||||
PIVector<HashEntry> & hv(pih_content[i]);
|
|
||||||
if (hv.size_s() == 1) {
|
|
||||||
if (hv[0].key == k)
|
|
||||||
return hv[0].value;
|
|
||||||
}
|
|
||||||
for (int j = 0; j < hv.size_s(); ++j)
|
|
||||||
if (hv[j].key == k)
|
|
||||||
return hv[j].value;
|
|
||||||
}
|
|
||||||
if (pih_content[i].size_s() >= 3)
|
|
||||||
_rehash(pih_content.size_s() * 2);
|
|
||||||
i = _index(k);
|
|
||||||
pih_content[i] << HashEntry(k);
|
|
||||||
return pih_content[i].back().value;
|
|
||||||
}
|
|
||||||
const T operator [](const Key & key) const {return value(key);}
|
|
||||||
T & at(const Key & key) {return (*this)[key];}
|
|
||||||
const T at(const Key & key) const {return (*this)[key];}
|
|
||||||
|
|
||||||
PIHash<Key, T> & operator <<(const PIHash<Key, T> & other) {
|
|
||||||
if (other.isEmpty()) return *this;
|
|
||||||
for (int i = 0; i < other.pih_content.size_s(); ++i)
|
|
||||||
for (int j = 0; j < other.pih_content[i].size_s(); ++j)
|
|
||||||
insert(other.pih_content[i][j].key, other.pih_content[i][j].value);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator ==(const PIHash<Key, T> & t) const {return (pih_content == t.pih_content);}
|
|
||||||
bool operator !=(const PIHash<Key, T> & t) const {return (pih_content != t.pih_content);}
|
|
||||||
bool contains(const Key & key) const {
|
|
||||||
bool f(false);
|
|
||||||
_find(key, f);
|
|
||||||
return f;
|
|
||||||
}
|
|
||||||
|
|
||||||
PIHash<Key, T> & reserve(size_t new_size) {_rehash(new_size); return *this;}
|
|
||||||
|
|
||||||
PIHash<Key, T> & remove(const Key & key) {
|
|
||||||
uint k = piHash(key);
|
|
||||||
int i = _index(k);
|
|
||||||
if (i >= pih_content.size_s()) return *this;
|
|
||||||
PIVector<HashEntry> & hv(pih_content[i]);
|
|
||||||
for (int j = 0; j < hv.size_s(); ++j)
|
|
||||||
if (hv[j].key == k) {
|
|
||||||
hv.remove(j);
|
|
||||||
--j;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
PIHash<Key, T> & erase(const Key & key) {return remove(key);}
|
|
||||||
PIHash<Key, T> & clear() {pih_content.clear(); return *this;}
|
|
||||||
|
|
||||||
void swap(PIHash<Key, T> & other) {
|
|
||||||
piSwapBinary<PIVector<PIVector<HashEntry> > >(pih_content, other.pih_content);
|
|
||||||
}
|
|
||||||
|
|
||||||
PIHash<Key, T> & insert(const Key & key, const T & value) {
|
|
||||||
(*this)[key] = value;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
const T value(const Key & key, const T & default_ = T()) const {
|
|
||||||
uint k = piHash(key);
|
|
||||||
int i = _index(k);
|
|
||||||
if (i >= pih_content.size_s()) return default_;
|
|
||||||
const PIVector<HashEntry> & hv(pih_content[i]);
|
|
||||||
for (int j = 0; j < hv.size_s(); ++j)
|
|
||||||
if (hv[j].key == k)
|
|
||||||
return hv[j].value;
|
|
||||||
return default_;
|
|
||||||
}
|
|
||||||
PIVector<T> values() const {
|
|
||||||
PIVector<T> ret;
|
|
||||||
for (int i = 0; i < pih_content.size_s(); ++i)
|
|
||||||
for (int j = 0; j < pih_content[i].size_s(); ++j)
|
|
||||||
ret << pih_content[i][j].value;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
/*Key key(const T & value_, const Key & default_ = Key()) const {
|
|
||||||
for (int i = 0; i < pih_content.size_s(); ++i)
|
|
||||||
for (int j = 0; j < pih_content[i].size_s(); ++j)
|
|
||||||
if (pih_content[i][j].value == value_)
|
|
||||||
return pih_content[i][j].key;
|
|
||||||
return default_;
|
|
||||||
}
|
|
||||||
PIVector<Key> keys() const {
|
|
||||||
PIVector<Key> ret;
|
|
||||||
for (int i = 0; i < pih_content.size_s(); ++i)
|
|
||||||
for (int j = 0; j < pih_content[i].size_s(); ++j)
|
|
||||||
ret << pih_content[i][j].key;
|
|
||||||
return ret;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*void dump() {
|
|
||||||
piCout << "PIHash" << size() << "entries" << PICoutManipulators::NewLine << "content:";
|
|
||||||
for (size_t i = 0; i < pih_content.size(); ++i)
|
|
||||||
piCout << PICoutManipulators::Tab << i << ":" << pih_content[i];
|
|
||||||
piCout << "index:";
|
|
||||||
for (size_t i = 0; i < pim_index.size(); ++i)
|
|
||||||
piCout << PICoutManipulators::Tab << i << ":" << pim_index[i].key << "->" << pim_index[i].index;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
protected:
|
|
||||||
struct HashEntry {
|
|
||||||
HashEntry(uint k = 0, const T & v = T()): key(k), value(v) {;}
|
|
||||||
uint key;
|
|
||||||
T value;
|
|
||||||
bool operator ==(const HashEntry & s) const {return key == s.key;}
|
|
||||||
bool operator !=(const HashEntry & s) const {return key != s.key;}
|
|
||||||
bool operator <(const HashEntry & s) const {return key < s.key;}
|
|
||||||
bool operator >(const HashEntry & s) const {return key > s.key;}
|
|
||||||
};
|
|
||||||
/*template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIDeque<typename PIHash<Key1, T1>::HashEntry> & v);
|
|
||||||
template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIDeque<typename PIHash<Key1, T1>::HashEntry> & v);
|
|
||||||
|
|
||||||
const value_type _pair(ssize_t index) const {
|
|
||||||
if (index < 0 || index >= pim_index.size_s())
|
|
||||||
return value_type();
|
|
||||||
//piCout << "_pair" << index << pim_index[index].index;
|
|
||||||
return value_type(pim_index[index].key, pih_content[pim_index[index].index]);
|
|
||||||
}
|
|
||||||
Key & _key(ssize_t index) {return pim_index[index].key;}
|
|
||||||
T & _value(ssize_t index) {return pih_content[pim_index[index].index];}*/
|
|
||||||
|
|
||||||
inline size_t asize(size_t s) {
|
|
||||||
if (s == 0) return 0;
|
|
||||||
if (pih_content.size() + pih_content.size() >= s && pih_content.size() < s)
|
|
||||||
return pih_content.size() + pih_content.size();
|
|
||||||
ssize_t t = 0, s_ = s - 1;
|
|
||||||
while (s_ >> t) ++t;
|
|
||||||
return (1 << t);
|
|
||||||
}
|
|
||||||
int _index(const uint & k) const {
|
|
||||||
return k % pih_content.size_s();
|
|
||||||
}
|
|
||||||
void _rehash(int ns) {
|
|
||||||
ns = asize(ns);
|
|
||||||
if (pih_content.size_s() == ns) return;
|
|
||||||
PIVector<PIVector<HashEntry> > nhc;
|
|
||||||
nhc.resize(ns);
|
|
||||||
for (int i = 0; i < pih_content.size_s(); ++i) {
|
|
||||||
for (int j = 0; j < pih_content[i].size_s(); ++j) {
|
|
||||||
HashEntry & e(pih_content[i][j]);
|
|
||||||
int ni = e.key % ns;
|
|
||||||
nhc[ni] << e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pih_content.swap(nhc);
|
|
||||||
}
|
|
||||||
|
|
||||||
PIVector<PIVector<HashEntry> > pih_content;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Key, typename Type>
|
|
||||||
inline PICout operator <<(PICout s, const PIHash<Key, Type> & v) {
|
|
||||||
s.space();
|
|
||||||
s.setControl(0, true);
|
|
||||||
s << "{";
|
|
||||||
bool first = true;
|
|
||||||
for (typename PIHash<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
|
|
||||||
if (!first)
|
|
||||||
s << ", ";
|
|
||||||
first = false;
|
|
||||||
s << i.key() << ": " << i.value();
|
|
||||||
}
|
|
||||||
s << "}";
|
|
||||||
s.restoreControl();
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
PIConnection con;
|
PITimer t([](){piCout << "timer";});
|
||||||
PIString _str = "[connection]\ndevice.d = ser://COM2:9600\n";
|
// t.setSlot(test);
|
||||||
con.configureFromString(&_str);
|
t.addDelimiter(5, [](void * d){piCout << "delim 5";});
|
||||||
con.start();
|
t.addDelimiter(2, [](){piCout << "delim 2";});
|
||||||
PISerial * s = con.deviceByName("d")->cast<PISerial>();
|
t.start(100);
|
||||||
while (1) {
|
piMSleep(2000);
|
||||||
piMSleep(500);
|
|
||||||
piCout << s;
|
|
||||||
if (s)
|
|
||||||
piCout << s->isOpened();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
PIHash<PIString, PIString> h;
|
|
||||||
h["1"] = "x";
|
|
||||||
h["2"] = "bbb";
|
|
||||||
h["3"] = "bbc";
|
|
||||||
h["4"] = "aaa";
|
|
||||||
h["5"] = "123";
|
|
||||||
h["6"] = "321";
|
|
||||||
h["7"] = "aaa";
|
|
||||||
h["7"] = "aaa!!!!!";
|
|
||||||
piCout << h.size() << h.capacity();
|
|
||||||
//h.reserve(64);
|
|
||||||
//piCout << h.keys() << h.values();
|
|
||||||
//piCout << (1 << 2);
|
|
||||||
PIHash<PIString, PIString> h2;
|
|
||||||
PIMap<PIString, PIString> m2;
|
|
||||||
PIString prefix = "1234567890";
|
|
||||||
PITimeMeasurer tm;
|
|
||||||
double el = 0.;
|
|
||||||
|
|
||||||
tm.reset();
|
|
||||||
for (int i=0; i<10000; ++i) {
|
|
||||||
h2[prefix + PIString::fromNumber(i)+"1234567890"] = PIString::fromNumber(randomi());
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m(); piCout << el << h2.capacity();
|
|
||||||
|
|
||||||
tm.reset();
|
|
||||||
for (int i=0; i<10000; ++i) {
|
|
||||||
m2[prefix + PIString::fromNumber(i)+"1234567890"] = PIString::fromNumber(randomi());
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m(); piCout << el;
|
|
||||||
piCout << "*********";
|
|
||||||
|
|
||||||
PIString _s;
|
|
||||||
tm.reset();
|
|
||||||
for (int i=10000; i>=0; --i) {
|
|
||||||
_s = h2.value(prefix + PIString::fromNumber(i)+"1234567890");
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m(); piCout << el << h2.capacity();
|
|
||||||
|
|
||||||
tm.reset();
|
|
||||||
for (int i=10000; i>=0; --i) {
|
|
||||||
_s = m2.value(prefix + PIString::fromNumber(i)+"1234567890");
|
|
||||||
}
|
|
||||||
el = tm.elapsed_m(); piCout << el;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,10 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
|
# include <functional>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
# ifdef CC_VC
|
# ifdef CC_VC
|
||||||
|
|||||||
@@ -20,6 +20,10 @@
|
|||||||
#ifndef PIPLATFORM_H
|
#ifndef PIPLATFORM_H
|
||||||
#define PIPLATFORM_H
|
#define PIPLATFORM_H
|
||||||
|
|
||||||
|
#if (__cplusplus >= 201103L) // стандарт C++ 11 или выше
|
||||||
|
#define PIP_CXX11_SUPPORT
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
|
||||||
# define WINDOWS
|
# define WINDOWS
|
||||||
# define ARCH_BITS_32
|
# define ARCH_BITS_32
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#define PIVERSION_H
|
#define PIVERSION_H
|
||||||
|
|
||||||
#define PIP_VERSION_MAJOR 1
|
#define PIP_VERSION_MAJOR 1
|
||||||
#define PIP_VERSION_MINOR 11
|
#define PIP_VERSION_MINOR 12
|
||||||
#define PIP_VERSION_REVISION 0
|
#define PIP_VERSION_REVISION 0
|
||||||
#define PIP_VERSION_SUFFIX "_alpha"
|
#define PIP_VERSION_SUFFIX "_alpha"
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,8 @@
|
|||||||
|
|
||||||
|
|
||||||
_PITimerBase::_PITimerBase() {
|
_PITimerBase::_PITimerBase() {
|
||||||
interval_ = deferred_delay = 0.;
|
interval_ = 1000;
|
||||||
|
deferred_delay = 0.;
|
||||||
running_ = deferred_ = deferred_mode = false;
|
running_ = deferred_ = deferred_mode = false;
|
||||||
tfunc = 0;
|
tfunc = 0;
|
||||||
parent = 0;
|
parent = 0;
|
||||||
@@ -481,14 +482,22 @@ PITimer::PITimer(TimerEvent slot, void * data, PITimer::TimerImplementation ti):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//PITimer::PITimer(const PITimer & other): PIObject() {
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
// imp_mode = other.imp_mode;
|
PITimer::PITimer(std::function<void ()> slot, PITimer::TimerImplementation ti) {
|
||||||
// initFirst();
|
imp_mode = ti;
|
||||||
// data_t = other.data_t;
|
initFirst();
|
||||||
// ret_func = other.ret_func;
|
ret_func = [slot](void *, int){slot();};
|
||||||
//}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PITimer::PITimer(std::function<void (void *)> slot, void * data, PITimer::TimerImplementation ti) {
|
||||||
|
imp_mode = ti;
|
||||||
|
initFirst();
|
||||||
|
data_t = data;
|
||||||
|
ret_func = [slot](void *d, int){slot(d);};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
PITimer::~PITimer() {
|
PITimer::~PITimer() {
|
||||||
destroy();
|
destroy();
|
||||||
}
|
}
|
||||||
@@ -579,6 +588,7 @@ void PITimer::tickImp() {
|
|||||||
|
|
||||||
|
|
||||||
bool PITimer::start() {
|
bool PITimer::start() {
|
||||||
|
init();
|
||||||
//piCout << this << "start" << imp;
|
//piCout << this << "start" << imp;
|
||||||
return imp->start();
|
return imp->start();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,11 @@
|
|||||||
#include "pithread.h"
|
#include "pithread.h"
|
||||||
#include "pitime.h"
|
#include "pitime.h"
|
||||||
|
|
||||||
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
typedef void (*TimerEvent)(void * , int );
|
typedef std::function<void(void *, int)> TimerEvent;
|
||||||
|
#else
|
||||||
|
typedef void (*TimerEvent)(void *, int);
|
||||||
|
#endif
|
||||||
|
|
||||||
class PITimer;
|
class PITimer;
|
||||||
|
|
||||||
@@ -91,10 +94,16 @@ public:
|
|||||||
//! \brief Constructs timer with "ti" implementation
|
//! \brief Constructs timer with "ti" implementation
|
||||||
explicit PITimer(TimerImplementation ti);
|
explicit PITimer(TimerImplementation ti);
|
||||||
|
|
||||||
//! \brief Constructs timer with "slot" slot, "data" data and "ti" implementation
|
//! \brief Constructs timer with "slot" slot void(void *,int), "data" data and "ti" implementation
|
||||||
explicit PITimer(TimerEvent slot, void * data = 0, TimerImplementation ti = Thread);
|
explicit PITimer(TimerEvent slot, void * data = 0, TimerImplementation ti = Thread);
|
||||||
|
|
||||||
// PITimer(const PITimer & other);
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
|
//! \brief Constructs timer with "slot" slot void(), and "ti" implementation
|
||||||
|
explicit PITimer(std::function<void ()> slot, TimerImplementation ti = Thread);
|
||||||
|
|
||||||
|
//! \brief Constructs timer with "slot" slot void(void *), "data" data and "ti" implementation
|
||||||
|
explicit PITimer(std::function<void (void *)> slot, void * data, TimerImplementation ti = Thread);
|
||||||
|
#endif
|
||||||
|
|
||||||
virtual ~PITimer();
|
virtual ~PITimer();
|
||||||
|
|
||||||
@@ -150,7 +159,15 @@ public:
|
|||||||
|
|
||||||
//! \brief Set timer tick function
|
//! \brief Set timer tick function
|
||||||
void setSlot(TimerEvent slot) {ret_func = slot;}
|
void setSlot(TimerEvent slot) {ret_func = slot;}
|
||||||
|
|
||||||
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
|
//! \brief Set timer tick function
|
||||||
|
void setSlot(std::function<void ()> slot) {ret_func = [slot](void *, int){slot();};}
|
||||||
|
|
||||||
|
//! \brief Set timer tick function
|
||||||
|
void setSlot(std::function<void (void *)> slot) {ret_func = [slot](void *d, int){slot(d);};}
|
||||||
|
#endif
|
||||||
|
|
||||||
//! \brief Returns common data passed to tick functions
|
//! \brief Returns common data passed to tick functions
|
||||||
void * data() const {return data_t;}
|
void * data() const {return data_t;}
|
||||||
|
|
||||||
@@ -168,21 +185,18 @@ public:
|
|||||||
|
|
||||||
//! \brief Add frequency delimiter \b delim with optional delimiter slot \b slot.
|
//! \brief Add frequency delimiter \b delim with optional delimiter slot \b slot.
|
||||||
void addDelimiter(int delim, TimerEvent slot = 0) {delims << Delimiter(slot, delim);}
|
void addDelimiter(int delim, TimerEvent slot = 0) {delims << Delimiter(slot, delim);}
|
||||||
|
|
||||||
|
#ifdef PIP_CXX11_SUPPORT
|
||||||
|
//! \brief Add frequency delimiter \b delim with optional delimiter slot \b slot.
|
||||||
|
void addDelimiter(int delim, std::function<void ()> slot) {delims << Delimiter([slot](void *, int){slot();}, delim);}
|
||||||
|
|
||||||
|
//! \brief Add frequency delimiter \b delim with optional delimiter slot \b slot.
|
||||||
|
void addDelimiter(int delim, std::function<void (void *)> slot) {delims << Delimiter([slot](void *d, int){slot(d);}, delim);}
|
||||||
|
#endif
|
||||||
|
|
||||||
//! \brief Remove all frequency delimiters \b delim.
|
//! \brief Remove all frequency delimiters \b delim.
|
||||||
void removeDelimiter(int delim) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].delim == delim) {delims.remove(i); i--;}}
|
void removeDelimiter(int delim) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].delim == delim) {delims.remove(i); i--;}}
|
||||||
|
|
||||||
//! \brief Remove all frequency delimiters with slot \b slot.
|
|
||||||
void removeDelimiter(TimerEvent slot) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].slot == slot) {delims.remove(i); i--;}}
|
|
||||||
|
|
||||||
//! \brief Remove all frequency delimiters \b delim with slot \b slot.
|
|
||||||
void removeDelimiter(int delim, TimerEvent slot) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].slot == slot && delims[i].delim == delim) {delims.remove(i); i--;}}
|
|
||||||
|
|
||||||
void setDelimiterValue(int delim, int value) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].delim == delim) delims[i].tick = value;}
|
|
||||||
void setDelimiterValue(TimerEvent slot, int value) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].slot == slot) delims[i].tick = value;}
|
|
||||||
void setDelimiterValue(int delim, TimerEvent slot, int value) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].slot == slot && delims[i].delim == delim) delims[i].tick = value;}
|
|
||||||
int delimiterValue(int delim) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].delim == delim) return delims[i].tick; return -1;}
|
|
||||||
int delimiterValue(int delim, TimerEvent slot) {for (int i = 0; i < delims.size_s(); ++i) if (delims[i].slot == slot && delims[i].delim == delim) return delims[i].tick; return -1;}
|
|
||||||
EVENT_HANDLER0(void, clearDelimiters) {delims.clear();}
|
EVENT_HANDLER0(void, clearDelimiters) {delims.clear();}
|
||||||
|
|
||||||
EVENT2(tickEvent, void * , data_, int, delimiter)
|
EVENT2(tickEvent, void * , data_, int, delimiter)
|
||||||
|
|||||||
Reference in New Issue
Block a user