Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:29:26

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 accel/ExceptionConverter.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <exception>
0011 
0012 namespace celeritas
0013 {
0014 class SharedParams;
0015 //---------------------------------------------------------------------------//
0016 /*!
0017  * Translate Celeritas C++ exceptions into Geant4 G4Exception calls.
0018  *
0019  * This should generally be used when wrapping calls to Celeritas in a user
0020  * application.
0021  *
0022  * For example, the user event action to transport particles on device could be
0023  * used as:
0024  * \code
0025    void EventAction::EndOfEventAction(const G4Event*)
0026    {
0027        // Transport any tracks left in the buffer
0028        celeritas::ExceptionConverter call_g4exception{"celer0003"};
0029        CELER_TRY_HANDLE(transport_->Flush(), call_g4exception);
0030    }
0031  * \endcode
0032  */
0033 class ExceptionConverter
0034 {
0035   public:
0036     // Construct with "error code" and optional pointer to shared params
0037     inline ExceptionConverter(char const* err_code, SharedParams const* params);
0038 
0039     // Construct with just an "error code"
0040     inline explicit ExceptionConverter(char const* err_code);
0041 
0042     // Capture the current exception and convert it to a G4Exception call
0043     void operator()(std::exception_ptr p) const;
0044 
0045   private:
0046     char const* err_code_;
0047     SharedParams const* params_{nullptr};
0048 
0049     void convert_device_exceptions(std::exception_ptr p) const;
0050 };
0051 
0052 //---------------------------------------------------------------------------//
0053 // INLINE DEFINITIONS
0054 //---------------------------------------------------------------------------//
0055 /*!
0056  * Construct with an error code and shared parameters.
0057  *
0058  * The error code is reported to the Geant4 exception manager. The shared
0059  * parameters are used to translate internal particle data if an exception
0060  * occurs.
0061  */
0062 ExceptionConverter::ExceptionConverter(char const* err_code,
0063                                        SharedParams const* params)
0064     : err_code_{err_code}, params_(params)
0065 {
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 /*!
0070  * Construct with an error code for dispatching to Geant4.
0071  */
0072 ExceptionConverter::ExceptionConverter(char const* err_code)
0073     : ExceptionConverter(err_code, nullptr)
0074 {
0075 }
0076 
0077 //---------------------------------------------------------------------------//
0078 }  // namespace celeritas