From e3f82b250a395b3363140f5492b29202d220237e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20J=C3=B3zsef?= Date: Sat, 24 Sep 2016 23:39:30 +0200 Subject: [PATCH] Add readArray and WriteArray to UhkBuffer --- src/config-serializer/UhkBuffer.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/config-serializer/UhkBuffer.ts b/src/config-serializer/UhkBuffer.ts index 50f6e511..d3ca33ba 100644 --- a/src/config-serializer/UhkBuffer.ts +++ b/src/config-serializer/UhkBuffer.ts @@ -1,3 +1,5 @@ +import { Serializable } from './Serializable'; + export class UhkBuffer { private static eepromSize = 32 * 1024; @@ -151,6 +153,23 @@ export class UhkBuffer { this.writeUInt8(bool ? 1 : 0); } + // See: How to create a new object from type parameter in generic class in typescript? http://stackoverflow.com/q/17382143 + readArray>(type: { new (): T }): T[] { + let array: T[] = []; + let length = this.readCompactLength(); + for (let i = 0; i < length; ++i) { + array.push(new type().fromBinary(this)); + } + return array; + } + + writeArray>(array: T[]): void { + this.writeCompactLength(array.length); + for (let element of array) { + element.toBinary(this); + } + } + backtrack(): void { this.offset -= this.bytesToBacktrack; this.bytesToBacktrack = 0;