blob: 38db7e73a9d39a68707a94b8dfad51df3bfc166e (
plain)
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
|
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef INKSCAPE_OBJECT_WEAKPTR_H
#define INKSCAPE_OBJECT_WEAKPTR_H
#include <sigc++/connection.h>
namespace Inkscape {
/**
* A weak pointer to an SPObject: it nulls itself upon the object's destruction.
*/
template <typename T>
class SPWeakPtr final
{
public:
SPWeakPtr() = default;
explicit SPWeakPtr(T *obj) : _obj(obj) { attach(); }
SPWeakPtr &operator=(T *obj) { reset(obj); return *this; }
SPWeakPtr(SPWeakPtr const &other) : SPWeakPtr(other._obj) {}
SPWeakPtr &operator=(SPWeakPtr const &other) { reset(other._obj); return *this; }
~SPWeakPtr() { detach(); }
void reset() { detach(); _obj = nullptr; }
void reset(T *obj) { detach(); _obj = obj; attach(); }
explicit operator bool() const { return _obj; }
T *get() const { return _obj; }
T &operator*() const { return *_obj; }
T *operator->() const { return _obj; }
private:
T *_obj = nullptr;
sigc::connection _conn;
void attach() { if (_obj) _conn = _obj->connectRelease([this] (auto) { _conn.disconnect(); _obj = nullptr; }); }
void detach() { if (_obj) _conn.disconnect(); }
};
} // namespace Inkscape
#endif // INKSCAPE_OBJECT_WEAKPTR_H
|