QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
unitofmeasureconversion.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 J. Erik Radmall
5 Copyright (C) 2009 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/experimental/commodities/unitofmeasureconversion.hpp>
22#include <ql/errors.hpp>
23
24using std::string;
25
26namespace QuantLib {
27
29 const CommodityType& commodityType,
30 const UnitOfMeasure& source,
31 const UnitOfMeasure& target,
32 Real conversionFactor) {
33 data_ = ext::make_shared<UnitOfMeasureConversion::Data>(
36 }
37
40 const UnitOfMeasureConversion& r2) {
41 data_ = ext::make_shared<UnitOfMeasureConversion::Data>(
42 r1, r2);
43 }
44
46 const UnitOfMeasure& source,
47 const UnitOfMeasure& target,
48 Real conversionFactor, Type type)
49 : commodityType(commodityType), source(source), target(target),
50 conversionFactor(conversionFactor), type(type) {
52 }
53
55 const UnitOfMeasureConversion& r2) {
56 conversionFactorChain = std::make_pair(
57 ext::make_shared<UnitOfMeasureConversion>(r1),
58 ext::make_shared<UnitOfMeasureConversion>(r2));
59 }
60
62 switch (data_->type) {
63 case Direct:
64 if (quantity.unitOfMeasure() == data_->source)
65 return Quantity(quantity.commodityType(),
66 data_->target,
67 quantity.amount()*data_->conversionFactor);
68 else if (quantity.unitOfMeasure() == data_->target)
69 return Quantity(quantity.commodityType(),
70 data_->source,
71 quantity.amount()/data_->conversionFactor);
72 else
73 QL_FAIL("direct conversion not applicable");
74 case Derived:
75 if (quantity.unitOfMeasure()
76 == data_->conversionFactorChain.first->source() ||
77 quantity.unitOfMeasure()
78 == data_->conversionFactorChain.first->target())
79 return data_->conversionFactorChain.second->convert(
80 data_->conversionFactorChain.first->convert(quantity));
81 else if (quantity.unitOfMeasure()
82 == data_->conversionFactorChain.second->source() ||
83 quantity.unitOfMeasure()
84 == data_->conversionFactorChain.second->target())
85 return data_->conversionFactorChain.first->convert(
86 data_->conversionFactorChain.second->convert(quantity));
87 else
88 QL_FAIL("derived conversion factor not applicable");
89 default:
90 QL_FAIL("unknown conversion-factor type");
91 }
92 }
93
96 const UnitOfMeasureConversion& r2) {
97 UnitOfMeasureConversion result(r1, r2);
98 result.data_->type = Derived;
99 if (r1.data_->source == r2.data_->source) {
100 result.data_->source = r1.data_->target;
101 result.data_->target = r2.data_->target;
102 result.data_->conversionFactor =
103 r2.data_->conversionFactor/r1.data_->conversionFactor;
104 } else if (r1.data_->source == r2.data_->target) {
105 result.data_->source = r1.data_->target;
106 result.data_->target = r2.data_->source;
107 result.data_->conversionFactor =
108 1.0/(r1.data_->conversionFactor*r2.data_->conversionFactor);
109 } else if (r1.data_->target == r2.data_->source) {
110 result.data_->source = r1.data_->source;
111 result.data_->target = r2.data_->target;
112 result.data_->conversionFactor =
113 r1.data_->conversionFactor*r2.data_->conversionFactor;
114 } else if (r1.data_->target == r2.data_->target) {
115 result.data_->source = r1.data_->source;
116 result.data_->target = r2.data_->source;
117 result.data_->conversionFactor =
118 r1.data_->conversionFactor/r2.data_->conversionFactor;
119 } else {
120 QL_FAIL("conversion factors not chainable");
121 }
122 return result;
123 }
124
125}
const std::string & name() const
name, e.g, "Heating Oil"
Amount of a commodity.
Definition: quantity.hpp:34
const CommodityType & commodityType() const
Definition: quantity.hpp:128
Real amount() const
Definition: quantity.hpp:136
const UnitOfMeasure & unitOfMeasure() const
Definition: quantity.hpp:132
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
const CommodityType & commodityType() const
the commodity type.
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"
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
Data(const CommodityType &commodityType, const UnitOfMeasure &source, const UnitOfMeasure &target, Real conversionFactor, Type type)