import { MagickError } from "../magick-error.ts";import { Exception } from "./exception/exception.ts";
export interface INativeInstance { dispose(): void;}
export abstract class NativeInstance implements NativeInstance { private readonly disposeMethod: (instance: number) => void; private instance: number;
protected constructor( instance: number, disposeMethod: (instance: number) => void, ) { this.instance = instance; this.disposeMethod = disposeMethod; }
get _instance(): number { if (this.instance > 0) { return this.instance; }
if (this.instance === -1) { this._instanceNotInitialized(); }
throw new MagickError("instance is disposed"); } set _instance(instance: number) { this.disposeInstance(this.instance); this.instance = instance; }
dispose(): void { this.instance = this.disposeInstance(this.instance); }
protected _setInstance(instance: number, exception: Exception): void { exception.check(() => { this.dispose(); this.instance = instance; }, () => { this.disposeInstance(instance); }); }
protected _instanceNotInitialized(): void { throw new MagickError("instance is not initialized"); }
private disposeInstance(instance: number): number { if (instance > 0) { this.disposeMethod(instance); }
return 0; }}