CMS 3D CMS Logo

Callback.h
Go to the documentation of this file.
1 #ifndef Framework_Callback_h
2 #define Framework_Callback_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class : Callback
7 //
16 //
17 // Author: Chris Jones
18 // Created: Sun Apr 17 14:30:24 EDT 2005
19 //
20 
21 // system include files
22 #include <vector>
23 #include <type_traits>
24 // user include files
27 
28 // forward declarations
29 namespace edm {
30  namespace eventsetup {
31 
32  //need a virtual distructor since owner of callback only knows
33  // about the base class. Other users of callback know the specific
34  // type
35  struct CallbackBase { virtual ~CallbackBase() {} };
36 
37  // The default decorator that does nothing
38  template< typename TRecord>
40  void pre(const TRecord&) {}
41  void post(const TRecord&) {}
42  };
43 
44  template<typename T, //producer's type
45  typename TReturn, //return type of the producer's method
46  typename TRecord, //the record passed in as an argument
47  typename TDecorator //allows customization using pre/post calls
49  class Callback : public CallbackBase {
50  public:
51  using method_type = TReturn (T ::*)(const TRecord&);
52 
53  Callback(T* iProd,
54  method_type iMethod,
55  const TDecorator& iDec = TDecorator()) :
56  proxyData_{},
57  producer_(iProd),
58  method_(iMethod),
59  wasCalledForThisRecord_(false),
60  decorator_(iDec) {}
61 
62 
63  // ---------- const member functions ---------------------
64 
65  // ---------- static member functions --------------------
66 
67  // ---------- member functions ---------------------------
68 
69 
70  void operator()(const TRecord& iRecord) {
71  if(!wasCalledForThisRecord_) {
72  decorator_.pre(iRecord);
73  storeReturnedValues((producer_->*method_)(iRecord));
74  wasCalledForThisRecord_ = true;
75  decorator_.post(iRecord);
76  }
77  }
78 
79  template<class DataT>
80  void holdOntoPointer(DataT* iData) {
82  }
83 
84  void storeReturnedValues(TReturn iReturn) {
85  //std::cout <<" storeReturnedValues "<< iReturn <<" " <<iReturn->value_ <<std::endl;
87  setData<typename type::head_type, typename type::tail_type>(iReturn);
88  }
89 
90  template<class RemainingContainerT, class DataT, class ProductsT>
91  void setData(ProductsT& iProducts) {
92  DataT* temp = reinterpret_cast< DataT*>(proxyData_[produce::find_index<TReturn,DataT>::value]) ;
93  if(nullptr != temp) { moveFromTo(iProducts, *temp); }
94  if constexpr( not std::is_same_v<produce::Null,RemainingContainerT> ) {
95  setData<typename RemainingContainerT::head_type,
96  typename RemainingContainerT::tail_type>(iProducts);
97  }
98  }
99  void newRecordComing() {
100  wasCalledForThisRecord_ = false;
101  }
102 
103  private:
104  Callback(const Callback&) = delete; // stop default
105 
106  const Callback& operator=(const Callback&) = delete; // stop default
107 
112  TDecorator decorator_;
113  };
114  }
115 }
116 
117 #endif
void storeReturnedValues(TReturn iReturn)
Definition: Callback.h:84
type
Definition: HCALResponse.h:21
void holdOntoPointer(DataT *iData)
Definition: Callback.h:80
#define constexpr
TReturn(T::*)(const TRecord &) method_type
Definition: Callback.h:51
std::array< void *, produce::size< TReturn >::value > proxyData_
Definition: Callback.h:108
void moveFromTo(FromT &iFrom, ToT &iTo)
Callback(T *iProd, method_type iMethod, const TDecorator &iDec=TDecorator())
Definition: Callback.h:53
void operator()(const TRecord &iRecord)
Definition: Callback.h:70
void setData(ProductsT &iProducts)
Definition: Callback.h:91
HLT enums.
edm::propagate_const< T * > producer_
Definition: Callback.h:109
long double T