Browse Source

added toEnum and toClass.

Nicolas Cannasse 19 years ago
parent
commit
bfd9d7a91b
1 changed files with 36 additions and 0 deletions
  1. 36 0
      std/Type.hx

+ 36 - 0
std/Type.hx

@@ -35,6 +35,42 @@ enum ValueType {
 **/
 **/
 class Type {
 class Type {
 
 
+	/**
+		Converts a value to an Enum or returns [null] if the value is not an Enum.
+	**/
+	public static function toEnum( t : Dynamic ) : Enum untyped {
+		try {
+		#if flash9
+			if( !t.__isenum )
+				return null;
+		#else true
+			if( t.__ename__ == null )
+				return null;
+		#end
+			return t;
+		} catch( e : Dynamic ) {
+			return null;
+		}
+	}
+	
+	/**
+		Converts a value to a Class or returns [null] if the value is not a Class.
+	**/
+	public static function toClass( t : Dynamic ) : Class untyped {
+		try {
+		#if flash9
+			if( !t.hasOwnProperty("prototype") )
+				return null;
+		#else true
+			if( t.__name__ == null )
+				return null;
+		#end
+			return t;
+		} catch( e : Dynamic ) {
+			return null;
+		}
+	}
+	
 	/**
 	/**
 		Returns the class of a value or [null] if this value is not a Class instance.
 		Returns the class of a value or [null] if this value is not a Class instance.
 	**/
 	**/