Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/commodities/commodity.cpp
Line
Count
Source
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
 <https://www.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
0
    : secondaryCosts_(std::move(secondaryCosts)) {}
28
29
0
    const SecondaryCostAmounts& Commodity::secondaryCostAmounts() const {
30
0
        return secondaryCostAmounts_;
31
0
    }
32
33
0
    const PricingErrors& Commodity::pricingErrors() const {
34
0
        return pricingErrors_;
35
0
    }
36
37
    void Commodity::addPricingError(PricingError::Level errorLevel,
38
                                    const std::string& error,
39
0
                                    const std::string& detail) const {
40
0
        pricingErrors_.emplace_back(errorLevel, error, detail);
41
0
    }
42
43
44
    std::ostream& operator<<(std::ostream& out,
45
0
                             const SecondaryCostAmounts& secondaryCostAmounts) {
46
0
        std::string currencyCode;
47
0
        Real totalAmount = 0;
48
49
0
        out << "secondary costs" << std::endl;
50
0
        for (const auto& secondaryCostAmount : secondaryCostAmounts) {
51
0
            Real amount = secondaryCostAmount.second.value();
52
0
            if (currencyCode.empty())
53
0
                currencyCode = secondaryCostAmount.second.currency().code();
54
0
            totalAmount += amount;
55
0
            out << std::setw(28) << std::left << secondaryCostAmount.first << std::setw(12)
56
0
                << std::right << std::fixed << std::setprecision(2) << amount << " " << currencyCode
57
0
                << std::endl;
58
0
        }
59
0
        out << std::setw(28) << std::left << "total"
60
0
            << std::setw(12) << std::right << std::fixed
61
0
            << std::setprecision(2) << totalAmount << " " << currencyCode
62
0
            << std::endl;
63
0
        return out;
64
0
    }
65
66
67
0
    std::ostream& operator<<(std::ostream& out, const PricingError& error) {
68
0
        switch (error.errorLevel) {
69
0
          case PricingError::Info:
70
0
            out << "info: ";
71
0
            break;
72
0
          case PricingError::Warning:
73
0
            out << "warning: ";
74
0
            break;
75
0
          case PricingError::Error:
76
0
            out << "*** error: ";
77
0
            break;
78
0
          case PricingError::Fatal:
79
0
            out << "*** fatal: ";
80
0
            break;
81
0
        }
82
0
        out << error.error;
83
0
        if (!error.detail.empty())
84
0
            out << ": " << error.detail;
85
0
        return out;
86
0
    }
87
88
0
    std::ostream& operator<<(std::ostream& out, const PricingErrors& errors) {
89
0
        if (!errors.empty()) {
90
0
            out << "*** pricing errors" << std::endl;
91
0
            for (const auto& error : errors)
92
0
                out << error << std::endl;
93
0
        }
94
0
        return out;
95
0
    }
96
97
}
98