QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
unitofmeasureconversion.hpp
Go to the documentation of this file.
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/*! \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
30#include <ql/utilities/null.hpp>
31#include <utility>
32
33namespace QuantLib {
34
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 //@{
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 */
49 const UnitOfMeasure& source,
50 const UnitOfMeasure& target,
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
76 const UnitOfMeasureConversion& r2);
77 //@}
78 protected:
80 const UnitOfMeasureConversion& r2);
81
82 struct Data;
83 ext::shared_ptr<Data> data_;
84
85 struct Data {
90 std::string code;
91 std::pair<ext::shared_ptr<UnitOfMeasureConversion>,
92 ext::shared_ptr<UnitOfMeasureConversion> >
94
96 const UnitOfMeasure& source,
97 const UnitOfMeasure& target,
99 Type type);
100
102 const UnitOfMeasureConversion& r2);
103 };
104 };
105
106 // inline definitions
107
109 return data_->commodityType;
110 }
111
113 return data_->source;
114 }
115
117 return data_->target;
118 }
119
121 return data_->conversionFactor;
122 }
123
125 return data_->type;
126 }
127
128 inline const std::string& UnitOfMeasureConversion::code() const {
129 return data_->code;
130 }
131
132}
133
134#endif
Amount of a commodity.
Definition: quantity.hpp:34
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
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
null values
Amount of a commodity.
std::pair< ext::shared_ptr< UnitOfMeasureConversion >, ext::shared_ptr< UnitOfMeasureConversion > > conversionFactorChain
Unit of measure.