SST  7.1.0
StructuralSimulationToolkit
oneshot.h
1 // Copyright 2009-2017 Sandia Corporation. Under the terms
2 // of Contract DE-NA0003525 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2017, Sandia Corporation
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 
13 #ifndef SST_CORE_ONESHOT_H
14 #define SST_CORE_ONESHOT_H
15 
16 #include <sst/core/sst_types.h>
17 
18 #include <cinttypes>
19 
20 #include <sst/core/action.h>
21 
22 #define _ONESHOT_DBG(fmt, args...)__DBG(DBG_ONESHOT, OneShot, fmt, ## args)
23 
24 namespace SST {
25 
26 class TimeConverter;
27 
28 /**
29  * A OneShot Event class.
30  *
31  * Calls callback functions (handlers) on a specified period
32  */
33 class OneShot : public Action
34 {
35 public:
36  /////////////////////////////////////////////////
37 
38  /** Functor classes for OneShot handling */
39  class HandlerBase {
40  public:
41  /** Function called when Handler is invoked */
42  virtual void operator()() = 0;
43  virtual ~HandlerBase() {}
44  };
45 
46  /////////////////////////////////////////////////
47 
48  /** Event Handler class with user-data argument
49  * @tparam classT Type of the Object
50  * @tparam argT Type of the argument
51  */
52  template <typename classT, typename argT = void>
53  class Handler : public HandlerBase {
54  private:
55  typedef void (classT::*PtrMember)(argT);
56  classT* object;
57  const PtrMember member;
58  argT data;
59 
60  public:
61  /** Constructor
62  * @param object - Pointer to Object upon which to call the handler
63  * @param member - Member function to call as the handler
64  * @param data - Additional argument to pass to handler
65  */
66  Handler(classT* const object, PtrMember member, argT data) :
67  object(object),
68  member(member),
69  data(data)
70  {}
71 
72  /** Operator to callback OneShot Handler; called
73  * by the OneShot object to execute the users callback.
74  * @param data - The data passed in when the handler was created
75  */
76  void operator()() override {
77  (object->*member)(data);
78  }
79  };
80 
81  /////////////////////////////////////////////////
82 
83  /** Event Handler class without user-data
84  * @tparam classT Type of the Object
85  */
86  template <typename classT>
87  class Handler<classT, void> : public HandlerBase {
88  private:
89  typedef void (classT::*PtrMember)();
90  classT* object;
91  const PtrMember member;
92 
93  public:
94  /** Constructor
95  * @param object - Pointer to Object upon which to call the handler
96  * @param member - Member function to call as the handler
97  */
98  Handler(classT* const object, PtrMember member) :
99  object(object),
100  member(member)
101  {}
102 
103  /** Operator to callback OneShot Handler; called
104  * by the OneShot object to execute the users callback.
105  */
106  void operator()() override {
107  (object->*member)();
108  }
109  };
110 
111  /////////////////////////////////////////////////
112 
113  /** Create a new One Shot for a specified time that will callback the
114  handler function.
115  Note: OneShot cannot be canceled, and will always callback after
116  the timedelay.
117  */
118  OneShot(TimeConverter* timeDelay, int priority = ONESHOTPRIORITY);
119  ~OneShot();
120 
121  /** Is OneShot scheduled */
122  bool isScheduled() {return m_scheduled;}
123 
124  /** Add a handler to be called on this OneShot Event */
125  void registerHandler(OneShot::HandlerBase* handler);
126 
127  /** Print details about the OneShot */
128  void print(const std::string& header, Output &out) const override;
129 
130 private:
131  typedef std::vector<OneShot::HandlerBase*> HandlerList_t;
132  typedef std::map<SimTime_t, HandlerList_t*> HandlerVectorMap_t;
133 
134  // Generic constructor for serialization
135  OneShot() { }
136 
137  // Called by the Simulation (Activity Queue) when delay time as elapsed
138  void execute(void) override;
139 
140  // Activates this OneShot object, by inserting into the simulation's
141  // timeVortex for future execution.
142  SimTime_t scheduleOneShot();
143 
144  TimeConverter* m_timeDelay;
145  HandlerVectorMap_t m_HandlerVectorMap;
146  bool m_scheduled;
147 
148 };
149 
150 } // namespace SST
151 
152 
153 #endif // SST_CORE_ONESHOT_H
Output object provides consistant method for outputing data to stdout, stderr and/or sst debug file...
Definition: output.h:54
An Action is a schedulable Activity which is not an Event.
Definition: action.h:30
void operator()() override
Operator to callback OneShot Handler; called by the OneShot object to execute the users callback...
Definition: oneshot.h:106
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:25
void print(const std::string &header, Output &out) const override
Print details about the OneShot.
Definition: oneshot.cc:144
Definition: action.cc:17
bool isScheduled()
Is OneShot scheduled.
Definition: oneshot.h:122
Handler(classT *const object, PtrMember member, argT data)
Constructor.
Definition: oneshot.h:66
void registerHandler(OneShot::HandlerBase *handler)
Add a handler to be called on this OneShot Event.
Definition: oneshot.cc:51
virtual void operator()()=0
Function called when Handler is invoked.
Handler(classT *const object, PtrMember member)
Constructor.
Definition: oneshot.h:98
A OneShot Event class.
Definition: oneshot.h:33
Event Handler class with user-data argument.
Definition: oneshot.h:53
void operator()() override
Operator to callback OneShot Handler; called by the OneShot object to execute the users callback...
Definition: oneshot.h:76
Functor classes for OneShot handling.
Definition: oneshot.h:39