32 Real conversionFactor) {
33 data_ = ext::make_shared<UnitOfMeasureConversion::Data>(
41 data_ = ext::make_shared<UnitOfMeasureConversion::Data>(
49 : commodityType(commodityType), source(source), target(target),
50 conversionFactor(conversionFactor), type(type) {
56 conversionFactorChain = std::make_pair(
57 ext::make_shared<UnitOfMeasureConversion>(r1),
58 ext::make_shared<UnitOfMeasureConversion>(r2));
62 switch (
data_->type) {
73 QL_FAIL(
"direct conversion not applicable");
76 ==
data_->conversionFactorChain.first->source() ||
78 ==
data_->conversionFactorChain.first->target())
79 return data_->conversionFactorChain.second->convert(
80 data_->conversionFactorChain.first->convert(quantity));
82 ==
data_->conversionFactorChain.second->source() ||
84 ==
data_->conversionFactorChain.second->target())
85 return data_->conversionFactorChain.first->convert(
86 data_->conversionFactorChain.second->convert(quantity));
88 QL_FAIL(
"derived conversion factor not applicable");
90 QL_FAIL(
"unknown conversion-factor type");
102 result.
data_->conversionFactor =
103 r2.
data_->conversionFactor/r1.
data_->conversionFactor;
104 }
else if (r1.
data_->source == r2.
data_->target) {
107 result.
data_->conversionFactor =
108 1.0/(r1.
data_->conversionFactor*r2.
data_->conversionFactor);
109 }
else if (r1.
data_->target == r2.
data_->source) {
112 result.
data_->conversionFactor =
113 r1.
data_->conversionFactor*r2.
data_->conversionFactor;
114 }
else if (r1.
data_->target == r2.
data_->target) {
117 result.
data_->conversionFactor =
118 r1.
data_->conversionFactor/r2.
data_->conversionFactor;
120 QL_FAIL(
"conversion factors not chainable");
const std::string & name() const
name, e.g, "Heating Oil"
const CommodityType & commodityType() const
const UnitOfMeasure & unitOfMeasure() const
const UnitOfMeasure & source() const
the source UOM.
Quantity convert(const Quantity &quantity) const
apply the conversion factor to a cash amount
const UnitOfMeasure & target() const
the target UOM.
Real conversionFactor() const
the conversion factor
UnitOfMeasureConversion()=default
const CommodityType & commodityType() const
the commodity type.
ext::shared_ptr< Data > data_
static UnitOfMeasureConversion chain(const UnitOfMeasureConversion &r1, const UnitOfMeasureConversion &r2)
chain two conversion factors
Unit of measure specification
const std::string & code() const
code, e.g, "BBL", "MT"
Classes and functions for error handling.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
CommodityType commodityType
Data(const CommodityType &commodityType, const UnitOfMeasure &source, const UnitOfMeasure &target, Real conversionFactor, Type type)
Unit of measure conversion.