Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:28:02

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