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:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user