Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:37

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 corecel/data/AuxStateVec.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <type_traits>
0011 #include <vector>
0012 
0013 #include "corecel/Config.hh"
0014 
0015 #include "corecel/Assert.hh"
0016 #include "corecel/Macros.hh"
0017 #include "corecel/sys/TypeDemangler.hh"
0018 
0019 #include "AuxInterface.hh"
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 class AuxParamsRegistry;
0025 
0026 //---------------------------------------------------------------------------//
0027 /*!
0028  * Manage single-stream auxiliary state data.
0029  *
0030  * This class is constructed from a \c AuxParamsRegistry after the params are
0031  * completely added and while the state is being constructed (with its size,
0032  * etc.). The AuxId for an element of this class corresponds to the
0033  * AuxParamsRegistry.
0034  *
0035  * This class can be empty either by default or if the given auxiliary registry
0036  * doesn't have any entries.
0037  */
0038 class AuxStateVec
0039 {
0040   public:
0041     //@{
0042     //! \name Type aliases
0043     using UPState = std::unique_ptr<AuxStateInterface>;
0044     using UPConstState = std::unique_ptr<AuxStateInterface const>;
0045     //@}
0046 
0047   public:
0048     //! Create without any auxiliary data
0049     AuxStateVec() = default;
0050 
0051     // Create from params on a device/host stream
0052     AuxStateVec(AuxParamsRegistry const&, MemSpace, StreamId, size_type);
0053 
0054     // Allow moving; copying is prohibited due to unique pointers
0055     CELER_DEFAULT_MOVE_DELETE_COPY(AuxStateVec);
0056 
0057     ~AuxStateVec() = default;
0058 
0059     // Access auxiliary state interfaces
0060     inline AuxStateInterface& at(AuxId);
0061     inline AuxStateInterface const& at(AuxId) const;
0062 
0063     //! Get the number of defined states
0064     AuxId::size_type size() const { return states_.size(); }
0065 
0066   private:
0067     std::vector<UPState> states_;
0068 };
0069 
0070 //---------------------------------------------------------------------------//
0071 // FREE FUNCTIONS
0072 //---------------------------------------------------------------------------//
0073 /*!
0074  * Get a mutable item from a state vector efficiently and safely.
0075  */
0076 template<class S>
0077 S& get(AuxStateVec& vec, AuxId auxid)
0078 {
0079     static_assert(std::is_base_of_v<AuxStateInterface, S>);
0080     CELER_EXPECT(auxid < vec.size());
0081     auto* ptr = &vec.at(auxid);
0082     if constexpr (CELERITAS_DEBUG)
0083     {
0084         CELER_VALIDATE(
0085             dynamic_cast<S*>(ptr),
0086             << "Aux state id " << auxid.get() << " should have type "
0087             << TypeDemangler<AuxStateInterface>{}(*ptr)
0088             << " but was accessed with type " << TypeDemangler<S>{}());
0089     }
0090     CELER_ENSURE(dynamic_cast<S*>(ptr));
0091     return *static_cast<S*>(ptr);
0092 }
0093 
0094 //---------------------------------------------------------------------------//
0095 /*!
0096  * Get a const item from a state vector efficiently and safely.
0097  */
0098 template<class S>
0099 S const& get(AuxStateVec const& vec, AuxId auxid)
0100 {
0101     static_assert(std::is_base_of_v<AuxStateInterface, S>);
0102     CELER_EXPECT(auxid < vec.size());
0103     auto* ptr = &vec.at(auxid);
0104     if constexpr (CELERITAS_DEBUG)
0105     {
0106         CELER_VALIDATE(
0107             dynamic_cast<S const*>(ptr),
0108             << "Aux state id " << auxid.get() << " should have type "
0109             << TypeDemangler<AuxStateInterface>{}(*ptr)
0110             << " but was accessed with type " << TypeDemangler<S>{}());
0111     }
0112     CELER_ENSURE(dynamic_cast<S const*>(ptr));
0113     return *static_cast<S const*>(ptr);
0114 }
0115 
0116 //---------------------------------------------------------------------------//
0117 // INLINE DEFININTIONS
0118 //---------------------------------------------------------------------------//
0119 /*!
0120  * Access a mutable auxiliary state interface for a given ID.
0121  */
0122 AuxStateInterface& AuxStateVec::at(AuxId id)
0123 {
0124     CELER_EXPECT(id < states_.size());
0125     return *states_[id.unchecked_get()];
0126 }
0127 
0128 //---------------------------------------------------------------------------//
0129 /*!
0130  * Access a mutable auxiliary state interface for a given ID.
0131  */
0132 AuxStateInterface const& AuxStateVec::at(AuxId id) const
0133 {
0134     CELER_EXPECT(id < states_.size());
0135     return *states_[id.unchecked_get()];
0136 }
0137 
0138 //---------------------------------------------------------------------------//
0139 }  // namespace celeritas