1 /* valaclassregisterfunction.vala
3 * Copyright (C) 2006-2008 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.1 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 weak Class class_reference
{ get; set; }
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 TypeSymbol
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 () {
53 if (class_reference
.class_constructor
!= null) {
54 return "%s_base_init".printf (class_reference
.get_lower_case_cname (null));
60 public override string get_class_init_func_name () {
61 return "%s_class_init".printf (class_reference
.get_lower_case_cname (null));
64 public override string get_instance_struct_size () {
65 return "sizeof (%s)".printf (class_reference
.get_cname ());
68 public override string get_instance_init_func_name () {
69 return "%s_instance_init".printf (class_reference
.get_lower_case_cname (null));
72 public override string get_parent_type_name () {
73 return class_reference
.base_class
.get_upper_case_cname ("TYPE_");
76 public override string get_type_flags () {
77 if (class_reference
.is_abstract
) {
78 return "G_TYPE_FLAG_ABSTRACT";
84 public override SymbolAccessibility
get_accessibility () {
85 return class_reference
.access
;
88 public override CCodeFragment
get_type_interface_init_declaration () {
89 var frag
= new
CCodeFragment ();
91 foreach (DataType base_type
in class_reference
.get_base_types ()) {
92 if (!(base_type
.data_type is Interface
)) {
96 var iface
= (Interface
) base_type
.data_type
;
98 var iface_info_name
= "%s_info".printf (iface
.get_lower_case_cname (null));
100 var ctypedecl
= new
CCodeDeclaration ("const GInterfaceInfo");
101 ctypedecl
.modifiers
= CCodeModifiers
.STATIC
;
102 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)))));
103 frag
.append (ctypedecl
);
109 public override CCodeFragment
get_type_interface_init_statements () {
110 var frag
= new
CCodeFragment ();
112 foreach (DataType base_type
in class_reference
.get_base_types ()) {
113 if (!(base_type
.data_type is Interface
)) {
117 var iface
= (Interface
) base_type
.data_type
;
119 var iface_info_name
= "%s_info".printf (iface
.get_lower_case_cname (null));
121 var reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_add_interface_static"));
122 reg_call
.add_argument (new
CCodeIdentifier ("%s_type_id".printf (class_reference
.get_lower_case_cname (null))));
123 reg_call
.add_argument (new
CCodeIdentifier (iface
.get_upper_case_cname ("TYPE_")));
124 reg_call
.add_argument (new
CCodeIdentifier ("&%s".printf (iface_info_name
)));
125 frag
.append (new
CCodeExpressionStatement (reg_call
));