SST  14.0.0
StructuralSimulationToolkit
oneshot.h
1 // Copyright 2009-2024 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-2024, 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_ONESHOT_H
13 #define SST_CORE_ONESHOT_H
14 
15 #include "sst/core/action.h"
16 #include "sst/core/sst_types.h"
17 #include "sst/core/ssthandler.h"
18 
19 #include <cinttypes>
20 
21 #define _ONESHOT_DBG(fmt, args...) __DBG(DBG_ONESHOT, OneShot, fmt, ##args)
22 
23 namespace SST {
24 
25 class TimeConverter;
26 
27 /**
28  * A OneShot Event class.
29  *
30  * Calls callback functions (handlers) on a specified period
31  */
32 class OneShot : public Action
33 {
34 public:
35  /**
36  Base handler for OneShot callbacks.
37  */
39 
40  /**
41  Used to create handlers for clock. The callback function is
42  expected to be in the form of:
43 
44  void func()
45 
46  In which case, the class is created with:
47 
48  new OneShot::Handler<classname>(this, &classname::function_name)
49 
50  Or, to add static data, the callback function is:
51 
52  void func(dataT data)
53 
54  and the class is created with:
55 
56  new OneShot::Handler<classname, dataT>(this, &classname::function_name, data)
57  */
58  template <typename classT, typename dataT = void>
60 
61 
62  /////////////////////////////////////////////////
63 
64  /** Create a new One Shot for a specified time that will callback the
65  handler function.
66  Note: OneShot cannot be canceled, and will always callback after
67  the timedelay.
68  */
69  OneShot(TimeConverter* timeDelay, int priority = ONESHOTPRIORITY);
70  ~OneShot();
71 
72  /** Is OneShot scheduled */
73  bool isScheduled() { return m_scheduled; }
74 
75  /** Add a handler to be called on this OneShot Event */
77 
78  /** Print details about the OneShot */
79  void print(const std::string& header, Output& out) const override;
80 
81  NotSerializable(SST::OneShot)
82 
83  private : typedef std::vector<OneShot::HandlerBase*> HandlerList_t;
84 
85  // Since this only gets fixed latency events, the times will fire
86  // in order of arrival. No need to use a full map, a double ended
87  // queue will work just as well
88  // typedef std::map<SimTime_t, HandlerList_t*> HandlerVectorMap_t;
89  typedef std::deque<std::pair<SimTime_t, HandlerList_t*>> HandlerVectorMap_t;
90 
91  // Generic constructor for serialization
92  OneShot() {}
93 
94  // Called by the Simulation (Activity Queue) when delay time as elapsed
95  void execute(void) override;
96 
97  // Activates this OneShot object, by inserting into the simulation's
98  // timeVortex for future execution.
99  void scheduleOneShot();
100  SimTime_t computeDeliveryTime();
101 
102  TimeConverter* m_timeDelay;
103  HandlerVectorMap_t m_HandlerVectorMap;
104  bool m_scheduled;
105 };
106 
107 } // namespace SST
108 
109 #endif // SST_CORE_ONESHOT_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:53
An Action is a schedulable Activity which is not an Event.
Definition: action.h:26
void execute(void) override
Function which will be called when the time for this Activity comes to pass.
Definition: oneshot.cc:102
Definition: ssthandler.h:372
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:27
void print(const std::string &header, Output &out) const override
Print details about the OneShot.
Definition: oneshot.cc:140
Definition: action.cc:18
bool isScheduled()
Is OneShot scheduled.
Definition: oneshot.h:73
void registerHandler(OneShot::HandlerBase *handler)
Add a handler to be called on this OneShot Event.
Definition: oneshot.cc:49
Event Handler class with user-data argument.
Definition: ssthandler.h:402
A OneShot Event class.
Definition: oneshot.h:32