| 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 | |
| 21 | /*! \file money.hpp |
| 22 | \brief cash amount in a given currency |
| 23 | */ |
| 24 | |
| 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 | |
| 32 | namespace QuantLib { |
| 33 | |
| 34 | //! amount of cash |
| 35 | /*! \test money arithmetic is tested with and without currency |
| 36 | conversions. |
| 37 | */ |
| 38 | class Money { |
| 39 | public: |
| 40 | //! \name Constructors |
| 41 | //@{ |
| 42 | Money() = default; |
| 43 | Money(Currency currency, Decimal value); |
| 44 | Money(Decimal value, Currency currency); |
| 45 | //@} |
| 46 | //! \name Inspectors |
| 47 | //@{ |
| 48 | const Currency& currency() const; |
| 49 | Decimal value() const; |
| 50 | Money rounded() const; |
| 51 | //@} |
| 52 | /*! \name Money arithmetics |
| 53 | |
| 54 | See below for non-member functions and for settings which |
| 55 | determine the behavior of the operators. |
| 56 | */ |
| 57 | //@{ |
| 58 | Money operator+() const; |
| 59 | Money operator-() const; |
| 60 | Money& operator+=(const Money&); |
| 61 | Money& operator-=(const Money&); |
| 62 | Money& operator*=(Decimal); |
| 63 | Money& operator/=(Decimal); |
| 64 | //@} |
| 65 | /*! \name Conversion settings |
| 66 | |
| 67 | These parameters are used for combining money amounts |
| 68 | in different currencies |
| 69 | */ |
| 70 | //@{ |
| 71 | enum ConversionType { |
| 72 | NoConversion, /*!< do not perform conversions */ |
| 73 | BaseCurrencyConversion, /*!< convert both operands to |
| 74 | the base currency before |
| 75 | converting */ |
| 76 | AutomatedConversion /*!< return the result in the |
| 77 | currency of the first |
| 78 | operand */ |
| 79 | }; |
| 80 | // Money::Settings forward declaration |
| 81 | class Settings; |
| 82 | //@} |
| 83 | private: |
| 84 | Decimal value_ = 0.0; |
| 85 | Currency currency_; |
| 86 | |
| 87 | // temporary support for old syntax |
| 88 | struct BaseCurrencyProxy { |
| 89 | public: |
| 90 | BaseCurrencyProxy& operator=(const Currency&); |
| 91 | operator Currency() const; |
| 92 | }; |
| 93 | |
| 94 | struct ConversionTypeProxy { |
| 95 | public: |
| 96 | ConversionTypeProxy& operator=(Money::ConversionType); |
| 97 | operator Money::ConversionType() const; |
| 98 | }; |
| 99 | }; |
| 100 | |
| 101 | //! Per-session settings for the Money class |
| 102 | class Money::Settings : public Singleton<Money::Settings> { |
| 103 | friend class Singleton<Money::Settings>; |
| 104 | private: |
| 105 | Settings() = default; |
| 106 | |
| 107 | public: |
| 108 | const Money::ConversionType & conversionType() const; |
| 109 | Money::ConversionType & conversionType(); |
| 110 | |
| 111 | const Currency & baseCurrency() const; |
| 112 | Currency & baseCurrency(); |
| 113 | |
| 114 | private: |
| 115 | Money::ConversionType conversionType_ = Money::NoConversion; |
| 116 | Currency baseCurrency_; |
| 117 | }; |
| 118 | |
| 119 | // More arithmetics and comparisons |
| 120 | |
| 121 | /*! \relates Money */ |
| 122 | Money operator+(const Money&, const Money&); |
| 123 | /*! \relates Money */ |
| 124 | Money operator-(const Money&, const Money&); |
| 125 | /*! \relates Money */ |
| 126 | Money operator*(const Money&, Decimal); |
| 127 | /*! \relates Money */ |
| 128 | Money operator*(Decimal, const Money&); |
| 129 | /*! \relates Money */ |
| 130 | Money operator/(const Money&, Decimal); |
| 131 | /*! \relates Money */ |
| 132 | Decimal operator/(const Money&, const Money&); |
| 133 | |
| 134 | /*! \relates Money */ |
| 135 | bool operator==(const Money&, const Money&); |
| 136 | /*! \relates Money */ |
| 137 | bool operator!=(const Money&, const Money&); |
| 138 | /*! \relates Money */ |
| 139 | bool operator<(const Money&, const Money&); |
| 140 | /*! \relates Money */ |
| 141 | bool operator<=(const Money&, const Money&); |
| 142 | /*! \relates Money */ |
| 143 | bool operator>(const Money&, const Money&); |
| 144 | /*! \relates Money */ |
| 145 | bool operator>=(const Money&, const Money&); |
| 146 | |
| 147 | /*! \relates Money */ |
| 148 | bool close(const Money&, const Money&, Size n = 42); |
| 149 | /*! \relates Money */ |
| 150 | bool close_enough(const Money&, const Money&, Size n = 42); |
| 151 | |
| 152 | // syntactic sugar |
| 153 | |
| 154 | /*! \relates Money */ |
| 155 | Money operator*(Decimal, const Currency&); |
| 156 | /*! \relates Money */ |
| 157 | Money operator*(const Currency&, Decimal); |
| 158 | |
| 159 | // formatting |
| 160 | |
| 161 | /*! \relates Money */ |
| 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 |
| 256 | |