Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
inmemoryreport.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/report/inmemoryreport.hpp
20 \brief In memory report class
21 \ingroup report
22*/
23
24#pragma once
25
28#include <ql/errors.hpp>
29#include <ql/tuple.hpp>
30#include <vector>
31
32namespace ore {
33namespace data {
34using std::string;
35using std::vector;
36
37/*! InMemoryReport just stores report information in local vectors and provides an interface to access
38 * the values. It could be used as a backend to a GUI
39 \ingroup report
40 */
41class InMemoryReport : public Report {
42public:
44
45 Report& addColumn(const string& name, const ReportType& rt, Size precision = 0) override;
46 Report& next() override;
47 Report& add(const ReportType& rt) override;
48 Report& add(const InMemoryReport& report);
49 void end() override;
50
51 // InMemoryInterface
52 Size columns() const { return headers_.size(); }
53 Size rows() const { return columns() == 0 ? 0 : data_[0].size(); }
54 const string& header(Size i) const { return headers_[i]; }
55 bool hasHeader(string h) const { return std::find(headers_.begin(), headers_.end(), h) != headers_.end(); }
56 ReportType columnType(Size i) const { return columnTypes_[i]; }
57 Size columnPrecision(Size i) const { return columnPrecision_[i]; }
58 //! Returns the data
59 const vector<ReportType>& data(Size i) const;
60 void toFile(const string& filename, const char sep = ',', const bool commentCharacter = true, char quoteChar = '\0',
61 const string& nullString = "#N/A", bool lowerHeader = false);
62 void jumpToColumn(Size i) { i_ = i; }
63
64private:
65 Size i_;
66 vector<string> headers_;
67 vector<ReportType> columnTypes_;
68 vector<Size> columnPrecision_;
69 vector<vector<ReportType>> data_;
70};
71
72//! InMemoryReport with access to plain types instead of boost::variant<>, to facilitate language bindings
74public:
75 PlainInMemoryReport(const QuantLib::ext::shared_ptr<InMemoryReport>& imReport)
76 : imReport_(imReport) {}
78 Size columns() const { return imReport_->columns(); }
79 std::string header(Size i) const { return imReport_->header(i); }
80 // returns: 0 Size, 1 Real, 2 string, 3 Date, 4 Period
81 Size columnType(Size i) const { return imReport_->columnType(i).which(); }
82 vector<int> dataAsSize(Size i) const { return sizeToInt(data_T<Size>(i, 0)); }
83 vector<Real> dataAsReal(Size i) const { return data_T<Real>(i, 1); }
84 vector<string> dataAsString(Size i) const { return data_T<string>(i, 2); }
85 vector<Date> dataAsDate(Size i) const { return data_T<Date>(i, 3); }
86 vector<Period> dataAsPeriod(Size i) const { return data_T<Period>(i, 4); }
87 // for convenience, access by row j and column i
88 Size rows() const { return columns() == 0 ? 0 : imReport_->data(0).size(); }
89 int dataAsSize(Size j, Size i) const { return int(boost::get<Size>(imReport_->data(i).at(j))); }
90 Real dataAsReal(Size j, Size i) const { return boost::get<Real>(imReport_->data(i).at(j)); }
91 string dataAsString(Size j, Size i) const { return boost::get<string>(imReport_->data(i).at(j)); }
92 Date dataAsDate(Size j, Size i) const { return boost::get<Date>(imReport_->data(i).at(j)); }
93 Period dataAsPeriod(Size j, Size i) const { return boost::get<Period>(imReport_->data(i).at(j)); }
94
95private:
96 template <typename T> vector<T> data_T(Size i, Size w) const {
97 QL_REQUIRE(columnType(i) == w,
98 "PlainTypeInMemoryReport::data_T(column=" << i << ",expectedType=" << w
99 << "): Type mismatch, have " << columnType(i));
100 vector<T> tmp;
101 for(auto const& d: imReport_->data(i))
102 tmp.push_back(boost::get<T>(d));
103 return tmp;
104 }
105 vector<int> sizeToInt(const vector<Size>& v) const {
106 std::vector<int> vi;
107 for (auto s : v)
108 vi.push_back(int(s));
109 return vi;
110 }
111 QuantLib::ext::shared_ptr<InMemoryReport> imReport_;
112};
113
114} // namespace data
115} // namespace ore
vector< Size > columnPrecision_
Report & add(const ReportType &rt) override
vector< ReportType > columnTypes_
const string & header(Size i) const
vector< vector< ReportType > > data_
ReportType columnType(Size i) const
Report & next() override
Size columnPrecision(Size i) const
Report & addColumn(const string &name, const ReportType &rt, Size precision=0) override
void toFile(const string &filename, const char sep=',', const bool commentCharacter=true, char quoteChar='\0', const string &nullString="#N/A", bool lowerHeader=false)
bool hasHeader(string h) const
InMemoryReport with access to plain types instead of boost::variant<>, to facilitate language binding...
string dataAsString(Size j, Size i) const
vector< T > data_T(Size i, Size w) const
Period dataAsPeriod(Size j, Size i) const
vector< Real > dataAsReal(Size i) const
vector< Date > dataAsDate(Size i) const
vector< int > sizeToInt(const vector< Size > &v) const
vector< int > dataAsSize(Size i) const
vector< string > dataAsString(Size i) const
PlainInMemoryReport(const QuantLib::ext::shared_ptr< InMemoryReport > &imReport)
int dataAsSize(Size j, Size i) const
std::string header(Size i) const
Date dataAsDate(Size j, Size i) const
vector< Period > dataAsPeriod(Size i) const
QuantLib::ext::shared_ptr< InMemoryReport > imReport_
Real dataAsReal(Size j, Size i) const
boost::variant< Size, Real, string, Date, Period > ReportType
Definition: report.hpp:66
CSV Report class.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Report interface class.
string name