[vala/wip/attributes: 4/9] Add helper methods to CodeNode for dealing with attributes
- From: Luca Bruno <lucabru src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/attributes: 4/9] Add helper methods to CodeNode for dealing with attributes
- Date: Wed, 6 Jul 2011 08:35:59 +0000 (UTC)
commit b20662d42a6e15523f7d844b140ea7045a1ce0ec
Author: Luca Bruno <lucabru src gnome org>
Date: Wed Jul 6 10:25:41 2011 +0200
Add helper methods to CodeNode for dealing with attributes
vala/valacodenode.vala | 193 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 193 insertions(+), 0 deletions(-)
---
diff --git a/vala/valacodenode.vala b/vala/valacodenode.vala
index 42f9402..2aa001f 100644
--- a/vala/valacodenode.vala
+++ b/vala/valacodenode.vala
@@ -150,6 +150,199 @@ public abstract class Vala.CodeNode {
}
/**
+ * Sets the specified named attribute to this code node.
+ *
+ * @param name attribute name
+ * @param value true to add the attribute, false to remove it
+ */
+ public void set_attribute (string name, bool value, SourceReference? source_reference = null) {
+ var a = get_attribute (name);
+ if (value && a == null) {
+ if (attributes == null) {
+ attributes = new HashMap<string, Attribute> (str_hash, str_equal);
+ }
+ attributes.set (name, new Attribute (name, source_reference));
+ } else if (!value && a != null) {
+ attributes.remove (name);
+ }
+ }
+
+ /**
+ * Remove the specified named attribute argument
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ */
+ public void remove_attribute_argument (string attribute, string argument) {
+ var a = get_attribute (attribute);
+ if (a != null) {
+ a.args.remove (argument);
+ if (a.args.size == 0) {
+ attributes.remove (attribute);
+ }
+ }
+ }
+
+ /**
+ * Returns the string value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @return string value
+ */
+ public string? get_attribute_string (string attribute, string argument) {
+ if (attributes == null) {
+ return null;
+ }
+ var a = get_attribute (attribute);
+ if (a == null) {
+ return null;
+ }
+ return a.get_string (argument);
+ }
+
+ /**
+ * Returns the integer value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @return integer value
+ */
+ public int get_attribute_integer (string attribute, string argument, int default_value = 0) {
+ if (attributes == null) {
+ return default_value;
+ }
+ var a = get_attribute (attribute);
+ if (a == null) {
+ return default_value;
+ }
+ return a.get_integer (argument, default_value);
+ }
+
+ /**
+ * Returns the double value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @return double value
+ */
+ public double get_attribute_double (string attribute, string argument, double default_value = 0) {
+ if (attributes == null) {
+ return default_value;
+ }
+ var a = get_attribute (attribute);
+ if (a == null) {
+ return default_value;
+ }
+ return a.get_double (argument, default_value);
+ }
+
+ /**
+ * Returns the bool value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @return bool value
+ */
+ public bool get_attribute_bool (string attribute, string argument, bool default_value = false) {
+ if (attributes == null) {
+ return default_value;
+ }
+ var a = get_attribute (attribute);
+ if (a == null) {
+ return default_value;
+ }
+ return a.get_bool (argument, default_value);
+ }
+
+ /**
+ * Sets the string value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @param value string value
+ */
+ public void set_attribute_string (string attribute, string argument, string value, SourceReference? source_reference = null) {
+ Attribute a = null;
+ if (attributes == null) {
+ attributes = new HashMap<string, Attribute> (str_hash, str_equal);
+ } else {
+ a = attributes.get (attribute);
+ }
+ if (a == null) {
+ a = new Attribute (attribute, source_reference);
+ attributes.set (attribute, a);
+ }
+ a.add_argument (argument, "\"%s\"".printf (value));
+ }
+
+ /**
+ * Sets the integer value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @param value integer value
+ */
+ public void set_attribute_integer (string attribute, string argument, int value, SourceReference? source_reference = null) {
+ Attribute a = null;
+ if (attributes == null) {
+ attributes = new HashMap<string, Attribute> (str_hash, str_equal);
+ } else {
+ a = attributes.get (attribute);
+ }
+ a = get_attribute (attribute);
+ if (a == null) {
+ a = new Attribute (attribute, source_reference);
+ attributes.set (attribute, a);
+ }
+ a.add_argument (argument, value.to_string ());
+ }
+
+ /**
+ * Sets the integer value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @param value double value
+ */
+ public void set_attribute_double (string attribute, string argument, double value, SourceReference? source_reference = null) {
+ Attribute a = null;
+ if (attributes == null) {
+ attributes = new HashMap<string, Attribute> (str_hash, str_equal);
+ } else {
+ a = attributes.get (attribute);
+ }
+ a = get_attribute (attribute);
+ if (a == null) {
+ a = new Attribute (attribute, source_reference);
+ attributes.set (attribute, a);
+ }
+ a.add_argument (argument, "%g".printf (value));
+ }
+
+ /**
+ * Sets the boolean value of the specified attribute argument.
+ *
+ * @param attribute attribute name
+ * @param argument argument name
+ * @param value bool value
+ */
+ public void set_attribute_bool (string attribute, string argument, bool value, SourceReference? source_reference = null) {
+ Attribute a = null;
+ if (attributes == null) {
+ attributes = new HashMap<string, Attribute> (str_hash, str_equal);
+ } else {
+ a = attributes.get (attribute);
+ }
+ a = get_attribute (attribute);
+ if (a == null) {
+ a = new Attribute (attribute, source_reference);
+ attributes.set (attribute, a);
+ }
+ a.add_argument (argument, value.to_string ());
+ }
+
+ /**
* Returns the attribute cache at the specified index.
*
* @param index attribute cache index
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]