Commit f05f6c2c authored by Mark Nordine's avatar Mark Nordine

Use generic functions

Enable implicit-casts: false lint
parent 035b56fe
Pipeline #29185106 passed with stages
in 2 minutes and 23 seconds
# Changelog
## 0.1.5
- Use generic functions
- Use `implicit-casts: false` lint
## 0.1.4
- Update for Dart 2
......
analyzer:
strong-mode:
implicit-casts: false
linter:
rules:
- camel_case_types
......
......@@ -25,26 +25,26 @@ class ByteArray
return ByteArray.fromByteData(view, endianness);
}
int readByte() => _getNum(_byteData.getInt8, 1);
int readUnsignedByte() => _getNum(_byteData.getUint8, 1);
int readByte() => _getNum<int>((i, _) => _byteData.getInt8(i), 1);
int readUnsignedByte() => _getNum<int>((i, _) => _byteData.getUint8(i), 1);
/// Returns true if not equal to zero
bool readBoolean() => readByte() != 0;
int readShort() => _getNum(_byteData.getInt16, 2);
int readUnsignedShort() => _getNum(_byteData.getUint16, 2);
int readShort() => _getNum<int>(_byteData.getInt16, 2);
int readUnsignedShort() => _getNum<int>(_byteData.getUint16, 2);
int readInt() => _getNum(_byteData.getInt32, 4);
int readUnsignedInt() => _getNum(_byteData.getUint32, 4);
int readInt() => _getNum<int>(_byteData.getInt32, 4);
int readUnsignedInt() => _getNum<int>(_byteData.getUint32, 4);
int readLong() => _getNum(_byteData.getInt64, 8);
int readUnsignedLong() => _getNum(_byteData.getUint64, 8);
int readLong() => _getNum<int>(_byteData.getInt64, 8);
int readUnsignedLong() => _getNum<int>(_byteData.getUint64, 8);
double readFloat() => _getNum(_byteData.getFloat32, 4);
double readDouble() => _getNum(_byteData.getFloat64, 8);
double readFloat() => _getNum<double>(_byteData.getFloat32, 4);
double readDouble() => _getNum<double>(_byteData.getFloat64, 8);
void writeByte(int value) => _setNum(_byteData.setInt8, value, 1);
void writeUnsignedByte(int value) => _setNum(_byteData.setUint8, value, 1);
void writeByte(int value) => _setNum<int>((i, v, _) => _byteData.setInt8(i, v), value, 1);
void writeUnsignedByte(int value) => _setNum<int>((i, v, _) => _byteData.setUint8(i, v), value, 1);
/// Writes [int], 1 if true, zero if false
void writeBoolean(bool value) => writeByte(value ? 1 : 0);
......@@ -109,30 +109,21 @@ class ByteArray
bytes.offset = oldOffset;
}
void _setNum(Function fun, num value, int size)
void _setNum<T extends num>(void Function(int, T, Endian) f, T value, int size)
{
if (_offset + size > length)
throw RangeError('attempted to write to offset ${_offset + size}, length is $length');
if (fun == _byteData.setInt8 || fun == _byteData.setUint8)
fun(_offset, value);
else
fun(_offset, value, endian);
f(offset, value, endian);
_offset += size;
}
num _getNum(Function fun, int size)
T _getNum<T extends num>(T Function(int, Endian) f, int size)
{
if (_offset + size > length)
throw RangeError('attempted to read from offset ${_offset + size}, length is $length');
num data = 0;
if (fun == _byteData.getInt8 || fun == _byteData.getUint8)
data = fun(_offset);
else
data = fun(_offset, endian);
final data = f(_offset, endian);
_offset += size;
return data;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment