/src/quantlib/ql/currencies/exchangeratemanager.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2004, 2005 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 exchangeratemanager.hpp |
22 | | \brief exchange-rate repository |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_exchange_rate_manager_hpp |
26 | | #define quantlib_exchange_rate_manager_hpp |
27 | | |
28 | | #include <ql/exchangerate.hpp> |
29 | | #include <ql/patterns/singleton.hpp> |
30 | | #include <ql/time/date.hpp> |
31 | | #include <list> |
32 | | #include <map> |
33 | | #include <utility> |
34 | | |
35 | | namespace QuantLib { |
36 | | |
37 | | //! exchange-rate repository |
38 | | /*! \test lookup of direct, triangulated, and derived exchange |
39 | | rates is tested. |
40 | | */ |
41 | | class ExchangeRateManager : public Singleton<ExchangeRateManager> { |
42 | | friend class Singleton<ExchangeRateManager>; |
43 | | private: |
44 | | ExchangeRateManager(); |
45 | | public: |
46 | | //! Add an exchange rate. |
47 | | /*! The given rate is valid between the given dates. |
48 | | |
49 | | \note If two rates are given between the same currencies |
50 | | and with overlapping date ranges, the latest one |
51 | | added takes precedence during lookup. |
52 | | */ |
53 | | void add(const ExchangeRate&, |
54 | | const Date& startDate = Date::minDate(), |
55 | | const Date& endDate = Date::maxDate()); |
56 | | /*! Lookup the exchange rate between two currencies at a given |
57 | | date. If the given type is Direct, only direct exchange |
58 | | rates will be returned if available; if Derived, direct |
59 | | rates are still preferred but derived rates are allowed. |
60 | | |
61 | | \warning if two or more exchange-rate chains are possible |
62 | | which allow to specify a requested rate, it is |
63 | | unspecified which one is returned. |
64 | | */ |
65 | | ExchangeRate lookup(const Currency& source, |
66 | | const Currency& target, |
67 | | Date date = Date(), |
68 | | ExchangeRate::Type type = |
69 | | ExchangeRate::Derived) const; |
70 | | //! remove the added exchange rates |
71 | | void clear(); |
72 | | |
73 | | struct Entry { |
74 | | Entry() = default; |
75 | | Entry(ExchangeRate rate, const Date& start, const Date& end) |
76 | 0 | : rate(std::move(rate)), startDate(start), endDate(end) {} |
77 | | ExchangeRate rate; |
78 | | Date startDate, endDate; |
79 | | }; |
80 | | private: |
81 | | typedef BigInteger Key; |
82 | | mutable std::map<Key, std::list<Entry> > data_; |
83 | | Key hash(const Currency&, const Currency&) const; |
84 | | bool hashes(Key, const Currency&) const; |
85 | | void addKnownRates(); |
86 | | ExchangeRate directLookup(const Currency& source, |
87 | | const Currency& target, |
88 | | const Date& date) const; |
89 | | ExchangeRate smartLookup(const Currency& source, |
90 | | const Currency& target, |
91 | | const Date& date, |
92 | | std::list<Integer> forbiddenCodes |
93 | | = std::list<Integer>()) const; |
94 | | const ExchangeRate* fetch(const Currency& source, |
95 | | const Currency& target, |
96 | | const Date& date) const; |
97 | | }; |
98 | | |
99 | | } |
100 | | |
101 | | |
102 | | #endif |