QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
quantity.hpp
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
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
34 class Quantity {
35 public:
37
38 Quantity() = default;
41
43 const CommodityType& commodityType() const;
44 const UnitOfMeasure& unitOfMeasure() const;
45 Real amount() const;
46 Quantity rounded() const;
48
54 Quantity operator+() const;
55 Quantity operator-() const;
61
75 };
79
80 friend std::ostream& operator<<(std::ostream&, const Quantity&);
81 private:
85 };
86
87
88 // More arithmetics and comparisons
89
91 Quantity operator+(const Quantity&, const Quantity&);
93 Quantity operator-(const Quantity&, const Quantity&);
101 Real operator/(const Quantity&, const Quantity&);
102
104 bool operator==(const Quantity&, const Quantity&);
106 bool operator!=(const Quantity&, const Quantity&);
108 bool operator<(const Quantity&, const Quantity&);
110 bool operator<=(const Quantity&, const Quantity&);
112 bool operator>(const Quantity&, const Quantity&);
114 bool operator>=(const Quantity&, const Quantity&);
115
117 bool close(const Quantity&, const Quantity&, Size n = 42);
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
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:191
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:196
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.