lyx_mirror/src/support/signals.h
Guillaume MM db58111360 Properly track the lifetime of signals2::slots (#8261)
Starting at 61b2bd5e, boost::bind was progressively replaced with
std::bind. They are not interchangeable though. boost::bind implements
the tracking of boost::signals{,2}::trackable objects. Now that
std::bind has completely replaced boost::bind, tracking never occurred.

This commit replaces boost::signals2::trackable with the new preferred
boost::signals2 methods: scoped_connections or slot::track_foreign. The
support::Trackable class introduced is less safe but easier for transitioning
old code.

Fixes the crash at #8261.
2017-06-11 19:51:17 +02:00

50 lines
1.4 KiB
C++

// -*- C++ -*-
/**
* \file signals.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Guillaume Munch
*
* Full author contact details are available in file CREDITS.
*/
#ifndef LYX_SIGNALS_H
#define LYX_SIGNALS_H
#include "boost/signals2.hpp"
#include <memory>
namespace lyx {
namespace signals2 = ::boost::signals2;
namespace support {
/// A small utility to use with signals2::slot_type::track_foreign when the
/// parent object is not handled by a shared_ptr, or to track the lifetime of an
/// object. Using Trackable to track lifetimes is less thread-safe than tracking
/// their parents directly with a shared_ptr as recommended by signals2, but it
/// makes it easier for transitioning old code. (Essentially because Trackable
/// will not prevent the deletion of the parent by a concurrent thread.)
class Trackable {
public:
Trackable() : p_(std::make_shared<int>(0)) {}
Trackable(Trackable const &) : Trackable() {}
Trackable(Trackable &&) : Trackable() {}
Trackable & operator=(Trackable const &) { return *this; }
Trackable & operator=(Trackable &&) { return *this; }
// This weak pointer lets you know if the parent object has been destroyed
std::weak_ptr<void> p() const { return p_; }
private:
std::shared_ptr<void> const p_;
};
} // namespace support
} // namespace lyx
#endif