update toolbox when reversing a way, or when adding nodes during drawway
[potlatch2.git] / hxasm / Type.as
1 package hxasm {
2         import flash.utils.getDefinitionByName;
3         import flash.utils.getQualifiedClassName;
4         import flash.utils.describeType;
5         import flash.utils.getQualifiedSuperclassName;
6         public class Type {
7                 static public function toEnum(t : *) : Class {
8                         try {
9                                 if(!t.__isenum) return null;
10                                 return t;
11                         }
12                         catch( e : * ){
13                                 null;
14                         }
15                         return null;
16                 }
17                 static public function toClass(t : *) : Class {
18                         try {
19                                 if(!t.hasOwnProperty("prototype")) return null;
20                                 return t;
21                         }
22                         catch( e : * ){
23                                 null;
24                         }
25                         return null;
26                 }
27                 static public function getClass(o : *) : Class {
28                         var cname : String = getQualifiedClassName(o);
29                         if(cname == "null" || cname == "Object" || cname == "int" || cname == "Number" || cname == "Boolean") return null;
30                         if(o.hasOwnProperty("prototype")) return null;
31                         var c : * = getDefinitionByName(cname) as Class;
32                         if(c.__isenum) return null;
33                         return c;
34                 }
35                 static public function getEnum(o : *) : Class {
36                         var cname : String = getQualifiedClassName(o);
37                         if(cname == "null" || cname.substr(0,8) == "builtin.") return null;
38                         if(o.hasOwnProperty("prototype")) return null;
39                         var c : * = getDefinitionByName(cname) as Class;
40                         if(!c.__isenum) return null;
41                         return c;
42                 }
43                 static public function getSuperClass(c : Class) : Class {
44                         var cname : String = getQualifiedSuperclassName(c);
45                         if(cname == "Object") return null;
46                         return getDefinitionByName(cname) as Class;
47                 }
48                 static public function getClassName(c : Class) : String {
49                         if(c == null) return null;
50                         var str : String = getQualifiedClassName(c);
51                         return str.split("::").join(".");
52                 }
53                 static public function getEnumName(e : Class) : String {
54                         var n : String = getQualifiedClassName(e);
55                         return n;
56                 }
57                 static public function resolveClass(name : String) : Class {
58                         var cl : Class;
59                         {
60                                 try {
61                                         cl = getDefinitionByName(name) as Class;
62                                         if(cl.__isenum) return null;
63                                         return cl;
64                                 }
65                                 catch( e : * ){
66                                         return null;
67                                 }
68                                 if(cl == null || cl.__name__ == null) return null;
69                                 else null;
70                         }
71                         return cl;
72                 }
73                 static public function resolveEnum(name : String) : Class {
74                         var e : *;
75                         {
76                                 try {
77                                         e = getDefinitionByName(name);
78                                         if(!e.__isenum) return null;
79                                         return e;
80                                 }
81                                 catch( e1 : * ){
82                                         return null;
83                                 }
84                                 if(e == null || e.__ename__ == null) return null;
85                                 else null;
86                         }
87                         return e;
88                 }
89                 static public function createInstance(cl : Class,args : Array) : * {
90                         return function() : * {
91                                 var $r : *;
92                                 switch(args.length) {
93                                 case 0:{
94                                         $r = new cl();
95                                 }break;
96                                 case 1:{
97                                         $r = new cl(args[0]);
98                                 }break;
99                                 case 2:{
100                                         $r = new cl(args[0],args[1]);
101                                 }break;
102                                 case 3:{
103                                         $r = new cl(args[0],args[1],args[2]);
104                                 }break;
105                                 case 4:{
106                                         $r = new cl(args[0],args[1],args[2],args[3]);
107                                 }break;
108                                 case 5:{
109                                         $r = new cl(args[0],args[1],args[2],args[3],args[4]);
110                                 }break;
111                                 default:{
112                                         $r = function() : * {
113                                                 var $r2 : *;
114                                                 throw "Too many arguments";
115                                                 return $r2;
116                                         }();
117                                 }break;
118                                 }
119                                 return $r;
120                         }();
121                 }
122                 static public function getEnumConstructs(e : Class) : Array {
123                         return e.__constructs__;
124                 }
125                 static public function enumEq(a : *,b : *) : Boolean {
126                         if(a == b) return true;
127                         try {
128                                 if(a.tag != b.tag) return false;
129                                 {
130                                         var _g1 : int = 0, _g : int = a.params.length;
131                                         while(_g1 < _g) {
132                                                 var i : int = _g1++;
133                                                 if(!enumEq(a.params[i],b.params[i])) return false;
134                                         }
135                                 }
136                         }
137                         catch( e : * ){
138                                 return false;
139                         }
140                         return true;
141                 }
142                 static public function enumConstructor(e : *) : String {
143                         return e.tag;
144                 }
145                 static public function enumParameters(e : *) : Array {
146                         return (e.params == null?[]:e.params);
147                 }
148                 static public function enumIndex(e : *) : int {
149                         return e.index;
150                 }
151         }
152 }