replace piForeach* to for(:)

another c++11 try ...
This commit is contained in:
2020-07-30 20:08:33 +03:00
parent 2ffc457566
commit 557f2a4d0d
18 changed files with 151 additions and 235 deletions

View File

@@ -97,141 +97,32 @@
*/
# define piForeachCR(i,c)
/*!\brief Macro for break from any piForeach* loop
* \details \warning C++ ordinary "break" doesn`t work inside piForeach*
* loops! Always use "piBreak" instead!
*/
# define piBreak
#else
# define piBreak {_for._end = true; break;}
template <typename C>
struct _reverse_wrapper {
C & c_;
_reverse_wrapper(C & c): c_(c) {}
_reverse_wrapper(const C & c): c_(const_cast<C&>(c)) {}
typename C::reverse_iterator begin() {return c_.rbegin();}
typename C::reverse_iterator end() {return c_.rend(); }
typename C::const_reverse_iterator begin() const {return c_.rbegin();}
typename C::const_reverse_iterator end() const {return c_.rend(); }
};
template <typename C> _reverse_wrapper<C> _reverse_wrap(C & c) {return _reverse_wrapper<C>(c);}
template <typename C> _reverse_wrapper<C> _reverse_wrap(const C & c) {return _reverse_wrapper<C>(c);}
# define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c)
#ifdef CC_GCC
# define piForeach(i,c) for(i : c)
# define piForeachC(i,c) for(const i : c)
# define piForeachR(i,c) for(i : _reverse_wrap(c))
# define piForeachRC(i,c) for(const i : _reverse_wrap(c))
template<typename Type>
class _PIForeach {
public:
_PIForeach(Type & t): _t(t), _break(false), _end(false) {_it = _t.begin();}
typename Type::value_type _var;
typename Type::iterator _it;
Type & _t;
bool _break, _end;
inline bool isEnd() {return _it == _t.end();}
inline void operator ++() {if (_end) _it = _t.end(); else _it++; _break = false;}
};
template<typename Type>
class _PIForeachR {
public:
_PIForeachR(Type & t): _t(t), _break(false), _end(false) {_rit = _t.rbegin();}
typename Type::value_type _var;
typename Type::reverse_iterator _rit;
Type & _t;
bool _break, _end;
inline bool isEnd() {return _rit == _t.rend();}
inline void operator ++() {if (_end) _rit = _t.rend(); else _rit++; _break = false;}
};
template<typename Type>
class _PIForeachC {
public:
_PIForeachC(const Type & t): _t(t), _break(false), _end(false) {_it = _t.begin();}
typename Type::value_type _var;
typename Type::const_iterator _it;
const Type & _t;
bool _break, _end;
inline bool isEnd() {return _it == _t.end();}
inline void operator ++() {if (_end) _it = _t.end(); else _it++; _break = false;}
};
template<typename Type>
class _PIForeachCR {
public:
_PIForeachCR(const Type & t): _t(t), _break(false), _end(false) {_rit = _t.rbegin();}
typename Type::value_type _var;
typename Type::const_reverse_iterator _rit;
const Type & _t;
bool _break, _end;
inline bool isEnd() {return _rit == _t.rend();}
inline void operator ++() {if (_end) _rit = _t.rend(); else _rit++; _break = false;}
};
#define piForeach(i,c) for(_PIForeach<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(i(*_for._it); !_for._break; _for._break = true)
#define piForeachR(i,c) for(_PIForeachR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(i(*_for._rit); !_for._break; _for._break = true)
#define piForeachA(i,c) for(_PIForeach<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true)
#define piForeachAR(i,c) for(_PIForeachR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true)
#define piForeachC(i,c) for(_PIForeachC<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const i(*_for._it); !_for._break; _for._break = true)
#define piForeachCR(i,c) for(_PIForeachCR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const i(*_for._rit); !_for._break; _for._break = true)
#define piForeachCA(i,c) for(_PIForeachC<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true)
#define piForeachCAR(i,c) for(_PIForeachCR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
for(const typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true)
#define piForeachRA piForeachAR
#define piForeachAC piForeachCA
#define piForeachCRA piForeachCAR
#define piForeachARC piForeachCAR
#define piForeachACR piForeachCAR
#define piForeachRCA piForeachCAR
#define piForeachRAC piForeachCAR
#else
class _PIForeachBase {public: mutable bool _break, _end; };
template<typename Type>
class _PIForeach: public _PIForeachBase {
public:
_PIForeach(Type & t, bool i = false): _t(t), _inv(i) {_break = _end = false; if (_inv) _rit = _t.rbegin(); else _it = _t.begin();}
mutable typename Type::value_type _var;
mutable typename Type::iterator _it;
mutable typename Type::reverse_iterator _rit;
Type & _t;
bool _inv;
bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();}
void operator ++() {if (_inv) {if (_end) _rit = _t.rend(); else _rit++;} else {if (_end) _it = _t.end(); else _it++;} _break = false;}
};
template<typename Type>
class _PIForeachC: public _PIForeachBase {
public:
_PIForeachC(const Type & t, bool i = false): _t(t), _inv(i) {_break = _end = false; if (_inv) _rit = _t.rbegin(); else _it = _t.begin();}
mutable typename Type::value_type _var;
mutable typename Type::const_iterator _it;
mutable typename Type::const_reverse_iterator _rit;
const Type & _t;
bool _inv;
bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();}
void operator ++() {if (_inv) {if (_end) _rit = _t.rend(); else _rit++;} else {if (_end) _it = _t.end(); else _it++;} _break = false;}
};
template <typename T> inline _PIForeach<T> _PIForeachNew(T & t, bool i = false) {return _PIForeach<T>(t, i);}
template <typename T> inline _PIForeach<T> * _PIForeachCast(_PIForeachBase & c, T & ) {return static_cast<_PIForeach<T> * >(&c);}
template <typename T> inline _PIForeachC<T> _PIForeachNewC(const T & t, bool i = false) {return _PIForeachC<T>(t, i);}
template <typename T> inline _PIForeachC<T> * _PIForeachCastC(_PIForeachBase & c, const T & ) {return static_cast<_PIForeachC<T> * >(&c);}
#define piForeach(i,c) for(_PIForeachBase & _for = _PIForeachNew(c); !_PIForeachCast(_for, c)->isEnd(); ++(*_PIForeachCast(_for, c))) \
for(i = *(_PIForeachCast(_for, c)->_it); !_for._break; _for._break = true)
#define piForeachR(i,c) for(_PIForeachBase & _for = _PIForeachNew(c, true); !_PIForeachCast(_for, c)->isEnd(); ++(*_PIForeachCast(_for, c))) \
for(i = *(_PIForeachCast(_for, c)->_rit); !_for._break; _for._break = true)
#define piForeachC(i,c) for(_PIForeachBase & _for = _PIForeachNewC(c); !_PIForeachCastC(_for, c)->isEnd(); ++(*_PIForeachCastC(_for, c))) \
for(const i = *(_PIForeachCastC(_for, c)->_it); !_for._break; _for._break = true)
#define piForeachCR(i,c) for(_PIForeachBase & _for = _PIForeachNewC(c, false); !_PIForeachCastC(_for, c)->isEnd(); ++(*_PIForeachCastC(_for, c))) \
for(const i = *(_PIForeachCastC(_for, c)->_rit); !_for._break; _for._break = true)
#endif
#define piForeachRC piForeachCR
# define piForeachCR piForeachRC
#endif // DOXYGEN

View File

@@ -70,7 +70,6 @@ public:
}
inline PIDeque<T> & operator =(PIDeque<T> && other) {
if (this == &other) return *this;
swap(other);
return *this;
}
@@ -324,6 +323,12 @@ public:
piSwap<size_t>(pid_rsize, other.pid_rsize);
piSwap<ssize_t>(pid_start, other.pid_start);
}
inline void swap(PIDeque<T> && other) {
piSwap<T*>(pid_data, other.pid_data);
piSwap<size_t>(pid_size, other.pid_size);
piSwap<size_t>(pid_rsize, other.pid_rsize);
piSwap<ssize_t>(pid_start, other.pid_start);
}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}

View File

@@ -226,6 +226,10 @@ public:
pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
}
void swap(PIMap<Key, T> && other) {
pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
}
PIMap<Key, T> & insert(const Key & key, const T & value) {
bool f(false);

View File

@@ -69,7 +69,6 @@ public:
}
inline PIVector<T> & operator =(PIVector<T> && other) {
if (this == &other) return *this;
swap(other);
return *this;
}
@@ -301,6 +300,11 @@ public:
piSwap<size_t>(piv_size, other.piv_size);
piSwap<size_t>(piv_rsize, other.piv_rsize);
}
inline void swap(PIVector<T> && other) {
piSwap<T*>(piv_data, other.piv_data);
piSwap<size_t>(piv_size, other.piv_size);
piSwap<size_t>(piv_rsize, other.piv_rsize);
}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}

View File

@@ -125,7 +125,7 @@ PIInit::PIInit() {
piForeachC (PIString & i, ifpathes) {
if (fileExists(i)) {
sinfo->ifconfigPath = i;
piBreak;
break;
}
}
# else

View File

@@ -190,39 +190,42 @@ PIStringList PIObject::scopeList() const {
PIStringList PIObject::methodsEH() const {
PIMutexLocker ml(__meta_mutex());
PIStringList ret;
__MetaData & ehd(__meta_data()[classNameID()]);
piForeachC (__EHPair & eh, ehd.eh_func)
ret << eh.second.fullFormat();
const __MetaData & ehd(__meta_data()[classNameID()]);
for (auto eh = ehd.eh_func.constBegin(); eh != ehd.eh_func.constEnd(); eh++)
ret << eh.value().fullFormat();
return ret;
}
bool PIObject::isMethodEHContains(const PIString & name) const {
PIMutexLocker ml(__meta_mutex());
__MetaData & ehd(__meta_data()[classNameID()]);
piForeachC (__EHPair & eh, ehd.eh_func)
if (eh.second.func_name == name)
const __MetaData & ehd(__meta_data()[classNameID()]);
for (auto eh = ehd.eh_func.constBegin(); eh != ehd.eh_func.constEnd(); eh++) {
if (eh.value().func_name == name)
return true;
}
return false;
}
PIString PIObject::methodEHArguments(const PIString & name) const {
PIMutexLocker ml(__meta_mutex());
__MetaData & ehd(__meta_data()[classNameID()]);
piForeachC (__EHPair & eh, ehd.eh_func)
if (eh.second.func_name == name)
return eh.second.arguments();
const __MetaData & ehd(__meta_data()[classNameID()]);
for (auto eh = ehd.eh_func.constBegin(); eh != ehd.eh_func.constEnd(); eh++) {
if (eh.value().func_name == name)
return eh.value().arguments();
}
return PIString();
}
PIString PIObject::methodEHFullFormat(const PIString & name) const {
PIMutexLocker ml(__meta_mutex());
__MetaData & ehd(__meta_data()[classNameID()]);
piForeachC (__EHPair & eh, ehd.eh_func)
if (eh.second.func_name == name)
return eh.second.fullFormat();
const __MetaData & ehd(__meta_data()[classNameID()]);
for (auto eh = ehd.eh_func.constBegin(); eh != ehd.eh_func.constEnd(); eh++) {
if (eh.value().func_name == name)
return eh.value().fullFormat();
}
return PIString();
}
@@ -234,10 +237,11 @@ PIString PIObject::methodEHFromAddr(const void * addr) const {
PIVector<PIObject::__MetaFunc> PIObject::findEH(const PIString & name) const {
PIVector<__MetaFunc> ret;
__MetaData & ehd(__meta_data()[classNameID()]);
piForeachC (__EHPair & eh, ehd.eh_func)
if (eh.second.func_name == name)
ret << eh.second;
const __MetaData & ehd(__meta_data()[classNameID()]);
for (auto eh = ehd.eh_func.constBegin(); eh != ehd.eh_func.constEnd(); eh++) {
if (eh.value().func_name == name)
ret << eh.value();
}
return ret;
}
@@ -582,11 +586,11 @@ void PIObject::dump(const PIString & line_prefix) const {
//printf("dump %d properties ok\n", properties_.size());
PICout(PICoutManipulators::AddNewLine) << line_prefix << " }";
PICout(PICoutManipulators::AddNewLine) << line_prefix << " methods {";
__MetaData & ehd(__meta_data()[classNameID()]);
const __MetaData & ehd(__meta_data()[classNameID()]);
PICout(PICoutManipulators::AddNewLine) << line_prefix << " count: " << ehd.eh_func.size_s();
//printf("dump %d methods\n", ehd.eh_func.size());
piForeachC (__EHPair & eh, ehd.eh_func) {
PICout(PICoutManipulators::AddNewLine) << line_prefix << " " << eh.second.fullFormat();
for (auto eh = ehd.eh_func.constBegin(); eh != ehd.eh_func.constEnd(); eh++) {
PICout(PICoutManipulators::AddNewLine) << line_prefix << " " << eh.value().fullFormat();
}
//printf("dump %d methods ok\n", ehd.eh_func.size());
PICout(PICoutManipulators::AddNewLine) << line_prefix << " }";

View File

@@ -96,7 +96,7 @@ bool PIBinaryLog::openDevice() {
piForeachC(PIFile::FileInfo &i, es) {
if (i.extension() == "binlog" && i.isFile() && i.baseName().startsWith(filePrefix())) {
setPath(i.path);
piBreak;
break;
}
}
}

View File

@@ -491,7 +491,7 @@ bool PIPeer::dataRead(uchar * readed, int size) {
if (p.name != from) continue;
piForeach (PeerInfo::PeerAddress & a, p.addresses) {
if (a.address != addr) continue;
if (a.last_ping >= time) piBreak;
if (a.last_ping >= time) break;
ptime = ctime - time;
a.last_ping = time;
a.wait_ping = false;
@@ -677,7 +677,7 @@ bool PIPeer::mbcastRead(uchar * data, int size) {
if (peer.name == pi.name) peer.sync = 0;
ch = true;
}
piBreak;
break;
}
}
if (exist || isRemoved(rpeer)) continue;

View File

@@ -544,7 +544,7 @@ bool PISerial::openDevice() {
piForeachC (DeviceInfo & d, devs) {
if (d.id() == pl) {
p = d.path;
piBreak;
break;
}
}
if (p.isEmpty()) {

View File

@@ -375,20 +375,20 @@ bool PIConnection::removeDevice(const PIString & full_path) {
PIStringList dntd(deviceNames(dev));
piForeachC (PIString & n, dntd)
device_names.removeOne(n);
piForeachC (SPair & s, senders) {
if (s.second == 0) continue;
s.second->lock();
s.second->devices.removeAll(dev);
s.second->unlock();
for (auto s = senders.constBegin(); s != senders.constEnd(); s++) {
if (s.value() == 0) continue;
s.value()->lock();
s.value()->devices.removeAll(dev);
s.value()->unlock();
}
device_modes.remove(dev);
piForeachC (PEPair & i, extractors) {
if (i.second == 0) continue;
i.second->devices.removeAll(dev);
for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) {
if (i.value() == 0) continue;
i.value()->devices.removeAll(dev);
}
bounded_extractors.remove(dev);
channels_.remove(dev);
for (PIMap<PIIODevice * , PIVector<PIIODevice * > >::iterator it = channels_.begin(); it != channels_.end(); ++it)
for (auto it = channels_.begin(); it != channels_.end(); it++)
it.value().removeAll(dev);
__device_pool__->lock();
if (diags_.value(dev, 0) != 0)
@@ -404,11 +404,11 @@ void PIConnection::removeAllDevices() {
PIVector<PIIODevice * > bdevs(__device_pool__->boundedDevices(this));
__device_pool__->lock();
piForeach (PIIODevice * d, bdevs) {
piForeachC (SPair & s, senders) {
if (s.second == 0) continue;
s.second->lock();
s.second->devices.removeAll(d);
s.second->unlock();
for (auto s = senders.constBegin(); s != senders.constEnd(); s++) {
if (s.value() == 0) continue;
s.value()->lock();
s.value()->devices.removeAll(d);
s.value()->unlock();
}
channels_.remove(d);
for (PIMap<PIIODevice * , PIVector<PIIODevice * > >::iterator it = channels_.begin(); it != channels_.end(); ++it)
@@ -421,9 +421,9 @@ void PIConnection::removeAllDevices() {
__device_pool__->unlock();
device_modes.clear();
bounded_extractors.clear();
piForeachC (PEPair & i, extractors) {
if (i.second == 0) continue;
i.second->devices.clear();
for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) {
if (i.value() == 0) continue;
i.value()->devices.clear();
}
}
@@ -563,15 +563,15 @@ bool PIConnection::removeFilter(const PIString & name_) {
void PIConnection::removeAllFilters() {
__device_pool__->lock();
piForeachC (PEPair & i, extractors) {
if (i.second == 0) continue;
channels_.remove(i.second->extractor);
for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) {
if (i.value() == 0) continue;
channels_.remove(i.value()->extractor);
for (PIMap<PIIODevice * , PIVector<PIIODevice * > >::iterator it = channels_.begin(); it != channels_.end(); ++it)
it.value().removeAll(i.second->extractor);
if (diags_.value(i.second->extractor, 0) != 0)
delete diags_.value(i.second->extractor);
diags_.remove(i.second->extractor);
delete i.second;
it.value().removeAll(i.value()->extractor);
if (diags_.value(i.value()->extractor, 0) != 0)
delete diags_.value(i.value()->extractor);
diags_.remove(i.value()->extractor);
delete i.value();
}
extractors.clear();
bounded_extractors.clear();
@@ -581,28 +581,31 @@ void PIConnection::removeAllFilters() {
PIVector<PIPacketExtractor * > PIConnection::filters() const {
PIVector<PIPacketExtractor * > ret;
piForeachC (PEPair & i, extractors)
if (i.second != 0)
if (i.second->extractor != 0) ret << i.second->extractor;
for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) {
if (i.value() != 0)
if (i.value()->extractor != 0) ret << i.value()->extractor;
}
return ret;
}
PIStringList PIConnection::filterNames() const {
PIStringList ret;
piForeachC (PEPair & i, extractors)
if (i.second != 0)
if (i.second->extractor != 0) ret << i.first;
for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) {
if (i.value() != 0)
if (i.value()->extractor != 0) ret << i.key();
}
return ret;
}
PIPacketExtractor * PIConnection::filter(const PIString & name_) const {
PIString fname_ = name_.trimmed();
piForeachC (PEPair & i, extractors)
if (i.second != 0)
if (i.second->extractor != 0 && i.first == fname_)
return i.second->extractor;
for (auto i = extractors.constBegin(); i != extractors.constEnd(); i++) {
if (i.value() != 0)
if (i.value()->extractor != 0 && i.key() == fname_)
return i.value()->extractor;
}
return 0;
}
@@ -784,9 +787,10 @@ float PIConnection::senderFrequency(const PIString & name) const {
void PIConnection::removeAllSenders() {
piForeachC (SPair & s, senders)
if (s.second != 0)
delete s.second;
for (auto s = senders.constBegin(); s != senders.constEnd(); s++) {
if (s.value() != 0)
delete s.value();
}
senders.clear();
}
@@ -802,8 +806,8 @@ void PIConnection::startThreadedRead(const PIString & full_path_name) {
void PIConnection::startAllThreadedReads() {
piForeachC (DevicePool::DDPair & d, __device_pool__->devices)
startThreadedRead(d.first);
for (auto d = __device_pool__->devices.constBegin(); d != __device_pool__->devices.constEnd(); d++)
startThreadedRead(d.key());
}
@@ -816,10 +820,10 @@ void PIConnection::startSender(const PIString & name) {
void PIConnection::startAllSenders() {
piForeachC (SPair & s, senders) {
if (s.second == 0) continue;
if (!s.second->isRunning() && !__device_pool__->fake)
s.second->start(s.second->int_);
for (auto s = senders.constBegin(); s != senders.constEnd(); s++) {
if (s.value() == 0) continue;
if (!s.value()->isRunning() && !__device_pool__->fake)
s.value()->start(s.value()->int_);
}
}
@@ -835,8 +839,8 @@ void PIConnection::stopThreadedRead(const PIString & full_path_name) {
void PIConnection::stopAllThreadedReads() {
piForeachC (DevicePool::DDPair & d, __device_pool__->devices)
stopThreadedRead(d.first);
for (auto d = __device_pool__->devices.constBegin(); d != __device_pool__->devices.constEnd(); d++)
stopThreadedRead(d.key());
}
@@ -848,10 +852,10 @@ void PIConnection::stopSender(const PIString & name) {
void PIConnection::stopAllSenders() {
piForeachC (SPair & s, senders) {
if (s.second == 0) continue;
if (s.second->isRunning())
s.second->stop();
for (auto s = senders.constBegin(); s != senders.constEnd(); s++) {
if (s.value() == 0) continue;
if (s.value()->isRunning())
s.value()->stop();
}
}
@@ -1012,14 +1016,14 @@ bool PIConnection::DevicePool::removeDevice(PIConnection * parent, const PIStrin
void PIConnection::DevicePool::unboundConnection(PIConnection * parent) {
PIStringList rem;
piForeachC (DDPair & i, devices) {
if (i.second == 0) {
rem << i.first;
for (auto i = devices.constBegin(); i != devices.constEnd(); i++) {
if (i.value() == 0) {
rem << i.key();
continue;
}
i.second->listeners.removeAll(parent);
if (i.second->listeners.isEmpty())
rem << i.first;
i.value()->listeners.removeAll(parent);
if (i.value()->listeners.isEmpty())
rem << i.key();
}
piForeachC (PIString & i, rem) {
DeviceData * dd = devices.value(i);
@@ -1105,9 +1109,9 @@ PIConnection::DevicePool::DeviceData::~DeviceData() {
void PIConnection::DevicePool::run() {
PIVector<PIConnection * > conns(PIConnection::allConnections());
piForeach (PIConnection * c, conns) {
piForeachC (PIConnection::DPair & d, c->diags_) {
if (d.second == 0) continue;
d.second->tick(0, 1);
for (auto d = c->diags_.constBegin(); d != c->diags_.constEnd(); d++) {
if (d.value() == 0) continue;
d.value()->tick(0, 1);
}
}
}

View File

@@ -164,7 +164,7 @@ public:
T t;
mutex.lock();
bool isNotEmpty = !PIDeque<T>::isEmpty();
t = isNotEmpty ? T(PIDeque<T>::take_front()) : defaultVal;
t = isNotEmpty ? PIDeque<T>::take_front() : defaultVal;
mutex.unlock();
if (isNotEmpty) cond_var_rem->notifyOne();
if (isOk) *isOk = isNotEmpty;