24#include "AstUtil/StringView.hpp"
39enum class EUnitKind: uint8_t{
60 , name_{std::string(name)}
61 , dimension_{dimension}
66 , name_{std::string(name)}
67 , dimension_{dimension}
75 bool operator==(
const UnitRep& other)
const;
78 virtual double toSI(
double value)
const = 0;
79 virtual double fromSI(
double value)
const = 0;
83 EUnitKind kind()
const {
return kind_; }
93 using UnitRep::UnitRep;
95 :
UnitRep(EUnitKind::eScale, name, dimension)
99 :
UnitRep(EUnitKind::eScale, name, dimension, subunits)
103 double toSI(
double value)
const override {
return value * scale_;}
104 double fromSI(
double value)
const override {
return value / scale_;}
106 double notused_{0.0};
115 using UnitRep::UnitRep;
117 :
UnitRep(EUnitKind::eAffine, name, dimension)
122 double toSI(
double value)
const override {
return value * scale_ + offset_;}
123 double fromSI(
double value)
const override {
return (value - offset_) / scale_;}
135 using UnitRep::UnitRep;
137 :
UnitRep(EUnitKind::eLogarithmic, name, dimension)
138 , reference_{reference}
142 double toSI(
double value)
const override {
return reference_ * std::pow(10.0, value / factor_);}
143 double fromSI(
double value)
const override {
return std::log10(value / reference_) * factor_;}
144 double reference_{1.0};
151inline bool UnitRep::operator==(
const UnitRep &rep)
const
158 auto self =
static_cast<const ScaleUnitRep*
>(
this);
159 auto other =
static_cast<const ScaleUnitRep*
>(&rep);
160 return dimension_ == other->dimension_ && kind_ == other->kind_ && self->scale_ == other->scale_ && self->notused_ == other->notused_;
163inline double UnitRep::scale()
const
165 if(kind_ == EUnitKind::eScale){
166 return static_cast<const ScaleUnitRep*
>(
this)->scale_;
168 else if(kind_ == EUnitKind::eAffine){
169 return static_cast<const AffineUnitRep*
>(
this)->scale_;
175inline bool UnitRep::valid()
const
177 if(kind_ == EUnitKind::eScale){
178 return static_cast<const ScaleUnitRep*
>(
this)->scale_ != 0.0;
180 else if(kind_ == EUnitKind::eAffine){
181 return static_cast<const AffineUnitRep*
>(
this)->scale_ != 0.0;
仿射型单位表示: SI = v * scale + offset
定义 UnitRep.hpp:113
UnitRepHandle clone() const override
深拷贝单位表示(独立副本)
定义 UnitRep.hpp:127
对数型单位表示: SI = reference * 10^(v / factor)
定义 UnitRep.hpp:133
UnitRepHandle clone() const override
深拷贝单位表示(独立副本)
定义 UnitRep.hpp:147
乘法型单位表示: SI = v * scale
定义 UnitRep.hpp:91
UnitRepHandle clone() const override
深拷贝单位表示(独立副本)
定义 UnitRep.hpp:108
SubUnitListConst subUnits_
子单位列表
定义 UnitRep.hpp:73
std::string name_
名称
定义 UnitRep.hpp:71
virtual UnitRepHandle clone() const =0
深拷贝单位表示(独立副本)
EUnitKind kind_
单位表示类型
定义 UnitRep.hpp:70
Dimension dimension_
量纲
定义 UnitRep.hpp:72
std::pair< UnitRepHandle, int > UnitRepPair
单位表示子项
定义 UnitRep.hpp:47
std::vector< UnitRepPair > SubUnitList
单位表示子项列表
定义 UnitRep.hpp:48
std::shared_ptr< const UnitRep > UnitRepHandleConst
单位表示句柄(常量)
定义 UnitRep.hpp:49
std::pair< UnitRepHandleConst, int > UnitRepPairConst
单位表示子项(常量)
定义 UnitRep.hpp:50
std::shared_ptr< UnitRep > UnitRepHandle
单位表示句柄
定义 UnitRep.hpp:46
std::vector< UnitRepPairConst > SubUnitListConst
单位表示子项列表(常量)
定义 UnitRep.hpp:51