Hi,

Please consider applying the attached patch. It adds support for
using foreach with enums like this (and closes #624691):

> foreach (Enum e in Enum.all_values) {
>     stdout.printf ("%s\n", e.to_string ());
> }

-- Sebastian
From f12ba46d78d1b3f448f8ef502b4bce970eced20f Mon Sep 17 00:00:00 2001
From: Sebastian Reichel <[email protected]>
Date: Wed, 8 Feb 2012 07:06:44 +0100
Subject: [PATCH] add .all_values constant to enums returning all values

enum.all_values can be used to get a constant enum[] with
all values specified in the enum. This allows to use foreach
with enums like this:

foreach (Enum e in Enum.all_values) {
	stdout.printf ("%s\n", e.to_string ());
}
---
 vala/valaenum.vala |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/vala/valaenum.vala b/vala/valaenum.vala
index eeabda9..5130cd7 100644
--- a/vala/valaenum.vala
+++ b/vala/valaenum.vala
@@ -44,6 +44,8 @@ public class Vala.Enum : TypeSymbol {
 
 	private bool? _is_flags;
 
+	private InitializerList? all_constant;
+
 	/**
 	 * Creates a new enum.
 	 *
@@ -53,6 +55,7 @@ public class Vala.Enum : TypeSymbol {
 	 */
 	public Enum (string name, SourceReference? source_reference = null, Comment? comment = null) {
 		base (name, source_reference, comment);
+		generate_all_constant (source_reference);
 	}
 	
 	/**
@@ -65,6 +68,7 @@ public class Vala.Enum : TypeSymbol {
 
 		values.add (value);
 		scope.add (value.name, value);
+		all_constant.append (new MemberAccess (null, value.name));
 	}
 
 	/**
@@ -188,4 +192,12 @@ public class Vala.Enum : TypeSymbol {
 
 		return !error;
 	}
+
+	private void generate_all_constant (SourceReference? source_reference = null) {
+		var type = new ArrayType (new EnumValueType (this), 1, source_reference);
+		all_constant = new InitializerList (source_reference);
+		var all = new Constant ("all_values", type, all_constant, source_reference);
+		all.access = SymbolAccessibility.PUBLIC;
+		add_constant (all);
+	}
 }
-- 
1.7.8.3

Attachment: signature.asc
Description: Digital signature

_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to