/*! \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 . */ #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 { \ private: \ friend class Internal::Registrator; \ 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 & allTypes() const { return types; } protected: PIVector types; }; template 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 typeClasses; static PIVector allTypeClasses; static const PIString unknown; }; } // namespace Class PIP_EXPORT PIVector allClasses(); } // namespace PIUnits #endif