SST  12.0.1
StructuralSimulationToolkit
ssthandler.h
1 // Copyright 2009-2022 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2022, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_SSTHANDLER_H
13 #define SST_CORE_SSTHANDLER_H
14 
15 #include "sst/core/sst_types.h"
16 
17 #include <atomic>
18 
19 namespace SST {
20 
21 // This file contains base classes for use as various handlers (object
22 // encapsulating callback functions) in SST. The classes expect to
23 // encapsulate a pointer to an object and a pointer to a member
24 // function of the objects class. The classes also allow you to
25 // optionally add one additional piece of static data to be passed
26 // into the callback function along with any data provided by the
27 // caller. There are two versions of this class, one that has no data
28 // passed from the caller (ending with SSTHandlerNoArgs), and one that has
29 // a single item passed from the caller (SSTHandler).
30 
31 // You can also optionally add unique handler ID generation to
32 // specific handler types. This is done by passing true for the
33 // createIdT template parameter. When this is done, each handler of
34 // that type that gets created will be assigned a unique id, which can
35 // be accessed by getId(). This is primarily for use in the core so
36 // that there is a unique identifier that can be used to store
37 // handlers in maps, etc.
38 
39 // These classes provide the full functionality of the handlers and
40 // can be added to a class with the "using" keyword, as follows (a
41 // class can use any type name they'd like in place of HandlerBase and
42 // Handler, though those names are preferred for consistency):
43 
44 // using HandlerBase = SSTHandlerBase<return_type_of_callback, arg_type_of_callback, true/false>;
45 
46 // template <typename classT, typename dataT = void>
47 // using Handler = SSTHandler<return_type_of_callback, arg_type_of_callback, true/false, classT, dataT>;
48 
49 // Or:
50 
51 // using HandlerBase = SSTHandlerBaseNoArgs<return_type_of_callback, true/false>;
52 
53 // template <return_type_of_callback, typename classT, typename dataT = void>
54 // using Handler = SSTHandlerNoArgs<return_type_of_callback, true/false, classT, dataT>;
55 
56 // The handlers are then instanced as follows:
57 
58 // new Class::Handler<Class>(this, &Class::callback_function)
59 
60 // Or:
61 
62 // new Class::Handler<Class,int>(this, &Class::callback_function, 1)
63 
64 
65 /// Functor classes for Event handling
66 
67 /// Handler with 1 argument to callback from caller
68 
69 // Class to allow optional generation of unique id's per handler
70 template <bool>
72 {
73 public:
74  virtual ~SSTHandlerBaseId() {}
75 
76 protected:
77  SSTHandlerBaseId() {}
78 };
79 
80 template <>
81 class SSTHandlerBaseId<true>
82 {
83  static std::atomic<HandlerId_t> id_counter;
84  HandlerId_t my_id;
85 
86 protected:
88 
89 public:
90  virtual ~SSTHandlerBaseId() {}
91 
92  /**
93  Get the unique ID of this handler
94  */
95  HandlerId_t getId() { return my_id; }
96 };
97 
98 
99 template <typename returnT, typename argT, bool createIdT>
100 class SSTHandlerBase : public SSTHandlerBaseId<createIdT>
101 {
102 public:
104 
105  /** Handler function */
106  virtual returnT operator()(argT) = 0;
107  virtual ~SSTHandlerBase() {}
108 };
109 
110 
111 /**
112  * Event Handler class with user-data argument
113  */
114 template <typename returnT, typename argT, bool createIdT, typename classT, typename dataT = void>
115 class SSTHandler : public SSTHandlerBase<returnT, argT, createIdT>
116 {
117 private:
118  typedef returnT (classT::*PtrMember)(argT, dataT);
119  classT* object;
120  const PtrMember member;
121  dataT data;
122 
123 public:
124  /** Constructor
125  * @param object - Pointer to Object upon which to call the handler
126  * @param member - Member function to call as the handler
127  * @param data - Additional argument to pass to handler
128  */
129  SSTHandler(classT* const object, PtrMember member, dataT data) :
130  SSTHandlerBase<returnT, argT, createIdT>(),
131  object(object),
132  member(member),
133  data(data)
134  {}
135 
136  returnT operator()(argT arg) { return (object->*member)(arg, data); }
137 };
138 
139 /**
140  * Event Handler class with no user-data.
141  */
142 template <typename returnT, typename argT, bool createIdT, typename classT>
143 class SSTHandler<returnT, argT, createIdT, classT, void> : public SSTHandlerBase<returnT, argT, createIdT>
144 {
145 private:
146  typedef returnT (classT::*PtrMember)(argT);
147  const PtrMember member;
148  classT* object;
149 
150 public:
151  /** Constructor
152  * @param object - Pointer to Object upon which to call the handler
153  * @param member - Member function to call as the handler
154  */
155  SSTHandler(classT* const object, PtrMember member) :
156  SSTHandlerBase<returnT, argT, createIdT>(),
157  member(member),
158  object(object)
159  {}
160 
161  returnT operator()(argT arg) { return (object->*member)(arg); }
162 };
163 
164 
165 /// Handler with no arguments to callback from caller
166 template <typename returnT, bool createIdT>
168 {
169 public:
171 
172  /** Handler function */
173  virtual returnT operator()() = 0;
174  virtual ~SSTHandlerBaseNoArgs() {}
175 };
176 
177 /**
178  * Event Handler class with user-data argument
179  */
180 template <typename returnT, typename classT, bool createIdT, typename dataT = void>
181 class SSTHandlerNoArgs : public SSTHandlerBaseNoArgs<returnT, createIdT>
182 {
183 private:
184  typedef returnT (classT::*PtrMember)(dataT);
185  classT* object;
186  const PtrMember member;
187  dataT data;
188 
189 public:
190  /** Constructor
191  * @param object - Pointer to Object upon which to call the handler
192  * @param member - Member function to call as the handler
193  * @param data - Additional argument to pass to handler
194  */
195  SSTHandlerNoArgs(classT* const object, PtrMember member, dataT data) :
196  SSTHandlerBaseNoArgs<returnT, createIdT>(),
197  object(object),
198  member(member),
199  data(data)
200  {}
201 
202  void operator()() { return (object->*member)(data); }
203 };
204 
205 /**
206  * Event Handler class with no user-data.
207  */
208 template <typename returnT, typename classT, bool createIdT>
209 class SSTHandlerNoArgs<returnT, classT, createIdT, void> : public SSTHandlerBaseNoArgs<returnT, createIdT>
210 {
211 private:
212  typedef returnT (classT::*PtrMember)();
213  const PtrMember member;
214  classT* object;
215 
216 public:
217  /** Constructor
218  * @param object - Pointer to Object upon which to call the handler
219  * @param member - Member function to call as the handler
220  */
221  SSTHandlerNoArgs(classT* const object, PtrMember member) :
222  SSTHandlerBaseNoArgs<returnT, createIdT>(),
223  member(member),
224  object(object)
225  {}
226 
227  void operator()() { return (object->*member)(); }
228 };
229 
230 
231 } // namespace SST
232 
233 #endif // SST_CORE_SSTHANDLER_H
HandlerId_t getId()
Get the unique ID of this handler.
Definition: ssthandler.h:95
void operator()()
Handler function.
Definition: ssthandler.h:227
void operator()()
Handler function.
Definition: ssthandler.h:202
virtual returnT operator()(argT)=0
Handler function.
Definition: ssthandler.h:100
returnT operator()(argT arg)
Handler function.
Definition: ssthandler.h:136
Event Handler class with user-data argument.
Definition: ssthandler.h:115
SSTHandler(classT *const object, PtrMember member)
Constructor.
Definition: ssthandler.h:155
returnT operator()(argT arg)
Handler function.
Definition: ssthandler.h:161
Handler with no arguments to callback from caller.
Definition: ssthandler.h:167
SSTHandlerNoArgs(classT *const object, PtrMember member)
Constructor.
Definition: ssthandler.h:221
SSTHandler(classT *const object, PtrMember member, dataT data)
Constructor.
Definition: ssthandler.h:129
Event Handler class with user-data argument.
Definition: ssthandler.h:181
SSTHandlerNoArgs(classT *const object, PtrMember member, dataT data)
Constructor.
Definition: ssthandler.h:195
virtual returnT operator()()=0
Handler function.
Functor classes for Event handling.
Definition: ssthandler.h:71