Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: lib/src/protobuf/coded_buffer_writer.dart

Issue 1277863003: cleanup: move fieldType constants and functions to a separate file (Closed) Base URL: git@github.com:dart-lang/dart-protobuf.git@master
Patch Set: _validateMessage should call _baseType, not callers Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/protobuf/builder_info.dart ('k') | lib/src/protobuf/field_info.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of protobuf; 5 part of protobuf;
6 6
7 class CodedBufferWriter { 7 class CodedBufferWriter {
8 8
9 final List<TypedData> _output = <TypedData>[]; 9 final List<TypedData> _output = <TypedData>[];
10 int _runningSizeInBytes = 0; 10 int _runningSizeInBytes = 0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 61
62 ByteData makeByteData32(int value) => 62 ByteData makeByteData32(int value) =>
63 new ByteData(4)..setUint32(0, value, Endianness.LITTLE_ENDIAN); 63 new ByteData(4)..setUint32(0, value, Endianness.LITTLE_ENDIAN);
64 64
65 ByteData makeByteData64(Int64 value) { 65 ByteData makeByteData64(Int64 value) {
66 var data = new Uint8List.fromList(value.toBytes()); 66 var data = new Uint8List.fromList(value.toBytes());
67 return new ByteData.view(data.buffer, 0, 8); 67 return new ByteData.view(data.buffer, 0, 8);
68 } 68 }
69 69
70 return new Map<int, dynamic>() 70 return new Map<int, dynamic>()
71 ..[GeneratedMessage._BOOL_BIT] = makeWriter( 71 ..[FieldType._BOOL_BIT] = makeWriter(
72 (value) => _int32ToBytes(value ? 1 : 0)) 72 (value) => _int32ToBytes(value ? 1 : 0))
73 ..[GeneratedMessage._BYTES_BIT] = writeBytesNoTag 73 ..[FieldType._BYTES_BIT] = writeBytesNoTag
74 ..[GeneratedMessage._STRING_BIT] = (output, value) { 74 ..[FieldType._STRING_BIT] = (output, value) {
75 writeBytesNoTag(output, _UTF8.encode(value)); 75 writeBytesNoTag(output, _UTF8.encode(value));
76 } 76 }
77 ..[GeneratedMessage._DOUBLE_BIT] = makeWriter((double value) { 77 ..[FieldType._DOUBLE_BIT] = makeWriter((double value) {
78 if (value.isNaN) return new ByteData(8) 78 if (value.isNaN) return new ByteData(8)
79 ..setUint32(0, 0x00000000, Endianness.LITTLE_ENDIAN) 79 ..setUint32(0, 0x00000000, Endianness.LITTLE_ENDIAN)
80 ..setUint32(4, 0x7ff80000, Endianness.LITTLE_ENDIAN); 80 ..setUint32(4, 0x7ff80000, Endianness.LITTLE_ENDIAN);
81 return new ByteData(8) 81 return new ByteData(8)
82 ..setFloat64(0, value, Endianness.LITTLE_ENDIAN); 82 ..setFloat64(0, value, Endianness.LITTLE_ENDIAN);
83 }) 83 })
84 ..[GeneratedMessage._FLOAT_BIT] = makeWriter((double value) { 84 ..[FieldType._FLOAT_BIT] = makeWriter((double value) {
85 const double MIN_FLOAT_DENORM = 1.401298464324817E-45; 85 const double MIN_FLOAT_DENORM = 1.401298464324817E-45;
86 const double MAX_FLOAT = 3.4028234663852886E38; 86 const double MAX_FLOAT = 3.4028234663852886E38;
87 // TODO(antonm): reevaluate once semantics of odd values 87 // TODO(antonm): reevaluate once semantics of odd values
88 // writes is clear. 88 // writes is clear.
89 if (value.isNaN) return makeByteData32(0x7fc00000); 89 if (value.isNaN) return makeByteData32(0x7fc00000);
90 if (value.abs() < MIN_FLOAT_DENORM) { 90 if (value.abs() < MIN_FLOAT_DENORM) {
91 return makeByteData32(value.isNegative ? 0x80000000 : 0x00000000); 91 return makeByteData32(value.isNegative ? 0x80000000 : 0x00000000);
92 } 92 }
93 if (value.isInfinite || value.abs() > MAX_FLOAT) { 93 if (value.isInfinite || value.abs() > MAX_FLOAT) {
94 return makeByteData32(value.isNegative ? 0xff800000 : 0x7f800000); 94 return makeByteData32(value.isNegative ? 0xff800000 : 0x7f800000);
95 } 95 }
96 return new ByteData(4) 96 return new ByteData(4)
97 ..setFloat32(0, value, Endianness.LITTLE_ENDIAN); 97 ..setFloat32(0, value, Endianness.LITTLE_ENDIAN);
98 }) 98 })
99 ..[GeneratedMessage._ENUM_BIT] = makeWriter( 99 ..[FieldType._ENUM_BIT] = makeWriter(
100 (value) => _int32ToBytes(value.value)) 100 (value) => _int32ToBytes(value.value))
101 ..[GeneratedMessage._GROUP_BIT] = (output, value) { 101 ..[FieldType._GROUP_BIT] = (output, value) {
102 value.writeToCodedBufferWriter(output); 102 value.writeToCodedBufferWriter(output);
103 } 103 }
104 ..[GeneratedMessage._INT32_BIT] = makeWriter(_int32ToBytes) 104 ..[FieldType._INT32_BIT] = makeWriter(_int32ToBytes)
105 ..[GeneratedMessage._INT64_BIT] = makeWriter( 105 ..[FieldType._INT64_BIT] = makeWriter(
106 (value) => _toVarint64(value)) 106 (value) => _toVarint64(value))
107 ..[GeneratedMessage._SINT32_BIT] = makeWriter( 107 ..[FieldType._SINT32_BIT] = makeWriter(
108 (int value) => _int32ToBytes(_encodeZigZag32(value))) 108 (int value) => _int32ToBytes(_encodeZigZag32(value)))
109 ..[GeneratedMessage._SINT64_BIT] = makeWriter( 109 ..[FieldType._SINT64_BIT] = makeWriter(
110 (Int64 value) => _toVarint64(_encodeZigZag64(value))) 110 (Int64 value) => _toVarint64(_encodeZigZag64(value)))
111 ..[GeneratedMessage._UINT32_BIT] = makeWriter(_toVarint32) 111 ..[FieldType._UINT32_BIT] = makeWriter(_toVarint32)
112 ..[GeneratedMessage._UINT64_BIT] = makeWriter(_toVarint64) 112 ..[FieldType._UINT64_BIT] = makeWriter(_toVarint64)
113 ..[GeneratedMessage._FIXED32_BIT] = makeWriter(makeByteData32) 113 ..[FieldType._FIXED32_BIT] = makeWriter(makeByteData32)
114 ..[GeneratedMessage._FIXED64_BIT] = makeWriter(makeByteData64) 114 ..[FieldType._FIXED64_BIT] = makeWriter(makeByteData64)
115 ..[GeneratedMessage._SFIXED32_BIT] = makeWriter(makeByteData32) 115 ..[FieldType._SFIXED32_BIT] = makeWriter(makeByteData32)
116 ..[GeneratedMessage._SFIXED64_BIT] = makeWriter(makeByteData64) 116 ..[FieldType._SFIXED64_BIT] = makeWriter(makeByteData64)
117 ..[GeneratedMessage._MESSAGE_BIT] = (output, value) { 117 ..[FieldType._MESSAGE_BIT] = (output, value) {
118 output._withDeferredSizeCalculation(() { 118 output._withDeferredSizeCalculation(() {
119 value.writeToCodedBufferWriter(output); 119 value.writeToCodedBufferWriter(output);
120 }); 120 });
121 }; 121 };
122 } 122 }
123 123
124 static final _OPEN_TAG_MAP = _makeOpenTagMap(); 124 static final _OPEN_TAG_MAP = _makeOpenTagMap();
125 125
126 static _makeOpenTagMap() { 126 static _makeOpenTagMap() {
127 return new Map<int, int>() 127 return new Map<int, int>()
128 ..[GeneratedMessage._BOOL_BIT] = WIRETYPE_VARINT 128 ..[FieldType._BOOL_BIT] = WIRETYPE_VARINT
129 ..[GeneratedMessage._BYTES_BIT] = WIRETYPE_LENGTH_DELIMITED 129 ..[FieldType._BYTES_BIT] = WIRETYPE_LENGTH_DELIMITED
130 ..[GeneratedMessage._STRING_BIT] = WIRETYPE_LENGTH_DELIMITED 130 ..[FieldType._STRING_BIT] = WIRETYPE_LENGTH_DELIMITED
131 ..[GeneratedMessage._DOUBLE_BIT] = WIRETYPE_FIXED64 131 ..[FieldType._DOUBLE_BIT] = WIRETYPE_FIXED64
132 ..[GeneratedMessage._FLOAT_BIT] = WIRETYPE_FIXED32 132 ..[FieldType._FLOAT_BIT] = WIRETYPE_FIXED32
133 ..[GeneratedMessage._ENUM_BIT] = WIRETYPE_VARINT 133 ..[FieldType._ENUM_BIT] = WIRETYPE_VARINT
134 ..[GeneratedMessage._GROUP_BIT] = WIRETYPE_START_GROUP 134 ..[FieldType._GROUP_BIT] = WIRETYPE_START_GROUP
135 ..[GeneratedMessage._INT32_BIT] = WIRETYPE_VARINT 135 ..[FieldType._INT32_BIT] = WIRETYPE_VARINT
136 ..[GeneratedMessage._INT64_BIT] = WIRETYPE_VARINT 136 ..[FieldType._INT64_BIT] = WIRETYPE_VARINT
137 ..[GeneratedMessage._SINT32_BIT] = WIRETYPE_VARINT 137 ..[FieldType._SINT32_BIT] = WIRETYPE_VARINT
138 ..[GeneratedMessage._SINT64_BIT] = WIRETYPE_VARINT 138 ..[FieldType._SINT64_BIT] = WIRETYPE_VARINT
139 ..[GeneratedMessage._UINT32_BIT] = WIRETYPE_VARINT 139 ..[FieldType._UINT32_BIT] = WIRETYPE_VARINT
140 ..[GeneratedMessage._UINT64_BIT] = WIRETYPE_VARINT 140 ..[FieldType._UINT64_BIT] = WIRETYPE_VARINT
141 ..[GeneratedMessage._FIXED32_BIT] = WIRETYPE_FIXED32 141 ..[FieldType._FIXED32_BIT] = WIRETYPE_FIXED32
142 ..[GeneratedMessage._FIXED64_BIT] = WIRETYPE_FIXED64 142 ..[FieldType._FIXED64_BIT] = WIRETYPE_FIXED64
143 ..[GeneratedMessage._SFIXED32_BIT] = WIRETYPE_FIXED32 143 ..[FieldType._SFIXED32_BIT] = WIRETYPE_FIXED32
144 ..[GeneratedMessage._SFIXED64_BIT] = WIRETYPE_FIXED64 144 ..[FieldType._SFIXED64_BIT] = WIRETYPE_FIXED64
145 ..[GeneratedMessage._MESSAGE_BIT] = WIRETYPE_LENGTH_DELIMITED; 145 ..[FieldType._MESSAGE_BIT] = WIRETYPE_LENGTH_DELIMITED;
146 } 146 }
147 147
148 void _withDeferredSizeCalculation(continuation) { 148 void _withDeferredSizeCalculation(continuation) {
149 // Reserve a place for size data. 149 // Reserve a place for size data.
150 int index = _output.length; 150 int index = _output.length;
151 _output.add(null); 151 _output.add(null);
152 int currentRunningSizeInBytes = _runningSizeInBytes; 152 int currentRunningSizeInBytes = _runningSizeInBytes;
153 continuation(); 153 continuation();
154 int writtenSizeInBytes = _runningSizeInBytes - currentRunningSizeInBytes; 154 int writtenSizeInBytes = _runningSizeInBytes - currentRunningSizeInBytes;
155 TypedData sizeMarker = _int32ToBytes(writtenSizeInBytes); 155 TypedData sizeMarker = _int32ToBytes(writtenSizeInBytes);
156 _output[index] = sizeMarker; 156 _output[index] = sizeMarker;
157 _runningSizeInBytes += sizeMarker.lengthInBytes; 157 _runningSizeInBytes += sizeMarker.lengthInBytes;
158 } 158 }
159 159
160 void writeField(int fieldNumber, int fieldType, fieldValue) { 160 void writeField(int fieldNumber, int fieldType, fieldValue) {
161 var valueType = fieldType & ~0x07; 161 var valueType = fieldType & ~0x07;
162 var writeFunction = _WRITE_FUNCTION_MAP[valueType]; 162 var writeFunction = _WRITE_FUNCTION_MAP[valueType];
163 163
164 writeTag(int wireFormat) { 164 writeTag(int wireFormat) {
165 writeInt32NoTag(makeTag(fieldNumber, wireFormat)); 165 writeInt32NoTag(makeTag(fieldNumber, wireFormat));
166 } 166 }
167 167
168 if ((fieldType & GeneratedMessage._PACKED_BIT) != 0) { 168 if ((fieldType & FieldType._PACKED_BIT) != 0) {
169 if (!fieldValue.isEmpty) { 169 if (!fieldValue.isEmpty) {
170 writeTag(WIRETYPE_LENGTH_DELIMITED); 170 writeTag(WIRETYPE_LENGTH_DELIMITED);
171 _withDeferredSizeCalculation(() { 171 _withDeferredSizeCalculation(() {
172 for (var value in fieldValue) { 172 for (var value in fieldValue) {
173 writeFunction(this, value); 173 writeFunction(this, value);
174 } 174 }
175 }); 175 });
176 } 176 }
177 return; 177 return;
178 } 178 }
179 179
180 writeValue(value) { 180 writeValue(value) {
181 writeTag(_OPEN_TAG_MAP[valueType]); 181 writeTag(_OPEN_TAG_MAP[valueType]);
182 writeFunction(this, value); 182 writeFunction(this, value);
183 if (valueType == GeneratedMessage._GROUP_BIT) { 183 if (valueType == FieldType._GROUP_BIT) {
184 writeTag(WIRETYPE_END_GROUP); 184 writeTag(WIRETYPE_END_GROUP);
185 } 185 }
186 } 186 }
187 187
188 if ((fieldType & GeneratedMessage._REPEATED_BIT) != 0) { 188 if ((fieldType & FieldType._REPEATED_BIT) != 0) {
189 fieldValue.forEach(writeValue); 189 fieldValue.forEach(writeValue);
190 return; 190 return;
191 } 191 }
192 192
193 writeValue(fieldValue); 193 writeValue(fieldValue);
194 } 194 }
195 195
196 void writeInt32NoTag(int value) { 196 void writeInt32NoTag(int value) {
197 writeRawBytes(_int32ToBytes(value)); 197 writeRawBytes(_int32ToBytes(value));
198 } 198 }
199 199
200 void writeRawBytes(TypedData value) { 200 void writeRawBytes(TypedData value) {
201 _output.add(value); 201 _output.add(value);
202 _runningSizeInBytes += value.lengthInBytes; 202 _runningSizeInBytes += value.lengthInBytes;
203 } 203 }
204 204
205 Uint8List toBuffer() { 205 Uint8List toBuffer() {
206 Uint8List result = new Uint8List(_runningSizeInBytes); 206 Uint8List result = new Uint8List(_runningSizeInBytes);
207 int position = 0; 207 int position = 0;
208 for (var typedData in _output) { 208 for (var typedData in _output) {
209 Uint8List asBytes = new Uint8List.view( 209 Uint8List asBytes = new Uint8List.view(
210 typedData.buffer, typedData.offsetInBytes, typedData.lengthInBytes); 210 typedData.buffer, typedData.offsetInBytes, typedData.lengthInBytes);
211 result.setRange(position, position + typedData.lengthInBytes, asBytes); 211 result.setRange(position, position + typedData.lengthInBytes, asBytes);
212 position += typedData.lengthInBytes; 212 position += typedData.lengthInBytes;
213 } 213 }
214 return result; 214 return result;
215 } 215 }
216 } 216 }
OLDNEW
« no previous file with comments | « lib/src/protobuf/builder_info.dart ('k') | lib/src/protobuf/field_info.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698