29 void convertTo(Money& m,
const Currency& target) {
30 if (m.currency() != target) {
34 m =
rate.exchange(m).rounded();
38 void convertToBase(Money& m) {
39 const auto & base_currency =
41 QL_REQUIRE(!base_currency.empty(),
"no base currency set");
42 convertTo(m, base_currency);
45 template<
typename ReturnValue,
typename Function >
46 ReturnValue
apply(
const Money& m1,
const Money& m2,
const Function
f) {
47 const auto & conversion_type =
49 if (m1.currency() == m2.currency()) {
50 return f(m1.value(), m2.value());
56 return f(tmp1.value(), tmp2.value());
59 convertTo(tmp, m1.currency());
60 return f(m1.value(), tmp.value());
62 QL_FAIL(
"currency mismatch and no conversion specified");
81 QL_FAIL(
"currency mismatch and no conversion specified");
91 return apply<Decimal>(
93 [](
const Real x,
const Real y) {
return x /
y; });
99 [](
const Real x,
const Real y) {
return x ==
y; });
105 [](
const Real x,
const Real y) {
return x <
y; });
111 [](
const Real x,
const Real y) {
return x <=
y; });
137 return conversionType_;
142 return baseCurrency_;
147 return baseCurrency_;
155 Money::BaseCurrencyProxy::operator
Currency()
const {
const std::string & code() const
ISO 4217 three-letter code, e.g, "USD".
ExchangeRate lookup(const Currency &source, const Currency &target, Date date=Date(), ExchangeRate::Type type=ExchangeRate::Derived) const
const Money::ConversionType & conversionType() const
Money::ConversionType conversionType_
const Currency & baseCurrency() const
Money & operator+=(const Money &)
const Currency & currency() const
Money & operator-=(const Money &)
static ExchangeRateManager & instance()
access to the unique instance
floating-point comparisons
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
detail::percent_holder rate(Rate)
output rates and spreads as percentages
Real Decimal
decimal number
std::size_t Size
size of a container
cash amount in a given currency
Array apply(const Array &x, const ext::function< Real(Real)> &f)
bool operator==(const Currency &c1, const Currency &c2)
bool operator<(const Quantity &m1, const Quantity &m2)
std::ostream & operator<<(std::ostream &out, GFunctionFactory::YieldCurveModel type)
bool close(const Quantity &m1, const Quantity &m2, Size n)
bool operator<=(const Quantity &m1, const Quantity &m2)
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Real operator/(const Quantity &m1, const Quantity &m2)
BaseCurrencyProxy & operator=(const Currency &)
ConversionTypeProxy & operator=(Money::ConversionType)