The Official GNOME 2 Developer's Guide GLib (二)

The Official GNOME 2 Developer's Guide GLib (一)

Yu posted @ 2011年8月09日 20:20 in The Official GNOME 2 Developer's Guide GLib with tags c gnome gtk , 1654 阅读

 

GLib

1.1 Introduction

在开源世界中,G中很常见的。 它代表了GNU ("GNU's Not Unix")。 像GTK+,GLib,GObject,以及GNOME,还有一些其它的软件包,如Ghostscript和gcc中都充满了G。

为了理解后面的章节,你必须学习一些GLib的基础知识(libglib-2.0)。它为GTK+和GNOME程序提供了基础的数据结构和实用函数。在本章中将会涉及到GLib的结构和API的介绍。 你将会在第二章中学习GLib's object system(GObject)。

在你使用GNOME和GTK+的时候,会不可避免的使用GLib。 其它的一些库,如ORBit除了GLib并没有引用其它的库。 GLib所提供的abstractions(这个不知道应该译成什么)和实用工具编程程序提供了方便, 而且很容易移植到其它平台。

在本章中并没有包含图形操作代码,它只是一个简单的,逐步讲述了GLib的函数和数据结构。 可能读起来有点枯燥,不过你也可以直接跳到第三章去阅读GTK+的内容。 但是你会经常的返回来查看这前两章的内容。

 

1.2 GLib Naming Coventions (GLib命名规则)

就像许多其它的库一下,GLib也为了一致性和易读性规范了命名规则。

函数的名字一般都是小写的,并且在每部分名字之间加下划线, 如g_timer_new(),g_list_append()。并且所有的函数名字都是以g_开头。

在GLib中,所有的函数都有前缀g_。

类型名并不包含下划线,并且GLib里面的所有类型组件都是以大写字母G开头的,如GTimer,GList。但GLib中的基本类型是值得注意的例外。在第三节中将会有介绍。

如果某个函数主要是操作某个特定的类型的话,这个函数的前缀就与相应的类型相匹配。例如,g_timer_*就是操作GTimer类型。 g_list_*就是操作GList类型。

这些规则听起来比实际要复杂。

 

 

 

1.3 Basic Types (基本类型)

在开始使用GLib之前,你首先要适应GLib的基本类型。 你或许很想知道为什么使用guchar要比使用unsigned char好。 如果你的程序一直待在同一个平台上执行,那么使用guchar与使用unsigned char并没有实质的差别。 但是,如果你想编写出在不同平台之间移植的程序,如Windows和Unix之间。 那你会很感谢GLib将基本类型给你抽象出来了。

例如,你想在所有可能的平台上定义16位的无符号整型,使用C语言的话可能看起来很麻烦。但幸运的是GLib帮你处理了这些。 所有的基本类型在下面的表格中列出。

要使用GLib和它的类型,必须要在源码中包含glib.h

#include <glib.h>

gpointer和gconstpointer类型在GLib的数据结构中经常出现,这两个是无类型的内存指针。在GLib中,函数负责检查这两个指针的类型,程序员和编译器并不管这些。这在回调函数以及排序和遍历中比较的时候尤其方便。

在GLib的头文件中为gboolean类型定义了TRUE和FALSE常量。 在使用这些常量的时候,不要使用比较运算符。例如要用:if (my_gboolean),而不是:if (my_gboolean == TRUE)。

 

 

 

GLib Type

Corresponding Type in C

gchar

char

guchar

unsigned char

gint

int

guint

unsigned int

gshort

short

gushort

unsigned short

glong

long

gulong

unsigned long

gfloat

float

gdouble

double

gint8

int, 8 bits wide

guint8

unsigned int, 8 bits wide

gint16

int, 16 bits wide

guint16

unsigned int, 16 bits wide

gint32

int, 32 bits wide

guint32

unsigned int, 32 bits wide

gint64

int, 64 bits wide

guint64

unsigned int, 64 bits wide

gpointer

void *, untyped pointer

gconstpointer

const void *, constant untyped pointer

gboolean

Boolean value, either TRUE or FALSE

 

 

guest 说:
2011年9月02日 16:40

勘误一下,GLib Type第二个应该是 guchar吧。

Avatar_small
Yu 说:
2011年9月03日 11:46

@guest: thx 你很细心阿。。。已改

Mystic Messenger 说:
2022年8月10日 02:57

This game is quite captivating and in this article we will be showing you how you can download this game, Mystic messenger for Android and IOS. At the same time we will also discuss the possibilities of downloading Mystic messenger for PC. Mystic Messenger One of the reasons why this game is quite sleek to play is because the size is around 28MB which ensures that your device is on top performance while playing this game.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter