/src/quantlib/ql/experimental/commodities/unitofmeasureconversion.hpp
Line | Count | Source |
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 | | <https://www.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 | | /*! \file unitofmeasureconversion.hpp |
22 | | \brief Unit of measure conversion |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_unit_of_measure_conversion_hpp |
26 | | #define quantlib_unit_of_measure_conversion_hpp |
27 | | |
28 | | #include <ql/experimental/commodities/quantity.hpp> |
29 | | #include <ql/experimental/commodities/unitofmeasure.hpp> |
30 | | #include <ql/utilities/null.hpp> |
31 | | #include <utility> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | class UnitOfMeasureConversion { |
36 | | public: |
37 | | enum Type { Direct, /*!< given directly by the user */ |
38 | | Derived /*!< derived from conversion factors |
39 | | between other uoms */ |
40 | | }; |
41 | | //! \name Constructors |
42 | | //@{ |
43 | | UnitOfMeasureConversion() = default; |
44 | | /*! the conversionFactor \f$ r \f$ is given with the |
45 | | convention that a unit of the source is worth \f$ r \f$ |
46 | | units of the target. |
47 | | */ |
48 | | UnitOfMeasureConversion(const CommodityType& commodityType, |
49 | | const UnitOfMeasure& source, |
50 | | const UnitOfMeasure& target, |
51 | | Real conversionFactor); |
52 | | //@} |
53 | | |
54 | | //! \name Inspectors |
55 | | //@{ |
56 | | //! the source UOM. |
57 | | const UnitOfMeasure& source() const; |
58 | | //! the target UOM. |
59 | | const UnitOfMeasure& target() const; |
60 | | //! the commodity type. |
61 | | const CommodityType& commodityType() const; |
62 | | //! the type |
63 | | Type type() const; |
64 | | //! the conversion factor |
65 | | Real conversionFactor() const; |
66 | | |
67 | | const std::string& code() const; |
68 | | //@} |
69 | | |
70 | | //! \name Utility methods |
71 | | //@{ |
72 | | //! apply the conversion factor to a cash amount |
73 | | Quantity convert(const Quantity& quantity) const; |
74 | | //! chain two conversion factors |
75 | | static UnitOfMeasureConversion chain(const UnitOfMeasureConversion& r1, |
76 | | const UnitOfMeasureConversion& r2); |
77 | | //@} |
78 | | protected: |
79 | | UnitOfMeasureConversion(const UnitOfMeasureConversion& r1, |
80 | | const UnitOfMeasureConversion& r2); |
81 | | |
82 | | struct Data; |
83 | | ext::shared_ptr<Data> data_; |
84 | | |
85 | | struct Data { |
86 | | CommodityType commodityType; |
87 | | UnitOfMeasure source, target; |
88 | | Real conversionFactor; |
89 | | Type type; |
90 | | std::string code; |
91 | | std::pair<ext::shared_ptr<UnitOfMeasureConversion>, |
92 | | ext::shared_ptr<UnitOfMeasureConversion> > |
93 | | conversionFactorChain; |
94 | | |
95 | | Data(const CommodityType& commodityType, |
96 | | const UnitOfMeasure& source, |
97 | | const UnitOfMeasure& target, |
98 | | Real conversionFactor, |
99 | | Type type); |
100 | | |
101 | | Data(const UnitOfMeasureConversion& r1, |
102 | | const UnitOfMeasureConversion& r2); |
103 | | }; |
104 | | }; |
105 | | |
106 | | // inline definitions |
107 | | |
108 | 0 | inline const CommodityType& UnitOfMeasureConversion::commodityType() const { |
109 | 0 | return data_->commodityType; |
110 | 0 | } |
111 | | |
112 | 0 | inline const UnitOfMeasure& UnitOfMeasureConversion::source() const { |
113 | 0 | return data_->source; |
114 | 0 | } |
115 | | |
116 | 0 | inline const UnitOfMeasure& UnitOfMeasureConversion::target() const { |
117 | 0 | return data_->target; |
118 | 0 | } |
119 | | |
120 | 0 | inline Real UnitOfMeasureConversion::conversionFactor() const { |
121 | 0 | return data_->conversionFactor; |
122 | 0 | } |
123 | | |
124 | 0 | inline UnitOfMeasureConversion::Type UnitOfMeasureConversion::type() const { |
125 | 0 | return data_->type; |
126 | 0 | } |
127 | | |
128 | 0 | inline const std::string& UnitOfMeasureConversion::code() const { |
129 | 0 | return data_->code; |
130 | 0 | } |
131 | | |
132 | | } |
133 | | |
134 | | #endif |