Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-02 09:36:47

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/sys/ActionRegistry.hh
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  * Construct and store metadata about end-of-step actions.
0027  *
0028  * The action manager helps create and retain access to "actions" (possible
0029  * control paths for a track) over the program's lifetime. "Implicit" actions
0030  * are primarily for debugging, but "explicit" actions indicate that a kernel
0031  * will change the state of a track on device.
0032  *
0033  * Associated actions use the \c ActionInterface class to provide debugging
0034  * information, and the \c CoreStepActionInterface is used to invoke
0035  * kernels with core data.
0036  *
0037  * New actions should be created with an action ID corresponding to \c
0038  * next_id . Registering an action checks its ID. Actions are always added
0039  * sequentially and can never be removed from the registry once added.
0040  */
0041 class ActionRegistry
0042 {
0043   public:
0044     //!@{
0045     //! \name Type aliases
0046     using SPAction = std::shared_ptr<ActionInterface>;
0047     using SPConstAction = std::shared_ptr<ActionInterface const>;
0048     //!@}
0049 
0050   public:
0051     //! Construct without any registered actions
0052     ActionRegistry() = default;
0053 
0054     //// CONSTRUCTION ////
0055 
0056     //! Get the next available action ID
0057     ActionId next_id() const { return ActionId(actions_.size()); }
0058 
0059     //! Register a mutable action
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     //! Register a const action
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     //// ACCESSORS ////
0074 
0075     //! Get the number of defined actions
0076     ActionId::size_type num_actions() const { return actions_.size(); }
0077 
0078     //! Whether no actions have been registered
0079     bool empty() const { return actions_.empty(); }
0080 
0081     // Access an action
0082     inline SPConstAction const& action(ActionId id) const;
0083 
0084     // Get the label corresponding to an action
0085     inline std::string const& id_to_label(ActionId id) const;
0086 
0087     // Find the action corresponding to an label
0088     ActionId find_action(std::string const& label) const;
0089 
0090     // View all actions that are able to change at runtime
0091     inline Span<SPAction const> mutable_actions() const;
0092 
0093   private:
0094     //// DATA ////
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 // INLINE DEFINITIONS
0108 //---------------------------------------------------------------------------//
0109 /*!
0110  * Access an action.
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  * Get the label corresponding to an action.
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  * View all actions that are able to change at runtime.
0131  */
0132 auto ActionRegistry::mutable_actions() const -> Span<SPAction const>
0133 {
0134     return make_span(mutable_actions_);
0135 }
0136 
0137 //---------------------------------------------------------------------------//
0138 }  // namespace celeritas