File: sec-basics-gobj-and-wrap.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 (96 lines) | stat: -rw-r--r-- 4,802 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
<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>Mixing C and C++ APIs</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="chapter-basics.html" title="Chapter 3. Basics">
<link rel="prev" href="sec-basics-ustring.html" title="Glib::ustring">
<link rel="next" href="sec-helloworld.html" title="Hello World in gtkmm">
</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">Mixing C and C++ APIs</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-basics-ustring.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 3. Basics</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-helloworld.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-basics-gobj-and-wrap"></a>Mixing C and C++ APIs</h2></div></div></div>


<p>You can use C APIs which do not yet have convenient C++ interfaces.
It is generally not a problem to use C APIs from C++, and <span class="application">gtkmm</span> helps by
providing access to the underlying C object, and providing an easy way to create
a C++ wrapper object from a C object, provided that the C API is also based on
the <code class="classname">GObject</code> system.</p>

<p>
To use a <span class="application">gtkmm</span> instance with a C function that requires a C
<code class="classname">GObject</code> instance, use the C++ instance’s
<code class="function">gobj()</code> function to obtain a pointer to the underlying C
instance. For example:</p>
<pre class="programlisting"><code class="code">Gtk::Button button("example");
gtk_button_do_something_that_gtkmm_cannot(button.gobj());
</code></pre>

<p>
To obtain a <span class="application">gtkmm</span> instance from a C <code class="classname">GObject</code> instance,
use one of the many overloaded <code class="function">Glib::wrap()</code> functions.
The C instance’s reference count is not incremented, unless you set the optional
<em class="parameter"><code>take_copy</code></em> argument to <code class="literal">true</code>. For
example:</p>
<pre class="programlisting"><code class="code">GtkButton* cbutton = get_a_button();
Gtk::Button* button = Glib::wrap(cbutton);
button-&gt;set_label("Now I speak C++ too!");
</code></pre>
<p>The C++ wrapper shall be explicitly deleted if
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>it's a widget or other class that inherits from <code class="classname">Gtk::Object</code>, and</p></li>
<li class="listitem"><p>the C instance has a floating reference when the wrapper is created, and</p></li>
<li class="listitem"><p><code class="function">Gtk::manage()</code> has not been called on it (which includes if it was created with <code class="function">Gtk::make_managed()</code>), or</p></li>
<li class="listitem"><p><code class="function">Gtk::manage()</code> was called on it, but it was never added to a parent.</p></li>
</ul></div>
<p>
<code class="function">Glib::wrap()</code> binds the C and C++ instances to each other.
Don't delete the C++ instance before you want the C instance to die.
</p>
<p>In all other cases the C++ instance is automatically deleted when the last reference
to the C instance is dropped. This includes all <code class="function">Glib::wrap()</code>
overloads that return a <code class="classname">Glib::RefPtr</code>.</p>

</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-basics-ustring.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-basics.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-helloworld.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Glib::ustring </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"> Hello World in <span class="application">gtkmm</span>
</td>
</tr>
</table>
</div>
</body>
</html>