File: valaclassregisterfunction.vala

package info (click to toggle)
vala 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 13,756 kB
  • ctags: 12,353
  • sloc: ansic: 116,516; sh: 9,897; yacc: 1,218; makefile: 837; xml: 657; lex: 285
file content (130 lines) | stat: -rw-r--r-- 4,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* valaclassregisterfunction.vala
 *
 * Copyright (C) 2006-2008  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

/**
 * C function to register a class at runtime.
 */
public class Vala.ClassRegisterFunction : TypeRegisterFunction {
	/**
	 * Specifies the class to be registered.
	 */
	public weak Class class_reference { get; set; }
	
	/**
	 * Creates a new C function to register the specified class at runtime.
	 *
	 * @param cl a class
	 * @return   newly created class register function
	 */
	public ClassRegisterFunction (Class cl) {
		class_reference = cl;
	}
	
	public override TypeSymbol get_type_declaration () {
		return class_reference;
	}
	
	public override string get_type_struct_name () {
		return "%sClass".printf (class_reference.get_cname ());
	}

	public override string get_base_init_func_name () {
		if (class_reference.class_constructor != null) {
			return "%s_base_init".printf (class_reference.get_lower_case_cname (null));
		} else {
			return "NULL";
		}
	}

	public override string get_class_init_func_name () {
		return "%s_class_init".printf (class_reference.get_lower_case_cname (null));
	}
	
	public override string get_instance_struct_size () {
		return "sizeof (%s)".printf (class_reference.get_cname ());
	}
	
	public override string get_instance_init_func_name () {
		return "%s_instance_init".printf (class_reference.get_lower_case_cname (null));
	}
	
	public override string get_parent_type_name () {
		return class_reference.base_class.get_type_id ();
	}

	public override string get_type_flags () {
		if (class_reference.is_abstract) {
			return "G_TYPE_FLAG_ABSTRACT";
		} else {
			return "0";
		}
	}

	public override SymbolAccessibility get_accessibility () {
		return class_reference.access;
	}

	public override CCodeFragment get_type_interface_init_declaration () {
		var frag = new CCodeFragment ();
		
		foreach (DataType base_type in class_reference.get_base_types ()) {
			if (!(base_type.data_type is Interface)) {
				continue;
			}
			
			var iface = (Interface) base_type.data_type;
			
			var iface_info_name = "%s_info".printf (iface.get_lower_case_cname (null));
			
			var ctypedecl = new CCodeDeclaration ("const GInterfaceInfo");
			ctypedecl.modifiers = CCodeModifiers.STATIC;
			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)))));
			frag.append (ctypedecl);
		}
		
		return frag;
	}

	public override CCodeFragment get_type_interface_init_statements () {
		var frag = new CCodeFragment ();
		
		foreach (DataType base_type in class_reference.get_base_types ()) {
			if (!(base_type.data_type is Interface)) {
				continue;
			}
			
			var iface = (Interface) base_type.data_type;
			
			var iface_info_name = "%s_info".printf (iface.get_lower_case_cname (null));
			
			var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_interface_static"));
			reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (class_reference.get_lower_case_cname (null))));
			reg_call.add_argument (new CCodeIdentifier (iface.get_type_id ()));
			reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
			frag.append (new CCodeExpressionStatement (reg_call));
		}
		
		return frag;
	}
}