File indexing completed on 2025-11-02 09:36:47
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009 #include <memory>
0010 #include <string>
0011 #include <unordered_map>
0012 #include <vector>
0013
0014 #include "corecel/Assert.hh"
0015 #include "corecel/Types.hh"
0016 #include "corecel/cont/Span.hh"
0017
0018 #include "ActionInterface.hh"
0019
0020 #include "detail/ActionRegistryImpl.hh"
0021
0022 namespace celeritas
0023 {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 class ActionRegistry
0042 {
0043 public:
0044
0045
0046 using SPAction = std::shared_ptr<ActionInterface>;
0047 using SPConstAction = std::shared_ptr<ActionInterface const>;
0048
0049
0050 public:
0051
0052 ActionRegistry() = default;
0053
0054
0055
0056
0057 ActionId next_id() const { return ActionId(actions_.size()); }
0058
0059
0060 template<class T, std::enable_if_t<detail::is_mutable_action_v<T>, bool> = true>
0061 void insert(std::shared_ptr<T> action)
0062 {
0063 return this->insert_mutable_impl(std::move(action));
0064 }
0065
0066
0067 template<class T, std::enable_if_t<detail::is_const_action_v<T>, bool> = true>
0068 void insert(std::shared_ptr<T> action)
0069 {
0070 return this->insert_const_impl(std::move(action));
0071 }
0072
0073
0074
0075
0076 ActionId::size_type num_actions() const { return actions_.size(); }
0077
0078
0079 bool empty() const { return actions_.empty(); }
0080
0081
0082 inline SPConstAction const& action(ActionId id) const;
0083
0084
0085 inline std::string const& id_to_label(ActionId id) const;
0086
0087
0088 ActionId find_action(std::string const& label) const;
0089
0090
0091 inline Span<SPAction const> mutable_actions() const;
0092
0093 private:
0094
0095
0096 std::vector<SPConstAction> actions_;
0097 std::vector<std::string> labels_;
0098 std::unordered_map<std::string, ActionId> action_ids_;
0099 std::vector<SPAction> mutable_actions_;
0100
0101 void insert_mutable_impl(SPAction&&);
0102 void insert_const_impl(SPConstAction&&);
0103 void insert_impl(SPConstAction&&);
0104 };
0105
0106
0107
0108
0109
0110
0111
0112 auto ActionRegistry::action(ActionId id) const -> SPConstAction const&
0113 {
0114 CELER_EXPECT(id < actions_.size());
0115 return actions_[id.unchecked_get()];
0116 }
0117
0118
0119
0120
0121
0122 std::string const& ActionRegistry::id_to_label(ActionId id) const
0123 {
0124 CELER_EXPECT(id < actions_.size());
0125 return labels_[id.unchecked_get()];
0126 }
0127
0128
0129
0130
0131
0132 auto ActionRegistry::mutable_actions() const -> Span<SPAction const>
0133 {
0134 return make_span(mutable_actions_);
0135 }
0136
0137
0138 }