Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cachingenginebuilder.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/portfolio/builders/cachingenginebuilder.hpp
20 \brief Abstract template engine builder class
21 \ingroup builders
22*/
23
24#pragma once
25
27
28#include <ql/cashflows/couponpricer.hpp>
29#include <ql/cashflows/inflationcouponpricer.hpp>
31
32namespace ore {
33namespace data {
34
35//! Abstract template EngineBuilder class that can cache engines and coupon pricers
36/*! Subclasses must implement two protected methods:
37 * - keyImpl() returns a key that is used to cache
38 * - engineImpl() return a new pricing engine or coupon pricer.
39 * When the engine() method is called the CachingEngineBuilder first
40 * looks in it's cache to see if it has an engine or coupon pricer for this key already
41 * if so it is returned, otherwise a new engine or coupon pricer is created, stored and
42 * returned.
43 *
44 * The first template argument is the cache key type (e.g. a std::string)
45 * The second template argument is PricingEngine or FloatingRateCouponPricer
46 * The remaining variable arguments are to be passed to engine() and
47 * engineImpl(), these are the specific parameters required to build
48 * an engine or coupon pricer for this trade type.
49 \ingroup builders
50 */
51template <class T, class U, typename... Args> class CachingEngineBuilder : public EngineBuilder {
52public:
53 /*! Constructor that takes a model and engine name
54 @param model the model name
55 @param engine the engine name
56 @param tradeTypes a set of trade types
57 */
58 CachingEngineBuilder(const string& model, const string& engine, const set<string>& tradeTypes)
60
61 //! Return a PricingEngine or a FloatingRateCouponPricer
62 QuantLib::ext::shared_ptr<U> engine(Args... params) {
63 T key = keyImpl(params...);
64 if (engines_.find(key) == engines_.end()) {
65 // build first (in case it throws)
66 QuantLib::ext::shared_ptr<U> engine = engineImpl(params...);
67 // then add to map
68 engines_[key] = engine;
69 }
70 return engines_[key];
71 }
72
73 void reset() override { engines_.clear(); }
74
75protected:
76 virtual T keyImpl(Args...) = 0;
77 virtual QuantLib::ext::shared_ptr<U> engineImpl(Args...) = 0;
78
79 map<T, QuantLib::ext::shared_ptr<U>> engines_;
80};
81
82template <class T, typename... Args>
83using CachingPricingEngineBuilder = CachingEngineBuilder<T, PricingEngine, Args...>;
84
85template <class T, typename... Args>
87
88template <class T, typename... Args>
89using CachingInflationCouponPricerBuilder = CachingEngineBuilder<T, InflationCouponPricer, Args...>;
90
91template <class T, typename... Args>
93
94} // namespace data
95} // namespace ore
Abstract template EngineBuilder class that can cache engines and coupon pricers.
CachingEngineBuilder(const string &model, const string &engine, const set< string > &tradeTypes)
virtual QuantLib::ext::shared_ptr< U > engineImpl(Args...)=0
map< T, QuantLib::ext::shared_ptr< U > > engines_
virtual T keyImpl(Args...)=0
void reset() override
reset the builder (e.g. clear cache)
QuantLib::ext::shared_ptr< U > engine(Args... params)
Return a PricingEngine or a FloatingRateCouponPricer.
Base PricingEngine Builder class for a specific model and engine.
const string & engine() const
Return the engine name.
const set< string > & tradeTypes() const
Return the possible trade types.
const string & model() const
Return the model name.
Pricing Engine Factory.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23