QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
derivedquote.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Ferdinando Ametrano
5 Copyright (C) 2006 François du Vignaud
6 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file derivedquote.hpp
23 \brief market quote whose value depends on another quote
24*/
25
26#ifndef quantlib_derived_quote_hpp
27#define quantlib_derived_quote_hpp
28
29#include <ql/handle.hpp>
30#include <ql/quote.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35 //! market quote whose value depends on another quote
36 /*! \test the correctness of the returned values is tested by
37 checking them against numerical calculations.
38 */
39 template <class UnaryFunction>
40 class DerivedQuote : public Quote, public Observer {
41 public:
42 DerivedQuote(Handle<Quote> element, const UnaryFunction& f);
43 //! \name Quote interface
44 //@{
45 Real value() const override;
46 bool isValid() const override;
47 //@}
48 //! \name Observer interface
49 //@{
50 void update() override;
51 //@}
52 private:
54 UnaryFunction f_;
55 };
56
57 //! creator method
58 template <class UnaryFunction>
60 const UnaryFunction& f) {
61 return DerivedQuote<UnaryFunction>(element, f);
62 }
63
64 // inline definitions
65 template <class UnaryFunction>
66 inline DerivedQuote<UnaryFunction>::DerivedQuote(Handle<Quote> element, const UnaryFunction& f)
67 : element_(std::move(element)), f_(f) {
69 }
70
71 template <class UnaryFunction>
73 QL_ENSURE(isValid(), "invalid DerivedQuote");
74 return f_(element_->value());
75 }
76
77 template <class UnaryFunction>
79 return !element_.empty() && element_->isValid();
80 }
81
82 template <class UnaryFunction>
84 notifyObservers();
85 }
86
87}
88
89#endif
market quote whose value depends on another quote
void update() override
DerivedQuote(Handle< Quote > element, const UnaryFunction &f)
Handle< Quote > element_
Real value() const override
returns the current value
bool isValid() const override
returns true if the Quote holds a valid value
Shared handle to an observable.
Definition: handle.hpp:41
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
purely virtual base class for market observables
Definition: quote.hpp:37
ext::function< Real(Real)> f_
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
QL_REAL Real
real number
Definition: types.hpp:50
Globally accessible relinkable pointer.
Definition: any.hpp:35
DerivedQuote< UnaryFunction > makeDerivedQuote(const Handle< Quote > &element, const UnaryFunction &f)
creator method
STL namespace.
purely virtual base class for market observables