From 4725eb96d64ac796a4668d3f0f81d02730508ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Fri, 29 Jul 2022 15:49:36 +0300 Subject: [PATCH] replace typedef function ptr by std::function start PIMap refactoring --- libs/main/containers/pimap.h | 96 ++++++++++---------------- libs/main/core/piobject.h | 2 - libs/main/io_devices/pibinarylog.h | 4 +- libs/main/io_devices/piiodevice.h | 3 +- libs/main/io_utils/pipacketextractor.h | 15 ++-- libs/main/math/pievaluator.cpp | 2 +- libs/main/math/pievaluator.h | 13 ++-- libs/main/system/pisignals.h | 4 +- main.cpp | 13 ++-- 9 files changed, 65 insertions(+), 87 deletions(-) diff --git a/libs/main/containers/pimap.h b/libs/main/containers/pimap.h index bfe94175..5f4e0ccb 100644 --- a/libs/main/containers/pimap.h +++ b/libs/main/containers/pimap.h @@ -1,8 +1,17 @@ -/*! \file pimap.h - * \brief Associative array with custom types of key and value - * - * This file declares PIMap -*/ +//! \addtogroup Containers +//! \{ +//! \file pideque.h +//! \brief +//! \~english Declares \a PIMap +//! \~russian Объявление \a PIMap +//! \~\authors +//! \~english +//! Ivan Pelipenko peri4ko@yandex.ru; +//! Andrey Bychkov work.a.b@yandex.ru; +//! \~russian +//! Иван Пелипенко peri4ko@yandex.ru; +//! Андрей Бычков work.a.b@yandex.ru; +//! \~\} /* PIP - Platform Independent Primitives Associative array with custom types of key and value @@ -30,41 +39,6 @@ #include "pipair.h" -template -void piQuickSort(T * a, ssize_t N) { - if (N < 1) return; - if (N < 46) { - T tmp; - ssize_t i,j; - for(i=1; i<=N; i++) { - tmp = a[i]; - j = i-1; - while(tmp=0) { - a[j+1] = a[j]; - j = j-1; - } - a[j+1] = tmp; - } - } else { - ssize_t i = 0, j = N; - T & p(a[N >> 1]); - do { - while (a[i] < p) i++; - while (a[j] > p) j--; - if (i <= j) { - if (i != j) { - //piCout << "swap" << i << j << a[i] << a[j]; - piSwap(a[i], a[j]); - } - i++; j--; - } - } while (i <= j); - if (j > 0) piQuickSort(a, j); - if (N > i) piQuickSort(a + i, N - i); - } -} - - template class PIMapIterator; @@ -81,8 +55,9 @@ public: PIMap(const PIMap & other) {*this = other;} PIMap(PIMap && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {} PIMap(std::initializer_list> init_list) { - for (auto i: init_list) + for (auto i: init_list) { insert(std::get<0>(i), std::get<1>(i)); + } } virtual ~PIMap() {;} @@ -150,7 +125,7 @@ public: 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 value_type* operator ->() const {cval = parent->_pair(pos); return &cval;} const Key & key() const {return const_cast * >(parent)->_key(pos);} const T & value() const {return const_cast * >(parent)->_value(pos);} void operator ++() {++pos;} @@ -159,7 +134,7 @@ public: 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);} - mutable value_type cval; + //mutable value_type cval; }; class const_reverse_iterator { @@ -171,14 +146,14 @@ public: 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;} + //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; + //mutable value_type cval; }; iterator begin() {return iterator(this, 0);} @@ -201,6 +176,7 @@ public: int size_s() const {return pim_content.size_s();} size_t length() const {return pim_content.size();} bool isEmpty() const {return (pim_content.size() == 0);} + bool isNotEmpty() const {return (pim_content.size() > 0);} T & operator [](const Key & key) { bool f(false); @@ -210,8 +186,7 @@ public: pim_index.insert(i, MapIndex(key, pim_content.size() - 1)); return pim_content.back(); } - const T operator [](const Key & key) const {bool f(false); ssize_t i = _find(key, f); if (f) return pim_content[pim_index[i].index]; return T();} - const T at(const Key & key) const {return (*this)[key];} + T at(const Key & key) const {return value(key);} PIMap & operator <<(const PIMap & other) { #ifndef NDEBUG @@ -268,7 +243,7 @@ public: } return *this; } - const T value(const Key & key, const T & default_ = T()) const {bool f(false); ssize_t i = _find(key, f); if (!f) return default_; return pim_content[pim_index[i].index];} + T value(const Key & key, const T & default_ = T()) const {bool f(false); ssize_t i = _find(key, f); if (!f) return default_; return pim_content[pim_index[i].index];} PIVector values() const {return pim_content;} Key key(const T & value_, const Key & default_ = Key()) const {for (int i = 0; i < pim_index.size_s(); ++i) if (pim_content[pim_index[i].index] == value_) return pim_index[i].key; return default_;} PIVector keys() const { @@ -278,16 +253,16 @@ public: return ret; } - void dump() { - piCout << "PIMap" << size() << "entries" << PICoutManipulators::NewLine << "content:"; - for (size_t i = 0; i < pim_content.size(); ++i) - piCout << PICoutManipulators::Tab << i << ":" << pim_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; - } +// void dump() { +// piCout << "PIMap" << size() << "entries" << PICoutManipulators::NewLine << "content:"; +// for (size_t i = 0; i < pim_content.size(); ++i) +// piCout << PICoutManipulators::Tab << i << ":" << pim_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: +private: struct MapIndex { MapIndex(Key k = Key(), size_t i = 0): key(k), index(i) {;} Key key; @@ -302,7 +277,7 @@ protected: template friend PIBinaryStream

& operator <<(PIBinaryStream

& s, const PIDeque::MapIndex> & v); - ssize_t binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const { + ssize_t _binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const { ssize_t mid; while (first <= last) { mid = (first + last) / 2; @@ -313,13 +288,12 @@ protected: found = false; return first; } - void _sort() {piQuickSort(pim_index.data(), pim_index.size_s() - 1);} ssize_t _find(const Key & k, bool & found) const { if (pim_index.isEmpty()) { found = false; return 0; } - return binarySearch(0, pim_index.size_s() - 1, k, found); + return _binarySearch(0, pim_index.size_s() - 1, k, found); } void _remove(ssize_t index) { //if (index >= pim_index.size()) return; @@ -415,7 +389,7 @@ inline PICout operator <<(PICout s, const PIMap & v) { if (!first) s << ", "; first = false; - s << i->first << ": " << i->second; + s << i.key() << ": " << i.value(); } s << "}"; s.restoreControl(); diff --git a/libs/main/core/piobject.h b/libs/main/core/piobject.h index 288ea456..0ce10869 100644 --- a/libs/main/core/piobject.h +++ b/libs/main/core/piobject.h @@ -34,8 +34,6 @@ #include "piqueue.h" #include "piobject_macros.h" -typedef void (*Handler)(void * ); - //! \ingroup Core //! \~\brief //! \~english This is base class for any classes which use events -> handlers mechanism. diff --git a/libs/main/io_devices/pibinarylog.h b/libs/main/io_devices/pibinarylog.h index 43c8e4a9..d6f8b62f 100644 --- a/libs/main/io_devices/pibinarylog.h +++ b/libs/main/io_devices/pibinarylog.h @@ -374,8 +374,8 @@ inline PICout operator <<(PICout s, const PIBinaryLog::BinLogInfo & bi) { s << "read records " << bi.records_count << " in " << bi.records.size() << " types, log size " << bi.log_size; s << "\nlog start " << bi.start_time << " , log end " << bi.end_time; PIVector keys = bi.records.keys(); - piForeachC(int i, keys) { - const PIBinaryLog::BinLogRecordInfo &bri(bi.records[i]); + for (int i : keys) { + PIBinaryLog::BinLogRecordInfo bri = bi.records.at(i); s << "\n record id " << bri.id << " , count " << bri.count; s << "\n record start " << bri.start_time << " , end " << bri.end_time; s << "\n record size " << bri.minimum_size << " - " << bri.maximum_size; diff --git a/libs/main/io_devices/piiodevice.h b/libs/main/io_devices/piiodevice.h index b7122695..e33fba97 100644 --- a/libs/main/io_devices/piiodevice.h +++ b/libs/main/io_devices/piiodevice.h @@ -32,8 +32,7 @@ /// TODO: написать документацию, тут ничего не понятно // function executed from threaded read, pass readedData, sizeOfData, ThreadedReadData -typedef bool (*ReadRetFunc)(const uchar *, int, void *); - +typedef std::function ReadRetFunc; #ifdef DOXYGEN diff --git a/libs/main/io_utils/pipacketextractor.h b/libs/main/io_utils/pipacketextractor.h index 4fd95725..e545f542 100644 --- a/libs/main/io_utils/pipacketextractor.h +++ b/libs/main/io_utils/pipacketextractor.h @@ -29,10 +29,17 @@ #include "piiodevice.h" /// TODO: написать документацию, тут ничего не понятно -// Pass data, recHeaderPtr, received_data, recHeaderSize. Return true if packet is correct nor return false. -typedef int (*PacketExtractorHeaderFunc)(const uchar *, const uchar *, int); -typedef bool (*PacketExtractorPayloadFunc)(const uchar *, int); -typedef bool (*PacketExtractorFooterFunc)(const uchar *, const uchar *, int); +/// Pass SourceHeaderPtr, ReceivedHeaderPtr, HeaderSize. +/// Return size of payload if packet is correct, or -1 if incorrect. +typedef std::function PacketExtractorHeaderFunc; + +/// Pass ReceivedDataPtr, DataSize. +/// Return true if packet is correct, false otherwise. +typedef std::function PacketExtractorPayloadFunc; + +/// Pass SourceFooterPtr, ReceivedFooterPtr, FooterSize. +/// Return true if packet is correct, false otherwise. +typedef std::function PacketExtractorFooterFunc; class PIP_EXPORT PIPacketExtractor: public PIIODevice { diff --git a/libs/main/math/pievaluator.cpp b/libs/main/math/pievaluator.cpp index 60f25658..42374584 100644 --- a/libs/main/math/pievaluator.cpp +++ b/libs/main/math/pievaluator.cpp @@ -194,7 +194,7 @@ int PIEvaluatorContent::addVariable(const PIString & name, const complexd & val) } -void PIEvaluatorContent::addCustomFunction(const PIString & name, int args_count, FuncFunc func) { +void PIEvaluatorContent::addCustomFunction(const PIString & name, int args_count, PIEvaluatorTypes::FuncHanlder func) { functions << PIEvaluatorTypes::Function(name, args_count, func); } diff --git a/libs/main/math/pievaluator.h b/libs/main/math/pievaluator.h index 89835ac3..c2ef6556 100644 --- a/libs/main/math/pievaluator.h +++ b/libs/main/math/pievaluator.h @@ -29,10 +29,11 @@ #include "pistringlist.h" #include "pimathcomplex.h" -typedef complexd (*FuncFunc)(void * , int, complexd * ); namespace PIEvaluatorTypes { + typedef std::function FuncHanlder; + enum eType {etNumber, etOperator, etVariable, etFunction}; enum Operation {oNone, oAdd, oSubtract, oMultiply, oDivide, oResidue, oPower, oEqual, oNotEqual, oGreater, oSmaller, oGreaterEqual, oSmallerEqual, @@ -70,12 +71,12 @@ namespace PIEvaluatorTypes { short var_num; }; struct PIP_EXPORT Function { - Function() {arguments = 0; type = bfUnknown; handler = 0;} - Function(const PIString & name, short args, BaseFunctions ftype) {identifier = name; arguments = args; type = ftype; handler = 0;} - Function(const PIString & name, short args, FuncFunc h) {identifier = name; arguments = args; type = bfCustom; handler = h;} + Function() {arguments = 0; type = bfUnknown; handler = nullptr;} + Function(const PIString & name, short args, BaseFunctions ftype) {identifier = name; arguments = args; type = ftype; handler = nullptr;} + Function(const PIString & name, short args, FuncHanlder h) {identifier = name; arguments = args; type = bfCustom; handler = h;} PIString identifier; BaseFunctions type; - FuncFunc handler; + FuncHanlder handler; short arguments; }; struct PIP_EXPORT Variable { @@ -103,7 +104,7 @@ public: void addFunction(const PIString & name, int args = 1); int addVariable(const PIString & name, const complexd & val = 0.); - void addCustomFunction(const PIString & name, int args_count, FuncFunc func); + void addCustomFunction(const PIString & name, int args_count, PIEvaluatorTypes::FuncHanlder func); int functionsCount() const {return functions.size();} int variablesCount() const {return variables.size();} int customVariablesCount() const; diff --git a/libs/main/system/pisignals.h b/libs/main/system/pisignals.h index a7b6408e..d93fa694 100644 --- a/libs/main/system/pisignals.h +++ b/libs/main/system/pisignals.h @@ -54,8 +54,8 @@ public: All = 0xFFFFF }; - typedef void (*SignalEvent)(PISignals::Signal); - // slot is any function format "void (PISignals::Signal)" + typedef std::function SignalEvent; + // slot is any function format "void(PISignals::Signal)" static void setSlot(SignalEvent slot) {ret_func = slot;} static void grabSignals(PIFlags signals_); static void raiseSignal(PISignals::Signal signal); diff --git a/main.cpp b/main.cpp index 779aec40..ec000d11 100644 --- a/main.cpp +++ b/main.cpp @@ -4,22 +4,21 @@ using namespace PICoutManipulators; int main(int argc, char * argv[]) { - PIByteArray ba = PIByteArray::fromHex("AA11FFAA22EEAA33FF"); + PIByteArray ba = PIByteArray::fromHex("AA1122BB11233AA4455"); PIIOByteArray b; b.open(ba); PIPacketExtractor p(&b); p.setSplitMode(PIPacketExtractor::Header); - p.setHeader(PIByteArray::fromHex("AABB")); -// p.setFooter(PIByteArray::fromHex("AA")); + p.setHeader(PIByteArray::fromHex("00")); + p.setFooter(PIByteArray::fromHex("AA")); p.setPayloadSize(2); p.setThreadedReadSlot([](const uchar * data, int size, void * d) { - piCout << size; return true; }); - p.setHeaderCheckSlot([](const uchar * src, const uchar * rec, int size) { + p.setHeaderCheckSlot([](const uchar * src, const uchar * rec, int size)->int { if (*src == *rec) { - if (rec[1] == 0x11) return 1; - if (rec[1] == 0x22) return 3; + if (rec[0] == 0xAA) return 2; + if (rec[0] == 0xBB) return 3; } return -1; });