1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2013 Chris Higgs
Copyright (C) 2015 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/patterns/observable.hpp>
#ifndef QL_ENABLE_THREAD_SAFE_OBSERVER_PATTERN
namespace QuantLib {
void ObservableSettings::enableUpdates() {
updatesEnabled_ = true;
updatesDeferred_ = false;
// if there are outstanding deferred updates, do the notification
if (!deferredObservers_.empty()) {
bool successful = true;
std::string errMsg;
for (auto* deferredObserver : deferredObservers_) {
try {
deferredObserver->update();
} catch (std::exception& e) {
successful = false;
errMsg = e.what();
} catch (...) {
successful = false;
}
}
deferredObservers_.clear();
QL_ENSURE(successful,
"could not notify one or more observers: " << errMsg);
}
}
void Observable::notifyObservers() {
if (!settings_.updatesEnabled()) {
// if updates are only deferred, flag this for later notification
// these are held centrally by the settings singleton
settings_.registerDeferredObservers(observers_);
} else if (!observers_.empty()) {
bool successful = true;
std::string errMsg;
for (auto* observer : observers_) {
try {
observer->update();
} catch (std::exception& e) {
// quite a dilemma. If we don't catch the exception,
// other observers will not receive the notification
// and might be left in an incorrect state. If we do
// catch it and continue the loop (as we do here) we
// lose the exception. The least evil might be to try
// and notify all observers, while raising an
// exception if something bad happened.
successful = false;
errMsg = e.what();
} catch (...) {
successful = false;
}
}
QL_ENSURE(successful,
"could not notify one or more observers: " << errMsg);
}
}
}
#else
#include <boost/signals2/signal_type.hpp>
namespace QuantLib {
namespace detail {
class Signal {
public:
typedef boost::signals2::signal_type<
void(),
boost::signals2::keywords::mutex_type<std::recursive_mutex> >
::type signal_type;
void connect(const signal_type::slot_type& slot) {
sig_.connect(slot);
}
template <class T>
void disconnect(const T& slot) {
sig_.disconnect(slot);
}
void operator()() const {
sig_();
}
private:
signal_type sig_;
};
template <class T>
class ProxyUpdater {
T* proxy_;
public:
explicit ProxyUpdater(const ext::shared_ptr<T>& observerProxy)
: proxy_(observerProxy.get()) {}
void operator()() const {
proxy_->update();
}
bool operator==(const ProxyUpdater<T>& other) const {
return proxy_ == other.proxy_;
}
bool operator!=(const ProxyUpdater<T>& other) const {
return proxy_ != other.proxy_;
}
};
}
void Observable::registerObserver(const ext::shared_ptr<Observer::Proxy>& observerProxy) {
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
observers_.insert(observerProxy);
}
detail::Signal::signal_type::slot_type slot {detail::ProxyUpdater<Observer::Proxy>(observerProxy)};
#if defined(QL_USE_STD_SHARED_PTR)
sig_->connect(slot.track_foreign(observerProxy));
#else
sig_->connect(slot.track(observerProxy));
#endif
}
void Observable::unregisterObserver(const ext::shared_ptr<Observer::Proxy>& observerProxy,
bool disconnect) {
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
observers_.erase(observerProxy);
}
if (settings_.updatesDeferred()) {
std::lock_guard<std::mutex> sLock(settings_.mutex_);
if (settings_.updatesDeferred()) {
settings_.unregisterDeferredObserver(observerProxy);
}
}
if (disconnect) {
sig_->disconnect(detail::ProxyUpdater<Observer::Proxy>(observerProxy));
}
}
void Observable::notifyObservers() {
if (settings_.updatesEnabled()) {
return (*sig_)();
}
std::lock_guard<std::mutex> sLock(settings_.mutex_);
if (settings_.updatesEnabled()) {
return (*sig_)();
}
else if (settings_.updatesDeferred()) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
// if updates are only deferred, flag this for later notification
// these are held centrally by the settings singleton
settings_.registerDeferredObservers(observers_);
}
}
Observable::Observable()
: sig_(new detail::Signal()),
settings_(ObservableSettings::instance()) { }
Observable::Observable(const Observable&)
: sig_(new detail::Signal()),
settings_(ObservableSettings::instance()) {
// the observer set is not copied; no observer asked to
// register with this object
}
}
#endif
|