1 /* valatyperegisterfunction.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 type at runtime.
28 public abstract class Vala
.TypeRegisterFunction
{
29 private CCodeFragment declaration_fragment
= new
CCodeFragment ();
31 private CCodeFragment definition_fragment
= new
CCodeFragment ();
34 * Constructs the C function from the specified type.
36 public void init_from_type (bool plugin
= false) {
37 string type_id_name
= "%s_type_id".printf (get_type_declaration ().get_lower_case_cname (null));
39 var type_block
= new
CCodeBlock ();
40 var cdecl
= new
CCodeDeclaration ("GType");
41 cdecl
.add_declarator (new CCodeVariableDeclarator
.with_initializer (type_id_name
, new
CCodeConstant ("0")));
42 cdecl
.modifiers
= CCodeModifiers
.STATIC
;
44 type_block
.add_statement (cdecl
);
46 definition_fragment
.append (cdecl
);
51 fun
= new
CCodeFunction ("%s_get_type".printf (get_type_declaration ().get_lower_case_cname (null)), "GType");
53 fun
= new
CCodeFunction ("%s_register_type".printf (get_type_declaration ().get_lower_case_cname (null)), "GType");
54 fun
.add_parameter (new
CCodeFormalParameter ("module", "GTypeModule *"));
56 var get_fun
= new
CCodeFunction ("%s_get_type".printf (get_type_declaration ().get_lower_case_cname (null)), "GType");
58 declaration_fragment
.append (get_fun
.copy ());
60 get_fun
.block
= new
CCodeBlock ();
61 get_fun
.block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier (type_id_name
)));
63 definition_fragment
.append (get_fun
);
66 var type_init
= new
CCodeBlock ();
67 var ctypedecl
= new
CCodeDeclaration ("const GTypeInfo");
68 ctypedecl
.modifiers
= CCodeModifiers
.STATIC
;
69 ctypedecl
.add_declarator (new CCodeVariableDeclarator
.with_initializer ("g_define_type_info", new
CCodeConstant ("{ sizeof (%s), (GBaseInitFunc) %s, (GBaseFinalizeFunc) NULL, (GClassInitFunc) %s, (GClassFinalizeFunc) NULL, NULL, %s, 0, (GInstanceInitFunc) %s }".printf (get_type_struct_name (), get_base_init_func_name (), get_class_init_func_name (), get_instance_struct_size (), get_instance_init_func_name ()))));
70 type_init
.add_statement (ctypedecl
);
72 type_init
.add_statement (get_type_interface_init_declaration ());
74 CCodeFunctionCall reg_call
;
76 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_register_static"));
78 reg_call
= new
CCodeFunctionCall (new
CCodeIdentifier ("g_type_module_register_type"));
79 reg_call
.add_argument (new
CCodeIdentifier ("module"));
81 reg_call
.add_argument (new
CCodeIdentifier (get_parent_type_name ()));
82 reg_call
.add_argument (new
CCodeConstant ("\"%s\"".printf (get_type_declaration ().get_cname ())));
83 reg_call
.add_argument (new
CCodeIdentifier ("&g_define_type_info"));
84 reg_call
.add_argument (new
CCodeConstant (get_type_flags ()));
85 type_init
.add_statement (new
CCodeExpressionStatement (new
CCodeAssignment (new
CCodeIdentifier (type_id_name
), reg_call
)));
87 type_init
.add_statement (get_type_interface_init_statements ());
90 var cond
= new
CCodeFunctionCall (new
CCodeIdentifier ("G_UNLIKELY"));
91 cond
.add_argument (new
CCodeBinaryExpression (CCodeBinaryOperator
.EQUALITY
, new
CCodeIdentifier (type_id_name
), new
CCodeConstant ("0")));
92 var cif
= new
CCodeIfStatement (cond
, type_init
);
93 type_block
.add_statement (cif
);
95 type_block
= type_init
;
98 type_block
.add_statement (new
CCodeReturnStatement (new
CCodeIdentifier (type_id_name
)));
100 declaration_fragment
.append (fun
.copy ());
102 fun
.block
= type_block
;
104 definition_fragment
.append (fun
);
108 * Returns the data type to be registered.
110 * @return type to be registered
112 public abstract DataType
! get_type_declaration ();
115 * Returns the name of the type struct in C code.
117 * @return C struct name
119 public abstract string! get_type_struct_name ();
122 * Returns the name of the base_init function in C code.
124 * @return C function name
126 public abstract string! get_base_init_func_name ();
129 * Returns the name of the class_init function in C code.
131 * @return C function name
133 public abstract string! get_class_init_func_name ();
136 * Returns the size of the instance struct in C code.
138 * @return C instance struct size
140 public abstract string! get_instance_struct_size ();
143 * Returns the name of the instance_init function in C code.
145 * @return C function name
147 public abstract string! get_instance_init_func_name ();
150 * Returns the name of the parent type in C code.
152 * @return C parent type name
154 public abstract string! get_parent_type_name ();
157 * Returns the set of type flags to be applied when registering.
161 public virtual string get_type_flags () {
166 * Returns additional C declarations to setup interfaces.
168 * @return C declarations
170 public virtual CCodeFragment
! get_type_interface_init_declaration () {
171 return new
CCodeFragment ();
175 * Returns additional C initialization statements to setup interfaces.
177 * @return C statements
179 public abstract CCodeFragment
! get_type_interface_init_statements ();
182 * Returns the declaration for this type register function in C code.
184 * @return C function declaration fragment
186 public CCodeFragment
! get_declaration () {
187 return declaration_fragment
;
191 * Returns the definition for this type register function in C code.
193 * @return C function definition fragment
195 public CCodeFragment
! get_definition () {
196 return definition_fragment
;