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
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <memory>
0024 #include "FWCore/Utilities/interface/Signal.h"
0025
0026
0027
0028
0029 namespace edm {
0030 namespace serviceregistry {
0031 template <typename Func>
0032 class BlockingWrapper {
0033 public:
0034 BlockingWrapper(Func iFunc) : func_(iFunc), numBlocks_(0) {}
0035
0036
0037
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
0047
0048
0049
0050 private:
0051
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 }
0063 }
0064
0065 #endif