SST/macro
event_callback.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_COMMON_EVENTCALLBACK_H_INCLUDED
13 #define SSTMAC_COMMON_EVENTCALLBACK_H_INCLUDED
14 
17 
18 namespace sstmac {
19 
20 template<int ...>
21 struct seq { };
22 
23 template<int N, int ...S>
24 struct gens : gens<N-1, N-1, S...> { };
25 
26 template<int ...S>
27 struct gens<0, S...> {
28  typedef seq<S...> type;
29 };
30 
32  public event_handler
33 {
34  public:
35  virtual ~member_fxn_handler(){}
36 
37  virtual std::string
38  to_string() const {
39  return "member fxn handler";
40  }
41  protected:
43  {
44  init_loc_id(id);
45  }
46 };
47 
48 template <class Cls, typename Fxn, class ...Args>
50  public member_fxn_handler
51 {
52 
53  public:
55 
56  void
57  handle(event* ev) {
58  dispatch(ev, typename gens<sizeof...(Args)>::type());
59  }
60 
61  member_fxn_handler_impl(event_loc_id id, Cls* obj, Fxn fxn, const Args&... args) :
62  params_(args...),
63  obj_(obj),
64  fxn_(fxn),
66  {
67  }
68 
69  private:
70  template <int ...S>
71  void
73  (obj_->*fxn_)(ev, std::get<S>(params_)...);
74  }
75 
76  std::tuple<Args...> params_;
77  Fxn fxn_;
78  Cls* obj_;
79 
80 };
81 
82 #if 0
83 template <class Cls, typename Fxn>
84 class member_fxn_handler_impl<Cls,Fxn> :
85  public member_fxn_handler
86 {
87  public:
88  virtual ~member_fxn_handler_impl(){}
89 
90  void
91  handle(event* ev) {
92  (obj_->*fxn_)(ev);
93  }
94 
95  member_fxn_handler_impl(event_loc_id id, Cls* obj, Fxn fxn) :
96  obj_(obj),
97  fxn_(fxn),
99  {
100  }
101 
102  private:
103  Fxn fxn_;
104  Cls* obj_;
105 };
106 #endif
107 
108 template<class Cls, typename Fxn, class ...Args>
110 new_handler(Cls* cls, Fxn fxn, const Args&... args)
111 {
112  return new member_fxn_handler_impl<Cls, Fxn, Args...>(
113  cls->event_location(), cls, fxn, args...);
114 }
115 
116 
117 template <class Cls, typename Fxn, class ...Args>
119  public callback
120 {
121 
122  public:
124 
125  void
127  dispatch(typename gens<sizeof...(Args)>::type());
128  }
129 
130  virtual std::string
131  to_string() const {
132  return "member fxn callback";
133  }
134 
135  member_fxn_callback(event_loc_id id, Cls* obj, Fxn fxn, const Args&... args) :
136  callback(id),
137  params_(args...),
138  obj_(obj),
139  fxn_(fxn)
140  {
141  }
142 
143  private:
144  template <int ...S>
145  void
147  (obj_->*fxn_)(std::get<S>(params_)...);
148  }
149 
150  std::tuple<Args...> params_;
151  Fxn fxn_;
152  Cls* obj_;
153 
154 };
155 
156 template<class Cls, typename Fxn, class ...Args>
157 callback*
158 new_callback(Cls* cls, Fxn fxn, const Args&... args)
159 {
160  return new member_fxn_callback<Cls, Fxn, Args...>(
161  cls->event_location(), cls, fxn, args...);
162 }
163 
164 template<class Cls, typename Fxn, class ...Args>
165 callback*
166 new_callback(event_loc_id id, Cls* cls, Fxn fxn, const Args&... args)
167 {
168  return new member_fxn_callback<Cls, Fxn, Args...>(
169  id, cls, fxn, args...);
170 }
171 
172 } // end of namespace sstmac
173 #endif
174 
std::tuple< Args... > params_
void dispatch(event *ev, seq< S... >)
member_fxn_handler(event_loc_id id)
event_handler * new_handler(Cls *cls, Fxn fxn, const Args &...args)
virtual std::string to_string() const
virtual std::string to_string() const
callback * new_callback(Cls *cls, Fxn fxn, const Args &...args)
The main interface for something that can respond to an event (sst_message).
Definition: event_handler.h:24
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
member_fxn_callback(event_loc_id id, Cls *obj, Fxn fxn, const Args &...args)
std::tuple< Args... > params_
void dispatch(seq< S... >)
member_fxn_handler_impl(event_loc_id id, Cls *obj, Fxn fxn, const Args &...args)