Added ByteEnum interface to ActionScript, and ByteEnumUtil.

Use this if you want your enums to persist to a byte reliably.
Coming soonesque: narya streaming support for ByteEnum.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5859 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-07-11 00:08:41 +00:00
parent 6dbdf8d413
commit 83ce3f004e
2 changed files with 42 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
//
// $Id$
package com.threerings.util {
/**
* An enum value that can be persisted as a byte.
*/
public interface ByteEnum
{
/**
* Return the byte form of this enum.
*/
function toByte () :int;
}
}
@@ -0,0 +1,26 @@
//
// $Id$
package com.threerings.util {
/**
* ByteEnum utility methods.
*/
public class ByteEnumUtil
{
/**
* Returns the enum value with the specified code in the supplied enum class.
* Throws ArgumentError if the enum lacks a value that maps to the supplied code.
*/
public static function fromByte (clazz :Class, code :int) :Enum
{
// we could do something fancier than this O(n) impl, in the future...
for each (var e :Enum in Enum.values(clazz)) {
if (ByteEnum(e).toByte() == code) {
return e;
}
}
throw new ArgumentError(clazz + " has no value with code " + code);
}
}
}