1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 J. Erik Radmall
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file quantity.hpp
21 \brief Amount of a commodity
22*/
23
24#ifndef quantlib_quantity_hpp
25#define quantlib_quantity_hpp
26
27#include <ql/experimental/commodities/commoditytype.hpp>
28#include <ql/experimental/commodities/unitofmeasure.hpp>
29#include <utility>
30
31namespace QuantLib {
32
33 //! Amount of a commodity
34 class Quantity {
35 public:
36 //! \name Constructors
37 //@{
38 Quantity() = default;
39 Quantity(CommodityType commodityType, UnitOfMeasure unitOfMeasure, Real amount);
40 //@}
41 //! \name Inspectors
42 //@{
43 const CommodityType& commodityType() const;
44 const UnitOfMeasure& unitOfMeasure() const;
45 Real amount() const;
46 Quantity rounded() const;
47 //@}
48 /*! \name Quantity arithmetics
49
50 See below for non-member functions and for settings which
51 determine the behavior of the operators.
52 */
53 //@{
54 Quantity operator+() const;
55 Quantity operator-() const;
56 Quantity& operator+=(const Quantity&);
57 Quantity& operator-=(const Quantity&);
58 Quantity& operator*=(Real);
59 Quantity& operator/=(Real);
60 //@}
61 /*! \name Conversion settings
62
63 These parameters are used for combining quantity amounts
64 in different currencies
65 */
66 //@{
67 enum ConversionType {
68 NoConversion, /*!< do not perform conversions */
69 BaseUnitOfMeasureConversion, /*!< convert both operands to
70 the base unitOfMeasure before
71 converting */
72 AutomatedConversion /*!< return the result in the
73 unitOfMeasure of the first
74 operand */
75 };
76 static ConversionType conversionType;
77 static UnitOfMeasure baseUnitOfMeasure;
78 //@}
79
80 friend std::ostream& operator<<(std::ostream&, const Quantity&);
81 private:
82 CommodityType commodityType_;
83 UnitOfMeasure unitOfMeasure_;
84 Real amount_ = 0.0;
85 };
86
87
88 // More arithmetics and comparisons
89
90 /*! \relates Quantity */
91 Quantity operator+(const Quantity&, const Quantity&);
92 /*! \relates Quantity */
93 Quantity operator-(const Quantity&, const Quantity&);
94 /*! \relates Quantity */
95 Quantity operator*(const Quantity&, Real);
96 /*! \relates Quantity */
97 Quantity operator*(Real, const Quantity&);
98 /*! \relates Quantity */
99 Quantity operator/(const Quantity&, Real);
100 /*! \relates Quantity */
101 Real operator/(const Quantity&, const Quantity&);
102
103 /*! \relates Quantity */
104 bool operator==(const Quantity&, const Quantity&);
105 /*! \relates Quantity */
106 bool operator!=(const Quantity&, const Quantity&);
107 /*! \relates Quantity */
108 bool operator<(const Quantity&, const Quantity&);
109 /*! \relates Quantity */
110 bool operator<=(const Quantity&, const Quantity&);
111 /*! \relates Quantity */
112 bool operator>(const Quantity&, const Quantity&);
113 /*! \relates Quantity */
114 bool operator>=(const Quantity&, const Quantity&);
115
116 /*! \relates Quantity */
117 bool close(const Quantity&, const Quantity&, Size n = 42);
118 /*! \relates Quantity */
119 bool close_enough(const Quantity&, const Quantity&, Size n = 42);
120
121
122 // inline definitions
123
124 inline Quantity::Quantity(CommodityType commodityType, UnitOfMeasure unitOfMeasure, Real amount)
125 : commodityType_(std::move(commodityType)), unitOfMeasure_(std::move(unitOfMeasure)),
126 amount_(amount) {}
127
128 inline const CommodityType& Quantity::commodityType() const {
129 return commodityType_;
130 }
131
132 inline const UnitOfMeasure& Quantity::unitOfMeasure() const {
133 return unitOfMeasure_;
134 }
135
136 inline Real Quantity::amount() const {
137 return amount_;
138 }
139
140 inline Quantity Quantity::rounded() const {
141 return Quantity(commodityType_,
142 unitOfMeasure_,
143 unitOfMeasure_.rounding()(amount_));
144 }
145
146 inline Quantity Quantity::operator+() const {
147 return *this;
148 }
149
150 inline Quantity Quantity::operator-() const {
151 return Quantity(commodityType_, unitOfMeasure_, -amount_);
152 }
153
154 inline Quantity& Quantity::operator*=(Real x) {
155 amount_ *= x;
156 return *this;
157 }
158
159 inline Quantity& Quantity::operator/=(Real x) {
160 amount_ /= x;
161 return *this;
162 }
163
164
165 inline Quantity operator+(const Quantity& m1, const Quantity& m2) {
166 Quantity tmp = m1;
167 tmp += m2;
168 return tmp;
169 }
170
171 inline Quantity operator-(const Quantity& m1, const Quantity& m2) {
172 Quantity tmp = m1;
173 tmp -= m2;
174 return tmp;
175 }
176
177 inline Quantity operator*(const Quantity& m, Real x) {
178 Quantity tmp = m;
179 tmp *= x;
180 return tmp;
181 }
182
183 inline Quantity operator*(Real x, const Quantity& m) {
184 return m*x;
185 }
186
187 inline Quantity operator/(const Quantity& m, Real x) {
188 Quantity tmp = m;
189 tmp /= x;
190 return tmp;
191 }
192
193 inline bool operator!=(const Quantity& m1, const Quantity& m2) {
194 return !(m1 == m2);
195 }
196
197 inline bool operator>(const Quantity& m1, const Quantity& m2) {
198 return m2 < m1;
199 }
200
201 inline bool operator>=(const Quantity& m1, const Quantity& m2) {
202 return m2 <= m1;
203 }
204
205}
206
207
208#endif
209

source code of quantlib/ql/experimental/commodities/quantity.hpp