3a35f995c60789b0b38b7573bdf8dba728f76198
[roobuilder] / src / codegen / valainterfaceregisterfunction.vala
1 /* valainterfaceregisterfunction.vala
2  *
3  * Copyright (C) 2006-2011  Jürg Billeter
4  * Copyright (C) 2006-2007  Raffaele Sandrini
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19  *
20  * Author:
21  *      Jürg Billeter <j@bitron.ch>
22  *      Raffaele Sandrini <raffaele@sandrini.ch>
23  */
24
25 using GLib;
26
27 /**
28  * C function to register an interface at runtime.
29  */
30 public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
31         /**
32          * Specifies the interface to be registered.
33          */
34         public weak Interface interface_reference { get; set; }
35
36         public InterfaceRegisterFunction (Interface iface) {
37                 interface_reference = iface;
38         }
39
40         public override TypeSymbol get_type_declaration () {
41                 return interface_reference;
42         }
43
44         public override string get_type_struct_name () {
45                 return get_ccode_type_name (interface_reference);
46         }
47
48         public override string get_base_init_func_name () {
49                 return "NULL";
50         }
51
52         public override string get_class_finalize_func_name () {
53                 return "NULL";
54         }
55
56         public override string get_base_finalize_func_name () {
57                 return "NULL";
58         }
59
60         public override string get_class_init_func_name () {
61                 return "%s_default_init".printf (get_ccode_lower_case_name (interface_reference));
62         }
63
64         public override string get_instance_struct_size () {
65                 return "0";
66         }
67
68         public override string get_instance_init_func_name () {
69                 return "NULL";
70         }
71
72         public override string get_parent_type_name () {
73                 return "G_TYPE_INTERFACE";
74         }
75
76         public override SymbolAccessibility get_accessibility () {
77                 return interface_reference.access;
78         }
79
80         public override void get_type_interface_init_statements (CodeContext context, CCodeBlock block, bool plugin) {
81                 /* register all prerequisites */
82                 foreach (DataType prereq_ref in interface_reference.get_prerequisites ()) {
83                         unowned TypeSymbol prereq = prereq_ref.type_symbol;
84
85                         var func = new CCodeFunctionCall (new CCodeIdentifier ("g_type_interface_add_prerequisite"));
86                         func.add_argument (new CCodeIdentifier ("%s_type_id".printf (get_ccode_lower_case_name (interface_reference))));
87                         func.add_argument (new CCodeIdentifier (get_ccode_type_id (prereq)));
88
89                         block.add_statement (new CCodeExpressionStatement (func));
90                 }
91
92                 ((CCodeBaseModule) context.codegen).register_dbus_info (block, interface_reference);
93         }
94 }