SST  10.0.0
StructuralSimulationToolkit
clock.h
1 // Copyright 2009-2020 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-2020, 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  bool operator()(Cycle_t cycle) override {
74  return (object->*member)(cycle,data);
75  }
76  };
77 
78  /** Event Handler class without user-data
79  * @tparam classT Type of the Object
80  */
81  template <typename classT>
82  class Handler<classT, void> : public HandlerBase {
83  private:
84  typedef bool (classT::*PtrMember)(Cycle_t);
85  classT* object;
86  const PtrMember member;
87 
88  public:
89  /** Constructor
90  * @param object - Pointer to Object upon which to call the handler
91  * @param member - Member function to call as the handler
92  */
93  Handler( classT* const object, PtrMember member ) :
94  object(object),
95  member(member)
96  {}
97 
98  bool operator()(Cycle_t cycle) override {
99  return (object->*member)(cycle);
100  }
101  };
102 
103  /**
104  * Activates this clock object, by inserting into the simulation's
105  * timeVortex for future execution.
106  */
107  void schedule();
108 
109  /** Return the time of the next clock tick */
110  Cycle_t getNextCycle();
111 
112  /** Add a handler to be called on this clock's tick */
113  bool registerHandler( Clock::HandlerBase* handler );
114  /** Remove a handler from the list of handlers to be called on the clock tick */
115  bool unregisterHandler( Clock::HandlerBase* handler, bool& empty );
116 
117  void print(const std::string& header, Output &out) const override;
118 
119 private:
120 /* typedef std::list<Clock::HandlerBase*> HandlerMap_t; */
121  typedef std::vector<Clock::HandlerBase*> StaticHandlerMap_t;
122 
123 
124  Clock() { }
125 
126  void execute( void ) override;
127 
128  Cycle_t currentCycle;
129  TimeConverter* period;
130  StaticHandlerMap_t staticHandlerMap;
131  SimTime_t next;
132  bool scheduled;
133 
134 };
135 
136 } // namespace SST
137 
138 
139 #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:93
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
Function called when Handler is invoked.
Definition: clock.h:98
bool operator()(Cycle_t cycle) override
Function called when Handler is invoked.
Definition: clock.h:73
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