Line data Source code
1 : /* valaattribute.vala
2 : *
3 : * Copyright (C) 2006-2008 Jürg Billeter
4 : *
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.1 of the License, or (at your option) any later version.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents an attribute specified in the source code.
27 : */
28 7384265 : public class Vala.Attribute : CodeNode {
29 : /**
30 : * The name of the attribute type.
31 : */
32 145500046 : public string name { get; private set; }
33 :
34 : /**
35 : * Contains all specified attribute arguments.
36 : */
37 19382565 : public Vala.Map<string,string> args { get; private set; }
38 :
39 : /**
40 : * Creates a new attribute.
41 : *
42 : * @param name attribute type name
43 : * @param source_reference reference to source code
44 : * @return newly created attribute
45 : */
46 14765458 : public Attribute (string name, SourceReference? source_reference = null) {
47 7382729 : this.name = name;
48 7382729 : this.source_reference = source_reference;
49 7382729 : this.args = new HashMap<string,string> (str_hash, str_equal);
50 :
51 7382729 : if (!CodeContext.get ().deprecated) {
52 7382729 : if (name == "Deprecated") {
53 0 : Report.deprecated (source_reference, "[Deprecated] is deprecated. Use [Version (deprecated = true, deprecated_since = \"\", replacement = \"\")]");
54 7382729 : } else if (name == "Experimental") {
55 0 : Report.deprecated (source_reference, "[Experimental] is deprecated. Use [Version (experimental = true, experimental_until = \"\")]");
56 : }
57 : }
58 : }
59 :
60 : /**
61 : * Adds an attribute argument.
62 : *
63 : * @param key argument name
64 : * @param value argument value
65 : */
66 9702639 : public void add_argument (string key, string value) {
67 9702639 : args.set (key, value);
68 : }
69 :
70 : /**
71 : * Returns whether this attribute has the specified named argument.
72 : *
73 : * @param name argument name
74 : * @return true if the argument has been found, false otherwise
75 : */
76 1284573 : public bool has_argument (string name) {
77 1284573 : return args.contains (name);
78 : }
79 :
80 : /**
81 : * Returns the string value of the specified named argument.
82 : *
83 : * @param name argument name
84 : * @return string value
85 : */
86 860765 : public string? get_string (string name, string? default_value = null) {
87 860765 : string value = args.get (name);
88 :
89 860765 : if (value == null) {
90 1174392 : return default_value;
91 : }
92 :
93 : /* remove quotes */
94 273569 : var noquotes = value.substring (1, (uint) (value.length - 2));
95 : /* unescape string */
96 273569 : return noquotes.compress ();
97 : }
98 :
99 : /**
100 : * Returns the integer value of the specified named argument.
101 : *
102 : * @param name argument name
103 : * @return integer value
104 : */
105 52381 : public int get_integer (string name, int default_value = 0) {
106 52381 : string value = args.get (name);
107 :
108 52381 : if (value == null) {
109 36429 : return default_value;
110 : }
111 :
112 15952 : return int.parse (value);
113 : }
114 :
115 : /**
116 : * Returns the double value of the specified named argument.
117 : *
118 : * @param name argument name
119 : * @return double value
120 : */
121 2673 : public double get_double (string name, double default_value = 0) {
122 2673 : string value = args.get (name);
123 :
124 2673 : if (value == null) {
125 2052 : return default_value;
126 : }
127 :
128 621 : return double.parse (value);
129 : }
130 :
131 : /**
132 : * Returns the boolean value of the specified named argument.
133 : *
134 : * @param name argument name
135 : * @return boolean value
136 : */
137 1741314 : public bool get_bool (string name, bool default_value = false) {
138 1741314 : string value = args.get (name);
139 :
140 1741314 : if (value == null) {
141 1667692 : return default_value;
142 : }
143 :
144 73622 : return bool.parse (value);
145 : }
146 : }
|