remove obsolete ref modifier and callback keyword
[vala-lang.git] / vala / valainterface.vala
blob5534bbe748741ae0a9f0767e097c7053f1b0ece9
1 /* valainterface.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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a class declaration in the source code.
28 public class Vala.Interface : DataType {
29 /**
30 * Specifies whether this interface is static. Static interfaces are not
31 * available at run-time. They can be implemented by structs.
33 public bool is_static { get; set; }
35 private List<TypeParameter> type_parameters;
37 private List<TypeReference> prerequisites;
39 private List<Method> methods;
40 private List<Property> properties;
41 private List<Signal> signals;
43 private string cname;
44 private string lower_case_csuffix;
45 private string type_cname;
46 private string type_id;
48 /**
49 * Creates a new interface.
51 * @param name type name
52 * @param source reference to source code
53 * @return newly created interface
55 public Interface (string! _name, SourceReference source = null) {
56 name = _name;
57 source_reference = source;
60 /**
61 * Appends the specified parameter to the list of type parameters.
63 * @param p a type parameter
65 public void add_type_parameter (TypeParameter! p) {
66 type_parameters.append (p);
67 p.type = this;
70 /**
71 * Adds the specified interface or class to the list of prerequisites of
72 * this interface.
74 * @param type an interface or class reference
76 public void add_prerequisite (TypeReference! type) {
77 prerequisites.append (type);
80 /**
81 * Returns a copy of the base type list.
83 * @return list of base types
85 public List<weak TypeReference> get_prerequisites () {
86 return prerequisites.copy ();
89 /**
90 * Adds the specified method as a member to this interface.
92 * @param m a method
94 public void add_method (Method! m) {
95 methods.append (m);
98 /**
99 * Returns a copy of the list of methods.
101 * @return list of methods
103 public List<weak Method> get_methods () {
104 return methods.copy ();
108 * Adds the specified property as a member to this interface.
110 * @param prop a property
112 public void add_property (Property! prop) {
113 properties.append (prop);
117 * Returns a copy of the list of properties.
119 * @return list of properties
121 public List<weak Property> get_properties () {
122 return properties.copy ();
126 * Adds the specified signal as a member to this interface.
128 * @param sig a signal
130 public void add_signal (Signal! sig) {
131 signals.append (sig);
135 * Returns a copy of the list of signals.
137 * @return list of signals
139 public List<weak Signal> get_signals () {
140 return signals.copy ();
143 public override string get_cname (bool const_type = false) {
144 if (cname == null) {
145 cname = "%s%s".printf (@namespace.get_cprefix (), name);
147 return cname;
151 * Returns the string to be prepended to the name of members of this
152 * interface when used in C code.
154 * @return the suffix to be used in C code
156 public string! get_lower_case_csuffix () {
157 if (lower_case_csuffix == null) {
158 lower_case_csuffix = Namespace.camel_case_to_lower_case (name);
160 return lower_case_csuffix;
164 * Sets the string to be prepended to the name of members of this
165 * interface when used in C code.
167 * @param csuffix the suffix to be used in C code
169 public void set_lower_case_csuffix (string! csuffix) {
170 this.lower_case_csuffix = csuffix;
173 public override string get_lower_case_cname (string infix) {
174 if (infix == null) {
175 infix = "";
177 return "%s%s%s".printf (@namespace.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
180 public override string get_lower_case_cprefix () {
181 return "%s_".printf (get_lower_case_cname (null));
184 public override string get_upper_case_cname (string infix) {
185 return get_lower_case_cname (infix).up ();
188 public override void accept (CodeVisitor! visitor) {
189 visitor.visit_interface (this);
192 public override void accept_children (CodeVisitor! visitor) {
193 foreach (TypeReference type in prerequisites) {
194 type.accept (visitor);
197 foreach (TypeParameter p in type_parameters) {
198 p.accept (visitor);
201 foreach (Method m in methods) {
202 m.accept (visitor);
205 foreach (Property prop in properties) {
206 prop.accept (visitor);
209 foreach (Signal sig in signals) {
210 sig.accept (visitor);
214 public override bool is_reference_type () {
215 return true;
218 public override bool is_reference_counting () {
219 return true;
222 public override string get_ref_function () {
223 return "g_object_ref";
226 public override string get_unref_function () {
227 return "g_object_unref";
230 public override bool is_subtype_of (DataType! t) {
231 foreach (TypeReference prerequisite in prerequisites) {
232 if (prerequisite.data_type == t ||
233 prerequisite.data_type.is_subtype_of (t)) {
234 return true;
238 return false;
241 private void process_ccode_attribute (Attribute! a) {
242 if (a.has_argument ("type_cname")) {
243 set_type_cname (a.get_string ("type_cname"));
248 * Process all associated attributes.
250 public void process_attributes () {
251 foreach (Attribute a in attributes) {
252 if (a.name == "CCode") {
253 process_ccode_attribute (a);
259 * Returns the name of the type struct as it is used in C code.
261 * @return the type struct name to be used in C code
263 public string get_type_cname () {
264 if (type_cname == null) {
265 type_cname = "%sIface".printf (get_cname ());
267 return type_cname;
271 * Sets the name of the type struct as it is used in C code.
273 * @param type_cname the type struct name to be used in C code
275 public void set_type_cname (string! type_cname) {
276 this.type_cname = type_cname;
279 public override string get_marshaller_type_name () {
280 return "OBJECT";
283 public override string get_get_value_function () {
284 return "g_value_get_object";
287 public override string get_set_value_function () {
288 return "g_value_set_object";
291 public override string get_type_id () {
292 if (type_id == null) {
293 type_id = get_upper_case_cname ("TYPE_");
296 return type_id;
299 public override int get_type_parameter_index (string! name) {
300 int i = 0;
301 foreach (TypeParameter parameter in type_parameters) {
302 if (parameter.name == name) {
303 return i;
305 i++;
307 return -1;