| 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 | <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 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 | 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 std::string& formatString, |
| 55 | const Currency& triangulationCurrency = Currency(), |
| 56 | const std::set<std::string>& minorUnitCodes = {}); |
| 57 | //@} |
| 58 | //! \name Inspectors |
| 59 | //@{ |
| 60 | //! currency name, e.g, "U.S. Dollar" |
| 61 | const std::string& name() const; |
| 62 | //! ISO 4217 three-letter code, e.g, "USD" |
| 63 | const std::string& code() const; |
| 64 | //! ISO 4217 numeric code, e.g, "840" |
| 65 | Integer numericCode() const; |
| 66 | //! symbol, e.g, "$" |
| 67 | const std::string& symbol() const; |
| 68 | //! fraction symbol, e.g, "ยข" |
| 69 | const std::string& fractionSymbol() const; |
| 70 | //! number of fractionary parts in a unit, e.g, 100 |
| 71 | Integer fractionsPerUnit() const; |
| 72 | //! rounding convention |
| 73 | const Rounding& rounding() const; |
| 74 | //! output format |
| 75 | /*! The format will be fed three positional parameters, |
| 76 | namely, value, code, and symbol, in this order. |
| 77 | */ |
| 78 | std::string format() const; |
| 79 | //@} |
| 80 | //! \name Other information |
| 81 | //@{ |
| 82 | //! is this a usable instance? |
| 83 | bool empty() const; |
| 84 | //! currency used for triangulated exchange when required |
| 85 | const Currency& triangulationCurrency() const; |
| 86 | //! minor unit codes, e.g. GBp, GBX for GBP |
| 87 | const std::set<std::string>& minorUnitCodes() const; |
| 88 | //@} |
| 89 | protected: |
| 90 | struct Data; |
| 91 | ext::shared_ptr<Data> data_; |
| 92 | private: |
| 93 | void checkNonEmpty() const; |
| 94 | }; |
| 95 | |
| 96 | struct Currency::Data { |
| 97 | std::string name, code; |
| 98 | Integer numeric; |
| 99 | std::string symbol, fractionSymbol; |
| 100 | Integer fractionsPerUnit; |
| 101 | Rounding rounding; |
| 102 | Currency triangulated; |
| 103 | std::string formatString; |
| 104 | std::set<std::string> minorUnitCodes; |
| 105 | |
| 106 | Data(std::string name, |
| 107 | std::string code, |
| 108 | Integer numericCode, |
| 109 | std::string symbol, |
| 110 | std::string fractionSymbol, |
| 111 | Integer fractionsPerUnit, |
| 112 | const Rounding& rounding, |
| 113 | std::string formatString, |
| 114 | Currency triangulationCurrency = Currency(), |
| 115 | std::set<std::string> minorUnitCodes = {}); |
| 116 | }; |
| 117 | |
| 118 | /*! \relates Currency */ |
| 119 | bool operator==(const Currency&, |
| 120 | const Currency&); |
| 121 | |
| 122 | /*! \relates Currency */ |
| 123 | bool operator!=(const Currency&, |
| 124 | const Currency&); |
| 125 | |
| 126 | /*! \relates Currency */ |
| 127 | std::ostream& operator<<(std::ostream&, |
| 128 | const Currency&); |
| 129 | |
| 130 | |
| 131 | // inline definitions |
| 132 | |
| 133 | inline void Currency::checkNonEmpty() const { |
| 134 | QL_REQUIRE(data_, "no currency data provided" ); |
| 135 | } |
| 136 | |
| 137 | inline const std::string& Currency::name() const { |
| 138 | checkNonEmpty(); |
| 139 | return data_->name; |
| 140 | } |
| 141 | |
| 142 | inline const std::string& Currency::code() const { |
| 143 | checkNonEmpty(); |
| 144 | return data_->code; |
| 145 | } |
| 146 | |
| 147 | inline Integer Currency::numericCode() const { |
| 148 | checkNonEmpty(); |
| 149 | return data_->numeric; |
| 150 | } |
| 151 | |
| 152 | inline const std::string& Currency::symbol() const { |
| 153 | checkNonEmpty(); |
| 154 | return data_->symbol; |
| 155 | } |
| 156 | |
| 157 | inline const std::string& Currency::fractionSymbol() const { |
| 158 | checkNonEmpty(); |
| 159 | return data_->fractionSymbol; |
| 160 | } |
| 161 | |
| 162 | inline Integer Currency::fractionsPerUnit() const { |
| 163 | checkNonEmpty(); |
| 164 | return data_->fractionsPerUnit; |
| 165 | } |
| 166 | |
| 167 | inline const Rounding& Currency::rounding() const { |
| 168 | checkNonEmpty(); |
| 169 | return data_->rounding; |
| 170 | } |
| 171 | |
| 172 | inline std::string Currency::format() const { |
| 173 | checkNonEmpty(); |
| 174 | return data_->formatString; |
| 175 | } |
| 176 | |
| 177 | inline bool Currency::empty() const { |
| 178 | return !data_; |
| 179 | } |
| 180 | |
| 181 | inline const Currency& Currency::triangulationCurrency() const { |
| 182 | checkNonEmpty(); |
| 183 | return data_->triangulated; |
| 184 | } |
| 185 | |
| 186 | inline const std::set<std::string>& Currency::minorUnitCodes() const { |
| 187 | checkNonEmpty(); |
| 188 | return data_->minorUnitCodes; |
| 189 | } |
| 190 | |
| 191 | inline bool operator==(const Currency& c1, const Currency& c2) { |
| 192 | return (c1.empty() && c2.empty()) || |
| 193 | (!c1.empty() && !c2.empty() && c1.name() == c2.name()); |
| 194 | } |
| 195 | |
| 196 | inline bool operator!=(const Currency& c1, const Currency& c2) { |
| 197 | return !(c1 == c2); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | #endif |
| 204 | |