Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-29 22:58:00

0001 #ifndef FWCore_Framework_ModuleSignalSentry_h
0002 #define FWCore_Framework_ModuleSignalSentry_h
0003 
0004 #include "FWCore/Utilities/interface/ConvertException.h"
0005 
0006 namespace edm {
0007   class ActivityRegistry;
0008   class ModuleCallingContext;
0009 
0010   template <typename T>
0011   class ModuleSignalSentry {
0012   public:
0013     ModuleSignalSentry(ActivityRegistry* a,
0014                        typename T::Context const* context,
0015                        ModuleCallingContext const* moduleCallingContext)
0016         : a_(a), context_(context), moduleCallingContext_(moduleCallingContext) {}
0017 
0018     ~ModuleSignalSentry() {
0019       // This destructor does nothing unless we are unwinding the
0020       // the stack from an earlier exception (a_ will be null if we are
0021       // are not). We want to report the earlier exception and ignore any
0022       // addition exceptions from the post module signal.
0023       CMS_SA_ALLOW try {
0024         if (a_) {
0025           T::postModuleSignal(a_, context_, moduleCallingContext_);
0026         }
0027       } catch (...) {
0028       }
0029     }
0030     void preModuleSignal() {
0031       if (a_) {
0032         try {
0033           convertException::wrap([this]() { T::preModuleSignal(a_, context_, moduleCallingContext_); });
0034         } catch (cms::Exception& ex) {
0035           ex.addContext("Handling pre module signal, likely in a service function immediately before module method");
0036           throw;
0037         }
0038       }
0039     }
0040     void postModuleSignal() {
0041       if (a_) {
0042         auto temp = a_;
0043         // Setting a_ to null informs the destructor that the signal
0044         // was already run and that it should do nothing.
0045         a_ = nullptr;
0046         try {
0047           convertException::wrap([this, temp]() { T::postModuleSignal(temp, context_, moduleCallingContext_); });
0048         } catch (cms::Exception& ex) {
0049           ex.addContext("Handling post module signal, likely in a service function immediately after module method");
0050           throw;
0051         }
0052       }
0053     }
0054 
0055   private:
0056     ActivityRegistry* a_;  // We do not use propagate_const because the registry itself is mutable.
0057     typename T::Context const* context_;
0058     ModuleCallingContext const* moduleCallingContext_;
0059   };
0060 }  // namespace edm
0061 
0062 #endif