GObject Builder 介绍

14 篇文章 0 订阅
10 篇文章 0 订阅

GObject Builder 介绍

GOB (GOB2 anyway) is a preprocessor for making GObjects with inline C code sothat generated files are not edited. Syntax is inspired by Java and Yacc orLex. The implementation is intentionally kept simple, and no C actual codeparsing is done.

Reasons:

  • C is a perfect (well mostly) language, no need for another language
  • Writing a fully featured GObject is a hassle
  • GObject has little type safety, GOB improves on it.
  • Need for a generator that doesn't require changes to generated code
  • I like how Java writes method code directly into the class definition.
/*
 * This file is a basic example of how a .gob file would be constructed,
 * how to add data members, properties and methods
 */
requires 2.0.0

%{
#include <time.h>
#include "my-person.h"
#include "my-person-private.h"
%}

class My:Person from G:Object {
        /* the name of the person */
        private char *name = {g_strdup(_("Nobody"))}
                destroywith g_free;
        property STRING name
                (nick = _("Name"),
                 blurb = _("Name of the person"),
                 default_value = _("Nobody"),
                 /* Export get/set functions for this property */
                 export,
                 /* link to the data memeber 'name' */
                 link);

        /* date of birth as a time_t */
        private long dob = 0;
        property LONG dob
                (nick = _("Date of birth"),
                 blurb = _("Date of birth of the person"),
                 minimum = 0,
                 maximum = LONG_MAX,
                 default_value = 0,
                 export)
                /* We could use 'link' as above, but the code below
                 * shows how to do this without linking */
                set {
                        self->_priv->dob = g_value_get_long (VAL);
                }
                get {
                        g_value_set_long (VAL, self->_priv->dob);
                };

        /* date of death as a time_t */
        private long dod = 0;
        property LONG dod
                (nick = _("Date of death"),
                 blurb = _("Date of death of the person"),
                 minimum = 0,
                 maximum = LONG_MAX,
                 default_value = 0,
                 export,
                 link);

        /* number of rounds in our shotgun */
        private int rounds_in_shotgun = 0;

        /* when the person gets born, sends out a signal, the caller
           of the signal should provide the date of birth */
        signal last NONE (LONG)
        void
        birth (self, long dob)
        {
                g_object_set (G_OBJECT (self),
                              "dob", dob);
        }
        
        /* when the person dies, sends out a signal, the caller
           of the signal should provide the date of death */
        signal last NONE (LONG)
        void
        death (self, long dod)
        {
                g_object_set (G_OBJECT (self),
                              "dod", dod);
        }

        public
        void
        load_shotgun (self)
        {
                /* add a round to our shotgun */
                self->_priv->rounds_in_shotgun++;
        }

        public
        void
        shoot_oneself_in_the_head (self)
        {
                if (self->_priv->rounds_in_shotgun == 0) {
                        g_warning (_("No rounds in the shotgun!"));
                        return;
                }
                
                /* one round was fired */
                self->_priv->rounds_in_shotgun--;

                /* death is imminent if we shoot oneself in the head */
                death (self, (long)time (NULL));
        }

        public GObject *
        new (void)
        {
                return (GObject *)GET_NEW;
        }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值