Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:22:25

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Navigation/INavigationPolicy.hpp"
0012 #include "Acts/Navigation/MultiNavigationPolicy.hpp"
0013 
0014 #include <concepts>
0015 #include <memory>
0016 namespace Acts {
0017 
0018 class TrackingVolume;
0019 class GeometryContext;
0020 class Logger;
0021 class INavigationPolicy;
0022 
0023 namespace detail {
0024 
0025 /// Concept for factory functions that create navigation policies
0026 /// @tparam F The factory function type
0027 /// @tparam Args The argument types for the factory function
0028 template <typename F, typename... Args>
0029 concept NavigationPolicyIsolatedFactoryConcept = requires(
0030     F f, const GeometryContext& gctx, const TrackingVolume& volume,
0031     const Logger& logger, Args&&... args) {
0032   { f(gctx, volume, logger, args...) } -> std::derived_from<INavigationPolicy>;
0033 
0034   requires NavigationPolicyConcept<decltype(f(gctx, volume, logger, args...))>;
0035 
0036   requires(std::is_copy_constructible_v<Args> && ...);
0037 };
0038 }  // namespace detail
0039 
0040 /// Base class for navigation policy factories. The factory can be assembled
0041 /// iteratively by using `make` followed by a number of calls to the `add`
0042 /// function of the helper type. Example:
0043 ///
0044 /// ```cpp
0045 /// auto factory = NavigationPolicyFactory{}
0046 ///  .add<NavigationPolicy1>(arg1, arg2)
0047 ///  .add<NavigationPolicy2>(/*no args*/)
0048 ///  .asUniquePtr();
0049 /// ```
0050 class NavigationPolicyFactory {
0051  private:
0052   /// Type alias for factory functions that create navigation policies
0053   using factory_type = std::function<std::unique_ptr<INavigationPolicy>(
0054       const GeometryContext&, const TrackingVolume&, const Logger&)>;
0055 
0056   /// Private constructor for internal use
0057   /// @param factories Vector of factory functions
0058   explicit NavigationPolicyFactory(std::vector<factory_type>&& factories)
0059       : m_factories(std::move(factories)) {}
0060 
0061  public:
0062   /// Default constructor
0063   NavigationPolicyFactory() = default;
0064 
0065   /// Add a navigation policy to the factory
0066   /// @tparam P The policy type to add
0067   /// @param args The arguments to pass to the policy constructor
0068   /// @note Arguments need to be copy constructible because the factory must be
0069   ///       able to execute multiple times.
0070   /// @return New instance of this object with the added factory for method
0071   ///         chaining
0072   template <NavigationPolicyConcept P, typename... Args>
0073     requires(std::is_constructible_v<P, const GeometryContext&,
0074                                      const TrackingVolume&, const Logger&,
0075                                      Args...> &&
0076              (std::is_copy_constructible_v<Args> && ...))
0077   constexpr NavigationPolicyFactory add(Args&&... args) && {
0078     auto factory = [=](const GeometryContext& gctx,
0079                        const TrackingVolume& volume, const Logger& logger) {
0080       return std::make_unique<P>(gctx, volume, logger, args...);
0081     };
0082 
0083     m_factories.push_back(std::move(factory));
0084     return std::move(*this);
0085   }
0086 
0087   /// Add a policy created by a factory function
0088   /// @tparam Fn The type of the function to construct the policy
0089   /// @param fn The factory function
0090   /// @param args The arguments to pass to the policy factory
0091   /// @note Arguments need to be copy constructible because the factory must be
0092   ///       able to execute multiple times.
0093   /// @return New instance of this object with the added factory for method
0094   ///         chaining
0095   template <typename Fn, typename... Args>
0096     requires(detail::NavigationPolicyIsolatedFactoryConcept<Fn, Args...>)
0097   constexpr NavigationPolicyFactory add(Fn&& fn, Args&&... args) && {
0098     auto factory = [=](const GeometryContext& gctx,
0099                        const TrackingVolume& volume, const Logger& logger) {
0100       using policy_type = decltype(fn(gctx, volume, logger, args...));
0101       return std::make_unique<policy_type>(fn(gctx, volume, logger, args...));
0102     };
0103 
0104     m_factories.push_back(std::move(factory));
0105     return std::move(*this);
0106   }
0107 
0108   /// Move the factory into a unique pointer
0109   /// @return A unique pointer to the factory
0110   std::unique_ptr<NavigationPolicyFactory> asUniquePtr() && {
0111     return std::make_unique<NavigationPolicyFactory>(std::move(*this));
0112   }
0113 
0114   /// Construct a multi-navigation policy using the registered factories
0115   /// @param gctx The geometry context
0116   /// @param volume The tracking volume
0117   /// @param logger The logger
0118   /// @return A unique pointer to the constructed MultiNavigationPolicy
0119   /// @throws std::runtime_error if no factories are registered
0120   std::unique_ptr<MultiNavigationPolicy> operator()(
0121       const GeometryContext& gctx, const TrackingVolume& volume,
0122       const Logger& logger) const {
0123     if (m_factories.empty()) {
0124       throw std::runtime_error(
0125           "No factories registered in the navigation policy factory");
0126     }
0127 
0128     std::vector<std::unique_ptr<INavigationPolicy>> policies;
0129     policies.reserve(m_factories.size());
0130     for (auto& factory : m_factories) {
0131       policies.push_back(factory(gctx, volume, logger));
0132     }
0133 
0134     return std::make_unique<MultiNavigationPolicy>(std::move(policies));
0135   }
0136 
0137   /// Construct a navigation policy using the factories (alias for operator())
0138   /// @param gctx The geometry context
0139   /// @param volume The tracking volume
0140   /// @param logger The logger
0141   /// @return A unique pointer to the constructed navigation policy
0142   std::unique_ptr<INavigationPolicy> build(const GeometryContext& gctx,
0143                                            const TrackingVolume& volume,
0144                                            const Logger& logger) const {
0145     return operator()(gctx, volume, logger);
0146   }
0147 
0148  private:
0149   /// Vector of factory functions to create navigation policies
0150   std::vector<factory_type> m_factories;
0151 };
0152 
0153 }  // namespace Acts