Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/currency.hpp
Line
Count
Source
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, 2007 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
 <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
/*! \file currency.hpp
22
    \brief Currency specification
23
*/
24
25
#ifndef quantlib_currency_hpp
26
#define quantlib_currency_hpp
27
28
#include <ql/math/rounding.hpp>
29
#include <ql/errors.hpp>
30
#include <iosfwd>
31
#include <set>
32
33
namespace QuantLib {
34
35
    //! %Currency specification
36
    class Currency {
37
      public:
38
        //! \name Constructors
39
        //@{
40
        //! default constructor
41
        /*! Instances built via this constructor have undefined
42
            behavior. Such instances can only act as placeholders
43
            and must be reassigned to a valid currency before being
44
            used.
45
        */
46
0
        Currency() = default;
47
        Currency(const std::string& name,
48
                 const std::string& code,
49
                 Integer numericCode,
50
                 const std::string& symbol,
51
                 const std::string& fractionSymbol,
52
                 Integer fractionsPerUnit,
53
                 const Rounding& rounding,
54
                 const Currency& triangulationCurrency = Currency(),
55
                 const std::set<std::string>& minorUnitCodes = {});
56
        //@}
57
        //! \name Inspectors
58
        //@{
59
        //! currency name, e.g, "U.S. Dollar"
60
        const std::string& name() const;
61
        //! ISO 4217 three-letter code, e.g, "USD"
62
        const std::string& code() const;
63
        //! ISO 4217 numeric code, e.g, "840"
64
        Integer numericCode() const;
65
        //! symbol, e.g, "$"
66
        const std::string& symbol() const;
67
        //! fraction symbol, e.g, "ยข"
68
        const std::string& fractionSymbol() const;
69
        //! number of fractionary parts in a unit, e.g, 100
70
        Integer fractionsPerUnit() const;
71
        //! rounding convention
72
        const Rounding& rounding() const;
73
        //@}
74
        //! \name Other information
75
        //@{
76
        //! is this a usable instance?
77
        bool empty() const;
78
        //! currency used for triangulated exchange when required
79
        const Currency& triangulationCurrency() const;
80
        //! minor unit codes, e.g. GBp, GBX for GBP
81
        const std::set<std::string>& minorUnitCodes() const;
82
        //@}
83
      protected:
84
        struct Data;
85
        ext::shared_ptr<Data> data_;
86
     private:
87
        void checkNonEmpty() const;
88
    };
89
90
    struct Currency::Data {
91
        std::string name, code;
92
        Integer numeric;
93
        std::string symbol, fractionSymbol;
94
        Integer fractionsPerUnit;
95
        Rounding rounding;
96
        Currency triangulated;
97
        std::set<std::string> minorUnitCodes;
98
99
        Data(std::string name,
100
             std::string code,
101
             Integer numericCode,
102
             std::string symbol,
103
             std::string fractionSymbol,
104
             Integer fractionsPerUnit,
105
             const Rounding& rounding,
106
             Currency triangulationCurrency = Currency(),
107
             std::set<std::string> minorUnitCodes = {});
108
    };
109
110
111
    /*! \relates Currency */
112
    bool operator==(const Currency&,
113
                    const Currency&);
114
115
    /*! \relates Currency */
116
    bool operator!=(const Currency&,
117
                    const Currency&);
118
119
    /*! \relates Currency */
120
    std::ostream& operator<<(std::ostream&,
121
                             const Currency&);
122
123
124
    // inline definitions
125
126
0
    inline void Currency::checkNonEmpty() const {
127
0
        QL_REQUIRE(data_, "no currency data provided");
128
0
    }
129
130
0
    inline const std::string& Currency::name() const {
131
0
        checkNonEmpty();
132
0
        return data_->name;
133
0
    }
134
135
0
    inline const std::string& Currency::code() const {
136
0
        checkNonEmpty();
137
0
        return data_->code;
138
0
    }
139
140
0
    inline Integer Currency::numericCode() const {
141
0
        checkNonEmpty();
142
0
        return data_->numeric;
143
0
    }
144
145
0
    inline const std::string& Currency::symbol() const {
146
0
        checkNonEmpty();
147
0
        return data_->symbol;
148
0
    }
149
150
0
    inline const std::string& Currency::fractionSymbol() const {
151
0
        checkNonEmpty();
152
0
        return data_->fractionSymbol;
153
0
    }
154
155
0
    inline Integer Currency::fractionsPerUnit() const {
156
0
        checkNonEmpty();
157
0
        return data_->fractionsPerUnit;
158
0
    }
159
160
0
    inline const Rounding& Currency::rounding() const {
161
0
        checkNonEmpty();
162
0
        return data_->rounding;
163
0
    }
164
165
0
    inline bool Currency::empty() const {
166
0
        return !data_;
167
0
    }
168
169
0
    inline const Currency& Currency::triangulationCurrency() const {
170
0
        checkNonEmpty();
171
0
        return data_->triangulated;
172
0
    }
173
174
0
    inline const std::set<std::string>& Currency::minorUnitCodes() const {
175
0
        checkNonEmpty();
176
0
        return data_->minorUnitCodes;
177
0
    }
178
179
0
    inline bool operator==(const Currency& c1, const Currency& c2) {
180
0
        return (c1.empty() && c2.empty()) ||
181
0
               (!c1.empty() && !c2.empty() && c1.name() == c2.name());
182
0
    }
183
184
0
    inline bool operator!=(const Currency& c1, const Currency& c2) {
185
0
        return !(c1 == c2);
186
0
    }
187
188
}
189
190
191
#endif