Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
gobjectptr.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6#ifndef INKSCAPE_UTIL_GOBJECTPTR_H
7#define INKSCAPE_UTIL_GOBJECTPTR_H
8
9#include <glib.h>
10
11namespace Inkscape::Util {
12
16template <typename T>
18{
19public:
20 GObjectPtr() = default;
21 explicit GObjectPtr(T *p, bool add_ref = false) : _p(p) { if (add_ref) _ref(); }
22 GObjectPtr(GObjectPtr const &other) : _p(other._p) { _ref(); }
23 GObjectPtr &operator=(GObjectPtr const &other) { if (&other != this) { _unref(); _p = other._p; _ref(); } return *this; }
24 GObjectPtr(GObjectPtr &&other) noexcept : _p(other._p) { other._p = nullptr; }
25 GObjectPtr &operator=(GObjectPtr &&other) { if (&other != this) { _unref(); _p = other._p; other._p = nullptr; } return *this; }
27
28 void reset() { _unref(); _p = nullptr; }
29 explicit operator bool() const { return _p; }
30 T *get() const { return _p; }
31 T &operator*() const { return *_p; }
32 T *operator->() const { return _p; }
33
34private:
35 T *_p = nullptr;
36
37 void _ref() { if (_p) g_object_ref(_p); }
38 void _unref() { if (_p) g_object_unref(_p); }
39};
40
41} // namespace Inkscape::Util
42
43#endif // INKSCAPE_UTIL_GOBJECTPTR_H
A smart pointer that shares ownership of a GObject.
Definition gobjectptr.h:18
GObjectPtr(T *p, bool add_ref=false)
Definition gobjectptr.h:21
GObjectPtr & operator=(GObjectPtr const &other)
Definition gobjectptr.h:23
GObjectPtr(GObjectPtr const &other)
Definition gobjectptr.h:22
GObjectPtr(GObjectPtr &&other) noexcept
Definition gobjectptr.h:24
GObjectPtr & operator=(GObjectPtr &&other)
Definition gobjectptr.h:25
Miscellaneous supporting code.
Definition document.h:93