From f47bc411bce840ef2c053da1cfb1436311956360 Mon Sep 17 00:00:00 2001 From: peri4 Date: Tue, 5 Mar 2024 17:55:25 +0300 Subject: [PATCH] version 3.16.0 new PISystemTime::Frequency type --- CMakeLists.txt | 4 +- doc/examples/pichunkstream.cpp | 23 ++-- libs/main/literals/piliterals_time.h | 60 +++++++++++ libs/main/types/pisystemtime.cpp | 14 +++ libs/main/types/pisystemtime.h | 152 ++++++++++++++++++++++++++- main.cpp | 12 ++- 6 files changed, 249 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b9cd963..e7754249 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(PIP) set(PIP_MAJOR 3) -set(PIP_MINOR 15) -set(PIP_REVISION 3) +set(PIP_MINOR 16) +set(PIP_REVISION 0) set(PIP_SUFFIX ) set(PIP_COMPANY SHS) set(PIP_DOMAIN org.SHS) diff --git a/doc/examples/pichunkstream.cpp b/doc/examples/pichunkstream.cpp index dc6003ce..c394d6c2 100644 --- a/doc/examples/pichunkstream.cpp +++ b/doc/examples/pichunkstream.cpp @@ -9,8 +9,14 @@ struct S { }; // Operators -PIByteArray & operator <<(PIByteArray & b, const S & s) {b << s.i << s.f << s.s; return b;} -PIByteArray & operator >>(PIByteArray & b, S & s) {b >> s.i >> s.f >> s.s; return b;} +PIByteArray & operator<<(PIByteArray & b, const S & s) { + b << s.i << s.f << s.s; + return b; +} +PIByteArray & operator>>(PIByteArray & b, S & s) { + b >> s.i >> s.f >> s.s; + return b; +} //! [struct] //! [write] // Write chunk stream @@ -22,10 +28,7 @@ PIVector f; f << -1. << 2.5 << 11.; // write some data to empty stream PIChunkStream cs; -cs << cs.chunk(1, int(10)) - << cs.chunk(2, PIString("text")) - << cs.chunk(4, f) - << cs.chunk(3, s); +cs << cs.chunk(1, int(10)) << cs.chunk(2, PIString("text")) << cs.chunk(4, f) << cs.chunk(3, s); // now you can take cs.data() and send or place it somewhere ... //! [write] //! [read] @@ -42,14 +45,14 @@ while (!cs2.atEnd()) { case 1: i = cs2.getData(); break; case 2: str = cs2.getData(); break; case 3: s = cs2.getData(); break; - case 4: f = cs2.getData >(); break; + case 4: f = cs2.getData>(); break; } } piCout << i << str << f << s.i << s.f << s.s; //! [read] //! [write_new] -PIByteArray & operator <<(PIByteArray & s, const S & value) { +PIByteArray & operator<<(PIByteArray & s, const S & value) { PIChunkStream cs; cs.add(1, value.i).add(2, value.f).add(3, value.s); s << cs.data(); @@ -57,11 +60,11 @@ PIByteArray & operator <<(PIByteArray & s, const S & value) { } //! [write_new] //! [read_new] -PIByteArray & operator >>(PIByteArray & s, S & value) { +PIByteArray & operator>>(PIByteArray & s, S & value) { PIChunkStream cs; if (!cs.extract(s)) return s; cs.readAll(); cs.get(1, value.i).get(2, value.f).get(3, value.s); - return b; + return s; } //! [read_new] diff --git a/libs/main/literals/piliterals_time.h b/libs/main/literals/piliterals_time.h index c6f1f670..a1e5ca0b 100644 --- a/libs/main/literals/piliterals_time.h +++ b/libs/main/literals/piliterals_time.h @@ -82,4 +82,64 @@ inline PISystemTime operator""_ns(unsigned long long v) { } +//! \~\brief +//! \~english PISystemTime::Frequency from hertz +//! \~russian PISystemTime::Frequency из герц +inline PISystemTime::Frequency operator""_Hz(long double v) { + return PISystemTime::Frequency::fromHertz(v); +} + +//! \~\brief +//! \~english PISystemTime::Frequency from hertz +//! \~russian PISystemTime::Frequency из герц +inline PISystemTime::Frequency operator""_Hz(unsigned long long v) { + return PISystemTime::Frequency::fromHertz(v); +} + + +//! \~\brief +//! \~english PISystemTime::Frequency from kilohertz +//! \~russian PISystemTime::Frequency из килогерц +inline PISystemTime::Frequency operator""_KHz(long double v) { + return PISystemTime::Frequency::fromKHertz(v); +} + +//! \~\brief +//! \~english PISystemTime::Frequency from kilohertz +//! \~russian PISystemTime::Frequency из килогерц +inline PISystemTime::Frequency operator""_KHz(unsigned long long v) { + return PISystemTime::Frequency::fromKHertz(v); +} + + +//! \~\brief +//! \~english PISystemTime::Frequency from megahertz +//! \~russian PISystemTime::Frequency из мегагерц +inline PISystemTime::Frequency operator""_MHz(long double v) { + return PISystemTime::Frequency::fromMHertz(v); +} + +//! \~\brief +//! \~english PISystemTime::Frequency from megahertz +//! \~russian PISystemTime::Frequency из мегагерц +inline PISystemTime::Frequency operator""_MHz(unsigned long long v) { + return PISystemTime::Frequency::fromMHertz(v); +} + + +//! \~\brief +//! \~english PISystemTime::Frequency from gigahertz +//! \~russian PISystemTime::Frequency из гигагерц +inline PISystemTime::Frequency operator""_GHz(long double v) { + return PISystemTime::Frequency::fromGHertz(v); +} + +//! \~\brief +//! \~english PISystemTime::Frequency from gigahertz +//! \~russian PISystemTime::Frequency из гигагерц +inline PISystemTime::Frequency operator""_GHz(unsigned long long v) { + return PISystemTime::Frequency::fromGHertz(v); +} + + #endif diff --git a/libs/main/types/pisystemtime.cpp b/libs/main/types/pisystemtime.cpp index de2bba27..cd99e88b 100644 --- a/libs/main/types/pisystemtime.cpp +++ b/libs/main/types/pisystemtime.cpp @@ -220,3 +220,17 @@ double PITimeMeasurer::elapsed_s() const { PISystemTime PITimeMeasurer::elapsed() const { return (PISystemTime::current(true) - t_st); } + + +// PISystemTime::Frequency + +PISystemTime PISystemTime::Frequency::toSystemTime() const { + if (value_hz <= 0.) return PISystemTime(); + return PISystemTime::fromSeconds(1. / value_hz); +} + + +PISystemTime::Frequency PISystemTime::Frequency::fromSystemTime(const PISystemTime & st) { + if (st == PISystemTime()) return Frequency(); + return Frequency(1. / st.toSeconds()); +} diff --git a/libs/main/types/pisystemtime.h b/libs/main/types/pisystemtime.h index ffc5993d..4f622e65 100644 --- a/libs/main/types/pisystemtime.h +++ b/libs/main/types/pisystemtime.h @@ -48,6 +48,143 @@ public: checkOverflows(); } + //! \~english Contructs time with "s" seconds and "ns" nanoseconds + //! \~russian Создает время с секундами "s" и наносекундами "ns" + PISystemTime(std::pair s_ns) { + seconds = s_ns.first; + nanoseconds = s_ns.second; + checkOverflows(); + } + + + //! \ingroup Types + //! \~\brief + //! \~english Frequency type. + //! \~russian Тип частоты. + class PIP_EXPORT Frequency { + public: + //! \~english Contructs null frequency + //! \~russian Создает нулевую частоту + Frequency() {} + + //! \~english Returns frequency as hertz + //! \~russian Возвращает частоту в герцах + double toHertz() const { return value_hz; } + + //! \~english Returns frequency as kilohertz + //! \~russian Возвращает частоту в килогерцах + double toKHertz() const { return value_hz / 1E+3; } + + //! \~english Returns frequency as megahertz + //! \~russian Возвращает частоту в мегагерцах + double toMHertz() const { return value_hz / 1E+6; } + + //! \~english Returns frequency as gigahertz + //! \~russian Возвращает частоту в гигагерцах + double toGHertz() const { return value_hz / 1E+9; } + + //! \~english Returns frequency as \a PISystemTime time interval + //! \~russian Возвращает частоту как \a PISystemTime временной интервал + PISystemTime toSystemTime() const; + + //! \~english Returns frequency as \a PISystemTime time interval + //! \~russian Возвращает частоту как \a PISystemTime временной интервал + operator PISystemTime() const { return toSystemTime(); } + + //! \~english Returns \a Frequency contains "hz" hertz + //! \~russian Возвращает \a Frequency содержащую "hz" герц + static Frequency fromHertz(double hz) { return Frequency(hz); } + + //! \~english Returns \a Frequency contains "khz" kilohertz + //! \~russian Возвращает \a Frequency содержащую "khz" килогерц + static Frequency fromKHertz(double khz) { return Frequency(khz * 1E+3); } + + //! \~english Returns \a Frequency contains "mhz" megahertz + //! \~russian Возвращает \a Frequency содержащую "mhz" мегагерц + static Frequency fromMHertz(double mhz) { return Frequency(mhz * 1E+6); } + + //! \~english Returns \a Frequency contains "ghz" gigahertz + //! \~russian Возвращает \a Frequency содержащую "ghz" гигагерц + static Frequency fromGHertz(double ghz) { return Frequency(ghz * 1E+9); } + + //! \~english Returns \a Frequency that corresponds "st" time interval + //! \~russian Возвращает \a Frequency соответствующую временному интервалу "st" + static Frequency fromSystemTime(const PISystemTime & st); + + //! \~english Returns sum between this frequency and "f" + //! \~russian Возвращает сумму этой частоты с "f" + Frequency operator+(const Frequency & f) const { return Frequency(value_hz + f.value_hz); } + + //! \~english Returns difference between this frequency and "f" + //! \~russian Возвращает разницу этой частоты с "f" + Frequency operator-(const Frequency & f) const { return Frequency(value_hz - f.value_hz); } + + //! \~english Returns multiplication between this frequency and "v" + //! \~russian Возвращает эту частоту умноженную на "v" + Frequency operator*(const double & v) const { return Frequency(value_hz * v); } + + //! \~english Returns division between this frequency and "v" + //! \~russian Возвращает эту частоту поделённую на "v" + Frequency operator/(const double & v) const { return Frequency(value_hz / v); } + + //! \~english Multiply frequency by "v" + //! \~russian Умножает частоту на "v" + Frequency & operator*=(const double & v) { + value_hz *= v; + return *this; + } + + //! \~english Divide frequency by "v" + //! \~russian Делит частоту на "v" + Frequency & operator/=(const double & v) { + value_hz /= v; + return *this; + } + + //! \~english Add to frequency "f" + //! \~russian Добавляет к частоте "f" + Frequency & operator+=(const Frequency & f) { + value_hz += f.value_hz; + return *this; + } + + //! \~english Subtract from frequency "f" + //! \~russian Вычитает из частоты "f" + Frequency & operator-=(const Frequency & f) { + value_hz -= f.value_hz; + return *this; + } + + //! \~english Compare operator + //! \~russian Оператор сравнения + bool operator==(const Frequency & f) const { return value_hz == f.value_hz; } + + //! \~english Compare operator + //! \~russian Оператор сравнения + bool operator!=(const Frequency & f) const { return value_hz != f.value_hz; } + + //! \~english Compare operator + //! \~russian Оператор сравнения + bool operator>(const Frequency & f) const { return value_hz > f.value_hz; } + + //! \~english Compare operator + //! \~russian Оператор сравнения + bool operator<(const Frequency & f) const { return value_hz < f.value_hz; } + + //! \~english Compare operator + //! \~russian Оператор сравнения + bool operator>=(const Frequency & f) const { return value_hz >= f.value_hz; } + + //! \~english Compare operator + //! \~russian Оператор сравнения + bool operator<=(const Frequency & f) const { return value_hz <= f.value_hz; } + + private: + Frequency(double hz): value_hz(hz) {} + + double value_hz = 0.; + }; + //! \~english Returns time value in seconds //! \~russian Возвращает значение времени в секундах @@ -128,11 +265,11 @@ public: } //! \~english Returns multiplication between this time and "t" - //! \~russian Возвращает это временя умноженное на "t" + //! \~russian Возвращает это время умноженное на "t" PISystemTime operator*(const double & v) const { return fromMilliseconds(toMilliseconds() * v); } //! \~english Returns division between this time and "t" - //! \~russian Возвращает это временя поделённое на "t" + //! \~russian Возвращает это время поделённое на "t" PISystemTime operator/(const double & v) const { return fromMilliseconds(toMilliseconds() / v); } //! \~english Add to time "t" @@ -269,6 +406,17 @@ inline PICout operator<<(PICout s, const PISystemTime & v) { return s; } +//! \relatesalso PICout +//! \~english \brief Output operator to PICout +//! \~russian \brief Оператор вывода в PICout +inline PICout operator<<(PICout s, const PISystemTime::Frequency & f) { + s.space(); + s.saveAndSetControls(0); + s << f.toHertz() << " Hz"; + s.restoreControls(); + return s; +} + //! \ingroup Types //! \~\brief diff --git a/main.cpp b/main.cpp index 17cc3dd0..8dedeecb 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include "picodeparser.h" #include "piiostream.h" #include "pijson.h" +#include "piliterals_time.h" #include "pimathbase.h" #include "pip.h" #include "pivaluetree.h" @@ -10,7 +11,14 @@ using namespace PICoutManipulators; int main(int argc, char * argv[]) { - PICodeParser cp; - cp.parseFile("kmm_types.h", false); + // PICodeParser cp; + // cp.parseFile("kmm_types.h", false); + piCout << (1_Hz * 100).toSystemTime().toSeconds(); + piCout << (1_Hz * 100); + piCout << (1_Hz + 10_GHz); + piCout << (100_Hz + 1_KHz - 10_Hz); + piCout << PISystemTime::Frequency::fromSystemTime({0, 200000000}); + piCout << PISystemTime::Frequency::fromSystemTime(25_ms); + piCout << PISystemTime::Frequency::fromSystemTime(25_ms).toSystemTime().toMilliseconds(); return 0; }