Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:13:39

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   /// Create a new navigation policy factory
0066   /// @deprecated Use the default constructor instead
0067   [[deprecated("Use the default constructor")]]
0068   static auto make() {
0069     return NavigationPolicyFactory{};
0070   }
0071 
0072   /// Add a navigation policy to the factory
0073   /// @tparam P The policy type to add
0074   /// @param args The arguments to pass to the policy constructor
0075   /// @note Arguments need to be copy constructible because the factory must be
0076   ///       able to execute multiple times.
0077   /// @return New instance of this object with the added factory for method
0078   ///         chaining
0079   template <NavigationPolicyConcept P, typename... Args>
0080     requires(std::is_constructible_v<P, const GeometryContext&,
0081                                      const TrackingVolume&, const Logger&,
0082                                      Args...> &&
0083              (std::is_copy_constructible_v<Args> && ...))
0084   constexpr NavigationPolicyFactory add(Args&&... args) && {
0085     auto factory = [=](const GeometryContext& gctx,
0086                        const TrackingVolume& volume, const Logger& logger) {
0087       return std::make_unique<P>(gctx, volume, logger, args...);
0088     };
0089 
0090     m_factories.push_back(std::move(factory));
0091     return std::move(*this);
0092   }
0093 
0094   /// Add a policy created by a factory function
0095   /// @tparam Fn The type of the function to construct the policy
0096   /// @param fn The factory function
0097   /// @param args The arguments to pass to the policy factory
0098   /// @note Arguments need to be copy constructible because the factory must be
0099   ///       able to execute multiple times.
0100   /// @return New instance of this object with the added factory for method
0101   ///         chaining
0102   template <typename Fn, typename... Args>
0103     requires(detail::NavigationPolicyIsolatedFactoryConcept<Fn, Args...>)
0104   constexpr NavigationPolicyFactory add(Fn&& fn, Args&&... args) && {
0105     auto factory = [=](const GeometryContext& gctx,
0106                        const TrackingVolume& volume, const Logger& logger) {
0107       using policy_type = decltype(fn(gctx, volume, logger, args...));
0108       return std::make_unique<policy_type>(fn(gctx, volume, logger, args...));
0109     };
0110 
0111     m_factories.push_back(std::move(factory));
0112     return std::move(*this);
0113   }
0114 
0115   /// Move the factory into a unique pointer
0116   /// @return A unique pointer to the factory
0117   std::unique_ptr<NavigationPolicyFactory> asUniquePtr() && {
0118     return std::make_unique<NavigationPolicyFactory>(std::move(*this));
0119   }
0120 
0121   /// Construct a multi-navigation policy using the registered factories
0122   /// @param gctx The geometry context
0123   /// @param volume The tracking volume
0124   /// @param logger The logger
0125   /// @return A unique pointer to the constructed MultiNavigationPolicy
0126   /// @throws std::runtime_error if no factories are registered
0127   std::unique_ptr<MultiNavigationPolicy> operator()(
0128       const GeometryContext& gctx, const TrackingVolume& volume,
0129       const Logger& logger) const {
0130     if (m_factories.empty()) {
0131       throw std::runtime_error(
0132           "No factories registered in the navigation policy factory");
0133     }
0134 
0135     std::vector<std::unique_ptr<INavigationPolicy>> policies;
0136     policies.reserve(m_factories.size());
0137     for (auto& factory : m_factories) {
0138       policies.push_back(factory(gctx, volume, logger));
0139     }
0140 
0141     return std::make_unique<MultiNavigationPolicy>(std::move(policies));
0142   }
0143 
0144   /// Construct a navigation policy using the factories (alias for operator())
0145   /// @param gctx The geometry context
0146   /// @param volume The tracking volume
0147   /// @param logger The logger
0148   /// @return A unique pointer to the constructed navigation policy
0149   std::unique_ptr<INavigationPolicy> build(const GeometryContext& gctx,
0150                                            const TrackingVolume& volume,
0151                                            const Logger& logger) const {
0152     return operator()(gctx, volume, logger);
0153   }
0154 
0155  private:
0156   /// Vector of factory functions to create navigation policies
0157   std::vector<factory_type> m_factories;
0158 };
0159 
0160 }  // namespace Acts