1 | | | import 'dart:typed_data'; |
2 | | |
|
3 | | | import '../converters/converter.dart'; |
4 | | | import 'squadron_marshaler.dart'; |
5 | | |
|
6 | | | class TypedDataMarshaler<T extends TypedData> |
7 | | | implements SquadronMarshaler<T, ByteBuffer> { |
8 | | 2 | const TypedDataMarshaler(); |
9 | | |
|
10 | | 1 | @override |
11 | | 2 | ByteBuffer marshal(T data) => data.buffer; |
12 | | |
|
13 | | 1 | @override |
14 | | 1 | T unmarshal(ByteBuffer data) { |
15 | | 3 | final castor = _typeDataCastors[T] as Cast<T>; |
16 | | 3 | return _td<T>(castor)(data); |
17 | | 1 | } |
18 | | | } |
19 | | |
|
20 | | 2 | Cast<T> _td<T>(T Function(ByteBuffer) view) => |
21 | | 4 | (x) => (x is T) ? x : view(x as ByteBuffer)!; |
22 | | |
|
23 | | 4 | final Map<Type, Cast> _typeDataCastors = { |
24 | | 2 | ByteData: _td<ByteData>(ByteData.view), |
25 | | 2 | Uint8ClampedList: _td<Uint8ClampedList>(Uint8ClampedList.view), |
26 | | 2 | Uint8List: _td<Uint8List>(Uint8List.view), |
27 | | 2 | Int8List: _td<Int8List>(Int8List.view), |
28 | | 2 | Uint16List: _td<Uint16List>(Uint16List.view), |
29 | | 2 | Int16List: _td<Int16List>(Int16List.view), |
30 | | 2 | Uint32List: _td<Uint32List>(Uint32List.view), |
31 | | 2 | Int32List: _td<Int32List>(Int32List.view), |
32 | | 2 | Int32x4List: _td<Int32x4List>(Int32x4List.view), |
33 | | 2 | Uint64List: _td<Uint64List>(Uint64List.view), |
34 | | 2 | Int64List: _td<Int64List>(Int64List.view), |
35 | | 2 | Float32List: _td<Float32List>(Float32List.view), |
36 | | 2 | Float32x4List: _td<Float32x4List>(Float32x4List.view), |
37 | | 2 | Float64List: _td<Float64List>(Float64List.view), |
38 | | 2 | Float64x2List: _td<Float64x2List>(Float64x2List.view), |
39 | | | }; |