File indexing completed on 2025-09-18 09:09:37
0001
0002
0003
0004
0005
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
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 class AuxStateVec
0039 {
0040 public:
0041
0042
0043 using UPState = std::unique_ptr<AuxStateInterface>;
0044 using UPConstState = std::unique_ptr<AuxStateInterface const>;
0045
0046
0047 public:
0048
0049 AuxStateVec() = default;
0050
0051
0052 AuxStateVec(AuxParamsRegistry const&, MemSpace, StreamId, size_type);
0053
0054
0055 CELER_DEFAULT_MOVE_DELETE_COPY(AuxStateVec);
0056
0057 ~AuxStateVec() = default;
0058
0059
0060 inline AuxStateInterface& at(AuxId);
0061 inline AuxStateInterface const& at(AuxId) const;
0062
0063
0064 AuxId::size_type size() const { return states_.size(); }
0065
0066 private:
0067 std::vector<UPState> states_;
0068 };
0069
0070
0071
0072
0073
0074
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
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
0118
0119
0120
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
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 }