VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
scriptRegistry.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 12.10.2025.
5//
6#include "scriptRegistry.h"
7
8#include <spdlog/spdlog.h>
9
10namespace visutwin::canvas
11{
12 void ScriptRegistry::registerType(const std::string& name, ScriptFactory factory) {
13 if (name.empty()) {
14 throw std::invalid_argument(
15 "ScriptRegistry::registerType: script name cannot be empty"
16 );
17 }
18
19 if (!factory) {
20 throw std::invalid_argument(
21 "ScriptRegistry::registerType: factory is null for script '" + name + "'"
22 );
23 }
24
25 auto [it, inserted] = _types.emplace(name, std::move(factory));
26
27 if (!inserted) {
28 throw std::runtime_error(
29 "ScriptRegistry::registerType: duplicate script type '" + name + "'"
30 );
31 }
32 }
33
34 std::unique_ptr<Script> ScriptRegistry::create(const std::string& name) const {
35 // First check local override, then fall back to global
36 if (const auto it = _types.find(name); it != _types.end()) {
37 return it->second();
38 }
39
40 // Fall back to global factory registry
41 if (const auto factory = ScriptFactories::instance().getFactory(name)) {
42 return factory();
43 }
44
45 spdlog::error("ScriptRegistry::create: unknown script type '{}'", name);
46 return nullptr;
47 }
48}
static ScriptFactories & instance()
std::unique_ptr< Script > create(const std::string &name) const
std::function< std::unique_ptr< Script >()> ScriptFactory