include/G4OCCT/G4OCCTAssemblyRegistry.hh
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later | ||
| 2 | // Copyright (C) 2026 G4OCCT Contributors | ||
| 3 | |||
| 4 | /// @file G4OCCTAssemblyRegistry.hh | ||
| 5 | /// @brief Declaration of G4OCCTAssemblyRegistry. | ||
| 6 | |||
| 7 | #ifndef G4OCCT_G4OCCTAssemblyRegistry_hh | ||
| 8 | #define G4OCCT_G4OCCTAssemblyRegistry_hh | ||
| 9 | |||
| 10 | #include <cstddef> | ||
| 11 | #include <map> | ||
| 12 | #include <string> | ||
| 13 | |||
| 14 | class G4OCCTAssemblyVolume; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * @brief Singleton registry for named G4OCCTAssemblyVolume instances. | ||
| 18 | * | ||
| 19 | * Stores heap-allocated `G4OCCTAssemblyVolume` objects by name and takes | ||
| 20 | * ownership of them. This is primarily used by the DD4hep plugin to keep | ||
| 21 | * assemblies alive past the plugin build phase, where local variables would | ||
| 22 | * otherwise be destroyed too early. | ||
| 23 | * | ||
| 24 | * ### Ownership | ||
| 25 | * | ||
| 26 | * The registry owns all registered assemblies. They are deleted when | ||
| 27 | * `Release()` is called or when the registry is destroyed. | ||
| 28 | * | ||
| 29 | * ### Usage | ||
| 30 | * ```cpp | ||
| 31 | * auto* assembly = G4OCCTAssemblyVolume::FromSTEP("detector.step", matMap); | ||
| 32 | * G4OCCTAssemblyRegistry::Instance().Register("myDetector", assembly); | ||
| 33 | * | ||
| 34 | * // Later: | ||
| 35 | * G4OCCTAssemblyVolume* a = G4OCCTAssemblyRegistry::Instance().Get("myDetector"); | ||
| 36 | * ``` | ||
| 37 | */ | ||
| 38 | class G4OCCTAssemblyRegistry { | ||
| 39 | public: | ||
| 40 | /// Return the singleton instance. | ||
| 41 | static G4OCCTAssemblyRegistry& Instance(); | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Register a named assembly (takes ownership). | ||
| 45 | * | ||
| 46 | * @param name Unique name for this assembly. | ||
| 47 | * @param assembly Non-null pointer to the assembly to register. | ||
| 48 | * @throws std::runtime_error if @p name is already registered. | ||
| 49 | */ | ||
| 50 | void Register(const std::string& name, G4OCCTAssemblyVolume* assembly); | ||
| 51 | |||
| 52 | /** | ||
| 53 | * Retrieve an assembly by name. | ||
| 54 | * | ||
| 55 | * @param name Assembly name as passed to `Register()`. | ||
| 56 | * @return Pointer to the assembly, or `nullptr` if not found. | ||
| 57 | */ | ||
| 58 | G4OCCTAssemblyVolume* Get(const std::string& name) const; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Remove an assembly from the registry and return ownership to the caller. | ||
| 62 | * | ||
| 63 | * @param name Assembly name to release. | ||
| 64 | * @return The previously registered pointer, or `nullptr` if not found. | ||
| 65 | * The caller is responsible for deleting the returned object. | ||
| 66 | */ | ||
| 67 | G4OCCTAssemblyVolume* Release(const std::string& name); | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Return the number of registered assemblies. | ||
| 71 | */ | ||
| 72 | std::size_t Size() const; | ||
| 73 | |||
| 74 | private: | ||
| 75 | 6 | G4OCCTAssemblyRegistry() = default; | |
| 76 | ~G4OCCTAssemblyRegistry(); | ||
| 77 | |||
| 78 | std::map<std::string, G4OCCTAssemblyVolume*> fAssemblies; | ||
| 79 | }; | ||
| 80 | |||
| 81 | #endif // G4OCCT_G4OCCTAssemblyRegistry_hh | ||
| 82 |