Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:02

0001 #ifndef ServiceRegistry_connect_but_block_self_h
0002 #define ServiceRegistry_connect_but_block_self_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     ServiceRegistry
0006 // Class  :     connect_but_block_self
0007 //
0008 /**\function connect_but_block_self connect_but_block_self.h FWCore/ServiceRegistry/interface/connect_but_block_self.h
0009 
0010  Description: Connects a functional object to a signal, but guarantees that the functional object will never see a
0011    signal caused by its own action.
0012 
0013  Usage:
0014     <usage>
0015 
0016 */
0017 //
0018 // Original Author:  Chris Jones
0019 //         Created:  Thu Sep 22 20:42:32 CEST 2005
0020 //
0021 
0022 // system include files
0023 #include <memory>
0024 #include "FWCore/Utilities/interface/Signal.h"
0025 
0026 // user include files
0027 
0028 // forward declarations
0029 namespace edm {
0030   namespace serviceregistry {
0031     template <typename Func>
0032     class BlockingWrapper {
0033     public:
0034       BlockingWrapper(Func iFunc) : func_(iFunc), numBlocks_(0) {}
0035       //virtual ~BlockingWrapper();
0036 
0037       // ---------- const member functions ---------------------
0038       template <typename... Args>
0039       void operator()(Args&&... args) {
0040         std::shared_ptr<void> guard(static_cast<void*>(nullptr), std::bind(&BlockingWrapper::unblock, this));
0041         if (startBlocking()) {
0042           func_(std::forward<Args>(args)...);
0043         }
0044       }
0045 
0046       // ---------- static member functions --------------------
0047 
0048       // ---------- member functions ---------------------------
0049 
0050     private:
0051       // ---------- member data --------------------------------
0052       bool startBlocking() { return 1 == ++numBlocks_; }
0053       void unblock() { --numBlocks_; }
0054       Func func_;
0055       int numBlocks_;
0056     };
0057 
0058     template <class Func, class Signal>
0059     void connect_but_block_self(Signal& oSignal, const Func& iFunc) {
0060       oSignal.connect(BlockingWrapper<Func>(iFunc));
0061     }
0062   }  // namespace serviceregistry
0063 }  // namespace edm
0064 
0065 #endif