Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--enable-thread-safe-observer-pattern works with --enable-std-pointers #549

Merged
merged 3 commits into from Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -129,7 +129,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