Skip to content

Commit d6b4dda

Browse files
pcaspersjenkins
authored andcommitted
QPR-11568 align behaviour of factories
1 parent b1a6bdb commit d6b4dda

6 files changed

Lines changed: 22 additions & 39 deletions

File tree

OREData/ored/model/calibrationinstrumentfactory.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ boost::shared_ptr<CalibrationInstrument> CalibrationInstrumentFactory::build(con
3333
}
3434

3535
void CalibrationInstrumentFactory::addBuilder(const string& instrumentType,
36-
function<boost::shared_ptr<CalibrationInstrument>()> builder) {
36+
function<boost::shared_ptr<CalibrationInstrument>()> builder,
37+
const bool allowOverwrite) {
3738
boost::unique_lock<boost::shared_mutex> lock(mutex_);
38-
map_[instrumentType] = builder;
39+
QL_REQUIRE(map_.insert(std::make_pair(instrumentType, builder)).second,
40+
"CalibrationInstrumentFactory::addBuilder(" << instrumentType << "): builder for key already exists.");
3941
}
40-
4142
}
4243
}

OREData/ored/model/calibrationinstrumentfactory.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class CalibrationInstrumentFactory
8787
/*! Add a builder function \p builder for a given \p instrumentType
8888
*/
8989
void addBuilder(const std::string& instrumentType,
90-
std::function<boost::shared_ptr<CalibrationInstrument>()> builder);
90+
std::function<boost::shared_ptr<CalibrationInstrument>()> builder,
91+
const bool allowOverwrite = false);
9192

9293
private:
9394
boost::shared_mutex mutex_;

OREData/ored/portfolio/legdatafactory.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <ored/portfolio/legdatafactory.hpp>
2020

21+
#include <ql/errors.hpp>
22+
2123
using std::function;
2224
using std::string;
2325

@@ -32,9 +34,11 @@ boost::shared_ptr<LegAdditionalData> LegDataFactory::build(const string& legType
3234
return it->second();
3335
}
3436

35-
void LegDataFactory::addBuilder(const string& legType, function<boost::shared_ptr<LegAdditionalData>()> builder) {
37+
void LegDataFactory::addBuilder(const string& legType, function<boost::shared_ptr<LegAdditionalData>()> builder,
38+
const bool allowOverwrite) {
3639
boost::unique_lock<boost::shared_mutex> lock(mutex_);
37-
map_[legType] = builder;
40+
QL_REQUIRE(map_.insert(std::make_pair(legType, builder)).second || allowOverwrite,
41+
"LegDataFactory::addBuilder(" << legType << "): builder for key already exists.");
3842
}
3943

4044
} // namespace data

OREData/ored/portfolio/legdatafactory.hpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,41 +82,13 @@ class LegDataFactory : public QuantLib::Singleton<LegDataFactory, std::integral_
8282

8383
/*! Add a builder function \p builder for a given \p legType
8484
*/
85-
void addBuilder(const std::string& legType, std::function<boost::shared_ptr<LegAdditionalData>()> builder);
85+
void addBuilder(const std::string& legType, std::function<boost::shared_ptr<LegAdditionalData>()> builder,
86+
const bool allowOverwrite = false);
8687

8788
private:
8889
boost::shared_mutex mutex_;
8990
map_type map_;
9091
};
9192

92-
/*! Leg data registration class
93-
94-
This class is used in any class derived from \c LegAdditionalData to register itself with the \c LegDataFactory so
95-
that it can be built via a call to <code>LegDataFactory::instance().build(const std::string& legType)</code>
96-
97-
As a concrete example, a \c FixedLegData class derived from \c LegAdditionalData should have the following form
98-
in order to register it with the \c LegDataFactory:
99-
100-
In fixedlegdata.hpp
101-
\code{.cpp}
102-
class FixedLegData : public LegAdditionalData {
103-
public:
104-
private:
105-
static LegDataRegister<FixedLegData> reg_;
106-
}
107-
\endcode
108-
109-
In fixedlegdata.cpp
110-
\code{.cpp}
111-
LegDataRegister<FixedLegData> FixedLegData::reg_("Fixed");
112-
\endcode
113-
114-
\ingroup portfolio
115-
*/
116-
template <class T> struct LegDataRegister {
117-
public:
118-
LegDataRegister(const std::string& legType) { LegDataFactory::instance().addBuilder(legType, &createLegData<T>); }
119-
};
120-
12193
} // namespace data
12294
} // namespace ore

OREData/ored/portfolio/referencedatafactory.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <ored/portfolio/referencedatafactory.hpp>
2020

21+
#include <ql/errors.hpp>
22+
2123
using std::string;
2224

2325
namespace ore {
@@ -32,9 +34,11 @@ boost::shared_ptr<ReferenceDatum> ReferenceDatumFactory::build(const string& ref
3234
}
3335

3436
void ReferenceDatumFactory::addBuilder(const string& refDatumType,
35-
std::function<boost::shared_ptr<AbstractReferenceDatumBuilder>()> builder) {
37+
std::function<boost::shared_ptr<AbstractReferenceDatumBuilder>()> builder,
38+
const bool allowOverwrite) {
3639
boost::unique_lock<boost::shared_mutex> lock(mutex_);
37-
map_[refDatumType] = builder;
40+
QL_REQUIRE(map_.insert(std::make_pair(refDatumType, builder)).second,
41+
"ReferenceDatumFactory::addBuilder(" << refDatumType << "): builder for key already exists.");
3842
}
3943

4044
} // namespace data

OREData/ored/portfolio/referencedatafactory.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class ReferenceDatumFactory : public QuantLib::Singleton<ReferenceDatumFactory,
5959
boost::shared_ptr<ReferenceDatum> build(const std::string& refDatumType);
6060

6161
void addBuilder(const std::string& refDatumType,
62-
std::function<boost::shared_ptr<AbstractReferenceDatumBuilder>()> builder);
62+
std::function<boost::shared_ptr<AbstractReferenceDatumBuilder>()> builder,
63+
const bool allowOverwrite = false);
6364

6465
private:
6566
boost::shared_mutex mutex_;

0 commit comments

Comments
 (0)