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:
124
libs/main/units/piunits_base.h
Normal file
124
libs/main/units/piunits_base.h
Normal 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
|
||||
Reference in New Issue
Block a user