Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
weakptr.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2#ifndef INKSCAPE_OBJECT_WEAKPTR_H
3#define INKSCAPE_OBJECT_WEAKPTR_H
4
5#include <sigc++/connection.h>
6
7namespace Inkscape {
8
12template <typename T>
13class SPWeakPtr final
14{
15public:
16 SPWeakPtr() = default;
17 explicit SPWeakPtr(T *obj) : _obj(obj) { attach(); }
18 SPWeakPtr &operator=(T *obj) { reset(obj); return *this; }
19 SPWeakPtr(SPWeakPtr const &other) : SPWeakPtr(other._obj) {}
20 SPWeakPtr &operator=(SPWeakPtr const &other) { reset(other._obj); return *this; }
22
23 void reset() { detach(); _obj = nullptr; }
24 void reset(T *obj) { detach(); _obj = obj; attach(); }
25 explicit operator bool() const { return _obj; }
26 T *get() const { return _obj; }
27 T &operator*() const { return *_obj; }
28 T *operator->() const { return _obj; }
29
30private:
31 T *_obj = nullptr;
32 sigc::connection _conn;
33
34 void attach() { if (_obj) _conn = _obj->connectRelease([this] (auto) { _conn.disconnect(); _obj = nullptr; }); }
35 void detach() { if (_obj) _conn.disconnect(); }
36};
37
38} // namespace Inkscape
39
40#endif // INKSCAPE_OBJECT_WEAKPTR_H
A weak pointer to an SPObject: it nulls itself upon the object's destruction.
Definition weakptr.h:14
T * operator->() const
Definition weakptr.h:28
SPWeakPtr & operator=(T *obj)
Definition weakptr.h:18
SPWeakPtr & operator=(SPWeakPtr const &other)
Definition weakptr.h:20
SPWeakPtr(SPWeakPtr const &other)
Definition weakptr.h:19
T * get() const
Definition weakptr.h:26
sigc::connection _conn
Definition weakptr.h:32
T & operator*() const
Definition weakptr.h:27
SPWeakPtr(T *obj)
Definition weakptr.h:17
void reset(T *obj)
Definition weakptr.h:24
Helper class to stream background task notifications as a series of messages.