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