simc-ast-builder
Version:
Parser and AST generator for SimulationCraft files
127 lines • 4.11 kB
JavaScript
;
/**
* Registry for Field instances
* Provides a central registry for field definitions with dynamic registration
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.fieldRegistry = exports.FieldRegistry = void 0;
const fieldMaps_1 = require("../parser/visitors/ast/utils/fieldMaps");
const Field_1 = require("./Field");
const FieldFactory_1 = require("./FieldFactory");
/**
* Registry for Field instances
*/
class FieldRegistry {
constructor() {
/**
* Map of field names to Field instances
*/
this.fields = new Map();
}
/**
* Clear all fields from the registry
*/
clear() {
this.fields.clear();
}
/**
* Create a boolean field and register it
* @param name Name of the field
* @param options Additional options for the field
* @returns The created and registered boolean field
*/
createBooleanField(name, options = {}) {
return this.register(FieldFactory_1.FieldFactory.boolean(name, options));
}
/**
* Create and register a field
* @param name Name of the field
* @param options Options for the field
* @returns The created and registered field
*/
createField(name, options = {}) {
const field = new Field_1.Field(name, options);
return this.register(field);
}
/**
* Create a neutral field and register it
* @param name Name of the field
* @param options Additional options for the field
* @returns The created and registered neutral field
*/
createNeutralField(name, options = {}) {
return this.register(FieldFactory_1.FieldFactory.neutral(name, options));
}
/**
* Create a numeric field and register it
* @param name Name of the field
* @param options Additional options for the field
* @returns The created and registered numeric field
*/
createNumericField(name, options = {}) {
return this.register(FieldFactory_1.FieldFactory.numeric(name, options));
}
/**
* Get a field from the registry by name
* @param name Name of the field to get
* @returns The field with the given name, or undefined if not found
*/
get(name) {
return this.fields.get(name);
}
/**
* Get all fields in the registry
* @returns Array of all fields in the registry
*/
getAll() {
return Array.from(this.fields.values());
}
/**
* Get a field by name, creating it if it doesn't exist
* @param name Name of the field
* @param defaultType Default type to use if the field doesn't exist
* @returns The existing or newly created field
*/
getOrCreate(name, defaultType = "neutral") {
const existing = this.get(name);
if (existing) {
return existing;
}
// Create a new field with the default type
return this.createField(name, { type: defaultType });
}
/**
* Check if a field exists in the registry
* @param name Name of the field to check
* @returns True if the field exists in the registry
*/
has(name) {
return this.fields.has(name);
}
/**
* Initialize the registry with default fields from the FIELD_MAP
*/
initializeDefaults() {
// Convert existing field definitions to Field instances and register them
for (const [name, def] of Object.entries(fieldMaps_1.FIELD_MAP)) {
this.register(FieldFactory_1.FieldFactory.fromDefinition(Object.assign(Object.assign({}, def), { name })));
}
}
/**
* Register a field in the registry
* @param field Field to register
* @returns The registered field
*/
register(field) {
this.fields.set(field.name, field);
return field;
}
}
exports.FieldRegistry = FieldRegistry;
/**
* Singleton instance of the FieldRegistry
*/
exports.fieldRegistry = new FieldRegistry();
// Initialize the registry with default fields
exports.fieldRegistry.initializeDefaults();
//# sourceMappingURL=FieldRegistry.js.map