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