Skip to content

Commit

Permalink
do not use pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasibule committed Apr 22, 2024
1 parent 9d8825f commit 3ad750a
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion quantlib/cashflows/fixed_rate_coupon.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cdef class FixedRateCoupon(Coupon):
cdef class FixedRateLeg(Leg):

def __init__(self, Schedule schedule):
self.frl = new _frc.FixedRateLeg(deref(schedule._thisptr))
self.frl = new _frc.FixedRateLeg(schedule._thisptr)

def with_notional(self, Real notional):
self.frl.withNotionals(notional)
Expand Down
2 changes: 1 addition & 1 deletion quantlib/instruments/bonds/cpibond.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cdef class CPIBond(Bond):
static_pointer_cast[_inf.ZeroInflationIndex](
cpi_index._thisptr),
observation_interpolation,
deref(schedule._thisptr), coupons,
schedule._thisptr, coupons,
deref(accrual_day_counter._thisptr), payment_convention,
deref(issue_date._thisptr),
payment_calendar._thisptr, deref(ex_coupon_period._thisptr),
Expand Down
2 changes: 1 addition & 1 deletion quantlib/instruments/bonds/fixedratebond.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cdef class FixedRateBond(Bond):
new _frb.FixedRateBond(
settlement_days,
face_amount,
deref(schedule._thisptr),
schedule._thisptr,
coupons,
deref(accrual_day_counter._thisptr),
payment_convention,
Expand Down
2 changes: 1 addition & 1 deletion quantlib/instruments/bonds/floatingratebond.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ cdef class FloatingRateBond(Bond):
self._thisptr.reset(
new _frb.FloatingRateBond(
settlement_days, face_amount,
deref(schedule._thisptr),
schedule._thisptr,
static_pointer_cast[_ii.IborIndex](ibor_index._thisptr),
deref(accrual_day_counter._thisptr),
payment_convention,
Expand Down
4 changes: 2 additions & 2 deletions quantlib/instruments/credit_default_swap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ cdef class CreditDefaultSwap(Instrument):

self._thisptr = shared_ptr[_instrument.Instrument](
new _cds.CreditDefaultSwap(
side, notional, spread, deref(schedule._thisptr),
side, notional, spread, schedule._thisptr,
payment_convention,
deref(day_counter._thisptr), settles_accrual, pays_at_default_time,
deref(protection_start._thisptr),
Expand Down Expand Up @@ -183,7 +183,7 @@ cdef class CreditDefaultSwap(Instrument):
cdef CreditDefaultSwap instance = CreditDefaultSwap.__new__(CreditDefaultSwap)
instance._thisptr = shared_ptr[_instrument.Instrument](
new _cds.CreditDefaultSwap(
side, notional, upfront, spread, deref(schedule._thisptr),
side, notional, upfront, spread, schedule._thisptr,
payment_convention,
deref(day_counter._thisptr), settles_accrual, pays_at_default_time,
deref(protection_start._thisptr),
Expand Down
4 changes: 2 additions & 2 deletions quantlib/instruments/overnightindexedswap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cdef class OvernightIndexedSwap(Swap):
if isinstance(nominal, float):
self._thisptr = static_pointer_cast[Instrument](
make_shared[_ois.OvernightIndexedSwap](
swap_type, <Real>nominal, deref(schedule._thisptr), fixed_rate,
swap_type, <Real>nominal, schedule._thisptr, fixed_rate,
deref(fixed_dc._thisptr), static_pointer_cast[_ii.OvernightIndex](overnight_index._thisptr),
spread, payment_lag, payment_adjustment, payment_calendar._thisptr,
telescopic_value_dates, averaging_method
Expand All @@ -49,7 +49,7 @@ cdef class OvernightIndexedSwap(Swap):
nominals.push_back(n)
self._thisptr = static_pointer_cast[Instrument](
make_shared[_ois.OvernightIndexedSwap](
swap_type, nominals, deref(schedule._thisptr), fixed_rate,
swap_type, nominals, schedule._thisptr, fixed_rate,
deref(fixed_dc._thisptr), static_pointer_cast[_ii.OvernightIndex](overnight_index._thisptr),
spread, payment_lag, payment_adjustment, payment_calendar._thisptr,
telescopic_value_dates, averaging_method
Expand Down
9 changes: 5 additions & 4 deletions quantlib/instruments/vanillaswap.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Simple fixed-rate vs Libor swap"""
from cython.operator cimport dereference as deref
from libcpp.utility cimport move
from quantlib.types cimport Rate, Real, Spread
from quantlib.handle cimport optional, static_pointer_cast
from quantlib.cashflows.fixed_rate_coupon cimport FixedRateLeg
Expand Down Expand Up @@ -44,10 +45,10 @@ cdef class VanillaSwap(Swap):
new _vanillaswap.VanillaSwap(
type,
nominal,
deref(fixed_schedule._thisptr),
fixed_schedule._thisptr,
fixed_rate,
deref(fixed_daycount._thisptr),
deref(float_schedule._thisptr),
float_schedule._thisptr,
static_pointer_cast[_ib.IborIndex](ibor_index._thisptr),
spread,
deref(floating_daycount._thisptr),
Expand Down Expand Up @@ -112,7 +113,7 @@ cdef class VanillaSwap(Swap):
@property
def fixed_schedule(self):
cdef Schedule sched = Schedule.__new__(Schedule)
sched._thisptr = new QlSchedule(get_vanillaswap(self).fixedSchedule())
sched._thisptr = move(QlSchedule(get_vanillaswap(self).fixedSchedule()))
return sched

@property
Expand All @@ -124,7 +125,7 @@ cdef class VanillaSwap(Swap):
@property
def floating_schedule(self):
cdef Schedule sched = Schedule.__new__(Schedule)
sched._thisptr = new QlSchedule(get_vanillaswap(self).floatingSchedule())
sched._thisptr = move(QlSchedule(get_vanillaswap(self).floatingSchedule()))
return sched

@property
Expand Down
2 changes: 1 addition & 1 deletion quantlib/termstructures/yields/bond_helpers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cdef class FixedRateBondHelper(BondHelper):
clean_price.handle(),
settlement_days,
face_amount,
deref(schedule._thisptr),
schedule._thisptr,
coupons,
deref(day_counter._thisptr),
payment_conv,
Expand Down
5 changes: 2 additions & 3 deletions quantlib/time/_schedule.pxd
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
include '../types.pxi'

from quantlib.types cimport Size
from libcpp cimport bool
from libcpp.vector cimport vector
from ._period cimport Period
from ._date cimport Date
from ._calendar cimport Calendar, BusinessDayConvention
from quantlib.handle cimport optional
from .dategeneration cimport DateGeneration
cdef extern from 'ql/time/schedule.hpp' namespace 'QuantLib':
cdef extern from 'ql/time/schedule.hpp' namespace 'QuantLib' nogil:

cdef cppclass Schedule:
Schedule()
Expand Down
4 changes: 2 additions & 2 deletions quantlib/time/daycounters/actual_actual.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ from quantlib.time.daycounter cimport DayCounter

cdef class ActualActual(DayCounter):

def __init__(self, Convention convention=Convention.ISDA, Schedule schedule=Schedule.from_dates([])):
def __init__(self, Convention convention=Convention.ISDA, Schedule schedule=Schedule.__new__(Schedule)):
""" Actual/Actual day count
The day count can be calculated according to:
Expand All @@ -37,7 +37,7 @@ cdef class ActualActual(DayCounter):
https://www.isda.org/a/pIJEE/The-Actual-Actual-Day-Count-Fraction-1999.pdf
"""
self._thisptr = <_daycounter.DayCounter*> new \
_aa.ActualActual(convention, deref(schedule._thisptr))
_aa.ActualActual(convention, schedule._thisptr)

cdef _daycounter.DayCounter* from_name(str convention):

Expand Down
2 changes: 1 addition & 1 deletion quantlib/time/schedule.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cimport quantlib.time._schedule as _schedule

cdef class Schedule:
cdef _schedule.Schedule* _thisptr
cdef _schedule.Schedule _thisptr
19 changes: 8 additions & 11 deletions quantlib/time/schedule.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from cython.operator cimport dereference as deref, preincrement as preinc
from libcpp cimport bool
from libcpp.vector cimport vector
from libcpp.utility cimport move
from quantlib.handle cimport optional

cimport quantlib.time._date as _date
Expand Down Expand Up @@ -31,7 +32,8 @@ cdef class Schedule:
warnings.warn("Deprecated: use class method from_rule instead",
DeprecationWarning)

self._thisptr = new _schedule.Schedule(
self._thisptr = move(
_schedule.Schedule(
deref(effective_date._thisptr),
deref(termination_date._thisptr),
deref(tenor._thisptr),
Expand All @@ -40,6 +42,7 @@ cdef class Schedule:
termination_date_convention,
date_generation_rule, end_of_month,
_date.Date(), _date.Date()
)
)

@classmethod
Expand Down Expand Up @@ -69,7 +72,7 @@ cdef class Schedule:
opt_rule = <DateGeneration>rule
if end_of_month is not None:
opt_end_of_month = <bool>end_of_month
instance._thisptr = new _schedule.Schedule(
instance._thisptr = move(_schedule.Schedule(
_dates,
calendar._thisptr,
business_day_convention,
Expand All @@ -78,7 +81,7 @@ cdef class Schedule:
opt_rule,
opt_end_of_month,
is_regular
)
))

return instance

Expand All @@ -92,7 +95,7 @@ cdef class Schedule:
Date first_date=Date(), Date next_to_lastdate=Date()):

cdef Schedule instance = Schedule.__new__(Schedule)
instance._thisptr = new _schedule.Schedule(
instance._thisptr = move(_schedule.Schedule(
deref(effective_date._thisptr),
deref(termination_date._thisptr),
deref(tenor._thisptr),
Expand All @@ -101,15 +104,9 @@ cdef class Schedule:
termination_date_convention,
rule, end_of_month,
deref(first_date._thisptr), deref(next_to_lastdate._thisptr)
)
))
return instance

def __dealloc__(self):
if self._thisptr is not NULL:
del self._thisptr
self._thisptr = NULL


def dates(self):
cdef vector[_date.Date] dates = self._thisptr.dates()
cdef list t = []
Expand Down

0 comments on commit 3ad750a

Please sign in to comment.