From 4ecf90798268cd2458023c49c81f29cb166a4466 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Fri, 21 Apr 2023 14:31:43 -0400 Subject: [PATCH] use optional from std --- quantlib/handle.pxd | 17 +++++++++++++---- quantlib/instruments/credit_default_swap.pyx | 2 +- quantlib/settings.pyx | 8 ++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/quantlib/handle.pxd b/quantlib/handle.pxd index c40d8266a..aace488c9 100644 --- a/quantlib/handle.pxd +++ b/quantlib/handle.pxd @@ -17,13 +17,22 @@ cdef extern from 'ql/shared_ptr.hpp' namespace 'QuantLib::ext' nogil: shared_ptr[T] static_pointer_cast[T](...) shared_ptr[T] dynamic_pointer_cast[T](...) -cdef extern from 'boost/optional.hpp' namespace 'boost': +cdef extern from 'ql/optional.hpp' namespace 'QuantLib::ext' nogil: + cdef cppclass nullopt_t: + nullopt_t() + + cdef nullopt_t nullopt + cdef cppclass optional[T]: optional() - optional(const T&) - T get() + optional(nullopt_t) + optional(const T&) except + + T& value() + T& value_or[U](U& default_value) bool operator!() - optional& operator=(T&) + optional& operator=(optional&) + + optional[T] make_optional[T](...) except + cdef extern from 'ql/handle.hpp' namespace 'QuantLib' nogil: cdef cppclass Handle[T]: diff --git a/quantlib/instruments/credit_default_swap.pyx b/quantlib/instruments/credit_default_swap.pyx index fe35b14f3..d78feb96d 100644 --- a/quantlib/instruments/credit_default_swap.pyx +++ b/quantlib/instruments/credit_default_swap.pyx @@ -218,7 +218,7 @@ cdef class CreditDefaultSwap(Instrument): @property def upfront(self): cdef optional[Rate] upf = _get_cds(self).upfront() - return None if not upf else upf.get() + return None if not upf else upf.value() @property def settles_accrual(self): diff --git a/quantlib/settings.pyx b/quantlib/settings.pyx index d8faed419..d7f4a9302 100644 --- a/quantlib/settings.pyx +++ b/quantlib/settings.pyx @@ -1,7 +1,7 @@ from cython.operator cimport dereference as deref from libcpp cimport bool -from quantlib.handle cimport optional, shared_ptr +from quantlib.handle cimport optional, shared_ptr, nullopt from quantlib.time._date cimport Date as QlDate from quantlib.time.date cimport Date, date_from_qldate from quantlib.observable cimport Observable @@ -80,13 +80,13 @@ cdef class Settings: if not flag: return None else: - return flag.get() + return flag.value() @include_todays_cashflows.setter def include_todays_cashflows(self, val): - cdef optional[bool] flag + cdef optional[bool] flag = nullopt if val is not None: - flag = val + flag = optional[bool](val) SET_VALUE(_settings.Settings.instance().includeTodaysCashFlows(), flag)