QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
handle.hpp
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 Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_handle_hpp
26#define quantlib_handle_hpp
27
28#include <ql/patterns/observable.hpp>
29
30namespace QuantLib {
31
33
40 template <class T>
41 class Handle {
42 protected:
43 class Link : public Observable, public Observer {
44 public:
45 explicit Link(const ext::shared_ptr<T>& h,
46 bool registerAsObserver);
47 void linkTo(const ext::shared_ptr<T>&,
48 bool registerAsObserver);
49 bool empty() const { return !h_; }
50 const ext::shared_ptr<T>& currentLink() const { return h_; }
51 void update() override { notifyObservers(); }
52
53 private:
54 ext::shared_ptr<T> h_;
55 bool isObserver_ = false;
56 };
57 ext::shared_ptr<Link> link_;
58 public:
77 : Handle(ext::shared_ptr<T>()) {}
78 explicit Handle(const ext::shared_ptr<T>& p,
79 bool registerAsObserver = true)
80 : link_(new Link(p,registerAsObserver)) {}
82
83 const ext::shared_ptr<T>& currentLink() const;
84 const ext::shared_ptr<T>& operator->() const;
85 const ext::shared_ptr<T>& operator*() const;
87 bool empty() const;
89 operator ext::shared_ptr<Observable>() const;
91 template <class U>
92 bool operator==(const Handle<U>& other) const { return link_==other.link_; }
94 template <class U>
95 bool operator!=(const Handle<U>& other) const { return link_!=other.link_; }
97 template <class U>
98 bool operator<(const Handle<U>& other) const { return link_ < other.link_; }
99 };
100
102
111 template <class T>
112 class RelinkableHandle : public Handle<T> {
113 public:
115 const ext::shared_ptr<T>& p = ext::shared_ptr<T>(),
116 bool registerAsObserver = true);
118 T* p,
119 bool registerAsObserver = true);
120 void linkTo(const ext::shared_ptr<T>&,
121 bool registerAsObserver = true);
122 };
123
124
125 // inline definitions
126
127 template <class T>
128 inline Handle<T>::Link::Link(const ext::shared_ptr<T>& h, bool registerAsObserver) {
129 linkTo(h,registerAsObserver);
130 }
131
132 template <class T>
133 inline void Handle<T>::Link::linkTo(const ext::shared_ptr<T>& h,
134 bool registerAsObserver) {
135 if ((h != h_) || (isObserver_ != registerAsObserver)) {
136 if (h_ && isObserver_)
137 unregisterWith(h_);
138 h_ = h;
139 isObserver_ = registerAsObserver;
140 if (h_ && isObserver_)
141 registerWith(h_);
142 notifyObservers();
143 }
144 }
145
146
147 template <class T>
148 inline const ext::shared_ptr<T>& Handle<T>::currentLink() const {
149 QL_REQUIRE(!empty(), "empty Handle cannot be dereferenced");
150 return link_->currentLink();
151 }
152
153 template <class T>
154 inline const ext::shared_ptr<T>& Handle<T>::operator->() const {
155 QL_REQUIRE(!empty(), "empty Handle cannot be dereferenced");
156 return link_->currentLink();
157 }
158
159 template <class T>
160 inline const ext::shared_ptr<T>& Handle<T>::operator*() const {
161 QL_REQUIRE(!empty(), "empty Handle cannot be dereferenced");
162 return link_->currentLink();
163 }
164
165 template <class T>
166 inline bool Handle<T>::empty() const {
167 return link_->empty();
168 }
169
170 template <class T>
171 inline Handle<T>::operator ext::shared_ptr<Observable>() const {
172 return link_;
173 }
174
175
176 template <class T>
177 inline RelinkableHandle<T>::RelinkableHandle(const ext::shared_ptr<T>& p,
178 bool registerAsObserver)
179 : Handle<T>(p,registerAsObserver) {}
180
181 template <class T>
183 bool registerAsObserver)
184 : Handle<T>(p,registerAsObserver) {}
185
186 template <class T>
187 inline void RelinkableHandle<T>::linkTo(const ext::shared_ptr<T>& h,
188 bool registerAsObserver) {
189 this->link_->linkTo(h,registerAsObserver);
190 }
191
192}
193
194#endif
Shared handle to an observable.
Definition: handle.hpp:41
const ext::shared_ptr< T > & operator*() const
Definition: handle.hpp:160
Handle(const ext::shared_ptr< T > &p, bool registerAsObserver=true)
Definition: handle.hpp:78
bool operator<(const Handle< U > &other) const
strict weak ordering
Definition: handle.hpp:98
const ext::shared_ptr< T > & operator->() const
Definition: handle.hpp:154
bool operator==(const Handle< U > &other) const
equality test
Definition: handle.hpp:92
bool operator!=(const Handle< U > &other) const
disequality test
Definition: handle.hpp:95
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:166
const ext::shared_ptr< T > & currentLink() const
dereferencing
Definition: handle.hpp:148
ext::shared_ptr< Link > link_
Definition: handle.hpp:57
Object that notifies its changes to a set of observers.
Definition: observable.hpp:62
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
Relinkable handle to an observable.
Definition: handle.hpp:112
RelinkableHandle(T *p, bool registerAsObserver=true)
Definition: handle.hpp:182
RelinkableHandle(const ext::shared_ptr< T > &p=ext::shared_ptr< T >(), bool registerAsObserver=true)
Definition: handle.hpp:177
void linkTo(const ext::shared_ptr< T > &, bool registerAsObserver=true)
Definition: handle.hpp:187
Definition: any.hpp:35