Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:39:32

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 // Copyright (C) 2026 G4OCCT Contributors
0003 
0004 /// @file G4OCCTAssemblyRegistry.hh
0005 /// @brief Declaration of G4OCCTAssemblyRegistry.
0006 
0007 #ifndef G4OCCT_G4OCCTAssemblyRegistry_hh
0008 #define G4OCCT_G4OCCTAssemblyRegistry_hh
0009 
0010 #include <cstddef>
0011 #include <map>
0012 #include <string>
0013 
0014 class G4OCCTAssemblyVolume;
0015 
0016 /**
0017  * @brief Singleton registry for named G4OCCTAssemblyVolume instances.
0018  *
0019  * Stores heap-allocated `G4OCCTAssemblyVolume` objects by name and takes
0020  * ownership of them.  This is primarily used by the DD4hep plugin to keep
0021  * assemblies alive past the plugin build phase, where local variables would
0022  * otherwise be destroyed too early.
0023  *
0024  * ### Ownership
0025  *
0026  * The registry owns all registered assemblies.  They are deleted when
0027  * `Release()` is called or when the registry is destroyed.
0028  *
0029  * ### Usage
0030  * ```cpp
0031  * auto* assembly = G4OCCTAssemblyVolume::FromSTEP("detector.step", matMap);
0032  * G4OCCTAssemblyRegistry::Instance().Register("myDetector", assembly);
0033  *
0034  * // Later:
0035  * G4OCCTAssemblyVolume* a = G4OCCTAssemblyRegistry::Instance().Get("myDetector");
0036  * ```
0037  */
0038 class G4OCCTAssemblyRegistry {
0039 public:
0040   /// Return the singleton instance.
0041   static G4OCCTAssemblyRegistry& Instance();
0042 
0043   /**
0044    * Register a named assembly (takes ownership).
0045    *
0046    * @param name     Unique name for this assembly.
0047    * @param assembly Non-null pointer to the assembly to register.
0048    * @throws std::runtime_error if @p name is already registered.
0049    */
0050   void Register(const std::string& name, G4OCCTAssemblyVolume* assembly);
0051 
0052   /**
0053    * Retrieve an assembly by name.
0054    *
0055    * @param name Assembly name as passed to `Register()`.
0056    * @return Pointer to the assembly, or `nullptr` if not found.
0057    */
0058   G4OCCTAssemblyVolume* Get(const std::string& name) const;
0059 
0060   /**
0061    * Remove an assembly from the registry and return ownership to the caller.
0062    *
0063    * @param name Assembly name to release.
0064    * @return The previously registered pointer, or `nullptr` if not found.
0065    *         The caller is responsible for deleting the returned object.
0066    */
0067   G4OCCTAssemblyVolume* Release(const std::string& name);
0068 
0069   /**
0070    * Return the number of registered assemblies.
0071    */
0072   std::size_t Size() const;
0073 
0074 private:
0075   G4OCCTAssemblyRegistry() = default;
0076   ~G4OCCTAssemblyRegistry();
0077 
0078   std::map<std::string, G4OCCTAssemblyVolume*> fAssemblies;
0079 };
0080 
0081 #endif // G4OCCT_G4OCCTAssemblyRegistry_hh