File: chapter-refptr.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (117 lines) | stat: -rw-r--r-- 5,015 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Appendix A. The RefPtr smartpointer</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="index.html" title="Programming with gtkmm 4">
<link rel="prev" href="chapter-contributing.html" title="Chapter 32. Contributing">
<link rel="next" href="sec-refptr-dereferencing.html" title="Dereferencing">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Appendix A. The RefPtr smartpointer</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="chapter-contributing.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="sec-refptr-dereferencing.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="appendix">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-refptr"></a>Appendix A. The RefPtr smartpointer</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc">
<li><span class="section"><a href="chapter-refptr.html#sec-refptr-copying">Copying</a></span></li>
<li><span class="section"><a href="sec-refptr-dereferencing.html">Dereferencing</a></span></li>
<li><span class="section"><a href="sec-refptr-casting.html">Casting</a></span></li>
<li><span class="section"><a href="sec-refptr-checking-for-null.html">Checking for nullptr</a></span></li>
<li><span class="section"><a href="sec-refptr-constness.html">Constness</a></span></li>
</ul>
</div>


<p>
<code class="classname">Glib::RefPtr</code> is a smartpointer. Specifically, it is a
reference-counting smartpointer. You might be familiar with
<code class="classname">std::unique_ptr&lt;&gt;</code>
and <code class="classname">std::shared_ptr&lt;&gt;</code>, which are also smartpointers.
In <span class="application">gtkmm</span>-4.0 <code class="classname">Glib::RefPtr&lt;&gt;</code> is an alias for
<code class="classname">std::shared_ptr&lt;&gt;</code>,
which is reference-counting. <code class="classname">Glib::RefPtr&lt;&gt;</code> was introduced
long before there was a reference-counting smartpointer in the C++ Standard Library.
</p>

<p>
If you make your own <code class="classname">Glib::ObjectBase</code>-derived classes with
<code class="methodname">create()</code> methods that return a <code class="classname">Glib::RefPtr</code>,
you must use <code class="function">Glib::make_refptr_for_instance()</code> in your
<code class="methodname">create()</code> methods. This function creates a <code class="classname">std::shared_ptr</code>
with a special deleter, which handles the reference-count for the wrapped C object.
</p>

<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/glibmm/group__RefPtr.html" target="_top">Reference</a></p>

<p>A smartpointer acts much like a normal pointer. Here are a few examples.</p>

<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-refptr-copying"></a>Copying</h2></div></div></div>

<p>
You can copy <code class="classname">RefPtr</code>s, just like normal pointers. But
unlike normal pointers, you don't need to worry about deleting the underlying
instance.
</p>
<pre class="programlisting"><code class="code">auto refPixbuf = Gdk::Pixbuf::create_from_file(filename);
auto refPixbuf2 = refPixbuf;
</code></pre>
<p>
Of course this means that you can store <code class="classname">RefPtr</code>s in
standard containers, such as <code class="classname">std::vector</code> or
<code class="classname">std::list</code>.</p>
<pre class="programlisting"><code class="code">std::list&lt;Glib::RefPtr&lt;Gdk::Pixbuf&gt;&gt; listPixbufs;
auto refPixbuf = Gdk::Pixbuf::create_from_file(filename);
listPixbufs.push_back(refPixbuf);
</code></pre>
</div>









</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="chapter-contributing.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="sec-refptr-dereferencing.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 32. Contributing </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Dereferencing</td>
</tr>
</table>
</div>
</body>
</html>