diff --git a/src/as/com/threerings/util/ByteEnum.as b/src/as/com/threerings/util/ByteEnum.as new file mode 100644 index 000000000..d6dee554f --- /dev/null +++ b/src/as/com/threerings/util/ByteEnum.as @@ -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; +} +} diff --git a/src/as/com/threerings/util/ByteEnumUtil.as b/src/as/com/threerings/util/ByteEnumUtil.as new file mode 100644 index 000000000..0d5c67e4e --- /dev/null +++ b/src/as/com/threerings/util/ByteEnumUtil.as @@ -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); + } +} +}