Skip to content

Commit

Permalink
Merge pull request #549.
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Oct 12, 2018
2 parents b3326a0 + 9dd4c1e commit 01bece2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion configure.ac
Expand Up @@ -261,7 +261,9 @@ AC_ARG_ENABLE([thread-safe-observer-pattern],
enable it if you want to use QuantLib
via the SWIG layer within the JVM or .NET
eco system or any environment with an
async garbage collector.]),
async garbage collector. C++-17 is required
if this option is used together with
--enable-std-pointers]),
[ql_use_tsop=$enableval],
[ql_use_tsop=no])
AC_MSG_RESULT([$ql_use_tsop])
Expand Down
4 changes: 4 additions & 0 deletions ql/patterns/observable.cpp
Expand Up @@ -130,7 +130,11 @@ namespace QuantLib {

detail::Signal::signal_type::slot_type slot(&Observer::Proxy::update,
observerProxy.get());
#if defined(QL_USE_STD_SHARED_PTR)
sig_->connect(slot.track_foreign(observerProxy));
#else
sig_->connect(slot.track(observerProxy));
#endif
}

void Observable::unregisterObserver(
Expand Down
13 changes: 9 additions & 4 deletions ql/patterns/observable.hpp
Expand Up @@ -261,17 +261,17 @@ namespace QuantLib {
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include <boost/smart_ptr/owner_less.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <set>

namespace QuantLib {


namespace QuantLib {
class Observable;
class ObservableSettings;

//! Object that gets notified when a given observable changes
/*! \ingroup patterns */
class Observer : public boost::enable_shared_from_this<Observer> {
class Observer : public ext::enable_shared_from_this<Observer> {
friend class Observable;
friend class ObservableSettings;
public:
Expand Down Expand Up @@ -318,9 +318,14 @@ namespace QuantLib {
void update() const {
boost::lock_guard<boost::recursive_mutex> lock(mutex_);
if (active_) {
// c++17 is required if used with std::shared_ptr<T>
const ext::weak_ptr<Observer> o
= observer_->weak_from_this();
if (!o._empty()) {

//check for empty weak reference
//https://stackoverflow.com/questions/45507041/how-to-check-if-weak-ptr-is-empty-non-assigned
const ext::weak_ptr<Observer> empty;
if (o.owner_before(empty) || empty.owner_before(o)) {
const ext::shared_ptr<Observer> obs(o.lock());
if (obs)
obs->update();
Expand Down
3 changes: 3 additions & 0 deletions ql/shared_ptr.hpp
Expand Up @@ -31,6 +31,7 @@
#else
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/enable_shared_from_this.hpp>
#endif

namespace QuantLib {
Expand All @@ -43,12 +44,14 @@ namespace QuantLib {
using std::make_shared;
using std::static_pointer_cast;
using std::dynamic_pointer_cast;
using std::enable_shared_from_this;
#else
using boost::shared_ptr;
using boost::weak_ptr;
using boost::make_shared;
using boost::static_pointer_cast;
using boost::dynamic_pointer_cast;
using boost::enable_shared_from_this;
#endif

}
Expand Down

0 comments on commit 01bece2

Please sign in to comment.