-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunion.ts
58 lines (50 loc) · 1.11 KB
/
union.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @module
*
* Union type.
*/
import { Endian } from './endian.ts';
import type { MemberInfos, Members } from './members.ts';
import type { Type } from './type.ts';
import { constant } from './util.ts';
let members: WeakMap<typeof Union, MemberInfos>;
/**
* Binary union buffer view.
*/
export class Union extends Endian implements Type, Members {
/**
* Union class.
*/
declare public readonly ['constructor']: Omit<typeof Union, 'new'>;
public get byteLength(): number {
return this.constructor.BYTE_LENGTH;
}
/**
* Byte length of struct.
*/
public static readonly BYTE_LENGTH: number = 0;
/**
* Non-overlapping members.
*/
public static readonly OVERLAPPING: boolean = true;
/**
* Members infos.
*/
public static get MEMBERS(): MemberInfos {
let r = (members ??= new WeakMap()).get(this);
if (!r) {
members.set(
this,
r = Object.create(
Object.getPrototypeOf(this).MEMBERS ?? null,
) as MemberInfos,
);
}
return r;
}
static {
constant(this.prototype, Symbol.toStringTag, 'Union');
constant(this, 'BYTE_LENGTH');
constant(this, 'OVERLAPPING');
}
}