blob: 93358d8252d6263ef23e12de492d3010bb5c504e [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Boilerplate code: GObject Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GObject Reference Manual">
<link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject">
<link rel="prev" href="howto-gobject.html" title="How to define and implement a new GObject">
<link rel="next" href="howto-gobject-construction.html" title="Object construction">
<meta name="generator" content="GTK-Doc V1.25.1 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="howto-gobject.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="howto-gobject.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="howto-gobject-construction.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="howto-gobject-code"></a>Boilerplate code</h2></div></div></div>
<p>
In your code, the first step is to <code class="function">#include</code> the
needed headers:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="comment">/*</span>
<span class="comment"> * Copyright information</span>
<span class="comment"> */</span>
<span class="preproc">#include</span><span class="normal"> </span><span class="string">"viewer-file.h"</span>
<span class="comment">/* Private structure definition. */</span>
<span class="keyword">typedef</span><span class="normal"> </span><span class="keyword">struct</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal"> </span><span class="usertype">gchar</span><span class="normal"> </span><span class="symbol">*</span><span class="normal">filename</span><span class="symbol">;</span>
<span class="normal"> </span><span class="comment">/* stuff */</span>
<span class="cbracket">}</span><span class="normal"> ViewerFilePrivate</span><span class="symbol">;</span>
<span class="comment">/* </span>
<span class="comment"> * forward definitions</span>
<span class="comment"> */</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
If the class is being declared as final using
<code class="function">G_DECLARE_FINAL_TYPE</code>, its instance structure should
be defined in the C file:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="keyword">struct</span><span class="normal"> </span><span class="classname">_ViewerFile</span>
<span class="cbracket">{</span>
<span class="normal"> </span><span class="usertype">GObject</span><span class="normal"> parent_instance</span><span class="symbol">;</span>
<span class="normal"> </span><span class="comment">/* Other members, including private data. */</span>
<span class="cbracket">}</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
Call the <code class="function">G_DEFINE_TYPE</code> macro (or
<code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code> if your class needs
private data — final types do <span class="emphasis"><em>not</em></span> need private data)
using the name
of the type, the prefix of the functions and the parent GType to
reduce the amount of boilerplate needed. This macro will:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">implement the <code class="function">viewer_file_get_type</code>
function</li>
<li class="listitem">define a parent class pointer accessible from
the whole .c file</li>
<li class="listitem">add private instance data to the type (if using
<code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code>)</li>
</ul></div>
<p>
</p>
<p>
If the class has been declared as final using
<code class="function">G_DECLARE_FINAL_TYPE</code> (see
<a class="xref" href="howto-gobject.html#howto-gobject-header" title="Boilerplate header code">the section called “Boilerplate header code”</a>), private data should be placed in
the instance structure, <span class="type">ViewerFile</span>, and
<code class="function">G_DEFINE_TYPE</code> should be used instead of
<code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code>. The instance structure
for a final class is not exposed publicly, and is not embedded in the
instance structures of any derived classes (because the class is final);
so its size can vary without causing incompatibilities for code which uses
the class. Conversely, private data for derivable classes
<span class="emphasis"><em>must</em></span> be included in a private structure, and
<code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code> must be used.
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="function"><a href="gobject-Type-Information.html#G-DEFINE-TYPE:CAPS">G_DEFINE_TYPE</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ViewerFile</span><span class="symbol">,</span><span class="normal"> viewer_file</span><span class="symbol">,</span><span class="normal"> <a href="gobject-Type-Information.html#G-TYPE-OBJECT:CAPS">G_TYPE_OBJECT</a></span><span class="symbol">)</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
or
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="function"><a href="gobject-Type-Information.html#G-DEFINE-TYPE-WITH-PRIVATE:CAPS">G_DEFINE_TYPE_WITH_PRIVATE</a></span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ViewerFile</span><span class="symbol">,</span><span class="normal"> viewer_file</span><span class="symbol">,</span><span class="normal"> <a href="gobject-Type-Information.html#G-TYPE-OBJECT:CAPS">G_TYPE_OBJECT</a></span><span class="symbol">)</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
It is also possible to use the
<code class="function">G_DEFINE_TYPE_WITH_CODE</code> macro to control the
<code class="function">get_type</code> function implementation — for instance, to
add a call to the <code class="function">G_IMPLEMENT_INTERFACE</code> macro to
implement an interface.
</p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.25.1</div>
</body>
</html>