Skip to content

Commit 848777a

Browse files
committed
Move helper function to public interface
1 parent ac1d143 commit 848777a

File tree

7 files changed

+51
-20
lines changed

7 files changed

+51
-20
lines changed

QuantLib.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,7 @@
28592859
<ClCompile Include="ql\money.cpp" />
28602860
<ClCompile Include="ql\position.cpp" />
28612861
<ClCompile Include="ql\prices.cpp" />
2862+
<ClCompile Include="ql\quote.cpp" />
28622863
<ClCompile Include="ql\rebatedexercise.cpp" />
28632864
<ClCompile Include="ql\settings.cpp" />
28642865
<ClCompile Include="ql\stochasticprocess.cpp" />

QuantLib.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6699,6 +6699,7 @@
66996699
<ClCompile Include="ql\event.cpp" />
67006700
<ClCompile Include="ql\exchangerate.cpp" />
67016701
<ClCompile Include="ql\exercise.cpp" />
6702+
<ClCompile Include="ql\quote.cpp" />
67026703
<ClCompile Include="ql\rebatedexercise.cpp" />
67036704
<ClCompile Include="ql\index.cpp" />
67046705
<ClCompile Include="ql\instrument.cpp" />

ql/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ set(QL_SOURCES
794794
processes/coxingersollrossprocess.cpp
795795
processes/squarerootprocess.cpp
796796
processes/stochasticprocessarray.cpp
797+
quote.cpp
797798
quotes/eurodollarfuturesquote.cpp
798799
quotes/forwardswapquote.cpp
799800
quotes/forwardvaluequote.cpp

ql/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ cpp_files = \
7777
money.cpp \
7878
position.cpp \
7979
prices.cpp \
80+
quote.cpp \
8081
rebatedexercise.cpp \
8182
settings.cpp \
8283
stochasticprocess.cpp \

ql/quote.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/*
4+
Copyright (C) 2025 StatPro Italia srl
5+
Copyright (C) 2025 Paolo D'Elia
6+
7+
This file is part of QuantLib, a free-software/open-source library
8+
for financial quantitative analysts and developers - http://quantlib.org/
9+
10+
QuantLib is free software: you can redistribute it and/or modify it
11+
under the terms of the QuantLib license. You should have received a
12+
copy of the license along with this program; if not, please email
13+
<quantlib-dev@lists.sf.net>. The license is also available online at
14+
<http://quantlib.org/license.shtml>.
15+
16+
This program is distributed in the hope that it will be useful, but WITHOUT
17+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18+
FOR A PARTICULAR PURPOSE. See the license for more details.
19+
*/
20+
21+
#include <ql/quote.hpp>
22+
#include <ql/quotes/simplequote.hpp>
23+
#include <ql/errors.hpp>
24+
25+
namespace QuantLib {
26+
27+
Handle<Quote> valueOrHandle(const std::variant<Real, Handle<Quote>>& value) {
28+
return std::visit(
29+
[](const auto& x) -> Handle<Quote> {
30+
using T = std::decay_t<decltype(x)>;
31+
if constexpr (std::is_same_v<T, Real>) {
32+
return makeQuoteHandle(x);
33+
} else if constexpr (std::is_same_v<T, Handle<Quote>>) {
34+
return x;
35+
} else {
36+
QL_FAIL("Unexpected type in quote variant");
37+
}
38+
},
39+
value);
40+
}
41+
42+
}

ql/quote.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <ql/handle.hpp>
2929
#include <ql/errors.hpp>
3030
#include <ql/utilities/null.hpp>
31+
#include <variant>
3132

3233
namespace QuantLib {
3334

@@ -43,6 +44,8 @@ namespace QuantLib {
4344
virtual bool isValid() const = 0;
4445
};
4546

47+
Handle<Quote> valueOrHandle(const std::variant<Real, Handle<Quote>>& value);
48+
4649
}
4750

4851
#endif

ql/termstructures/yield/oisratehelper.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,6 @@
2828

2929
namespace QuantLib {
3030

31-
namespace {
32-
Handle<Quote> normalizeSpreadType(const std::variant<Spread, Handle<Quote>>& overnightSpread) {
33-
return std::visit(
34-
[](const auto& value) -> Handle<Quote> {
35-
using T = std::decay_t<decltype(value)>;
36-
if constexpr (std::is_same_v<T, Spread>) {
37-
return makeQuoteHandle(value);
38-
} else if constexpr (std::is_same_v<T, Handle<Quote>>) {
39-
return value;
40-
} else {
41-
QL_FAIL("Unexpected type in overnightSpread variant");
42-
}
43-
},
44-
overnightSpread
45-
);
46-
}
47-
}
48-
4931
OISRateHelper::OISRateHelper(Natural settlementDays,
5032
const Period& tenor, // swap maturity
5133
const Handle<Quote>& fixedRate,
@@ -73,7 +55,7 @@ namespace QuantLib {
7355
discountHandle_(std::move(discount)), telescopicValueDates_(telescopicValueDates),
7456
paymentLag_(paymentLag), paymentConvention_(paymentConvention),
7557
paymentFrequency_(paymentFrequency), paymentCalendar_(std::move(paymentCalendar)),
76-
forwardStart_(forwardStart), overnightSpread_(normalizeSpreadType(overnightSpread)), pillarChoice_(pillar),
58+
forwardStart_(forwardStart), overnightSpread_(valueOrHandle(overnightSpread)), pillarChoice_(pillar),
7759
averagingMethod_(averagingMethod), endOfMonth_(endOfMonth),
7860
fixedPaymentFrequency_(fixedPaymentFrequency), fixedCalendar_(std::move(fixedCalendar)),
7961
lookbackDays_(lookbackDays), lockoutDays_(lockoutDays), applyObservationShift_(applyObservationShift),
@@ -107,7 +89,7 @@ namespace QuantLib {
10789
discountHandle_(std::move(discount)), telescopicValueDates_(telescopicValueDates),
10890
paymentLag_(paymentLag), paymentConvention_(paymentConvention),
10991
paymentFrequency_(paymentFrequency), paymentCalendar_(std::move(paymentCalendar)),
110-
overnightSpread_(normalizeSpreadType(overnightSpread)), pillarChoice_(pillar),
92+
overnightSpread_(valueOrHandle(overnightSpread)), pillarChoice_(pillar),
11193
averagingMethod_(averagingMethod), endOfMonth_(endOfMonth),
11294
fixedPaymentFrequency_(fixedPaymentFrequency), fixedCalendar_(std::move(fixedCalendar)),
11395
lookbackDays_(lookbackDays), lockoutDays_(lockoutDays), applyObservationShift_(applyObservationShift),

0 commit comments

Comments
 (0)