Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:20

0001 #ifndef FWCore_ServiceRegistry_Signal_h
0002 #define FWCore_ServiceRegistry_Signal_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     ServiceRegistry
0006 // Class  :     Signal
0007 //
0008 /**\class Signal Signal.h FWCore/ServiceRegistry/interface/Signal.h
0009 
0010  Description: A simple implementation of the signal/slot pattern
0011 
0012  Usage:
0013     This is a simple version of the signal/slot pattern and is used by the Framework. It is safe
0014  to call 'emit' from multiple threads simultaneously.
0015  Assumptions:
0016  -The attached slots have a life-time greater than the last 'emit' call issued from the Signal.
0017  -'connect' is not called simultaneously with any other methods of the class.
0018 */
0019 //
0020 // Original Author:  Chris Jones
0021 //         Created:  Thu Jan 17 16:03:51 CST 2013
0022 //
0023 
0024 // system include files
0025 #include <vector>
0026 #include <functional>
0027 
0028 // user include files
0029 
0030 // forward declarations
0031 
0032 namespace edm {
0033   namespace signalslot {
0034     template <typename T>
0035     class Signal {
0036     public:
0037       typedef std::function<T> slot_type;
0038       typedef std::vector<slot_type> slot_list_type;
0039 
0040       Signal() = default;
0041       ~Signal() = default;
0042       Signal(Signal&&) = default;
0043       Signal(const Signal&) = delete;
0044       Signal& operator=(const Signal&) = delete;
0045 
0046       // ---------- const member functions ---------------------
0047       template <typename... Args>
0048       void emit(Args&&... args) const {
0049         for (auto& slot : m_slots) {
0050           slot(std::forward<Args>(args)...);
0051         }
0052       }
0053 
0054       template <typename... Args>
0055       void operator()(Args&&... args) const {
0056         emit(std::forward<Args>(args)...);
0057       }
0058 
0059       slot_list_type const& slots() const { return m_slots; }
0060       // ---------- static member functions --------------------
0061 
0062       // ---------- member functions ---------------------------
0063       template <typename U>
0064       void connect(U iFunc) {
0065         m_slots.push_back(std::function<T>(iFunc));
0066       }
0067 
0068       template <typename U>
0069       void connect_front(U iFunc) {
0070         m_slots.insert(m_slots.begin(), std::function<T>(iFunc));
0071       }
0072 
0073     private:
0074       // ---------- member data --------------------------------
0075       slot_list_type m_slots;
0076     };
0077   }  // namespace signalslot
0078 }  // namespace edm
0079 
0080 #endif