version 5.4.0

remove CORS default header from PIHTTPServer
fix several docs
fix PIMathVector::dot return type
add units directory with PIUnits facility
This commit is contained in:
2025-09-26 21:33:45 +03:00
parent daab41e41e
commit 5db97ca959
30 changed files with 1888 additions and 556 deletions

37
libs/main/units/piunits.h Normal file
View File

@@ -0,0 +1,37 @@
/*! \file piunits.h
* \ingroup Core
* \~\brief
* \~english Unit conversions
* \~russian Преобразование единиц измерения
*/
/*
PIP - Platform Independent Primitives
Unit conversions
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_H
#define PIUNITS_H
#include "piunits_class_angle.h"
#include "piunits_class_distance.h"
#include "piunits_class_information.h"
#include "piunits_class_pressure.h"
#include "piunits_class_temperature.h"
#include "piunits_class_time.h"
#include "piunits_value.h"
#endif

View File

@@ -0,0 +1,52 @@
/*
PIP - Platform Independent Primitives
Unit conversions
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_base.h"
#include "piliterals_string.h"
PIMap<int, PIUnits::Class::Internal::ClassBase *> PIUnits::Class::Internal::typeClasses;
PIVector<PIUnits::Class::Internal::ClassBase *> PIUnits::Class::Internal::allTypeClasses;
const PIString PIUnits::Class::Internal::unknown = "?"_a;
PIString PIUnits::className(int type) {
auto * uc = Class::Internal::typeClasses.value(type);
if (!uc) return Class::Internal::unknown;
return uc->className();
}
PIString PIUnits::name(int type) {
auto * uc = Class::Internal::typeClasses.value(type);
if (!uc) return Class::Internal::unknown;
return uc->name(type);
}
PIString PIUnits::unit(int type) {
auto * uc = Class::Internal::typeClasses.value(type);
if (!uc) return Class::Internal::unknown;
return uc->unit(type);
}
PIVector<PIUnits::Class::Internal::ClassBase *> PIUnits::allClasses() {
return Class::Internal::allTypeClasses;
}

View File

@@ -0,0 +1,124 @@
/*! \file piunits_base.h
* \ingroup Core
* \~\brief
* \~english Unit conversions
* \~russian Преобразование единиц измерения
*/
/*
PIP - Platform Independent Primitives
Unit conversions
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_BASE_H
#define PIUNITS_BASE_H
#include "pitranslator.h"
#define DECLARE_UNIT_CLASS_BEGIN(Name, StartIndex) \
namespace PIUnits { \
namespace Class { \
class PIP_EXPORT Name \
: public Internal::ClassBase \
, public Internal::Registrator<Name> { \
private: \
friend class Internal::Registrator<Name>; \
constexpr static int typeStart = StartIndex; \
PIString name(int type) const override; \
PIString unit(int type) const override; \
PIString valueToString(double v, char format, int prec) const override; \
double convert(double v, int from, int to) const override; \
bool supportPrefixes(int type) const override; \
bool supportPrefixesNon3(int type) const override; \
bool supportPrefixesGreater(int type) const override; \
bool supportPrefixesSmaller(int type) const override; \
\
public: \
PIString className() const override { \
return piTr(#Name, "PIUnits"); \
} \
uint classID() const override { \
static uint ret = PIStringAscii(#Name).hash(); \
return ret; \
}
#define DECLARE_UNIT_CLASS_END(Name) \
} \
; \
} \
} \
STATIC_INITIALIZER_BEGIN \
PIUnits::Class::Name::registerSelf(); \
STATIC_INITIALIZER_END
namespace PIUnits {
PIP_EXPORT PIString className(int type);
PIP_EXPORT PIString name(int type);
PIP_EXPORT PIString unit(int type);
namespace Class {
enum {
Invalid = -1
};
class PIP_EXPORT Internal {
public:
class PIP_EXPORT ClassBase {
public:
virtual uint classID() const = 0;
virtual PIString className() const = 0;
virtual PIString name(int type) const = 0;
virtual PIString unit(int type) const = 0;
virtual PIString valueToString(double v, char format = 'g', int prec = 5) const = 0;
virtual double convert(double v, int from, int to) const = 0;
virtual bool supportPrefixes(int type) const { return true; }
virtual bool supportPrefixesNon3(int type) const { return false; }
virtual bool supportPrefixesGreater(int type) const { return true; }
virtual bool supportPrefixesSmaller(int type) const { return true; }
const PIVector<int> & allTypes() const { return types; }
protected:
PIVector<int> types;
};
template<typename P>
class Registrator {
public:
static void registerSelf() {
auto * uc = new P();
for (int t = P::typeStart; t < P::_LastType; ++t) {
uc->types << t;
Internal::typeClasses[t] = uc;
}
if (!Internal::allTypeClasses.contains(uc)) Internal::allTypeClasses << uc;
}
};
static PIMap<int, ClassBase *> typeClasses;
static PIVector<ClassBase *> allTypeClasses;
static const PIString unknown;
};
} // namespace Class
PIP_EXPORT PIVector<Class::Internal::ClassBase *> allClasses();
} // namespace PIUnits
#endif

View File

@@ -0,0 +1,74 @@
/*
PIP - Platform Independent Primitives
Angle units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_class_angle.h"
#include "pimathbase.h"
PIString PIUnits::Class::Angle::name(int type) const {
switch (type) {
case Degree: return "degree"_tr("PIUnitsAngle");
case Radian: return "radian"_tr("PIUnitsAngle");
}
return Class::Internal::unknown;
}
PIString PIUnits::Class::Angle::unit(int type) const {
switch (type) {
case Degree: return "°"_tr("PIUnitsAngle");
case Radian: return "rad"_tr("PIUnitsAngle");
}
return Class::Internal::unknown;
}
double PIUnits::Class::Angle::convert(double v, int from, int to) const {
switch (to) {
case Degree: return toDeg(v);
case Radian: return toRad(v);
}
return v;
}
PIString PIUnits::Class::Angle::valueToString(double v, char format, int prec) const {
return PIString::fromNumber(v, format, prec);
}
bool PIUnits::Class::Angle::supportPrefixes(int type) const {
return false;
}
bool PIUnits::Class::Angle::supportPrefixesNon3(int type) const {
return false;
}
bool PIUnits::Class::Angle::supportPrefixesGreater(int type) const {
return ClassBase::supportPrefixesGreater(type);
}
bool PIUnits::Class::Angle::supportPrefixesSmaller(int type) const {
return ClassBase::supportPrefixesSmaller(type);
}

View File

@@ -0,0 +1,39 @@
/*! \file piunits_class_angle.h
* \ingroup Core
* \~\brief
* \~english Angle units
* \~russian Единицы измерения угла
*/
/*
PIP - Platform Independent Primitives
Angle units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_CLASS_ANGLE_H
#define PIUNITS_CLASS_ANGLE_H
#include "piunits_base.h"
DECLARE_UNIT_CLASS_BEGIN(Angle, 0x200)
enum {
Degree = typeStart,
Radian,
_LastType,
};
DECLARE_UNIT_CLASS_END(Angle)
#endif

View File

@@ -0,0 +1,69 @@
/*
PIP - Platform Independent Primitives
Distance units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_class_distance.h"
PIString PIUnits::Class::Distance::name(int type) const {
switch (type) {
case Meter: return "meter"_tr("PIUnitsDistance");
}
return Class::Internal::unknown;
}
PIString PIUnits::Class::Distance::unit(int type) const {
switch (type) {
case Meter: return "m"_tr("PIUnitsDistance");
}
return Class::Internal::unknown;
}
double PIUnits::Class::Distance::convert(double v, int from, int to) const {
switch (to) {
case Meter: return v;
}
return v;
}
PIString PIUnits::Class::Distance::valueToString(double v, char format, int prec) const {
return PIString::fromNumber(v, format, prec);
}
bool PIUnits::Class::Distance::supportPrefixes(int type) const {
return true;
}
bool PIUnits::Class::Distance::supportPrefixesNon3(int type) const {
return true;
}
bool PIUnits::Class::Distance::supportPrefixesGreater(int type) const {
return ClassBase::supportPrefixesGreater(type);
}
bool PIUnits::Class::Distance::supportPrefixesSmaller(int type) const {
return ClassBase::supportPrefixesSmaller(type);
}

View File

@@ -0,0 +1,38 @@
/*! \file piunits_class_distance.h
* \ingroup Core
* \~\brief
* \~english Distance units
* \~russian Единицы измерения расстояния
*/
/*
PIP - Platform Independent Primitives
Distance units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_CLASS_DISTANCE_H
#define PIUNITS_CLASS_DISTANCE_H
#include "piunits_base.h"
DECLARE_UNIT_CLASS_BEGIN(Distance, 0x600)
enum {
Meter = typeStart,
_LastType,
};
DECLARE_UNIT_CLASS_END(Distance)
#endif

View File

@@ -0,0 +1,72 @@
/*
PIP - Platform Independent Primitives
Information units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_class_information.h"
PIString PIUnits::Class::Information::name(int type) const {
switch (type) {
case Bit: return "bit"_tr("PIUnitsInformation");
case Byte: return "byte"_tr("PIUnitsInformation");
}
return Class::Internal::unknown;
}
PIString PIUnits::Class::Information::unit(int type) const {
switch (type) {
case Bit: return "b"_tr("PIUnitsInformation");
case Byte: return "B"_tr("PIUnitsInformation");
}
return Class::Internal::unknown;
}
double PIUnits::Class::Information::convert(double v, int from, int to) const {
switch (to) {
case Bit: return v * 8;
case Byte: return v / 8;
}
return v;
}
PIString PIUnits::Class::Information::valueToString(double v, char format, int prec) const {
return PIString::fromNumber(static_cast<llong>(v));
}
bool PIUnits::Class::Information::supportPrefixes(int type) const {
return true;
}
bool PIUnits::Class::Information::supportPrefixesNon3(int type) const {
return false;
}
bool PIUnits::Class::Information::supportPrefixesGreater(int type) const {
return true;
}
bool PIUnits::Class::Information::supportPrefixesSmaller(int type) const {
return false;
}

View File

@@ -0,0 +1,39 @@
/*! \file piunits_class_information.h
* \ingroup Core
* \~\brief
* \~english Information units
* \~russian Единицы измерения информации
*/
/*
PIP - Platform Independent Primitives
Information units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_CLASS_INFORMATION_H
#define PIUNITS_CLASS_INFORMATION_H
#include "piunits_base.h"
DECLARE_UNIT_CLASS_BEGIN(Information, 0x100)
enum {
Bit = typeStart,
Byte,
_LastType,
};
DECLARE_UNIT_CLASS_END(Information)
#endif

View File

@@ -0,0 +1,86 @@
/*
PIP - Platform Independent Primitives
Pressure units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_class_pressure.h"
PIString PIUnits::Class::Pressure::name(int type) const {
switch (type) {
case Pascal: return "pascal"_tr("PIUnitsPressure");
case Atmosphere: return "atmosphere"_tr("PIUnitsPressure");
case Bar: return "bar"_tr("PIUnitsPressure");
case MillimetreOfMercury: return "mm Hg"_tr("PIUnitsPressure");
}
return Class::Internal::unknown;
}
PIString PIUnits::Class::Pressure::unit(int type) const {
switch (type) {
case Pascal: return "Pa"_tr("PIUnitsPressure");
case Atmosphere: return "atm"_tr("PIUnitsPressure");
case Bar: return "bar"_tr("PIUnitsPressure");
case MillimetreOfMercury: return "mmHg"_tr("PIUnitsPressure");
}
return Class::Internal::unknown;
}
double PIUnits::Class::Pressure::convert(double v, int from, int to) const {
double pa = v;
switch (from) {
case Atmosphere: pa /= 9.86923E-6; break;
case Bar: pa /= 1.E-5; break;
case MillimetreOfMercury: pa *= 133.322387415; break;
default: break;
}
switch (to) {
case Atmosphere: return pa * 9.86923E-6;
case Bar: return pa * 1.E-5;
case MillimetreOfMercury: return pa / 133.322387415;
default: break;
}
return pa;
}
PIString PIUnits::Class::Pressure::valueToString(double v, char format, int prec) const {
return PIString::fromNumber(v, format, prec);
}
bool PIUnits::Class::Pressure::supportPrefixes(int type) const {
if (type == Pascal) return true;
return false;
}
bool PIUnits::Class::Pressure::supportPrefixesNon3(int type) const {
return false;
}
bool PIUnits::Class::Pressure::supportPrefixesGreater(int type) const {
return ClassBase::supportPrefixesGreater(type);
}
bool PIUnits::Class::Pressure::supportPrefixesSmaller(int type) const {
return ClassBase::supportPrefixesSmaller(type);
}

View File

@@ -0,0 +1,41 @@
/*! \file piunits_class_pressure.h
* \ingroup Core
* \~\brief
* \~english Pressure units
* \~russian Единицы измерения давления
*/
/*
PIP - Platform Independent Primitives
Pressure units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_CLASS_PRESSURE_H
#define PIUNITS_CLASS_PRESSURE_H
#include "piunits_base.h"
DECLARE_UNIT_CLASS_BEGIN(Pressure, 0x500)
enum {
Pascal = typeStart,
Atmosphere,
Bar,
MillimetreOfMercury,
_LastType,
};
DECLARE_UNIT_CLASS_END(Pressure)
#endif

View File

@@ -0,0 +1,82 @@
/*
PIP - Platform Independent Primitives
Temperature units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_class_temperature.h"
PIString PIUnits::Class::Temperature::name(int type) const {
switch (type) {
case Kelvin: return "Kelvin"_tr("PIUnitsTemperature");
case Celsius: return "Celsius"_tr("PIUnitsTemperature");
case Fahrenheit: return "Fahrenheit"_tr("PIUnitsTemperature");
}
return Class::Internal::unknown;
}
PIString PIUnits::Class::Temperature::unit(int type) const {
switch (type) {
case Kelvin: return "K"_tr("PIUnitsTemperature");
case Celsius: return "°C"_tr("PIUnitsTemperature");
case Fahrenheit: return "°F"_tr("PIUnitsTemperature");
}
return Class::Internal::unknown;
}
double PIUnits::Class::Temperature::convert(double v, int from, int to) const {
double K = v;
switch (from) {
case Celsius: K += 273.15; break;
case Fahrenheit: K = (v + 459.67) * (5. / 9.); break;
default: break;
}
switch (to) {
case Celsius: return K - 273.15;
case Fahrenheit: return (K * (9. / 5.) - 459.67);
default: break;
}
return K;
}
PIString PIUnits::Class::Temperature::valueToString(double v, char format, int prec) const {
return PIString::fromNumber(v, format, prec);
}
bool PIUnits::Class::Temperature::supportPrefixes(int type) const {
if (type == Kelvin) return true;
return false;
}
bool PIUnits::Class::Temperature::supportPrefixesNon3(int type) const {
return false;
}
bool PIUnits::Class::Temperature::supportPrefixesGreater(int type) const {
return true;
}
bool PIUnits::Class::Temperature::supportPrefixesSmaller(int type) const {
return false;
}

View File

@@ -0,0 +1,40 @@
/*! \file piunits_class_temperature.h
* \ingroup Core
* \~\brief
* \~english Temperature units
* \~russian Единицы измерения температуры
*/
/*
PIP - Platform Independent Primitives
Temperature units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_CLASS_TEMPERATURE_H
#define PIUNITS_CLASS_TEMPERATURE_H
#include "piunits_base.h"
DECLARE_UNIT_CLASS_BEGIN(Temperature, 0x400)
enum {
Kelvin = typeStart,
Celsius,
Fahrenheit,
_LastType,
};
DECLARE_UNIT_CLASS_END(Temperature)
#endif

View File

@@ -0,0 +1,75 @@
/*
PIP - Platform Independent Primitives
Time units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_class_time.h"
PIString PIUnits::Class::Time::name(int type) const {
switch (type) {
case Second: return "second"_tr("PIUnitsTime");
case Hertz: return "hertz"_tr("PIUnitsTime");
}
return Class::Internal::unknown;
}
PIString PIUnits::Class::Time::unit(int type) const {
switch (type) {
case Second: return "s"_tr("PIUnitsTime");
case Hertz: return "Hz"_tr("PIUnitsTime");
}
return Class::Internal::unknown;
}
double PIUnits::Class::Time::convert(double v, int from, int to) const {
if (piCompared(v, 0.)) return 0.;
switch (to) {
case Second: return 1. / v;
case Hertz: return 1. / v;
}
return v;
}
PIString PIUnits::Class::Time::valueToString(double v, char format, int prec) const {
return PIString::fromNumber(v, format, prec);
}
bool PIUnits::Class::Time::supportPrefixes(int type) const {
return true;
}
bool PIUnits::Class::Time::supportPrefixesNon3(int type) const {
return false;
}
bool PIUnits::Class::Time::supportPrefixesGreater(int type) const {
if (type == Hertz) return true;
return false;
}
bool PIUnits::Class::Time::supportPrefixesSmaller(int type) const {
if (type == Second) return true;
return false;
}

View File

@@ -0,0 +1,39 @@
/*! \file piunits_class_time.h
* \ingroup Core
* \~\brief
* \~english Time units
* \~russian Единицы измерения времени
*/
/*
PIP - Platform Independent Primitives
Time units
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_CLASS_TIME_H
#define PIUNITS_CLASS_TIME_H
#include "piunits_base.h"
DECLARE_UNIT_CLASS_BEGIN(Time, 0x300)
enum {
Second = typeStart,
Hertz,
_LastType,
};
DECLARE_UNIT_CLASS_END(Time)
#endif

View File

@@ -0,0 +1,137 @@
/*
PIP - Platform Independent Primitives
Unit prefix
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_prefix.h"
#include "piliterals_string.h"
#include "pimathbase.h"
#include "pitranslator.h"
#include "piunits_base.h"
// quetta Q 10^30 1000000000000000000000000000000
// ronna R 10^27 1000000000000000000000000000
// yotta Y 10^24 1000000000000000000000000
// zetta Z 10^21 1000000000000000000000
// exa E 10^18 1000000000000000000
// peta P 10^15 1000000000000000
// tera T 10^12 1000000000000
// giga G 10^9 1000000000
// mega M 10^6 1000000
// kilo k 10^3 1000
// hecto h 10^2 100
// deca da 10^1 10
// — — 100 1 —
// deci d 10^1 0.1
// centi c 10^2 0.01
// milli m 10^3 0.001
// micro μ 10^6 0.000001
// nano n 10^9 0.000000001
// pico p 10^12 0.000000000001
// femto f 10^15 0.000000000000001
// atto a 10^18 0.000000000000000001
// zepto z 10^21 0.000000000000000000001
// yocto y 10^24 0.000000000000000000000001
// ronto r 10^27 0.000000000000000000000000001
PIString PIUnits::Prefix::name(int prefix) {
return instance().getPrefix(prefix).name;
}
PIString PIUnits::Prefix::prefix(int prefix) {
return instance().getPrefix(prefix).prefix;
}
PIString PIUnits::Prefix::valueToString(double v, void * type_class, int type, char format, int prec) {
auto * tc = reinterpret_cast<PIUnits::Class::Internal::ClassBase *>(type_class);
auto p =
instance().getPrefixForValue(v, tc->supportPrefixesNon3(type), tc->supportPrefixesGreater(type), tc->supportPrefixesSmaller(type));
return PIString::fromNumber(v / p.divider, format, prec) + " "_a + p.prefix;
}
double PIUnits::Prefix::multiplier(int prefix) {
return instance().getPrefix(prefix).divider;
}
PIUnits::Prefix::Prefix() {
def_prefix = {"", "", 0, 1., false};
// clang-format off
prefixes = {
{Deca, {"deca"_tr ("PIUnits"), "da"_tr("PIUnits") , 1 , pow10(1. ), true }},
{Hecto, {"hecto"_tr ("PIUnits"), "h"_tr ("PIUnits") , 2 , pow10(2. ), true }},
{Kilo, {"kilo"_tr ("PIUnits"), "k"_tr ("PIUnits") , 3 , pow10(3. ), false}},
{Mega, {"mega"_tr ("PIUnits"), "M"_tr ("PIUnits") , 6 , pow10(6. ), false}},
{Giga, {"giga"_tr ("PIUnits"), "G"_tr ("PIUnits") , 9 , pow10(9. ), false}},
{Tera, {"tera"_tr ("PIUnits"), "T"_tr ("PIUnits") , 12 , pow10(12. ), false}},
{Peta, {"peta"_tr ("PIUnits"), "P"_tr ("PIUnits") , 15 , pow10(15. ), false}},
{Exa, {"exa"_tr ("PIUnits"), "E"_tr ("PIUnits") , 18 , pow10(18. ), false}},
{Zetta, {"zetta"_tr ("PIUnits"), "Z"_tr ("PIUnits") , 21 , pow10(21. ), false}},
{Yotta, {"yotta"_tr ("PIUnits"), "Y"_tr ("PIUnits") , 24 , pow10(24. ), false}},
{Ronna, {"ronna"_tr ("PIUnits"), "R"_tr ("PIUnits") , 27 , pow10(27. ), false}},
{Quetta, {"quetta"_tr("PIUnits"), "Q"_tr ("PIUnits") , 30 , pow10(30. ), false}},
{Deci, {"deci"_tr ("PIUnits"), "d"_tr ("PIUnits") , -1 , pow10(-1. ), true }},
{Centi, {"centi"_tr ("PIUnits"), "c"_tr ("PIUnits") , -2 , pow10(-2. ), true }},
{Milli, {"milli"_tr ("PIUnits"), "m"_tr ("PIUnits") , -3 , pow10(-3. ), false}},
{Micro, {"micro"_tr ("PIUnits"), "u"_tr ("PIUnits") , -6 , pow10(-6. ), false}},
{Nano, {"nano"_tr ("PIUnits"), "n"_tr ("PIUnits") , -9 , pow10(-9. ), false}},
{Pico, {"pico"_tr ("PIUnits"), "p"_tr ("PIUnits") , -12, pow10(-12.), false}},
{Femto, {"femto"_tr ("PIUnits"), "f"_tr ("PIUnits") , -15, pow10(-15.), false}},
{Atto, {"atto"_tr ("PIUnits"), "a"_tr ("PIUnits") , -18, pow10(-18.), false}},
{Zepto, {"zepto"_tr ("PIUnits"), "z"_tr ("PIUnits") , -21, pow10(-21.), false}},
{Yocto, {"yocto"_tr ("PIUnits"), "y"_tr ("PIUnits") , -24, pow10(-24.), false}},
{Ronto, {"ronto"_tr ("PIUnits"), "r"_tr ("PIUnits") , -27, pow10(-27.), false}},
};
// clang-format on
auto it = prefixes.makeIterator();
while (it.next()) {
prefixes_by_pow[it.value().pow] = &it.value();
}
prefixes_by_pow[0] = &def_prefix;
}
const PIUnits::Prefix::P PIUnits::Prefix::getPrefixForValue(double v, bool use_non3, bool use_greater, bool use_smaller) const {
auto it = prefixes_by_pow.makeIterator();
const P * ret = &def_prefix;
while (it.next()) {
if (it.value()->pow < 0 && !use_smaller) continue;
if (it.value()->pow > 0 && !use_greater) continue;
if (it.value()->non3 && !use_non3) continue;
if (v < it.value()->divider) return *ret;
ret = it.value();
}
return def_prefix;
}
const PIUnits::Prefix::P PIUnits::Prefix::getPrefix(int p) const {
return prefixes.value(p, def_prefix);
}
PIUnits::Prefix & PIUnits::Prefix::instance() {
static Prefix ret;
return ret;
}

View File

@@ -0,0 +1,95 @@
/*! \file piunits_prefix.h
* \ingroup Core
* \~\brief
* \~english Unit prefixes
* \~russian Префиксы единиц измерения
*/
/*
PIP - Platform Independent Primitives
Unit prefix
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_PREFIX_H
#define PIUNITS_PREFIX_H
#include "pistring.h"
namespace PIUnits {
class PIP_EXPORT Prefix {
friend class Value;
public:
enum {
None,
Deca = 0x100, // da 10^1 10
Hecto, // h 10^2 100
Kilo, // k 10^3 1000
Mega, // M 10^6 1000000
Giga, // G 10^9 1000000000
Tera, // T 10^12 1000000000000
Peta, // P 10^15 1000000000000000
Exa, // E 10^18 1000000000000000000
Zetta, // Z 10^21 1000000000000000000000
Yotta, // Y 10^24 1000000000000000000000000
Ronna, // R 10^27 1000000000000000000000000000
Quetta, // Q 10^30 1000000000000000000000000000000
Deci = 0x200, // d 10^1 0.1
Centi, // c 10^2 0.01
Milli, // m 10^3 0.001
Micro, // μ 10^6 0.000001
Nano, // n 10^9 0.000000001
Pico, // p 10^12 0.000000000001
Femto, // f 10^15 0.000000000000001
Atto, // a 10^18 0.000000000000000001
Zepto, // z 10^21 0.000000000000000000001
Yocto, // y 10^24 0.000000000000000000000001
Ronto, // r 10^27 0.000000000000000000000000001
};
static PIString name(int prefix);
static PIString prefix(int prefix);
static double multiplier(int prefix);
private:
Prefix();
NO_COPY_CLASS(Prefix);
static Prefix & instance();
static PIString valueToString(double v, void * type_class, int type, char format = 'g', int prec = 5);
struct P {
PIString name;
PIString prefix;
int pow;
double divider;
bool non3;
};
const P getPrefix(int p) const;
const P getPrefixForValue(double v, bool use_non3, bool use_greater, bool use_smaller) const;
PIMap<int, P> prefixes;
PIMap<int, P *> prefixes_by_pow;
P def_prefix;
};
} // namespace PIUnits
#endif

View File

@@ -0,0 +1,60 @@
/*
PIP - Platform Independent Primitives
Unit value
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piunits_value.h"
#include "piliterals_string.h"
#include "piunits_prefix.h"
PIUnits::Value::Value(double v, int t) {
m_value = v;
m_class = Class::Internal::typeClasses.value(t);
if (m_class) m_type = t;
}
PIString PIUnits::Value::toString(char format, int prec) const {
if (isNotValid()) return Class::Internal::unknown;
PIString ret;
if (m_class->supportPrefixes(m_type)) {
ret = Prefix::valueToString(m_value, m_class, m_type, format, prec);
} else
ret = m_class->valueToString(m_value, format, prec) + " "_a;
ret += m_class->unit(m_type);
return ret;
}
bool PIUnits::Value::convert(int type_to) {
if (m_type == type_to) return true;
auto * class_to = Class::Internal::typeClasses.value(type_to);
if (!class_to) return false;
if (m_class->classID() != class_to->classID()) return false;
m_value = m_class->convert(m_value, m_type, type_to);
m_type = type_to;
return true;
}
PIUnits::Value PIUnits::Value::converted(int type_to) {
Value ret(*this);
if (!ret.convert(type_to)) return {};
return ret;
}

View File

@@ -0,0 +1,81 @@
/*! \file piunits_value.h
* \ingroup Core
* \~\brief
* \~english Unit value
* \~russian Единица измерения
*/
/*
PIP - Platform Independent Primitives
Unit value
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIUNITS_VALUE_H
#define PIUNITS_VALUE_H
#include "piunits_base.h"
namespace PIUnits {
/*
enum class Angle {
Degree = 0x200,
Radian,
};
enum class Time {
Second = 0x300,
Hertz,
};
enum class Temperature {
Kelvin = 0x400,
Celsius,
Fahrenheit,
};
enum class Pressure {
Pascal = 0x500,
};
enum class Distance {
Meter = 0x500,
};
};
*/
class PIP_EXPORT Value {
public:
Value(double v = 0., int t = Class::Invalid);
bool isValid() const { return m_type >= 0 && m_class; }
bool isNotValid() const { return m_type < 0 || !m_class; }
double value() const { return m_value; }
PIString toString(char format = 'g', int prec = 5) const;
bool convert(int type_to);
Value converted(int type_to);
private:
double m_value = 0.;
int m_type = -1;
Class::Internal::ClassBase * m_class = nullptr;
};
}; // namespace PIUnits
inline PICout operator<<(PICout s, const PIUnits::Value & v) {
s << v.toString();
return s;
}
#endif