Line data Source code
1 : /* valaclassregisterfunction.vala
2 : *
3 : * Copyright (C) 2006-2008 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * C function to register a class at runtime.
27 : */
28 386 : public class Vala.ClassRegisterFunction : TypeRegisterFunction {
29 : /**
30 : * Specifies the class to be registered.
31 : */
32 : public weak Class class_reference {
33 36338 : get {
34 36338 : return (Class) type_symbol;
35 : }
36 : }
37 :
38 : /**
39 : * Creates a new C function to register the specified class at runtime.
40 : *
41 : * @param cl a class
42 : * @return newly created class register function
43 : */
44 6789 : public ClassRegisterFunction (Class cl) {
45 2263 : base (cl);
46 : }
47 :
48 2263 : public override string get_type_struct_name () {
49 2263 : return get_ccode_type_name (class_reference);
50 : }
51 :
52 2263 : public override string get_base_init_func_name () {
53 2263 : if (class_reference.class_constructor != null) {
54 23 : return "%s_base_init".printf (get_ccode_lower_case_name (class_reference, null));
55 : } else {
56 2240 : return "NULL";
57 : }
58 : }
59 :
60 2263 : public override string get_class_finalize_func_name () {
61 2263 : if (class_reference.static_destructor != null) {
62 3 : return "%s_class_finalize".printf (get_ccode_lower_case_name (class_reference, null));
63 : } else {
64 2260 : return "NULL";
65 : }
66 : }
67 :
68 4 : public override string get_base_finalize_func_name () {
69 4 : if (class_reference.class_destructor != null) {
70 0 : return "%s_base_finalize".printf (get_ccode_lower_case_name (class_reference, null));
71 : } else {
72 4 : return "NULL";
73 : }
74 : }
75 :
76 2263 : public override string get_class_init_func_name () {
77 2263 : return "%s_class_init".printf (get_ccode_lower_case_name (class_reference, null));
78 : }
79 :
80 2263 : public override string get_instance_struct_size () {
81 2263 : return "sizeof (%s)".printf (get_ccode_name (class_reference));
82 : }
83 :
84 2263 : public override string get_instance_init_func_name () {
85 2263 : return "%s_instance_init".printf (get_ccode_lower_case_name (class_reference, null));
86 : }
87 :
88 1439 : public override string get_parent_type_name () {
89 1439 : return get_ccode_type_id (class_reference.base_class);
90 : }
91 :
92 2263 : public override string get_type_flags () {
93 2263 : if (class_reference.is_abstract) {
94 115 : return "G_TYPE_FLAG_ABSTRACT";
95 2148 : } else if (CodeContext.get ().require_glib_version (2, 70) && class_reference.is_sealed) {
96 0 : return "G_TYPE_FLAG_FINAL";
97 2148 : } else if (CodeContext.get ().require_glib_version (2, 74)) {
98 6 : return "G_TYPE_FLAG_NONE";
99 : } else {
100 2142 : return "0";
101 : }
102 : }
103 :
104 824 : public override string? get_gtype_value_table_init_function_name () {
105 824 : bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
106 824 : if ( is_fundamental )
107 824 : return "%s_init".printf (get_ccode_lower_case_name (class_reference, "value_"));
108 :
109 824 : return null;
110 : }
111 :
112 824 : public override string? get_gtype_value_table_free_function_name () {
113 824 : bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
114 824 : if ( is_fundamental )
115 824 : return "%s_free_value".printf (get_ccode_lower_case_name (class_reference, "value_"));
116 :
117 824 : return null;
118 : }
119 :
120 824 : public override string? get_gtype_value_table_copy_function_name () {
121 824 : bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
122 824 : if ( is_fundamental )
123 824 : return "%s_copy_value".printf (get_ccode_lower_case_name (class_reference, "value_"));
124 :
125 824 : return null;
126 : }
127 :
128 824 : public override string? get_gtype_value_table_peek_pointer_function_name () {
129 824 : bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
130 824 : if ( is_fundamental )
131 824 : return "%s_peek_pointer".printf (get_ccode_lower_case_name (class_reference, "value_"));
132 :
133 824 : return null;
134 : }
135 :
136 824 : public override string? get_gtype_value_table_collect_value_function_name () {
137 824 : bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
138 824 : if ( is_fundamental )
139 824 : return "%s_collect_value".printf (get_ccode_lower_case_name (class_reference, "value_"));
140 :
141 824 : return null;
142 : }
143 :
144 824 : public override string? get_gtype_value_table_lcopy_value_function_name () {
145 824 : bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
146 824 : if ( is_fundamental )
147 824 : return "%s_lcopy_value".printf (get_ccode_lower_case_name (class_reference, "value_"));
148 :
149 824 : return null;
150 : }
151 :
152 2263 : public override CCodeFragment get_type_interface_init_declaration () {
153 2263 : var frag = new CCodeFragment ();
154 :
155 4372 : foreach (DataType base_type in class_reference.get_base_types ()) {
156 1774 : if (!(base_type.type_symbol is Interface)) {
157 1439 : continue;
158 : }
159 :
160 335 : unowned Interface iface = (Interface) base_type.type_symbol;
161 :
162 335 : var iface_info_name = "%s_info".printf (get_ccode_lower_case_name (iface, null));
163 :
164 335 : var ctypedecl = new CCodeDeclaration ("const GInterfaceInfo");
165 335 : ctypedecl.modifiers = CCodeModifiers.STATIC;
166 335 : ctypedecl.add_declarator (new CCodeVariableDeclarator (iface_info_name, new CCodeConstant ("{ (GInterfaceInitFunc) %s_%s_interface_init, (GInterfaceFinalizeFunc) NULL, NULL}".printf (get_ccode_lower_case_name (class_reference), get_ccode_lower_case_name (iface)))));
167 335 : frag.append (ctypedecl);
168 : }
169 :
170 2263 : return frag;
171 : }
172 :
173 734 : public override void get_type_interface_init_statements (CodeContext context, CCodeBlock block, bool plugin) {
174 1460 : foreach (DataType base_type in class_reference.get_base_types ()) {
175 602 : if (!(base_type.type_symbol is Interface)) {
176 478 : continue;
177 : }
178 :
179 124 : unowned Interface iface = (Interface) base_type.type_symbol;
180 :
181 124 : var iface_info_name = "%s_info".printf (get_ccode_lower_case_name (iface, null));
182 248 : if (!plugin) {
183 124 : var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_interface_static"));
184 124 : reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (get_ccode_lower_case_name (class_reference, null))));
185 124 : reg_call.add_argument (new CCodeIdentifier (get_ccode_type_id (iface)));
186 124 : reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
187 124 : block.add_statement (new CCodeExpressionStatement (reg_call));
188 : } else {
189 0 : var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_module_add_interface"));
190 0 : reg_call.add_argument (new CCodeIdentifier ("module"));
191 0 : reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (get_ccode_lower_case_name (class_reference, null))));
192 0 : reg_call.add_argument (new CCodeIdentifier (get_ccode_type_id (iface)));
193 0 : reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
194 0 : block.add_statement (new CCodeExpressionStatement (reg_call));
195 : }
196 : }
197 :
198 734 : ((CCodeBaseModule) context.codegen).register_dbus_info (block, class_reference);
199 : }
200 : }
|