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