1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Boilerplate code: GObject Reference Manual</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="index.html" title="GObject Reference Manual">
8 <link rel="up" href="howto-gobject.html" title="How to define and implement a new GObject">
9 <link rel="prev" href="howto-gobject.html" title="How to define and implement a new GObject">
10 <link rel="next" href="howto-gobject-construction.html" title="Object construction">
11 <meta name="generator" content="GTK-Doc V1.25.1 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
16 <td width="100%" align="left" class="shortcuts"></td>
17 <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
18 <td><a accesskey="u" href="howto-gobject.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
19 <td><a accesskey="p" href="howto-gobject.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
20 <td><a accesskey="n" href="howto-gobject-construction.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="howto-gobject-code"></a>Boilerplate code</h2></div></div></div>
26 In your code, the first step is to <code class="function">#include</code> the
29 <div class="informalexample">
30 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
33 <td class="listing_lines" align="right"><pre>1
48 <td class="listing_code"><pre class="programlisting"><span class="comment">/*</span>
49 <span class="comment"> * Copyright information</span>
50 <span class="comment"> */</span>
52 <span class="gtkdoc ppc">#include</span> <span class="gtkdoc pps">"viewer-file.h"</span><span class="gtkdoc ppc"></span>
54 <span class="comment">/* Private structure definition. */</span>
55 <span class="gtkdoc kwc">typedef</span> <span class="gtkdoc kwb">struct</span> <span class="gtkdoc opt">{</span>
56 gchar <span class="gtkdoc opt">*</span>filename<span class="gtkdoc opt">;</span>
57 <span class="comment">/* stuff */</span>
58 <span class="gtkdoc opt">}</span> ViewerFilePrivate<span class="gtkdoc opt">;</span>
60 <span class="comment">/* </span>
61 <span class="comment"> * forward definitions</span>
62 <span class="comment"> */</span></pre></td>
71 If the class is being declared as final using
72 <code class="function">G_DECLARE_FINAL_TYPE</code>, its instance structure should
73 be defined in the C file:
75 <div class="informalexample">
76 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
79 <td class="listing_lines" align="right"><pre>1
85 <td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">struct</span> _ViewerFile
86 <span class="gtkdoc opt">{</span>
87 GObject parent_instance<span class="gtkdoc opt">;</span>
89 <span class="comment">/* Other members, including private data. */</span>
90 <span class="gtkdoc opt">}</span></pre></td>
99 Call the <code class="function">G_DEFINE_TYPE</code> macro (or
100 <code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code> if your class needs
101 private data — final types do <span class="emphasis"><em>not</em></span> need private data)
103 of the type, the prefix of the functions and the parent GType to
104 reduce the amount of boilerplate needed. This macro will:
107 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
108 <li class="listitem">implement the <code class="function">viewer_file_get_type</code>
110 <li class="listitem">define a parent class pointer accessible from
111 the whole .c file</li>
112 <li class="listitem">add private instance data to the type (if using
113 <code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code>)</li>
118 If the class has been declared as final using
119 <code class="function">G_DECLARE_FINAL_TYPE</code> (see
120 <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
121 the instance structure, <span class="type">ViewerFile</span>, and
122 <code class="function">G_DEFINE_TYPE</code> should be used instead of
123 <code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code>. The instance structure
124 for a final class is not exposed publicly, and is not embedded in the
125 instance structures of any derived classes (because the class is final);
126 so its size can vary without causing incompatibilities for code which uses
127 the class. Conversely, private data for derivable classes
128 <span class="emphasis"><em>must</em></span> be included in a private structure, and
129 <code class="function">G_DEFINE_TYPE_WITH_PRIVATE</code> must be used.
132 <div class="informalexample">
133 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
136 <td class="listing_lines" align="right"><pre>1</pre></td>
137 <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="gtkdoc opt">(</span>ViewerFile<span class="gtkdoc opt">,</span> viewer_file<span class="gtkdoc opt">,</span> G_TYPE_OBJECT<span class="gtkdoc opt">)</span></pre></td>
146 <div class="informalexample">
147 <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
150 <td class="listing_lines" align="right"><pre>1</pre></td>
151 <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="gtkdoc opt">(</span>ViewerFile<span class="gtkdoc opt">,</span> viewer_file<span class="gtkdoc opt">,</span> G_TYPE_OBJECT<span class="gtkdoc opt">)</span></pre></td>
160 It is also possible to use the
161 <code class="function">G_DEFINE_TYPE_WITH_CODE</code> macro to control the
162 <code class="function">get_type</code> function implementation — for instance, to
163 add a call to the <code class="function">G_IMPLEMENT_INTERFACE</code> macro to
164 implement an interface.
168 <hr>Generated by GTK-Doc V1.25.1</div>