valaclassregisterfunction.vala 6.12 KB
Newer Older
1 2
/* valaclassregisterfunction.vala
 *
3
 * Copyright (C) 2006-2008  Jürg Billeter
4 5 6 7
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

 * 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;

25 26 27 28 29 30 31
/**
 * C function to register a class at runtime.
 */
public class Vala.ClassRegisterFunction : TypeRegisterFunction {
	/**
	 * Specifies the class to be registered.
	 */
32
	public weak Class class_reference { get; set; }
33

34 35 36 37 38 39
	/**
	 * Creates a new C function to register the specified class at runtime.
	 *
	 * @param cl a class
	 * @return   newly created class register function
	 */
40
	public ClassRegisterFunction (Class cl, CodeContext context) {
41
		class_reference = cl;
42
		this.context = context;
43
	}
44
	
Jürg Billeter's avatar
Jürg Billeter committed
45
	public override TypeSymbol get_type_declaration () {
46 47 48
		return class_reference;
	}
	
49
	public override string get_type_struct_name () {
50 51
		return "%sClass".printf (class_reference.get_cname ());
	}
52

53
	public override string get_base_init_func_name () {
54
		if (class_reference.class_constructor != null || class_reference.has_class_private_fields) {
55 56 57 58
			return "%s_base_init".printf (class_reference.get_lower_case_cname (null));
		} else {
			return "NULL";
		}
59 60
	}

61 62 63 64 65 66 67 68
	public override string get_base_finalize_func_name () {
		if (class_reference.has_class_private_fields) {
			return "%s_base_finalize".printf (class_reference.get_lower_case_cname (null));
		} else {
			return "NULL";
		}
	}

69
	public override string get_class_init_func_name () {
70 71 72
		return "%s_class_init".printf (class_reference.get_lower_case_cname (null));
	}
	
73
	public override string get_instance_struct_size () {
74 75 76
		return "sizeof (%s)".printf (class_reference.get_cname ());
	}
	
77
	public override string get_instance_init_func_name () {
78
		return "%s_instance_init".printf (class_reference.get_lower_case_cname (null));
79 80
	}
	
81
	public override string get_parent_type_name () {
Jürg Billeter's avatar
Jürg Billeter committed
82
		return class_reference.base_class.get_type_id ();
83 84
	}

85 86 87 88 89 90 91 92
	public override string get_type_flags () {
		if (class_reference.is_abstract) {
			return "G_TYPE_FLAG_ABSTRACT";
		} else {
			return "0";
		}
	}

93
	public override SymbolAccessibility get_accessibility () {
94 95 96
		return class_reference.access;
	}

97
	public override string? get_gtype_value_table_init_function_name () {
98
		bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
99
		if ( is_fundamental )
100
			return "%s_init".printf (class_reference.get_lower_case_cname ("value_"));
101 102 103 104 105

		return null;
	}

	public override string? get_gtype_value_table_free_function_name () {
106
		bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
107
		if ( is_fundamental )
108
			return "%s_free_value".printf (class_reference.get_lower_case_cname ("value_"));
109 110 111 112 113

		return null;
	}

	public override string? get_gtype_value_table_copy_function_name () {
114
		bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
115
		if ( is_fundamental )
116
			return "%s_copy_value".printf (class_reference.get_lower_case_cname ("value_"));
117 118 119 120 121

		return null;
	}

	public override string? get_gtype_value_table_peek_pointer_function_name () {
122
		bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
123
		if ( is_fundamental )
124
			return "%s_peek_pointer".printf (class_reference.get_lower_case_cname ("value_"));
125 126 127 128 129

		return null;
	}

	public override string? get_gtype_value_table_collect_value_function_name () {
130
		bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
131
		if ( is_fundamental )
132
			return "%s_collect_value".printf (class_reference.get_lower_case_cname ("value_"));
133 134 135 136 137

		return null;
	}

	public override string? get_gtype_value_table_lcopy_value_function_name () {
138
		bool is_fundamental = !class_reference.is_compact && class_reference.base_class == null;
139
		if ( is_fundamental )
140
			return "%s_lcopy_value".printf (class_reference.get_lower_case_cname ("value_"));
141 142 143 144

		return null;
	}

145
	public override CCodeFragment get_type_interface_init_declaration () {
146
		var frag = new CCodeFragment ();
147
		
148
		foreach (DataType base_type in class_reference.get_base_types ()) {
149
			if (!(base_type.data_type is Interface)) {
150 151 152
				continue;
			}
			
153
			var iface = (Interface) base_type.data_type;
154 155 156
			
			var iface_info_name = "%s_info".printf (iface.get_lower_case_cname (null));
			
157
			var ctypedecl = new CCodeDeclaration ("const GInterfaceInfo");
158
			ctypedecl.modifiers = CCodeModifiers.STATIC;
159
			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)))));
160
			frag.append (ctypedecl);
161 162 163 164 165
		}
		
		return frag;
	}

166
	public override CCodeFragment get_type_interface_init_statements () {
167 168
		var frag = new CCodeFragment ();
		
169
		foreach (DataType base_type in class_reference.get_base_types ()) {
170 171 172 173 174 175 176 177
			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));
			
178
			var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_interface_static"));
179
			reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (class_reference.get_lower_case_cname (null))));
Jürg Billeter's avatar
Jürg Billeter committed
180
			reg_call.add_argument (new CCodeIdentifier (iface.get_type_id ()));
181 182
			reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
			frag.append (new CCodeExpressionStatement (reg_call));
183 184
		}
		
185
		return frag;
186 187
	}
}