Home Contact

[July 28, 2005]

From C++ to ActionScript, Chapter 1: Enumerated Types, Generated

Filed under: ActionScript — @ 8:49 pm

Note: This post has been superseded by Doh! Enumerated Types Simplified.

Perhaps I'm being foolish, but as part of a "research" project, I'm attempting to translate a class library from C++ to AS2. So I came across an enum, and I was wondering what to do with it.

A bit of googling revealed that last year Darron Schall presented an implementation of enums in ActionScript: Enumerated Types in ActionScript. It's a great thing to have in AS (and may be provided natively by Macromedia in the future, as the keyword enum is reserved), but I wasn't quite satisfied with it because each enum is not a distinct type, but just an instance of Darron's EnumeratedType class that you have to create explicitly in code that's going to use it, like so:

days = new EnumeratedType("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

I’ve built on Darron's class to create something a little more Type-y.

First of all, I wanted each enum to be a distinct type:

class DaysOfWeek extends EnumeratedType

And I wanted each value to be accessed as a member of the type, as DaysOfWeek.Sunday, for example. So once again (as in my previous post) I'm thinking Singleton:

private static var _instance:DaysOfWeek = undefined;

private function DaysOfWeek()
{
	super("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
}

and access to each value is provided like so:

public static function get Sunday ():Number

{
	if (_instance == undefined)
	{
		_instance = new DaysOfWeek();
	}
	return _instance["Sunday"];
}

Now it would be tedious to write out all seven accessors. So I've written an enum generator in Perl, and here it is. (You'll want to replace the first line with the path to Perl in your environment.)

#!/c/Perl/bin/perl

# genEnum.pl
# This script generates an ActionScript class representing an enumerated type.
# Alan Shaw, 2005 http://nodename.com/blog/2005/07/28/jenny-noom/
# Based on (and requires) the EnumeratedType class by Darron Schall http://www.darronschall.com/weblog/archives/000097.cfm

sub usage {
	print STDERR "Usage: $0 className value1 value2 ...\n";
	exit;
}

usage() if (scalar @ARGV < 2);

$className = @ARGV[0];
shift;
@values = @ARGV;

$fileName = "$className.as";

if (-e $fileName) {
	print STDERR "File $fileName exists; overwrite? (type Y for yes):\n";
LINE:	while (<STDIN>) {
		last LINE if (/^Y$/);
		exit;
	}
}

print STDERR "Creating $fileName\n";

open(ASFILE, ">", "$fileName") or die "Can’t create $fileName: $!";

print ASFILE "import com.darronschall.weblog.EnumeratedType;\n\n";
print ASFILE "class $className extends EnumeratedType\n";
print ASFILE "{\n\n\n";
foreach $value (@values) {
	print ASFILE "public static function get $value ():Number\n";
	print ASFILE "{\n";
	print ASFILE "\tif (_instance == undefined)\n";
	print ASFILE "\t{\n";
	print ASFILE "\t\t_instance = new $className();\n";
	print ASFILE "\t}\n";
	print ASFILE "\treturn _instance[\"$value\"];\n";
	print ASFILE "}\n\n\n";
}
print ASFILE "private static var _instance:$className = undefined;\n\n\n";
print ASFILE "private function $className()\n";
print ASFILE "{\n";
print ASFILE "\tsuper(";
$i = 0;
while (@values[$i + 1]) {
	print ASFILE "\"@values[$i]\", ";
	$i++;
};
print ASFILE "\"@values[$i]\");\n";
print ASFILE "}\n";
print ASFILE "\n\n}\n";

close ASFILE;

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment


Contents copyright © Alan Shaw 2005-2008

25 queries. 0.223 seconds. Powered by WordPress version 2.5.1