From 0db3f72fae384d6829c2a8f849a518b5df14a531 Mon Sep 17 00:00:00 2001 From: crusader Date: Sat, 23 Dec 2017 21:14:08 +0900 Subject: [PATCH] ing --- .../@overflow/commons/core/reflect/class.ts | 16 ++--- .../commons/core/reflect/class_registry.ts | 11 ++++ src/ts/@overflow/commons/core/util/index.ts | 1 + .../@overflow/commons/core/util/registry.ts | 66 +++++++++++++++++++ 4 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 src/ts/@overflow/commons/core/reflect/class_registry.ts create mode 100644 src/ts/@overflow/commons/core/util/index.ts create mode 100644 src/ts/@overflow/commons/core/util/registry.ts diff --git a/src/ts/@overflow/commons/core/reflect/class.ts b/src/ts/@overflow/commons/core/reflect/class.ts index cccc2e4..6918a45 100644 --- a/src/ts/@overflow/commons/core/reflect/class.ts +++ b/src/ts/@overflow/commons/core/reflect/class.ts @@ -5,10 +5,13 @@ import { } from '@overflow/commons/core/type'; import {AccessibleObject} from './accessible_object'; +import {systemClassRegistry} from './class_registry'; import {Constructor} from './constructor'; import {Field} from './field'; import {Method} from './method'; + + export class Class extends AccessibleObject { private _clazzType: ClassType; private _constructor: Constructor; @@ -19,17 +22,17 @@ export class Class extends AccessibleObject { * forClass */ public static forClass(clazzType: ClassType): Class { - return Metadata.get(LOAFER_CLASS, clazzType); + return systemClassRegistry.get(clazzType); } /** * _defineClass */ public static _defineClass(clazzType: ClassType): Class { - let clazz: Class = Metadata.get(LOAFER_CLASS, clazzType); + let clazz: Class = Class.forClass(clazzType); if (undefined === clazz) { clazz = new Class(clazzType); - Metadata.set(LOAFER_CLASS, clazz, clazzType); + systemClassRegistry.set(clazzType, clazz); } if (null === clazz._constructor) { @@ -126,10 +129,3 @@ export class Class extends AccessibleObject { return null; } } - -/** - * Metadata key - * @private - * @type {string} - */ -const LOAFER_CLASS = Symbol('loafer:class'); diff --git a/src/ts/@overflow/commons/core/reflect/class_registry.ts b/src/ts/@overflow/commons/core/reflect/class_registry.ts new file mode 100644 index 0000000..e56534d --- /dev/null +++ b/src/ts/@overflow/commons/core/reflect/class_registry.ts @@ -0,0 +1,11 @@ +import { Registry } from '@overflow/commons/core/util'; +import { ClassType } from '@overflow/commons/core/type'; +import { Class } from './class'; + +export class ClassRegistry extends Registry { + public constructor(parent?: ClassRegistry) { + super(parent); + } +} + +export const systemClassRegistry = new ClassRegistry(); diff --git a/src/ts/@overflow/commons/core/util/index.ts b/src/ts/@overflow/commons/core/util/index.ts new file mode 100644 index 0000000..585b667 --- /dev/null +++ b/src/ts/@overflow/commons/core/util/index.ts @@ -0,0 +1 @@ +export * from './registry'; diff --git a/src/ts/@overflow/commons/core/util/registry.ts b/src/ts/@overflow/commons/core/util/registry.ts new file mode 100644 index 0000000..f22cfa9 --- /dev/null +++ b/src/ts/@overflow/commons/core/util/registry.ts @@ -0,0 +1,66 @@ +export abstract class Registry { + private _parent: Registry; + private _map: Map; + + protected constructor(parent?: Registry) { + this._parent = parent; + this._map = new Map(); + } + + public get parent(): Registry { + return this._parent; + } + + public get size(): number { + return this._map.size; + } + + public get(key: K): V | undefined { + let v = this._map.get(key); + if (undefined === v && undefined !== this._parent) { + v = this._parent.get(key); + } + + return v; + } + + public has(key: K): boolean { + let exist = this._map.has(key); + if (!exist && undefined !== this._parent) { + exist = this._parent.has(key); + } + return exist; + } + + public set(key: K, value: V): void { + this._map.set(key, value); + } + + public entries(): IterableIterator<[K, V]> { + return this._map.entries(); + } + + public keys(): IterableIterator { + return this._map.keys(); + } + + public values(): IterableIterator { + return this._map.values(); + } + + public clear(): void { + this._map.clear(); + } + + public delete(key: K): boolean { + let result = this._map.delete(key); + if (!result && undefined !== this._parent) { + result = this._parent.delete(key); + } + return result; + } + + public forEach(callback: (vlaue: V, key: K, map: Map) => void, thisArg?: any): void { + this._map.forEach(callback, thisArg); + } +}