SST  11.0.0
StructuralSimulationToolkit
clock.h
1 // Copyright 2009-2021 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-2021, 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 
13 #ifndef SST_CORE_CLOCK_H
14 #define SST_CORE_CLOCK_H
15 
16 #include <vector>
17 #include <cinttypes>
18 
19 #include "sst/core/action.h"
20 
21 #define _CLE_DBG( fmt, args...)__DBG( DBG_CLOCK, Clock, fmt, ## args )
22 
23 namespace SST {
24 
25 class TimeConverter;
26 
27 /**
28  * A Clock class.
29  *
30  * Calls callback functions (handlers) on a specified period
31  */
32 class Clock : public Action
33 {
34 public:
35 
36  /** Create a new clock with a specified period */
37  Clock( TimeConverter* period, int priority = CLOCKPRIORITY);
38  ~Clock();
39 
40  /** Functor classes for Clock handling */
41  class HandlerBase {
42  public:
43  /** Function called when Handler is invoked */
44  virtual bool operator()(Cycle_t) = 0;
45  virtual ~HandlerBase() {}
46  };
47 
48 
49  /** Event Handler class with user-data argument
50  * @tparam classT Type of the Object
51  * @tparam argT Type of the argument
52  */
53  template <typename classT, typename argT = void>
54  class Handler : public HandlerBase {
55  private:
56  typedef bool (classT::*PtrMember)(Cycle_t, argT);
57  classT* object;
58  const PtrMember member;
59  argT data;
60 
61  public:
62  /** Constructor
63  * @param object - Pointer to Object upon which to call the handler
64  * @param member - Member function to call as the handler
65  * @param data - Additional argument to pass to handler
66  */
67  Handler( classT* const object, PtrMember member, argT data ) :
68  object(object),
69  member(member),
70  data(data)
71  {}
72 
73  /**
74  Calls underlying handler function, passing in the current
75  cycle count and additional metadata supplied by the user.
76 
77  If the hander function returns true, the handler will be
78  removed from the clock list.
79  */
80  bool operator()(Cycle_t cycle) override {
81  return (object->*member)(cycle,data);
82  }
83  };
84 
85  /** Event Handler class without user-data
86  * @tparam classT Type of the Object
87  */
88  template <typename classT>
89  class Handler<classT, void> : public HandlerBase {
90  private:
91  typedef bool (classT::*PtrMember)(Cycle_t);
92  classT* object;
93  const PtrMember member;
94 
95  public:
96  /** Constructor
97  * @param object - Pointer to Object upon which to call the handler
98  * @param member - Member function to call as the handler
99  */
100  Handler( classT* const object, PtrMember member ) :
101  object(object),
102  member(member)
103  {}
104 
105  /**
106  Calls underlying handler function, passing in the current
107  cycle count.
108 
109  If the hander function returns true, the handler will be
110  removed from the clock list.
111  */
112  bool operator()(Cycle_t cycle) override {
113  return (object->*member)(cycle);
114  }
115  };
116 
117  /**
118  * Activates this clock object, by inserting into the simulation's
119  * timeVortex for future execution.
120  */
121  void schedule();
122 
123  /** Return the time of the next clock tick */
124  Cycle_t getNextCycle();
125 
126  /** Add a handler to be called on this clock's tick */
127  bool registerHandler( Clock::HandlerBase* handler );
128  /** Remove a handler from the list of handlers to be called on the clock tick */
129  bool unregisterHandler( Clock::HandlerBase* handler, bool& empty );
130 
131  void print(const std::string& header, Output &out) const override;
132 
133 private:
134 /* typedef std::list<Clock::HandlerBase*> HandlerMap_t; */
135  typedef std::vector<Clock::HandlerBase*> StaticHandlerMap_t;
136 
137 
138  Clock() { }
139 
140  void execute( void ) override;
141 
142  Cycle_t currentCycle;
143  TimeConverter* period;
144  StaticHandlerMap_t staticHandlerMap;
145  SimTime_t next;
146  bool scheduled;
147 
148 };
149 
150 } // namespace SST
151 
152 
153 #endif // SST_CORE_CLOCK_H
Output object provides consistent method for outputting 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
A class to convert between a component&#39;s view of time and the core&#39;s view of time.
Definition: timeConverter.h:25
Handler(classT *const object, PtrMember member)
Constructor.
Definition: clock.h:100
void print(const std::string &header, Output &out) const override
Generic print-print function for this Activity.
Definition: clock.cc:126
Cycle_t getNextCycle()
Return the time of the next clock tick.
Definition: clock.cc:68
bool registerHandler(Clock::HandlerBase *handler)
Add a handler to be called on this clock&#39;s tick.
Definition: clock.cc:40
Handler(classT *const object, PtrMember member, argT data)
Constructor.
Definition: clock.h:67
A Clock class.
Definition: clock.h:32
bool operator()(Cycle_t cycle) override
Calls underlying handler function, passing in the current cycle count.
Definition: clock.h:112
bool operator()(Cycle_t cycle) override
Calls underlying handler function, passing in the current cycle count and additional metadata supplie...
Definition: clock.h:80
Functor classes for Clock handling.
Definition: clock.h:41
virtual bool operator()(Cycle_t)=0
Function called when Handler is invoked.
Event Handler class with user-data argument.
Definition: clock.h:54
bool unregisterHandler(Clock::HandlerBase *handler, bool &empty)
Remove a handler from the list of handlers to be called on the clock tick.
Definition: clock.cc:50
void schedule()
Activates this clock object, by inserting into the simulation&#39;s timeVortex for future execution...
Definition: clock.cc:103