?? cha-glib.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> glib: Portability and Utility </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="Overview" href="overview.html"> <link rel="PREVIOUS" title="Structure of the Book" href= "z22.html"> <link rel="NEXT" title="Data Structures" href="z29.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z22.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="z29.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> </table> </div> <div class="CHAPTER"> <h1> <a name="CHA-GLIB">glib: Portability and Utility</a> </h1> <div class="TOC"> <dl> <dt> <b>Table of Contents</b> </dt> <dt> <a href="cha-glib.html#Z24">Basics</a> </dt> <dt> <a href="z29.html">Data Structures</a> </dt> <dt> <a href="z35.html">Other Features</a> </dt> </dl> </div> <p> glib is a C portability and utility library for UNIX-like systems and Windows. This chapter covers some of the most commonly-used library features in GTK+ and Gnome applications. glib is simple, and the concepts are familiar; so we'll move quickly. For more complete coverage of glib, see <tt class="FILENAME">glib.h</tt> or the free glib reference manual that comes with the library. (By the way: don't be afraid of using the glib, GTK+, or Gnome header files; they are very clean and easy to read, and are handy as a quick reference. For that matter, don't be afraid to look at the source code, if you have very specific questions about the implementation.) </p> <p> glib's various facilities are intended to have a consistent interface; the coding style is semi-object-oriented, and identifiers are prefixed with "g" to create a kind of namespace. </p> <p> glib has a single header file, <tt class="FILENAME"> glib.h</tt>. </p> <div class="SECT1"> <h1 class="SECT1"> <a name="Z24">Basics</a> </h1> <p> glib provides substitutes for many standard and commonly-used C language constructs. This section describes glib's fundamental type definitions, macros, memory allocation routines, and string utility functions. </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z25">Type Definitions</a> </h2> <p> Rather than using C's standard types (<span class= "STRUCTNAME">int</span>, <span class="STRUCTNAME"> long</span>, etc.) glib defines its own. These serve a variety of purposes. For example, <span class= "STRUCTNAME">gint32</span> is guaranteed to be 32 bits wide, something no standard C type can ensure. <span class="STRUCTNAME">guint</span> is simply easier to type than <span class="STRUCTNAME">unsigned</span>. A few of the typedefs exist only for consistency; for example, <span class="STRUCTNAME">gchar</span> is always equivalent to the standard <span class= "STRUCTNAME">char</span>. </p> <p> The following primitive types are defined by glib: </p> <ul> <li> <p> <span class="STRUCTNAME">gint8</span>, <span class= "STRUCTNAME">guint8</span>, <span class= "STRUCTNAME">gint16</span>, <span class= "STRUCTNAME">guint16</span>, <span class= "STRUCTNAME">gint32</span>, <span class= "STRUCTNAME">guint32</span>, <span class= "STRUCTNAME">gint64</span>, <span class= "STRUCTNAME">guint64</span>---these give you integers of a guaranteed size. Not all platforms provide 64-bit integers; if a platform has them, glib will define <tt class="FUNCTION"> G_HAVE_GINT64</tt>. (If it isn't obvious, the <span class="STRUCTNAME">guint</span> types are unsigned, the <span class="STRUCTNAME">gint</span> types are signed.) </p> </li> <li> <p> <span class="STRUCTNAME">gboolean</span> is useful to make your code more readable, since C has no <span class="STRUCTNAME">bool</span> type. </p> </li> <li> <p> <span class="STRUCTNAME">gchar</span>, <span class= "STRUCTNAME">gshort</span>, <span class= "STRUCTNAME">glong</span>, <span class= "STRUCTNAME">gint</span>, <span class="STRUCTNAME"> gfloat</span>, <span class="STRUCTNAME"> gdouble</span> are purely cosmetic. </p> </li> <li> <p> <span class="STRUCTNAME">gpointer</span> may be more convenient to type than <span class= "STRUCTNAME">void *</span>. <span class= "STRUCTNAME">gconstpointer</span> gives you <span class="STRUCTNAME">const void*</span>. (<span class="STRUCTNAME">const gpointer</span> will <i class="EMPHASIS">not</i> do what you typically mean it to; spend some time with a good book on C if you don't see why.) </p> </li> </ul> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z26">Frequently Used Macros</a> </h2> <p> glib defines a number of familiar macros used in many C programs, shown in <a href= "cha-glib.html#ML-SIMPLEMACROS">Figure 1</a>. All of these should be self-explanatory. <tt class="FUNCTION"> MIN()</tt>/<tt class="FUNCTION">MAX()</tt> return the "FUNCTION">ABS()</tt> returns the absolute value of its argument. <tt class="FUNCTION">CLAMP(x, low, high)</tt> means <span class="STRUCTNAME">x</span>, unless <span class="STRUCTNAME">x</span> is outside the range [<span class="STRUCTNAME">low</span>, <span class= "STRUCTNAME">high</span>]; if <span class="STRUCTNAME"> x</span> is below the range, <span class="STRUCTNAME"> low</span> is returned; if <span class="STRUCTNAME"> x</span> is above the range, <span class="STRUCTNAME"> high</span> is returned. In addition to the macros shown in <a href="cha-glib.html#ML-SIMPLEMACROS">Figure 1</a>, <tt class="FUNCTION">TRUE</tt>/<tt class= "FUNCTION">FALSE</tt>/<tt class="FUNCTION">NULL</tt> are defined as the usual <span class="STRUCTNAME"> 1</span>/<span class="STRUCTNAME">0</span>/<span class= "STRUCTNAME">((void*)0)</span>. </p> <div class="FIGURE"> <a name="ML-SIMPLEMACROS"></a> <div class="FUNCSYNOPSIS"> <a name="ML-SIMPLEMACROS.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> MAX</tt></code>(<tt class= "PARAMETER"><i>a</i></tt>, <tt class="PARAMETER"> <i>b</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> MIN</tt></code>(<tt class= "PARAMETER"><i>a</i></tt>, <tt class="PARAMETER"> <i>b</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> ABS</tt></code>(<tt class= "PARAMETER"><i>x</i></tt>);</code> </p> <p> <code><code class="FUNCDEF"><tt class="FUNCTION"> CLAMP</tt></code>(<tt class= "PARAMETER"><i>x</i></tt>, <tt class="PARAMETER"> <i>low</i></tt>, <tt class="PARAMETER"><i> high</i></tt>);</code> </p> </div> <p> <b>Figure 1. Familiar C Macros</b> </p> </div> <p> There are also many macros unique to glib, such as the portable <span class="STRUCTNAME"> gpointer</span>-to-<span class="STRUCTNAME">gint</span> and <span class="STRUCTNAME">gpointer</span>-to-<span class="STRUCTNAME">guint</span> conversions shown in <a href="cha-glib.html#ML-POINTERINT">Figure 2</a>. </p> <p> Most of glib's data structures are designed to store a <span class="STRUCTNAME">gpointer</span>. If you want to store pointers to dynamically allocated objects, this is the right thing. However, sometimes you want to store a simple list of integers without having to dynamically allocate them. Though the C standard does not strictly guarantee it, it is possible to store a <span class="STRUCTNAME">gint</span> or <span class= "STRUCTNAME">guint</span> in a <span class= "STRUCTNAME">gpointer</span> variable on the wide range of platforms glib has been ported to; in some cases, an intermediate cast is required. The macros in <a href= "cha-glib.html#ML-POINTERINT">Figure 2</a> abstract the presence of the cast. </p> <p> Here's an example: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> gint my_int; gpointer my_pointer; my_int = 5; my_pointer = GINT_TO_POINTER(my_int); printf("We are storing %d\n", GPOINTER_TO_INT(my_pointer)); </pre> </td> </tr> </table> <p> Be careful, though; these macros allow you to store an integer in a pointer, but storing a pointer in an integer will <i class="EMPHASIS">not</i> work. To do that portably, you must store the pointer in a <span class="STRUCTNAME">long</span>. (It's undoubtedly a bad idea to do so, however.) </p> <div class="FIGURE"> <a name="ML-POINTERINT"></a> <div class="FUNCSYNOPSIS"> <a name="ML-POINTERINT.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -