| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2004 StatPro Italia srl |
| 5 | Copyright (C) 2004 Decillion Pty(Ltd) |
| 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 exchangerate.hpp |
| 22 | \brief exchange rate between two currencies |
| 23 | */ |
| 24 | |
| 25 | #ifndef quantlib_exchange_rate_hpp |
| 26 | #define quantlib_exchange_rate_hpp |
| 27 | |
| 28 | #include <ql/money.hpp> |
| 29 | #include <ql/utilities/null.hpp> |
| 30 | #include <utility> |
| 31 | |
| 32 | namespace QuantLib { |
| 33 | |
| 34 | //! exchange rate between two currencies |
| 35 | /*! \test application of direct and derived exchange rate is |
| 36 | tested against calculations. |
| 37 | */ |
| 38 | class ExchangeRate { |
| 39 | public: |
| 40 | enum Type { Direct, /*!< given directly by the user */ |
| 41 | Derived /*!< derived from exchange rates between |
| 42 | other currencies */ |
| 43 | }; |
| 44 | //! \name Constructors |
| 45 | //@{ |
| 46 | ExchangeRate(); |
| 47 | /*! the rate \f$ r \f$ is given with the convention that a |
| 48 | unit of the source is worth \f$ r \f$ units of the target. |
| 49 | */ |
| 50 | ExchangeRate(Currency source, Currency target, Decimal rate); |
| 51 | //@} |
| 52 | |
| 53 | //! \name Inspectors |
| 54 | //@{ |
| 55 | //! the source currency. |
| 56 | const Currency& source() const; |
| 57 | //! the target currency. |
| 58 | const Currency& target() const; |
| 59 | //! the type |
| 60 | Type type() const; |
| 61 | //! the exchange rate (when available) |
| 62 | Decimal rate() const; |
| 63 | //@} |
| 64 | |
| 65 | //! \name Utility methods |
| 66 | //@{ |
| 67 | //! apply the exchange rate to a cash amount |
| 68 | Money exchange(const Money& amount) const; |
| 69 | //! chain two exchange rates |
| 70 | static ExchangeRate chain(const ExchangeRate& r1, |
| 71 | const ExchangeRate& r2); |
| 72 | //@} |
| 73 | private: |
| 74 | Currency source_, target_; |
| 75 | Decimal rate_; |
| 76 | Type type_; |
| 77 | std::pair<ext::shared_ptr<ExchangeRate>, |
| 78 | ext::shared_ptr<ExchangeRate> > rateChain_; |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | // inline definitions |
| 83 | |
| 84 | inline ExchangeRate::ExchangeRate() : rate_(Null<Decimal>()), type_(Direct) {} |
| 85 | |
| 86 | inline ExchangeRate::ExchangeRate(Currency source, Currency target, Decimal rate) |
| 87 | : source_(std::move(source)), target_(std::move(target)), rate_(rate), type_(Direct) {} |
| 88 | |
| 89 | inline const Currency& ExchangeRate::source() const { |
| 90 | return source_; |
| 91 | } |
| 92 | |
| 93 | inline const Currency& ExchangeRate::target() const { |
| 94 | return target_; |
| 95 | } |
| 96 | |
| 97 | inline ExchangeRate::Type ExchangeRate::type() const { |
| 98 | return type_; |
| 99 | } |
| 100 | |
| 101 | inline Decimal ExchangeRate::rate() const { |
| 102 | return rate_; |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | #endif |
| 109 | |