QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
quantity.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
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
29#include <utility>
30
31namespace QuantLib {
32
33 //! Amount of a commodity
34 class Quantity {
35 public:
36 //! \name Constructors
37 //@{
38 Quantity() = default;
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;
60 //@}
61 /*! \name Conversion settings
62
63 These parameters are used for combining quantity amounts
64 in different currencies
65 */
66 //@{
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 };
78 //@}
79
80 friend std::ostream& operator<<(std::ostream&, const Quantity&);
81 private:
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 */
96 /*! \relates Quantity */
98 /*! \relates Quantity */
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
129 return commodityType_;
130 }
131
133 return unitOfMeasure_;
134 }
135
136 inline Real Quantity::amount() const {
137 return amount_;
138 }
139
144 }
145
147 return *this;
148 }
149
152 }
153
155 amount_ *= x;
156 return *this;
157 }
158
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
Amount of a commodity.
Definition: quantity.hpp:34
static ConversionType conversionType
Definition: quantity.hpp:76
UnitOfMeasure unitOfMeasure_
Definition: quantity.hpp:83
static UnitOfMeasure baseUnitOfMeasure
Definition: quantity.hpp:77
Quantity operator+() const
Definition: quantity.hpp:146
Quantity & operator-=(const Quantity &)
Definition: quantity.cpp:68
CommodityType commodityType_
Definition: quantity.hpp:82
Quantity operator-() const
Definition: quantity.hpp:150
friend std::ostream & operator<<(std::ostream &, const Quantity &)
Definition: quantity.cpp:202
Quantity & operator*=(Real)
Definition: quantity.hpp:154
Quantity & operator+=(const Quantity &)
Definition: quantity.cpp:50
const CommodityType & commodityType() const
Definition: quantity.hpp:128
Quantity & operator/=(Real)
Definition: quantity.hpp:159
Quantity rounded() const
Definition: quantity.hpp:140
Real amount() const
Definition: quantity.hpp:136
const UnitOfMeasure & unitOfMeasure() const
Definition: quantity.hpp:132
Unit of measure specification
const Rounding & rounding() const
commodity type
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Quantity operator-(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:171
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:231
bool operator<(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:125
Quantity operator*(const Quantity &m, Real x)
Definition: quantity.hpp:177
bool operator>=(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:201
Quantity operator+(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:165
bool operator!=(const Currency &c1, const Currency &c2)
Definition: currency.hpp:236
bool operator>(const Quantity &m1, const Quantity &m2)
Definition: quantity.hpp:197
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
bool operator<=(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:144
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
Real operator/(const Quantity &m1, const Quantity &m2)
Definition: quantity.cpp:86
STL namespace.
Unit of measure.