Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:00

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/EventData/TrackProxy.hpp"
0012 #include "Acts/EventData/TrackStateProxy.hpp"
0013 #include "Acts/Utilities/Concepts.hpp"
0014 #include "Acts/Utilities/HashedString.hpp"
0015 
0016 #include <type_traits>
0017 
0018 #if defined(__cpp_concepts)
0019 #include <concepts>
0020 namespace Acts::detail {
0021 
0022 template <typename T>
0023 concept MutableProxyType = requires(T t, HashedString key) {
0024   requires !T::ReadOnly;
0025 
0026   {
0027     t.template component<int>(key)
0028     } -> std::same_as<std::conditional_t<T::ReadOnly, const int&, int&>>;
0029 };
0030 
0031 template <typename T>
0032 concept ConstProxyType = requires(T t, HashedString key) {
0033   requires T::ReadOnly;
0034   { t.template component<int>(key) } -> std::same_as<const int&>;
0035 };
0036 
0037 template <typename T>
0038 concept ProxyType = (MutableProxyType<T> || ConstProxyType<T>)&&requires {
0039   typename T::ConstProxyType;
0040 
0041   requires ConstProxyType<typename T::ConstProxyType>;
0042 };
0043 
0044 }  // namespace Acts::detail
0045 #endif
0046 
0047 namespace Acts {
0048 
0049 namespace detail {
0050 template <typename... Args>
0051 struct associatedConstProxy;
0052 
0053 template <typename trajectory_t, std::size_t M, bool read_only>
0054 struct associatedConstProxy<TrackStateProxy<trajectory_t, M, read_only>> {
0055   using type = TrackStateProxy<trajectory_t, M, true>;
0056 };
0057 
0058 template <typename track_container_t, typename trajectory_t,
0059           template <typename> class holder_t, bool read_only>
0060 struct associatedConstProxy<
0061     TrackProxy<track_container_t, trajectory_t, holder_t, read_only>> {
0062   using type = TrackProxy<track_container_t, trajectory_t, holder_t, true>;
0063 };
0064 
0065 }  // namespace detail
0066 
0067 /// Utility class that eases accessing dynamic columns in track and track state
0068 /// containers
0069 /// @tparam T the type of the value to access
0070 /// @tparam ReadOnly true if this is a const accessor
0071 template <typename T, bool ReadOnly>
0072 struct ProxyAccessorBase {
0073   HashedString key;
0074 
0075   /// Create the accessor from an already-hashed string key
0076   /// @param _key the key
0077   constexpr ProxyAccessorBase(HashedString _key) : key{_key} {}
0078 
0079   /// Create the accessor from a string key
0080   /// @param _key the key
0081   ProxyAccessorBase(const std::string& _key) : key{hashString(_key)} {}
0082 
0083   /// Access the stored key on the proxy given as an argument. Mutable version
0084   /// @tparam proxy_t the type of the proxy
0085   /// @param proxy the proxy object to access
0086   /// @return mutable reference to the column behind the key
0087   template <ACTS_CONCEPT(detail::MutableProxyType) proxy_t, bool RO = ReadOnly,
0088             typename = std::enable_if_t<!RO>>
0089   T& operator()(proxy_t proxy) const {
0090     static_assert(!proxy_t::ReadOnly,
0091                   "Cannot get mutable ref for const track proxy");
0092     return proxy.template component<T>(key);
0093   }
0094 
0095   /// Access the stored key on the proxy given as an argument. Const version
0096   /// @tparam proxy_t the type of the track proxy
0097   /// @param proxy the proxy to access
0098   /// @return const reference to the column behind the key
0099   template <ACTS_CONCEPT(detail::ProxyType) proxy_t, bool RO = ReadOnly,
0100             typename = std::enable_if_t<RO>>
0101   const T& operator()(proxy_t proxy) const {
0102     if constexpr (proxy_t::ReadOnly) {
0103       return proxy.template component<T>(key);
0104 
0105     } else {
0106       using const_proxy_t = typename proxy_t::ConstProxyType;
0107       const_proxy_t cproxy{proxy};
0108       return cproxy.template component<T>(key);
0109     }
0110   }
0111 };
0112 
0113 template <typename T>
0114 using ProxyAccessor = ProxyAccessorBase<T, false>;
0115 template <typename T>
0116 using ConstProxyAccessor = ProxyAccessorBase<T, true>;
0117 }  // namespace Acts