QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
money.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2004 Decillion Pty(Ltd)
5 Copyright (C) 2004, 2005, 2006 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
25#ifndef quantlib_money_hpp
26#define quantlib_money_hpp
27
28#include <ql/currency.hpp>
29#include <ql/patterns/singleton.hpp>
30#include <utility>
31
32namespace QuantLib {
33
35
38 class Money {
39 public:
41
42 Money() = default;
46
48 const Currency& currency() const;
49 Decimal value() const;
50 Money rounded() const;
52
58 Money operator+() const;
59 Money operator-() const;
60 Money& operator+=(const Money&);
61 Money& operator-=(const Money&);
65
79 };
80 // Money::Settings forward declaration
81 class Settings;
83 private:
86
87 // temporary support for old syntax
89 public:
91 operator Currency() const;
92 };
93
95 public:
97 operator Money::ConversionType() const;
98 };
99 };
100
102 class Money::Settings : public Singleton<Money::Settings> {
103 friend class Singleton<Money::Settings>;
104 private:
105 Settings() = default;
106
107 public:
110
111 const Currency & baseCurrency() const;
113
114 private:
117 };
118
119 // More arithmetics and comparisons
120
122 Money operator+(const Money&, const Money&);
124 Money operator-(const Money&, const Money&);
126 Money operator*(const Money&, Decimal);
128 Money operator*(Decimal, const Money&);
130 Money operator/(const Money&, Decimal);
132 Decimal operator/(const Money&, const Money&);
133
135 bool operator==(const Money&, const Money&);
137 bool operator!=(const Money&, const Money&);
139 bool operator<(const Money&, const Money&);
141 bool operator<=(const Money&, const Money&);
143 bool operator>(const Money&, const Money&);
145 bool operator>=(const Money&, const Money&);
146
148 bool close(const Money&, const Money&, Size n = 42);
150 bool close_enough(const Money&, const Money&, Size n = 42);
151
152 // syntactic sugar
153
155 Money operator*(Decimal, const Currency&);
157 Money operator*(const Currency&, Decimal);
158
159 // formatting
160
162 std::ostream& operator<<(std::ostream&, const Money&);
163
164
165 // inline definitions
166
167 inline Money::Money(Currency currency, Decimal value)
168 : value_(value), currency_(std::move(currency)) {}
169
170 inline Money::Money(Decimal value, Currency currency)
171 : value_(value), currency_(std::move(currency)) {}
172
173 inline const Currency& Money::currency() const {
174 return currency_;
175 }
176
177 inline Decimal Money::value() const {
178 return value_;
179 }
180
181 inline Money Money::rounded() const {
182 return Money(currency_.rounding()(value_), currency_);
183 }
184
185 inline Money Money::operator+() const {
186 return *this;
187 }
188
189 inline Money Money::operator-() const {
190 return Money(-value_, currency_);
191 }
192
193 inline Money& Money::operator*=(Decimal x) {
194 value_ *= x;
195 return *this;
196 }
197
198 inline Money& Money::operator/=(Decimal x) {
199 value_ /= x;
200 return *this;
201 }
202
203
204 inline Money operator+(const Money& m1, const Money& m2) {
205 Money tmp = m1;
206 tmp += m2;
207 return tmp;
208 }
209
210 inline Money operator-(const Money& m1, const Money& m2) {
211 Money tmp = m1;
212 tmp -= m2;
213 return tmp;
214 }
215
216 inline Money operator*(const Money& m, Decimal x) {
217 Money tmp = m;
218 tmp *= x;
219 return tmp;
220 }
221
222 inline Money operator*(Decimal x, const Money& m) {
223 return m*x;
224 }
225
226 inline Money operator/(const Money& m, Decimal x) {
227 Money tmp = m;
228 tmp /= x;
229 return tmp;
230 }
231
232 inline bool operator!=(const Money& m1, const Money& m2) {
233 return !(m1 == m2);
234 }
235
236 inline bool operator>(const Money& m1, const Money& m2) {
237 return m2 < m1;
238 }
239
240 inline bool operator>=(const Money& m1, const Money& m2) {
241 return m2 <= m1;
242 }
243
244 inline Money operator*(Decimal value, const Currency& c) {
245 return Money(value,c);
246 }
247
248 inline Money operator*(const Currency& c, Decimal value) {
249 return Money(value,c);
250 }
251
252}
253
254
255#endif
Currency specification
Definition: currency.hpp:36
Per-session settings for the Money class.
Definition: money.hpp:102
const Money::ConversionType & conversionType() const
Definition: money.cpp:218
Money::ConversionType conversionType_
Definition: money.hpp:115
const Currency & baseCurrency() const
Definition: money.cpp:228
amount of cash
Definition: money.hpp:38
Decimal value_
Definition: money.hpp:84
bool close(const Money &, const Money &, Size n=42)
Definition: money.cpp:167
Money & operator+=(const Money &)
Definition: money.cpp:49
Money & operator*=(Decimal)
Definition: money.hpp:193
Money rounded() const
Definition: money.hpp:181
Money()=default
const Currency & currency() const
Definition: money.hpp:173
Money operator-() const
Definition: money.hpp:189
Decimal value() const
Definition: money.hpp:177
Money & operator-=(const Money &)
Definition: money.cpp:68
Currency currency_
Definition: money.hpp:85
@ BaseCurrencyConversion
Definition: money.hpp:73
@ AutomatedConversion
Definition: money.hpp:76
bool close_enough(const Money &, const Money &, Size n=42)
Definition: money.cpp:187
Money & operator/=(Decimal)
Definition: money.hpp:198
Money operator+() const
Definition: money.hpp:185
global repository for run-time library settings
Definition: settings.hpp:37
Basic support for the singleton pattern.
Definition: singleton.hpp:58
Real Decimal
decimal number
Definition: types.hpp:54
Definition: any.hpp:35
BaseCurrencyProxy & operator=(const Currency &)
Definition: money.cpp:238
ConversionTypeProxy & operator=(Money::ConversionType)
Definition: money.cpp:247