Vala语言简介

来源:互联网 发布:linux 病毒查杀 编辑:程序博客网 时间:2024/05/18 01:29

在此次项目开发中,由于是基于Deja-dup进行开发,而Deja-dup使用Vala语言。因而不得不学习了一下vala。在这里总结一下,方便其他有需要的人。

关于Glib&GObject:

GLib是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义、相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事件循环、线程、动态调用、对象系统等的API。它能够在类UNIX的操作系统平台(如LINUX, HP-UNIX等),WINDOWS,OS2和BeOS等操作系统台上运行。Glib提供一系列常用的、标准的C语言结构以及功能函数。例如链表、字符串、树等等。
简单的说,GObject对象系统是一个建立在GLIB基础上的,用C语言完成的,具有跨平台特色的、灵活的、可扩展的、非常容易映射到其它语言的面向对象的框架。利用Gobject,你可以使用面向对象的思想进行程序开发,而不必专门去学习一门面向对象的语言。然而GObject的代码不太直观,类似链表而不是树形结构,而且代码较冗长,通常一个很小的功能却要上百行的代码。

Vala:

Vala 是一种新的语言,它允许在 Gnome 运行时库的基础上使用一些现代的编程技巧。通过使用 GLib 和 GObject,Vala 提供了动态类型系统和补助内存管理的功能。Vala 将被先编译为 C 语言文件,然后再编译成机器代码,因此,Vala 程序拥有很高的执行效率。 Vala 借鉴了大量的 C# 语法,因此看起来非常相似。这样就可以像C#或是Java一样面向对象,程序写起来及其简便。但是它最大的优点在于vala语言编译后是被转换为C语言,再用gcc编译,这样其实就是利用了C#的简便和C语言在编程中的高执行效率。

Hello World:

依照惯例,先上个Hello World:
class Demo.HelloWorld : GLib.Object {    public static int main(string[] args) {        stdout.printf("Hello, World\n");        return 0;    }}
编译运行:
$ valac hello.vala$ ./hello
前边我们有说道,valac是将vala代码翻译为C代码,然后调用GCC编译的,所以我们来看翻译为C的这段代码是什么样子的:使用命令
valac -C demo.vala
没有错误的话会在当前目录生成demo.c,其内容为:
/* a.c generated by valac 0.24.0, the Vala compiler * generated from a.vala, do not modify */#include <glib.h>#include <glib-object.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#define DEMO_TYPE_HELLO_WORLD (demo_hello_world_get_type ())#define DEMO_HELLO_WORLD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEMO_TYPE_HELLO_WORLD, DemoHelloWorld))#define DEMO_HELLO_WORLD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEMO_TYPE_HELLO_WORLD, DemoHelloWorldClass))#define DEMO_IS_HELLO_WORLD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEMO_TYPE_HELLO_WORLD))#define DEMO_IS_HELLO_WORLD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEMO_TYPE_HELLO_WORLD))#define DEMO_HELLO_WORLD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEMO_TYPE_HELLO_WORLD, DemoHelloWorldClass))typedef struct _DemoHelloWorld DemoHelloWorld;typedef struct _DemoHelloWorldClass DemoHelloWorldClass;typedef struct _DemoHelloWorldPrivate DemoHelloWorldPrivate;struct _DemoHelloWorld {GObject parent_instance;DemoHelloWorldPrivate * priv;};struct _DemoHelloWorldClass {GObjectClass parent_class;};static gpointer demo_hello_world_parent_class = NULL;GType demo_hello_world_get_type (void) G_GNUC_CONST;enum  {DEMO_HELLO_WORLD_DUMMY_PROPERTY};gint demo_hello_world_main (gchar** args, int args_length1);DemoHelloWorld* demo_hello_world_new (void);DemoHelloWorld* demo_hello_world_construct (GType object_type);gint demo_hello_world_main (gchar** args, int args_length1) {gint result = 0;FILE* _tmp0_ = NULL;_tmp0_ = stdout;fprintf (_tmp0_, "Hello, World\n");result = 0;return result;}int main (int argc, char ** argv) {#if !GLIB_CHECK_VERSION (2,35,0)g_type_init ();#endifreturn demo_hello_world_main (argv, argc);}DemoHelloWorld* demo_hello_world_construct (GType object_type) {DemoHelloWorld * self = NULL;self = (DemoHelloWorld*) g_object_new (object_type, NULL);return self;}DemoHelloWorld* demo_hello_world_new (void) {return demo_hello_world_construct (DEMO_TYPE_HELLO_WORLD);}static void demo_hello_world_class_init (DemoHelloWorldClass * klass) {demo_hello_world_parent_class = g_type_class_peek_parent (klass);}static void demo_hello_world_instance_init (DemoHelloWorld * self) {}GType demo_hello_world_get_type (void) {static volatile gsize demo_hello_world_type_id__volatile = 0;if (g_once_init_enter (&demo_hello_world_type_id__volatile)) {static const GTypeInfo g_define_type_info = { sizeof (DemoHelloWorldClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) demo_hello_world_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DemoHelloWorld), 0, (GInstanceInitFunc) demo_hello_world_instance_init, NULL };GType demo_hello_world_type_id;demo_hello_world_type_id = g_type_register_static (G_TYPE_OBJECT, "DemoHelloWorld", &g_define_type_info, 0);g_once_init_leave (&demo_hello_world_type_id__volatile, demo_hello_world_type_id);}return demo_hello_world_type_id__volatile;}
这就是翻译为C代码的Vala版HelloWorld程序。

资源:

这里总结了一些假期里找到的Vala的资源。
  • http://www.valadoc.org一个很好很全的vala在线文档库,可以很方便地下载devhelp的包,以便本地使用。
  • https://wiki.gnome.org/Projects/Vala/DocumentationGnome官方的文档,还有许多示例,很不错。
  • https://wiki.gnome.org/Projects/Vala/Documentation#Sample_Code这里有各种库的示例代码。
0 0