Skip to content

Commit

Permalink
use optional from std
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasibule committed Nov 21, 2023
1 parent 90c1505 commit 4ecf907
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions quantlib/handle.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion quantlib/instruments/credit_default_swap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions quantlib/settings.pyx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = <bool>val
flag = optional[bool](<bool>val)
SET_VALUE(_settings.Settings.instance().includeTodaysCashFlows(),
flag)

Expand Down

0 comments on commit 4ecf907

Please sign in to comment.