| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
package smetana.core; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.List; |
| 41 |
|
| 42 |
public class CEnumInterpretor { |
| 43 |
|
| 44 |
private final List<String> keys = new ArrayList<String>(); |
| 45 |
private String keyRef; |
| 46 |
private int valueRef; |
| 47 |
|
| 48 |
public CEnumInterpretor(Class enumClass) { |
| 49 |
List<String> def = CType.getDefinition(enumClass); |
| 50 |
JUtils.LOG("def1=" + def); |
| 51 |
if (def.get(0).equals("typedef enum") == false) { |
| 52 |
throw new IllegalArgumentException(); |
| 53 |
} |
| 54 |
if (def.get(1).equals("{") == false) { |
| 55 |
throw new IllegalArgumentException(); |
| 56 |
} |
| 57 |
if (def.get(def.size() - 2).equals("}") == false) { |
| 58 |
throw new IllegalArgumentException(); |
| 59 |
} |
| 60 |
def = def.subList(2, def.size() - 2); |
| 61 |
JUtils.LOG("def2=" + def); |
| 62 |
|
| 63 |
for (String s1 : def) { |
| 64 |
for (String s2 : s1.split(",")) { |
| 65 |
s2 = s2.trim(); |
| 66 |
final int idx = s2.indexOf('='); |
| 67 |
final String k = idx == -1 ? s2 : s2.substring(0, idx); |
| 68 |
keys.add(k.trim()); |
| 69 |
if (idx == -1) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
if (keyRef != null) { |
| 73 |
throw new IllegalStateException(); |
| 74 |
} |
| 75 |
keyRef = k.trim(); |
| 76 |
valueRef = Integer.parseInt(s2.substring(idx + 1).trim()); |
| 77 |
} |
| 78 |
} |
| 79 |
JUtils.LOG("keys=" + keys); |
| 80 |
} |
| 81 |
|
| 82 |
public int valueOf(String name) { |
| 83 |
JUtils.LOG("keys=" + keys); |
| 84 |
final int idx = keys.indexOf(name); |
| 85 |
if (idx == -1) { |
| 86 |
throw new IllegalArgumentException(name + " is no enum value"); |
| 87 |
} |
| 88 |
if (keyRef == null) { |
| 89 |
return idx; |
| 90 |
} |
| 91 |
final int keyRefIndex = keys.indexOf(keyRef); |
| 92 |
if (keyRefIndex == -1) { |
| 93 |
throw new IllegalStateException(); |
| 94 |
} |
| 95 |
return idx - keyRefIndex + valueRef; |
| 96 |
} |
| 97 |
} |