QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
compositequote.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) 2000, 2001, 2002, 2003 RiskMap srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file compositequote.hpp
21 \brief purely virtual base class for market observables
22*/
23
24#ifndef quantlib_composite_quote_hpp
25#define quantlib_composite_quote_hpp
26
27#include <ql/errors.hpp>
28#include <ql/handle.hpp>
29#include <ql/quote.hpp>
30#include <ql/types.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35 //! market element whose value depends on two other market element
36 /*! \test the correctness of the returned values is tested by
37 checking them against numerical calculations.
38 */
39 template <class BinaryFunction>
40 class CompositeQuote : public Quote, public Observer {
41 public:
42 CompositeQuote(Handle<Quote> element1, Handle<Quote> element2, const BinaryFunction& f);
43 //! \name inspectors
44 //@{
45 Real value1() const { return element1_->value(); }
46 Real value2() const { return element2_->value(); }
47 //@}
48 //! \name Quote interface
49 //@{
50 Real value() const override;
51 bool isValid() const override;
52 //@}
53 //! \name Observer interface
54 //@{
55 void update() override;
56 //@}
57 private:
59 BinaryFunction f_;
60 };
61
62 //! creator method
63 template <class BinaryFunction>
65 const Handle<Quote>& element2,
66 const BinaryFunction& f) {
67 return CompositeQuote<BinaryFunction>(element1, element2, f);
68 }
69
70 // inline definitions
71
72 template <class BinaryFunction>
74 Handle<Quote> element2,
75 const BinaryFunction& f)
76 : element1_(std::move(element1)), element2_(std::move(element2)), f_(f) {
79 }
80
81 template <class BinaryFunction>
83 QL_ENSURE(isValid(), "invalid CompositeQuote");
84 return f_(element1_->value(),element2_->value());
85 }
86
87 template <class BinaryFunction>
89 return !element1_.empty() && !element2_.empty() &&
90 element1_->isValid() && element2_->isValid();
91 }
92
93 template <class BinaryFunction>
95 notifyObservers();
96 }
97
98}
99
100#endif
market element whose value depends on two other market element
CompositeQuote(Handle< Quote > element1, Handle< Quote > element2, const BinaryFunction &f)
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_
Classes and functions for error handling.
#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
CompositeQuote< BinaryFunction > makeCompositeQuote(const Handle< Quote > &element1, const Handle< Quote > &element2, const BinaryFunction &f)
creator method
STL namespace.
purely virtual base class for market observables
Custom types.