Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:47

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