| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 J. Erik Radmall |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include <ql/experimental/commodities/commodity.hpp> |
| 21 | #include <iomanip> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | Commodity::Commodity(ext::shared_ptr<SecondaryCosts> secondaryCosts) |
| 27 | : secondaryCosts_(std::move(secondaryCosts)) {} |
| 28 | |
| 29 | const SecondaryCostAmounts& Commodity::secondaryCostAmounts() const { |
| 30 | return secondaryCostAmounts_; |
| 31 | } |
| 32 | |
| 33 | const PricingErrors& Commodity::pricingErrors() const { |
| 34 | return pricingErrors_; |
| 35 | } |
| 36 | |
| 37 | void Commodity::addPricingError(PricingError::Level errorLevel, |
| 38 | const std::string& error, |
| 39 | const std::string& detail) const { |
| 40 | pricingErrors_.emplace_back(args&: errorLevel, args: error, args: detail); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | std::ostream& operator<<(std::ostream& out, |
| 45 | const SecondaryCostAmounts& secondaryCostAmounts) { |
| 46 | std::string currencyCode; |
| 47 | Real totalAmount = 0; |
| 48 | |
| 49 | out << "secondary costs" << std::endl; |
| 50 | for (const auto& secondaryCostAmount : secondaryCostAmounts) { |
| 51 | Real amount = secondaryCostAmount.second.value(); |
| 52 | if (currencyCode.empty()) |
| 53 | currencyCode = secondaryCostAmount.second.currency().code(); |
| 54 | totalAmount += amount; |
| 55 | out << std::setw(28) << std::left << secondaryCostAmount.first << std::setw(12) |
| 56 | << std::right << std::fixed << std::setprecision(2) << amount << " " << currencyCode |
| 57 | << std::endl; |
| 58 | } |
| 59 | out << std::setw(28) << std::left << "total" |
| 60 | << std::setw(12) << std::right << std::fixed |
| 61 | << std::setprecision(2) << totalAmount << " " << currencyCode |
| 62 | << std::endl; |
| 63 | return out; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | std::ostream& operator<<(std::ostream& out, const PricingError& error) { |
| 68 | switch (error.errorLevel) { |
| 69 | case PricingError::Info: |
| 70 | out << "info: " ; |
| 71 | break; |
| 72 | case PricingError::Warning: |
| 73 | out << "warning: " ; |
| 74 | break; |
| 75 | case PricingError::Error: |
| 76 | out << "*** error: " ; |
| 77 | break; |
| 78 | case PricingError::Fatal: |
| 79 | out << "*** fatal: " ; |
| 80 | break; |
| 81 | } |
| 82 | out << error.error; |
| 83 | if (!error.detail.empty()) |
| 84 | out << ": " << error.detail; |
| 85 | return out; |
| 86 | } |
| 87 | |
| 88 | std::ostream& operator<<(std::ostream& out, const PricingErrors& errors) { |
| 89 | if (!errors.empty()) { |
| 90 | out << "*** pricing errors" << std::endl; |
| 91 | for (const auto& error : errors) |
| 92 | out << error << std::endl; |
| 93 | } |
| 94 | return out; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | |