Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/exchangerate.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2004, 2008 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
 <https://www.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
#include <ql/exchangerate.hpp>
22
23
namespace QuantLib {
24
25
0
    Money ExchangeRate::exchange(const Money& amount) const {
26
0
        switch (type_) {
27
0
          case Direct:
28
0
            if (amount.currency() == source_)
29
0
                return Money(amount.value()*rate_, target_);
30
0
            else if (amount.currency() == target_)
31
0
                return Money(amount.value()/rate_, source_);
32
0
            else
33
0
                QL_FAIL("exchange rate not applicable");
34
0
          case Derived:
35
0
            if (amount.currency() == rateChain_.first->source() ||
36
0
                amount.currency() == rateChain_.first->target())
37
0
                return rateChain_.second->exchange(
38
0
                                         rateChain_.first->exchange(amount));
39
0
            else if (amount.currency() == rateChain_.second->source() ||
40
0
                       amount.currency() == rateChain_.second->target())
41
0
                return rateChain_.first->exchange(
42
0
                                         rateChain_.second->exchange(amount));
43
0
            else
44
0
                QL_FAIL("exchange rate not applicable");
45
0
          default:
46
0
            QL_FAIL("unknown exchange-rate type");
47
0
        }
48
0
    }
49
50
    ExchangeRate ExchangeRate::chain(const ExchangeRate& r1,
51
0
                                     const ExchangeRate& r2) {
52
0
        ExchangeRate result;
53
0
        result.type_ = Derived;
54
0
        result.rateChain_ = std::make_pair(
55
0
                       ext::make_shared<ExchangeRate>(r1),
56
0
                       ext::make_shared<ExchangeRate>(r2));
57
0
        if (r1.source_ == r2.source_) {
58
0
            result.source_ = r1.target_;
59
0
            result.target_ = r2.target_;
60
0
            result.rate_ = r2.rate_/r1.rate_;
61
0
        } else if (r1.source_ == r2.target_) {
62
0
            result.source_ = r1.target_;
63
0
            result.target_ = r2.source_;
64
0
            result.rate_ = 1.0/(r1.rate_*r2.rate_);
65
0
        } else if (r1.target_ == r2.source_) {
66
0
            result.source_ = r1.source_;
67
0
            result.target_ = r2.target_;
68
0
            result.rate_ = r1.rate_*r2.rate_;
69
0
        } else if (r1.target_ == r2.target_) {
70
0
            result.source_ = r1.source_;
71
0
            result.target_ = r2.source_;
72
0
            result.rate_ = r1.rate_/r2.rate_;
73
0
        } else {
74
0
            QL_FAIL("exchange rates not chainable");
75
0
        }
76
0
        return result;
77
0
    }
78
79
}