Skip to content

Commit

Permalink
Reduce duplication in rate helpers constructors (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio authored Feb 19, 2024
2 parents ce909f4 + 30d6eb5 commit 72012a7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 189 deletions.
2 changes: 1 addition & 1 deletion Examples/CDS/CDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void example01() {
std::vector<ext::shared_ptr<DefaultProbabilityHelper>> instruments;
for (Size i = 0; i < 4; i++) {
instruments.push_back(ext::make_shared<SpreadCdsHelper>(
Handle<Quote>(ext::make_shared<SimpleQuote>(quoted_spreads[i])),
makeQuoteHandle(quoted_spreads[i]),
tenors[i], settlementDays, calendar, Quarterly, Following,
DateGeneration::TwentiethIMM, Actual365Fixed(),
recovery_rate, tsCurve));
Expand Down
2 changes: 1 addition & 1 deletion Examples/CVAIRS/CVAIRS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main(int, char* []) {
vector<ext::shared_ptr<RateHelper>> swapHelpers;
for(Size i=0; i<sizeof(tenorsSwapMkt)/sizeof(Size); i++)
swapHelpers.push_back(ext::make_shared<SwapRateHelper>(
Handle<Quote>(ext::make_shared<SimpleQuote>(ratesSwapmkt[i])),
makeQuoteHandle(ratesSwapmkt[i]),
tenorsSwapMkt[i] * Years,
TARGET(),
Quarterly,
Expand Down
4 changes: 2 additions & 2 deletions Examples/ConvertibleBonds/ConvertibleBonds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int main(int, char* []) {
auto exercise = ext::make_shared<EuropeanExercise>(exerciseDate);
auto amExercise = ext::make_shared<AmericanExercise>(settlementDate, exerciseDate);

Handle<Quote> underlyingH(ext::make_shared<SimpleQuote>(underlying));
auto underlyingH = makeQuoteHandle(underlying);

Handle<YieldTermStructure> flatTermStructure(
ext::make_shared<FlatForward>(settlementDate, riskFreeRate, dayCounter));
Expand All @@ -160,7 +160,7 @@ int main(int, char* []) {

Size timeSteps = 801;

Handle<Quote> creditSpread(ext::make_shared<SimpleQuote>(spreadRate));
auto creditSpread = makeQuoteHandle(spreadRate);

auto rate = ext::make_shared<SimpleQuote>(riskFreeRate);

Expand Down
3 changes: 1 addition & 2 deletions Examples/DiscreteHedging/DiscreteHedging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ void ReplicationError::compute(Size nTimeSteps, Size nSamples)
Calendar calendar = TARGET();
Date today = Date::todaysDate();
DayCounter dayCount = Actual365Fixed();
Handle<Quote> stateVariable(
ext::make_shared<SimpleQuote>(s0_));
auto stateVariable = makeQuoteHandle(s0_);
Handle<YieldTermStructure> riskFreeRate(
ext::make_shared<FlatForward>(today, r_, dayCount));
Handle<YieldTermStructure> dividendYield(
Expand Down
2 changes: 1 addition & 1 deletion Examples/EquityOption/EquityOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int main(int, char* []) {

auto americanExercise = ext::make_shared<AmericanExercise>(settlementDate, maturity);

Handle<Quote> underlyingH(ext::make_shared<SimpleQuote>(underlying));
auto underlyingH = makeQuoteHandle(underlying);

// bootstrap the yield/dividend/vol curves
Handle<YieldTermStructure> flatTermStructure(
Expand Down
12 changes: 5 additions & 7 deletions Examples/Gaussian1dModels/Gaussian1dModels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void printModelCalibration(

// here the main part of the code starts

int main(int argc, char *argv[]) {
int main(int, char *[]) {

try {

Expand All @@ -137,9 +137,8 @@ int main(int argc, char *argv[]) {
Real forward6mLevel = 0.025;
Real oisLevel = 0.02;

Handle<Quote> forward6mQuote(
ext::make_shared<SimpleQuote>(forward6mLevel));
Handle<Quote> oisQuote(ext::make_shared<SimpleQuote>(oisLevel));
auto forward6mQuote = makeQuoteHandle(forward6mLevel);
auto oisQuote = makeQuoteHandle(oisLevel);

Handle<YieldTermStructure> yts6m(ext::make_shared<FlatForward>(
0, TARGET(), forward6mQuote, Actual365Fixed()));
Expand All @@ -156,7 +155,7 @@ int main(int argc, char *argv[]) {
<< "\nat a level of " << forward6mLevel << std::endl;

Real volLevel = 0.20;
Handle<Quote> volQuote(ext::make_shared<SimpleQuote>(volLevel));
auto volQuote = makeQuoteHandle(volLevel);
Handle<SwaptionVolatilityStructure> swaptionVol(
ext::make_shared<ConstantSwaptionVolatility>(
0, TARGET(), ModifiedFollowing, volQuote, Actual365Fixed()));
Expand Down Expand Up @@ -483,8 +482,7 @@ int main(int argc, char *argv[]) {
"\npricing this using the LinearTsrPricer for CMS coupon "
"estimation" << std::endl;

Handle<Quote> reversionQuote(
ext::make_shared<SimpleQuote>(reversion));
auto reversionQuote = makeQuoteHandle(reversion);

const Leg &leg0 = underlying4->leg(0);
const Leg &leg1 = underlying4->leg(1);
Expand Down
16 changes: 8 additions & 8 deletions ql/handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ namespace QuantLib {
protected:
class Link : public Observable, public Observer {
public:
explicit Link(const ext::shared_ptr<T>& h,
explicit Link(ext::shared_ptr<T> h,
bool registerAsObserver);
void linkTo(const ext::shared_ptr<T>&,
void linkTo(ext::shared_ptr<T>,
bool registerAsObserver);
bool empty() const { return !h_; }
const ext::shared_ptr<T>& currentLink() const { return h_; }
Expand Down Expand Up @@ -75,9 +75,9 @@ namespace QuantLib {
//@{
Handle()
: Handle(ext::shared_ptr<T>()) {}
explicit Handle(const ext::shared_ptr<T>& p,
explicit Handle(ext::shared_ptr<T> p,
bool registerAsObserver = true)
: link_(new Link(p,registerAsObserver)) {}
: link_(new Link(std::move(p), registerAsObserver)) {}
//@}
//! dereferencing
const ext::shared_ptr<T>& currentLink() const;
Expand Down Expand Up @@ -125,17 +125,17 @@ namespace QuantLib {
// inline definitions

template <class T>
inline Handle<T>::Link::Link(const ext::shared_ptr<T>& h, bool registerAsObserver) {
linkTo(h,registerAsObserver);
inline Handle<T>::Link::Link(ext::shared_ptr<T> h, bool registerAsObserver) {
linkTo(std::move(h), registerAsObserver);
}

template <class T>
inline void Handle<T>::Link::linkTo(const ext::shared_ptr<T>& h,
inline void Handle<T>::Link::linkTo(ext::shared_ptr<T> h,
bool registerAsObserver) {
if ((h != h_) || (isObserver_ != registerAsObserver)) {
if (h_ && isObserver_)
unregisterWith(h_);
h_ = h;
h_ = std::move(h);
isObserver_ = registerAsObserver;
if (h_ && isObserver_)
registerWith(h_);
Expand Down
7 changes: 7 additions & 0 deletions ql/quotes/simplequote.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ namespace QuantLib {
Real value_;
};

RelinkableHandle<Quote> makeQuoteHandle(Real value);


// inline definitions

inline RelinkableHandle<Quote> makeQuoteHandle(Real value) {
return RelinkableHandle<Quote>(ext::make_shared<SimpleQuote>(value));
}

inline SimpleQuote::SimpleQuote(Real value)
: value_(value) {}

Expand Down
3 changes: 1 addition & 2 deletions ql/termstructures/bootstraphelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ namespace QuantLib {

template <class TS>
BootstrapHelper<TS>::BootstrapHelper(Real quote)
: quote_(Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(quote)))),
termStructure_(nullptr) {}
: quote_(makeQuoteHandle(quote)), termStructure_(nullptr) {}

template <class TS>
void BootstrapHelper<TS>::setTermStructure(TS* t) {
Expand Down
Loading

0 comments on commit 72012a7

Please sign in to comment.