Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
calibrationinstrumentfactory.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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/model/calibrationinstrumentfactory.hpp
20 \brief factory for making calibration instruments.
21
22 The idea here is taken from: https://stackoverflow.com/a/582456/1771882 and is used in
23 \c ore::data::LegDataFactory also.
24
25 \ingroup models
26*/
27
28#pragma once
29
30#include <boost/make_shared.hpp>
31#include <functional>
32#include <map>
35#include <ql/patterns/singleton.hpp>
36
37namespace ore {
38namespace data {
39
40/*! Function that is used to build instances of CalibrationInstrument
41
42 The template parameter is simply a particular instance of a \c CalibrationInstrument class that is default
43 constructible. The function returns the default constructed CalibrationInstrument object. A simple example is the
44 function to build an instance of \c CpiCapFloor would be called via \c createLegData<CpiCapFloor>().
45
46 \ingroup models
47*/
48template <class T>
49QuantLib::ext::shared_ptr<CalibrationInstrument> createCalibrationInstrument() { return QuantLib::ext::make_shared<T>(); }
50
51/*! Calibration instrument factory class
52
53 This class is a repository of functions that can build instances of \c CalibrationInstrument. The functions are
54 keyed on the calibration instrument type that they can build. An instance of this factory class can be asked to
55 build a particular instance of the CalibrationInstrument class via a call to
56 <code>build(const std::string& instrumentType)</code> with the correct calibration instrument type,
57 \c instrumentType. For example, a call to <code>build("CpiCapFloor")</code> should return a \c CpiCapFloor
58 instance if the CpiCapFloor calibration instrument building function has been added to the factory.
59
60 It is up to each class derived from \c CalibrationInstrument to register itself with the
61 \c CalibrationInstrumentFactory via the \c CalibrationInstrumentRegister class below. All registration does is add
62 a function that can build an instance of that class to the factory and store it against its calibration instrument
63 type key.
64
65 \ingroup models
66*/
68 : public QuantLib::Singleton<CalibrationInstrumentFactory, std::integral_constant<bool, true>> {
69
70 friend class QuantLib::Singleton<CalibrationInstrumentFactory, std::integral_constant<bool, true>>;
71
72public:
73 /*! The container type used to store the calibration instrument type key and the function that will be used to
74 build a default instance of that calibration instrument type.
75 */
76 typedef std::map<std::string, std::function<QuantLib::ext::shared_ptr<CalibrationInstrument>()>> map_type;
77
78 /*! A call to \c build should return an instance of \c CalibrationInstrument corresponding to the required
79 \p instrumentType. For example, a call to <code>build("CpiCapFloor")</code> should return a \c CpiCapFloor
80 instance.
81
82 \warning If the \p instrumentType has not been added to the factory then a call to this method for that
83 \p instrumentType will return a \c nullptr
84 */
85 QuantLib::ext::shared_ptr<CalibrationInstrument> build(const std::string& instrumentType);
86
87 /*! Add a builder function \p builder for a given \p instrumentType
88 */
89 void addBuilder(const std::string& instrumentType,
90 std::function<QuantLib::ext::shared_ptr<CalibrationInstrument>()> builder,
91 const bool allowOverwrite = false);
92
93private:
94 boost::shared_mutex mutex_;
96};
97
98}
99}
class for holding details of the calibration instruments for a model
std::map< std::string, std::function< QuantLib::ext::shared_ptr< CalibrationInstrument >()> > map_type
QuantLib::ext::shared_ptr< CalibrationInstrument > build(const std::string &instrumentType)
void addBuilder(const std::string &instrumentType, std::function< QuantLib::ext::shared_ptr< CalibrationInstrument >()> builder, const bool allowOverwrite=false)
QuantLib::ext::shared_ptr< CalibrationInstrument > createCalibrationInstrument()
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23