Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:44

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