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