Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:03:18

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file orange/univ/TrackerVisitor.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/math/Algorithms.hh"
0010 #include "orange/OrangeData.hh"
0011 
0012 #include "RectArrayTracker.hh"
0013 #include "SimpleUnitTracker.hh"
0014 #include "UniverseTypeTraits.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Apply a functor to a universe tracker of unknown type.
0021  *
0022  * An instance of this class is like \c std::visit but accepting a UniverseId
0023  * rather than a \c std::variant .
0024  *
0025  * Example: \code
0026  TrackerVisitor visit_tracker{params_};
0027  auto new_pos = visit_tracker(
0028     [&pos](auto&& u) { return u.initialize(pos); },
0029     univ_id);
0030  \endcode
0031  */
0032 class TrackerVisitor
0033 {
0034   public:
0035     //!@{
0036     //! \name Type aliases
0037     using ParamsRef = NativeCRef<OrangeParamsData>;
0038     //!@}
0039 
0040   public:
0041     // Construct from ORANGE params
0042     explicit inline CELER_FUNCTION TrackerVisitor(ParamsRef const& params);
0043 
0044     // Apply the function to the universe specified by the given ID
0045     template<class F>
0046     CELER_FUNCTION decltype(auto) operator()(F&& func, UniverseId id);
0047 
0048   private:
0049     ParamsRef const& params_;
0050 };
0051 
0052 //---------------------------------------------------------------------------//
0053 // INLINE DEFINITIONS
0054 //---------------------------------------------------------------------------//
0055 /*!
0056  * Construct from ORANGE params.
0057  */
0058 CELER_FUNCTION TrackerVisitor::TrackerVisitor(ParamsRef const& params)
0059     : params_(params)
0060 {
0061 }
0062 
0063 #if !defined(__DOXYGEN__) || __DOXYGEN__ > 0x010908
0064 //---------------------------------------------------------------------------//
0065 /*!
0066  * Apply the function to the universe specified by the given ID.
0067  */
0068 template<class F>
0069 CELER_FUNCTION decltype(auto)
0070 TrackerVisitor::operator()(F&& func, UniverseId id)
0071 {
0072     CELER_EXPECT(id < params_.universe_types.size());
0073     size_type universe_idx = params_.universe_indices[id];
0074 
0075     // Apply type-deleted functor based on type
0076     return visit_universe_type(
0077         [&](auto u_traits) {
0078             using UTraits = decltype(u_traits);
0079             using UId = OpaqueId<typename UTraits::record_type>;
0080             using Tracker = typename UTraits::tracker_type;
0081             return func(Tracker{params_, UId{universe_idx}});
0082         },
0083         params_.universe_types[id]);
0084 }
0085 #endif
0086 
0087 //---------------------------------------------------------------------------//
0088 }  // namespace celeritas