QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
LazyObject Class Referenceabstract

Framework for calculation on demand and result caching. More...

#include <lazyobject.hpp>

+ Inheritance diagram for LazyObject:
+ Collaboration diagram for LazyObject:

Classes

class  Defaults
 Per-session settings for the LazyObject class. More...
 
class  UpdateChecker
 

Public Member Functions

 LazyObject ()
 
 ~LazyObject () override=default
 
Observer interface
void update () override
 
bool isCalculated () const
 
- Public Member Functions inherited from Observable
 Observable ()
 
 Observable (const Observable &)
 
Observableoperator= (const Observable &)
 
 Observable (Observable &&)=delete
 
Observableoperator= (Observable &&)=delete
 
virtual ~Observable ()=default
 
void notifyObservers ()
 
- Public Member Functions inherited from Observer
 Observer ()=default
 
 Observer (const Observer &)
 
Observeroperator= (const Observer &)
 
virtual ~Observer ()
 
std::pair< iterator, boolregisterWith (const ext::shared_ptr< Observable > &)
 
void registerWithObservables (const ext::shared_ptr< Observer > &)
 
Size unregisterWith (const ext::shared_ptr< Observable > &)
 
void unregisterWithAll ()
 
virtual void update ()=0
 
virtual void deepUpdate ()
 

Notification settings

bool calculated_ = false
 
bool frozen_ = false
 
bool alwaysForward_
 
bool updating_ = false
 
void forwardFirstNotificationOnly ()
 
void alwaysForwardNotifications ()
 

Calculations

These methods do not modify the structure of the object and are therefore declared as const. Data members which will be calculated on demand need to be declared as mutable.

void recalculate ()
 
void freeze ()
 
void unfreeze ()
 
virtual void calculate () const
 
virtual void performCalculations () const =0
 

Additional Inherited Members

- Public Types inherited from Observer
typedef set_type::iterator iterator
 

Detailed Description

Framework for calculation on demand and result caching.

Definition at line 34 of file lazyobject.hpp.

Constructor & Destructor Documentation

◆ LazyObject()

Definition at line 185 of file lazyobject.hpp.

◆ ~LazyObject()

~LazyObject ( )
overridedefault

Member Function Documentation

◆ update()

void update ( )
overridevirtual

This method must be implemented in derived classes. An instance of Observer does not call this method directly: instead, it will be called by the observables the instance registered with when they need to notify any changes.

Implements Observer.

Reimplemented in RandomLM< RandomDefaultLM, copulaPolicy, SobolRsg >, RandomLM< RandomLossLM, copulaPolicy, SobolRsg >, ForwardSwapQuote, PiecewiseDefaultCurve< Traits, Interpolator, Bootstrap >, PiecewiseYoYInflationCurve< Interpolator, Bootstrap, Traits >, PiecewiseZeroInflationCurve< Interpolator, Bootstrap, Traits >, CapFloorTermVolCurve, CapFloorTermVolSurface, InterpolatedSmileSection< Interpolator >, StrippedOptionletAdapter, SabrInterpolatedSmileSection, CmsMarket, SwaptionVolatilityDiscrete, FittedBondDiscountCurve, FlatForward, and PiecewiseYieldCurve< Traits, Interpolator, Bootstrap >.

Definition at line 188 of file lazyobject.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isCalculated()

bool isCalculated ( ) const

Returns true if the instrument is calculated

Definition at line 266 of file lazyobject.hpp.

◆ recalculate()

void recalculate ( )

This method force the recalculation of any results which would otherwise be cached. It is not declared as const since it needs to call the non-const notifyObservers method.

Note
Explicit invocation of this method is not necessary if the object registered itself as observer with the structures on which such results depend. It is strongly advised to follow this policy when possible.

Definition at line 218 of file lazyobject.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ freeze()

void freeze ( )

This method constrains the object to return the presently cached results on successive invocations, even if arguments upon which they depend should change.

Definition at line 232 of file lazyobject.hpp.

◆ unfreeze()

void unfreeze ( )

This method reverts the effect of the freeze method, thus re-enabling recalculations.

Definition at line 236 of file lazyobject.hpp.

+ Here is the call graph for this function:

◆ calculate()

void calculate ( ) const
protectedvirtual

This method performs all needed calculations by calling the performCalculations method.

Warning:
Objects cache the results of the previous calculation. Such results will be returned upon later invocations of calculate. When the results depend on arguments which could change between invocations, the lazy object must register itself as observer of such objects for the calculations to be performed again when they change.
Warning:
Should this method be redefined in derived classes, LazyObject::calculate() should be called in the overriding method.

Reimplemented in Instrument.

Definition at line 253 of file lazyobject.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ performCalculations()

virtual void performCalculations ( ) const
protectedpure virtual

This method must implement any calculations which must be (re)done in order to calculate the desired results.

Implemented in CashFlow, CappedFlooredCoupon, DigitalCoupon, FixedRateCoupon, FloatingRateCoupon, IndexedCashFlow, InflationCoupon, ZeroInflationCashFlow, EnergyBasisSwap, EnergyFuture, EnergyVanillaSwap, StrippedCappedFlooredCoupon, Basket, CDO, OneFactorGaussianCopula, OneFactorStudentCopula, OneFactorGaussianStudentCopula, OneFactorStudentGaussianCopula, RandomLM< derivedRandomLM, copulaPolicy, USNG >, RandomLM< RandomDefaultLM, copulaPolicy, SobolRsg >, RandomLM< RandomLossLM, copulaPolicy, SobolRsg >, RiskyAssetSwap, RiskyAssetSwapOption, FdmExtOUJumpSolver, FdmKlugeExtOUSolver< N >, FdmSimple2dExtOUSolver, FdmSimple3dExtOUJumpSolver, PiecewiseYoYOptionletVolatilityCurve< Interpolator, Bootstrap, Traits >, NormalCLVModel, SquareRootCLVModel, AbcdAtmVolCurve, NoArbSabrInterpolatedSmileSection, SviInterpolatedSmileSection, ZabrInterpolatedSmileSection< Evaluation >, Instrument, BondForward, RendistatoCalculator, CompositeInstrument, Forward, ForwardRateAgreement, OvernightIndexFuture, Stock, Fdm1DimSolver, Fdm2dBlackScholesSolver, Fdm2DimSolver, Fdm3DimSolver, FdmBatesSolver, FdmBlackScholesSolver, FdmCIRSolver, FdmG2Solver, FdmHestonHullWhiteSolver, FdmHestonSolver, FdmHullWhiteSolver, FdmNdimSolver< N >, FdmSimple2dBSSolver, LocalVolRNDCalculator, BlackCalibrationHelper, HestonModelHelper, HestonSLVFDMModel, HestonSLVMCModel, CapHelper, SwaptionHelper, Gaussian1dModel, Gsr, MarkovFunctional, EurodollarFuturesImpliedStdDevQuote, ForwardSwapQuote, ImpliedStdDevQuote, PiecewiseDefaultCurve< Traits, Interpolator, Bootstrap >, PiecewiseYoYInflationCurve< Interpolator, Bootstrap, Traits >, PiecewiseZeroInflationCurve< Interpolator, Bootstrap, Traits >, CapFloorTermVolCurve, CapFloorTermVolSurface, AndreasenHugeVolatilityInterpl, InterpolatedSmileSection< Interpolator >, OptionletStripper1, OptionletStripper2, StrippedOptionlet, StrippedOptionletAdapter, SabrInterpolatedSmileSection, CmsMarket, InterpolatedSwaptionVolatilityCube, XabrSwaptionVolatilityCube< Model >, SwaptionVolatilityCube, SwaptionVolatilityDiscrete, SwaptionVolatilityMatrix, FittedBondDiscountCurve, FlatForward, and PiecewiseYieldCurve< Traits, Interpolator, Bootstrap >.

+ Here is the caller graph for this function:

◆ forwardFirstNotificationOnly()

void forwardFirstNotificationOnly ( )

This method causes the object to forward the first notification received, and discard the others until recalculated; the rationale is that observers were already notified, and don't need further notifications until they recalculate, at which point this object would be recalculated too. After recalculation, this object would again forward the first notification received.

Although not always correct, this behavior is a lot faster and thus is the current default. The default can be changed at compile time, or at at run time by calling LazyObject::Defaults::instance().alwaysForwardNotifications(); the run-time change won't affect lazy objects already created.

Definition at line 245 of file lazyobject.hpp.

◆ alwaysForwardNotifications()

void alwaysForwardNotifications ( )

This method causes the object to forward all notifications received.

Although safer, this behavior is a lot slower and thus usually not the default. The default can be changed at compile time, or at run-time by calling LazyObject::Defaults::instance().alwaysForwardNotifications(); the run-time change won't affect lazy objects already created.

Definition at line 249 of file lazyobject.hpp.

Member Data Documentation

◆ calculated_

bool calculated_ = false
mutableprotected

Definition at line 129 of file lazyobject.hpp.

◆ frozen_

bool frozen_ = false
protected

Definition at line 129 of file lazyobject.hpp.

◆ alwaysForward_

bool alwaysForward_
protected

Definition at line 129 of file lazyobject.hpp.

◆ updating_

bool updating_ = false
private

Definition at line 131 of file lazyobject.hpp.