美文网首页
Gobject C语言库 II 对象方法

Gobject C语言库 II 对象方法

作者: Charles_linzc | 来源:发表于2021-10-31 17:03 被阅读0次

面向对象语言里,方法是一类对象的通用行为,我们通常需要定义这些方法方便其它对象调用,方法的调用方式类似下面的方式:
VcamSource source=new VcamSource(....);
MediaType mtype = source.getMediaType();
GObject中,一般使用下面的代码习惯:
VcamSource *source=g_object_new(VCAM_TYPE_SOURCE, NULL);
MediaType *mtype ;
vcam_source_getMediaType(source, mtype );
它需要两部分的实现, 第一部分在.h文件中的class中定义getMediaType的函数指针和对外的公用方法:

struct _VcamSourceClass {
    GObjectClass parent_class;
    //定义
    void (*getMediaType) (VcamSource* self,   GError** error);

};

void vcam_source_getMediaTyp(VcamSource self, MediaType* typeptr);
~~~
第二部分在.c 文件中实现vcam_source_getMediaTyp,以及在class的init函数中给函数指针一个默认实现:
~~~
static void
vcam_source_class_init(VcamSourceClass *kclass) {
    /* this is not necessary, except for demonstration purposes.
   *
   * pure virtual method: mandates implementation in children.
   */
    kclass->getMediaType = NULL;

    /* merely virtual method. */
    kclass->getMediaType = getMediaTyp;
}

 static void getMediaTyp(VcamSource* self, MediaType* typeptr)
 {
     /* Default implementation for the virtual method. */
 }
 void  vcam_source_getMediaTyp(VcamSource *self, MediaType* typeptr)
 {
     VcamSourceClass* klass;

     g_return_if_fail(VCAM_IS_SOURCE(self));
     g_return_if_fail(typeptr == NULL || *typeptr == NULL);

     klass = VCAM_SOURCE_GET_CLASS(self);
     g_return_if_fail(klass->getMediaType != NULL);

     klass->getMediaType(self, NULL);
 }
~~~

这种对象方法的声明和调用方式被称为**virtual public methods**,它很适合与方法重载的场景,父类声明虚函数以及一个默认的实现,并实现通用的调用方式**vcam_source_getMediaType**;子类可以根据自己需求,实现自己的方法,并在类初始化时提供过父类的函数指针重新设置方法调用。这样调用方式不变,但是父类和各个子类将有不同的表现行为。
正如它适合类继承,GObject做了一个限制:final类是不能用这种方式的,因此我们需要对.h文件进行修改使用G_DECLARE_DERIVABLE_TYPE替换G_DECLARE_FINAL_TYPE:

~~~
#ifndef VCAM_SOURCE_H
#define VCAM_SOURCE_H

#include <glib-object.h>

#define VCAM_TYPE_SOURCE (vcam_source_get_type())
G_DECLARE_DERIVABLE_TYPE(VcamSource, vcam_source, VCAM, SOURCE, GObject)

struct  _VcamSource {
    GObject parent_instance;
};

struct _VcamSourceClass {
    GObjectClass parent_class;
    //定义
    void (*getMediaType) (VcamSource* self,   GError** error);

};
void vcam_source_getMediaTyp(VcamSource *self, MediaType* typeptr);
#endif /* __VCAM_SOURCE_H__ */

那么在final类型的类中声明方法,就直接用方法声明(而不是指针),并在.c文件中实现,郑重方式就是non-virtual public methods : 在.h文件中直接移除void (getMediaType) (VcamSource self, GError** error);**函数指针,并且在.c文件中将getMediatype的具体实现直接写在void vcam_source_getMediaTyp方法中。

另外GObject还有一种方法的声明和使用方式,它称为Virtual private Methods
在.h文件中只有函数指针声明;同时在.c文件中提供一个默认的实现,和在class初始化函数中提供一个默认实现。 这时候这个函数只用于内部的调用,并且能被子类继承。
vcam_source_hasMediaType调用方式实现,vcam_source_hasMediaType_default是提供的默认实现。

#ifndef VCAM_SOURCE_H
#define VCAM_SOURCE_H

#include <glib-object.h>

#define VCAM_TYPE_SOURCE (vcam_source_get_type())
G_DECLARE_DERIVABLE_TYPE(VcamSource, vcam_source, VCAM, SOURCE, GObject)

struct  _VcamSource {
    GObject parent_instance;
};


struct _VcamSourceClass {
    GObjectClass parent_class;
    //定义
    void (*getMediaType) (VcamSource* self,   GError** error);

   /*Private helper function to work out whether has a Media Type */
   gboolean(*hasMediaType) (VcamSource* self);

};

void vcam_source_getMediaTyp(VcamSource *self, MediaType* typeptr);
#endif /* __VCAM_SOURCE_H__ */

上面的.h文件中声明一个私有虚拟函数hasMediaType,他没有对应的对外的调用声明。下面时对应的.c文件,这里有调用方式和默认实现,并在class初始化中提供这个默认实现。

#include "VcamSource.h"

G_DEFINE_TYPE(VcamSource, vcam_source, G_TYPE_OBJECT)

static void
vcam_source_class_init(VcamSourceClass *kclass) {
    /* this is not necessary, except for demonstration purposes.
   *
   * pure virtual method: mandates implementation in children.
   */
    kclass->getMediaType = NULL;

    /* merely virtual method. */
    kclass->getMediaType = getMediaTyp;
    kclass->hasMediaType = vcam_source_hasMediaType_default;

}

 static void
vcam_source_init(VcamSource *d) {


}

 static void
    getMediaTyp(VcamSource* self, MediaType* typeptr)
 {
     /* Default implementation for the virtual method. */
 }


 void
     vcam_source_getMediaTyp(VcamSource *self, MediaType* typeptr)
 {
     VcamSourceClass* klass;

     g_return_if_fail(VCAM_IS_SOURCE(self));
     g_return_if_fail(typeptr == NULL || *typeptr == NULL);

     klass = VCAM_SOURCE_GET_CLASS(self);
     g_return_if_fail(klass->getMediaType != NULL);

     klass->getMediaType(self, NULL);
 }

 /* this accessor function is static: it is not exported outside of this file. */
 static gboolean
     vcam_source_hasMediaType(VcamSource* self)
 {
     return VCAM_SOURCE_GET_CLASS(self)->hasMediaType(self);
 }

 static gboolean
     vcam_source_hasMediaType_default(VcamSource * self)
 {
     return FALSE;
 }

 int  main(int argc, char** argv) {
     GType dtype;
     VcamSource* d;
     
          dtype = vcam_source_get_type(); /* or d = T_TYPE_DOUBLE */
          if (dtype) 
             g_print("Registration was a success. The type is %lx.\n", dtype);
        else
              g_print("Registration failed.\n");
     return 0;
 }

相关文章

网友评论

      本文标题:Gobject C语言库 II 对象方法

      本文链接:https://www.haomeiwen.com/subject/owoialtx.html