Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
analyticfactory.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2024 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
20
21#include <ql/errors.hpp>
22
23using namespace std;
24
25namespace ore {
26namespace analytics {
27
28std::map<std::string, std::pair<std::set<std::string>, QuantLib::ext::shared_ptr<AbstractAnalyticBuilder>>> AnalyticFactory::getBuilders() const {
29 boost::shared_lock<boost::shared_mutex> lock(mutex_);
30 return builders_;
31}
32
33std::pair<std::string,
34 QuantLib::ext::shared_ptr<AbstractAnalyticBuilder>> AnalyticFactory::getBuilder(const std::string& analyticName) const {
35 boost::shared_lock<boost::shared_mutex> lock(mutex_);
36 // check if matching main analytic
37 auto b = builders_.find(analyticName);
38 if (b != builders_.end())
39 return std::make_pair(b->first, b->second.second);
40
41 // then check subanalytics
42 for (const auto& ba : builders_) {
43 if (ba.second.first.size() > 0) {
44 auto sa = ba.second.first.find(analyticName);
45 if (sa != ba.second.first.end())
46 return std::make_pair(ba.first, ba.second.second);
47 }
48 }
49 WLOG("AnalyticFactory::getBuilder(" << analyticName << "): no builder found");
50 return std::make_pair(analyticName, nullptr);
51}
52
53void AnalyticFactory::addBuilder(const std::string& className, const std::set<std::string>& subAnalytics,
54 const QuantLib::ext::shared_ptr<AbstractAnalyticBuilder>& builder, const bool allowOverwrite) {
55 boost::unique_lock<boost::shared_mutex> lock(mutex_);
56 QL_REQUIRE(builders_.insert(std::make_pair(className, std::make_pair(subAnalytics, builder))).second ||
57 allowOverwrite,
58 "AnalyticFactory: duplicate builder for className '" << className << "'.");
59}
60
61std::pair<std::string, QuantLib::ext::shared_ptr<Analytic>> AnalyticFactory::build(const string& subAnalytic,
62 const QuantLib::ext::shared_ptr<ore::analytics::InputParameters>& inputs) const {
63 auto builder = getBuilder(subAnalytic);
64 QuantLib::ext::shared_ptr<Analytic> a;
65 if (builder.second)
66 a = builder.second->build(inputs);
67 return std::make_pair(builder.first, a);
68}
69
70} // namespace analytics
71} // namespace ore
std::pair< std::string, QuantLib::ext::shared_ptr< AbstractAnalyticBuilder > > getBuilder(const std::string &analyticType) const
std::map< std::string, std::pair< std::set< std::string >, QuantLib::ext::shared_ptr< AbstractAnalyticBuilder > > > getBuilders() const
void addBuilder(const std::string &className, const std::set< std::string > &subAnalytics, const QuantLib::ext::shared_ptr< AbstractAnalyticBuilder > &builder, const bool allowOverwrite=false)
std::map< std::string, std::pair< std::set< std::string >, QuantLib::ext::shared_ptr< AbstractAnalyticBuilder > > > builders_
std::pair< std::string, QuantLib::ext::shared_ptr< Analytic > > build(const std::string &subAnalytic, const QuantLib::ext::shared_ptr< ore::analytics::InputParameters > &inputs) const
Build, throws for unknown className.
#define WLOG(text)