OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 class EnumAlias { |
| 6 EnumAlias(GoogleProtobuf_EnumValueDescriptorProto this.value, |
| 7 GoogleProtobuf_EnumValueDescriptorProto this.canonicalValue); |
| 8 GoogleProtobuf_EnumValueDescriptorProto value; |
| 9 GoogleProtobuf_EnumValueDescriptorProto canonicalValue; |
| 10 } |
| 11 |
| 12 class EnumGenerator implements ProtobufContainer { |
| 13 |
| 14 // Whitespace |
| 15 static final String sp = MessageGenerator.sp; |
| 16 |
| 17 EnumGenerator(GoogleProtobuf_EnumDescriptorProto this._descriptor, |
| 18 ProtobufContainer this._parent, GenerationContext this._context) |
| 19 : _canonicalValues = [], _aliases = [] { |
| 20 _classname = _parent === null ? |
| 21 _descriptor.name : |
| 22 "${_parent.classname}_${_descriptor.name}"; |
| 23 _fqname = _parent === null ? |
| 24 _descriptor.name : |
| 25 "${_parent.fqname}.${_descriptor.name}"; |
| 26 _optimizeFor = _parent != null ? _parent.optimizeFor : null; |
| 27 |
| 28 for (GoogleProtobuf_EnumValueDescriptorProto value in _descriptor.value) { |
| 29 GoogleProtobuf_EnumValueDescriptorProto canonicalValue = |
| 30 findValueByNumber(value.number); |
| 31 if (value === canonicalValue) { |
| 32 _canonicalValues.add(value); |
| 33 } else { |
| 34 _aliases.add(new EnumAlias(value, canonicalValue)); |
| 35 } |
| 36 } |
| 37 _context.register(this); |
| 38 } |
| 39 |
| 40 GoogleProtobuf_EnumValueDescriptorProto findValueByNumber(int number) { |
| 41 for (GoogleProtobuf_EnumValueDescriptorProto value in _descriptor.value) { |
| 42 if (value.number == number) return value; |
| 43 } |
| 44 return null; |
| 45 } |
| 46 |
| 47 /* |
| 48 * Not clear on the Reflection-labeled section. |
| 49 */ |
| 50 void generate(IndentingWriter out) { |
| 51 out.addBlock("class ${_classname} extends ProtobufEnum " |
| 52 "implements Hashable {", "}\n", () { |
| 53 // ----------------------------------------------------------------- |
| 54 // define enum types |
| 55 int index = 0; |
| 56 for (GoogleProtobuf_EnumValueDescriptorProto val in _canonicalValues) { |
| 57 out.println("static ${_classname} get ${val.name}()${sp}=>" |
| 58 "${sp}values[${index++}];"); |
| 59 } |
| 60 out.println("static List<${_classname}> _byIndex;"); |
| 61 out.println("static Map<int, ${_classname}> _byValue;"); |
| 62 out.blankLine(); |
| 63 out.println("static void _init()${sp}{"); |
| 64 out.println("${sp}${sp}_byIndex${sp}=${sp}const${sp}["); |
| 65 index = 0; |
| 66 for (GoogleProtobuf_EnumValueDescriptorProto val in _canonicalValues) { |
| 67 out.println("${sp}${sp}${sp}${sp}const ${_classname}._(${index++}," |
| 68 "${sp}${val.number},${sp}'${val.name}'),"); |
| 69 } |
| 70 out.println("${sp}${sp}];"); |
| 71 out.println("${sp}${sp}_byValue = " |
| 72 "ProtobufEnum.initByValue(_byIndex);"); |
| 73 out.println("}"); |
| 74 out.blankLine(); |
| 75 out.println("static List<${_classname}> get values()${sp}{"); |
| 76 out.println("${sp}${sp}if (null${sp}==${sp}_byIndex)${sp}_init();"); |
| 77 out.println("${sp}${sp}return${sp}_byIndex;"); |
| 78 out.println("}"); |
| 79 out.blankLine(); |
| 80 out.println("static ${_classname} valueOf(int value)${sp}{"); |
| 81 out.println("${sp}${sp}if (null${sp}==${sp}_byValue)${sp}_init();"); |
| 82 out.println("${sp}${sp}return _byValue[value];"); |
| 83 out.println("}"); |
| 84 |
| 85 // ----------------------------------------------------------------- |
| 86 // define aliases and _VALUE fields |
| 87 if (!_aliases.isEmpty()) { |
| 88 out.blankLine(); |
| 89 } |
| 90 for (EnumAlias alias in _aliases) { |
| 91 out.println("static final ${_classname} ${alias.value.name} =" |
| 92 " ${alias.canonicalValue.name};"); |
| 93 } |
| 94 out.blankLine(); |
| 95 |
| 96 // ----------------------------------------------------------------- |
| 97 // define equals operator |
| 98 out.addBlock("bool operator ==(Object o) {", "}", () { |
| 99 out.println("if (o is ${_classname}) {"); |
| 100 out.println(" ${_classname} p = o;"); |
| 101 out.println(" return value == p.value;"); |
| 102 out.println("} else {"); |
| 103 out.println(" return false;"); |
| 104 out.println("}"); |
| 105 }); |
| 106 out.blankLine(); |
| 107 |
| 108 // ----------------------------------------------------------------- |
| 109 // constructor |
| 110 out.println("const ${_classname}._(int i,${sp}int v,${sp}String n)${sp}" |
| 111 ":${sp}super(i,${sp}v,${sp}n);"); |
| 112 }); |
| 113 } |
| 114 |
| 115 String get classname() => _classname; |
| 116 String get fqname() => _fqname; |
| 117 GoogleProtobuf_FileOptions_OptimizeMode get optimizeFor() => _optimizeFor; |
| 118 |
| 119 GoogleProtobuf_EnumDescriptorProto _descriptor; |
| 120 ProtobufContainer _parent; |
| 121 GenerationContext _context; |
| 122 String _classname; |
| 123 String _fqname; |
| 124 List<GoogleProtobuf_EnumValueDescriptorProto> _canonicalValues; |
| 125 List<EnumAlias> _aliases; |
| 126 GoogleProtobuf_FileOptions_OptimizeMode _optimizeFor; |
| 127 } |
OLD | NEW |