1 /* valaclassregisterfunction.vala
3 * Copyright (C) 2006-2007 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
26 * C function to register a class at runtime.
28 public class Vala
.ClassRegisterFunction
: TypeRegisterFunction
{
30 * Specifies the class to be registered.
32 public Class
! class_reference
{ get; set construct; }
35 * Creates a new C function to register the specified class at runtime.
38 * @return newly created class register function
40 public ClassRegisterFunction (Class
! cl
) {
44 public override DataType
! get_type_declaration () {
45 return class_reference
;
48 public override string! get_type_struct_name () {
49 return "%sClass".printf (class_reference
.get_cname ());
52 public override string! get_base_init_func_name () {
56 public override string! get_class_init_func_name () {
57 return "%s_class_init".printf (class_reference
.get_lower_case_cname (null));
60 public override string! get_instance_struct_size () {
61 return "sizeof (%s)".printf (class_reference
.get_cname ());
64 public override string! get_instance_init_func_name () {
65 return "%s_init".printf (class_reference
.get_lower_case_cname (null));
68 public override string! get_parent_type_name () {
69 return class_reference
.base_class
.get_upper_case_cname ("TYPE_");
72 public override string get_type_flags () {
73 if (class_reference
.is_abstract
) {
74 return "G_TYPE_FLAG_ABSTRACT";
80 public override CCodeFragment
! get_type_interface_init_declaration () {
81 var frag
= new
CCodeFragment ();
83 foreach (TypeReference base_type
in class_reference
.get_base_types ()) {
84 if (!(base_type
.data_type is Interface
)) {
88 var iface
= (Interface
) base_type
.data_type
;
90 var iface_info_name
= "%s_info".printf (iface
.get_lower_case_cname (null));
92 var ctypedecl
= new
CCodeDeclaration ("const GInterfaceInfo");
93 ctypedecl
.modifiers
= CCodeModifiers
.STATIC
;
94 ctypedecl
.add_declarator (new CCodeVariableDeclarator
.with_initializer (iface_info_name
, new
CCodeConstant ("{ (GInterfaceInitFunc) %s_%s_interface_init, (GInterfaceFinalizeFunc) NULL, NULL}".printf (class_reference
.get_lower_case_cname (null), iface
.get_lower_case_cname (null)))));
95 frag
.append (ctypedecl
);
101 public override CCodeFragment
! get_type_interface_init_statements () {
102 var frag
= new
CCodeFragment ();
104 foreach (TypeReference base_type
in class_reference
.get_base_types ()) {
105 if (!(base_type
.data_type is Interface
)) {
109 var iface
= (Interface
) base_type
.data_type
;
111 var iface_info_name
= "%s_info".printf (iface
.get_lower_case_cname (null));
113 var reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_add_interface_static"));
114 reg_call
.add_argument (new
CCodeIdentifier ("%s_type_id".printf (class_reference
.get_lower_case_cname (null))));
115 reg_call
.add_argument (new
CCodeIdentifier (iface
.get_upper_case_cname ("TYPE_")));
116 reg_call
.add_argument (new
CCodeIdentifier ("&%s".printf (iface_info_name
)));
117 frag
.append (new
CCodeExpressionStatement (reg_call
));