The Sparta Modeling Framework
Loading...
Searching...
No Matches
SpartaHandler.hpp
Go to the documentation of this file.
1// <SpartaHandler.hpp> -*- C++ -*-
2
8#pragma once
9
10#include <iostream>
11#include <cinttypes>
12#include <limits>
13
14namespace sparta
15{
16#ifndef DO_NOT_DOCUMENT
30 class SpartaHandler
31 {
32 public:
33 friend std::ostream & operator<<(std::ostream & os, const SpartaHandler & md);
34
38 SpartaHandler() = delete;
39
43 explicit SpartaHandler(const char * name) :
44 object_ptr(nullptr),
45 clear_ptr(&do_nothing_method_stub),
46 stub_ptr(nullptr),
47 stub_ptr_1(nullptr),
48 stub_ptr_2(nullptr),
49 arg_count_(std::numeric_limits<uint32_t>::max()),
50 name_(name)
51 {}
52
56 SpartaHandler(const SpartaHandler& rhp) = default;
57
59 SpartaHandler& operator=(const SpartaHandler&) = default;
60
61 template <class T, void (T::*TMethod)()>
62 static SpartaHandler from_member(T* object_ptr, const char * name = "")
63 {
64 SpartaHandler d(name);
65 d.object_ptr = object_ptr;
66 d.stub_ptr = &method_stub<T, TMethod>;
67 d.arg_count_ = 0;
68 return d;
69 }
70
71 template <class T, void (T::*TMethod)() const>
72 static SpartaHandler from_member(T* object_ptr, const char * name = "")
73 {
74 SpartaHandler d(name);
75 d.object_ptr = object_ptr;
76 d.stub_ptr = &method_stub<T, TMethod>;
77 d.arg_count_ = 0;
78 return d;
79 }
80
81 template <class T, void (T::*TMethod)(), void (T::*TMethodClear)()>
82 static SpartaHandler from_member_clear(T* object_ptr, const char * name = "")
83 {
84 SpartaHandler d(name);
85 d.object_ptr = object_ptr;
86 d.stub_ptr = &method_stub<T, TMethod>;
87 d.clear_ptr = &method_stub<T, TMethodClear>;
88 d.arg_count_ = 0;
89 return d;
90 }
91
92 template <class T, void (T::*TMethod)() const, void (T::*TMethodClear)()>
93 static SpartaHandler from_member_clear(T* object_ptr, const char * name = "")
94 {
95 SpartaHandler d(name);
96 d.object_ptr = object_ptr;
97 d.stub_ptr = &method_stub<T, TMethod>;
98 d.clear_ptr = &method_stub<T, TMethodClear>;
99 d.arg_count_ = 0;
100 return d;
101 }
102
103 template <class T, class DataT, void (T::*TMethod)(const DataT &)>
104 static SpartaHandler from_member_1(T* object_ptr, const char * name = "")
105 {
106 SpartaHandler d(name);
107 d.object_ptr = object_ptr;
108 d.stub_ptr_1 = &method_stub_1<T, DataT, TMethod>;
109 d.arg_count_ = 1;
110 return d;
111 }
112
113 template <class T, class DataT, void (T::*TMethod)(const DataT &) const>
114 static SpartaHandler from_member_1(T* object_ptr, const char * name = "")
115 {
116 SpartaHandler d(name);
117 d.object_ptr = object_ptr;
118 d.stub_ptr_1 = &method_stub_1<T, DataT, TMethod>;
119 d.arg_count_ = 1;
120 return d;
121 }
122
123 template <class T, class DataT, class DataTwo, void (T::*TMethod)(const DataT &, const DataTwo&)>
124 static SpartaHandler from_member_2(T* object_ptr, const char * name = "")
125 {
126 SpartaHandler d(name);
127 d.object_ptr = object_ptr;
128 d.stub_ptr_2 = &method_stub_2<T, DataT, DataTwo, TMethod>;
129 d.arg_count_ = 1;
130 return d;
131 }
132
133 template <class T, class DataT, class DataTwo, void (T::*TMethod)(const DataT &, const DataTwo&) const>
134 static SpartaHandler from_member_2(T* object_ptr, const char * name = "")
135 {
136 SpartaHandler d(name);
137 d.object_ptr = object_ptr;
138 d.stub_ptr_2 = &method_stub_2<T, DataT, DataTwo, TMethod>;
139 d.arg_count_ = 1;
140 return d;
141 }
142
143 void operator()() const
144 {
145 (*stub_ptr)(object_ptr);
146 }
147
148 // Can this be changed to a template DataT and the void cast
149 // be hidden in the function?
150 void operator()(const void * dat) const
151 {
152 (*stub_ptr_1)(object_ptr, dat);
153 }
154
155 void operator()(const void* dat, const void* dat_two) const
156 {
157 (*stub_ptr_2)(object_ptr, dat, dat_two);
158 }
159
160 template<class ObjT>
161 void setObjectPtr(ObjT * obj) {
162 object_ptr = (void *)obj;
163 }
164
165 template<class ObjT>
166 ObjT* getObjectPtr() {
167 return static_cast<ObjT*>(object_ptr);
168 }
169
170 void clear() {
171 (*clear_ptr)(object_ptr);
172 }
173
174 uint32_t argCount() const {
175 return arg_count_;
176 }
177
178 const char * getName() const {
179 return name_;
180 }
181
182 void setName(const char * name) {
183 name_ = name;
184 }
185
186 bool operator==(const SpartaHandler & rhs) const {
187 return (std::string(name_) == rhs.name_);
188 }
189
190 bool operator!=(const SpartaHandler & rhs) const {
191 return !operator==(rhs);
192 }
193
194 template<class ObjT>
195 bool operator==(ObjT* obj) const {
196 return object_ptr == static_cast<void*>(obj);
197 }
198
199 template<class ObjT>
200 bool operator!=(ObjT* obj) const {
201 return !operator==(obj);
202 }
203
204 operator bool() const {
205 return (object_ptr != nullptr);
206 }
207
208 private:
209
210 typedef void (*stub_type)(void* object_ptr);
211 typedef void (*stub_type_1)(void* object_ptr, const void*);
212 typedef void (*stub_type_2)(void* object_ptr, const void*, const void*);
213
214 void* object_ptr;
215 stub_type clear_ptr;
216 stub_type stub_ptr;
217 stub_type_1 stub_ptr_1;
218 stub_type_2 stub_ptr_2;
219
220 uint32_t arg_count_;
221
222 const char * name_;
223
224 static void do_nothing_method_stub(void*)
225 { }
226
227 template <class T, void (T::*TMethod)()>
228 static void method_stub(void* object_ptr)
229 {
230 T* p = static_cast<T*>(object_ptr);
231 (p->*TMethod)();
232 }
233
234 template <class T, void (T::*TMethod)() const>
235 static void method_stub(void* object_ptr)
236 {
237 T* p = static_cast<T*>(object_ptr);
238 (p->*TMethod)();
239 }
240
241 template <class T, class DataT, void (T::*TMethod)(const DataT &)>
242 static void method_stub_1(void* object_ptr, const void * dat)
243 {
244 T* p = static_cast<T*>(object_ptr);
245 const DataT & data = *(static_cast<const DataT *>(dat));
246 (p->*TMethod)(data);
247 }
248
249 template <class T, class DataT, void (T::*TMethod)(const DataT &) const>
250 static void method_stub_1(void* object_ptr, const void * dat)
251 {
252 T* p = static_cast<T*>(object_ptr);
253 const DataT & data = *(static_cast<const DataT *>(dat));
254 (p->*TMethod)(data);
255 }
256
257 template <class T, class DataT, class DataTwo, void (T::*TMethod)(const DataT &, const DataTwo&)>
258 static void method_stub_2(void* object_ptr, const void * dat, const void * dat_two)
259 {
260 T* p = static_cast<T*>(object_ptr);
261 const DataT & data = *(static_cast<const DataT *>(dat));
262 const DataTwo & data_two = *(static_cast<const DataTwo *>(dat_two));
263 (p->*TMethod)(data, data_two);
264 }
265
266 template <class T, class DataT, class DataTwo, void (T::*TMethod)(const DataT &, const DataTwo&) const>
267 static void method_stub_2(void* object_ptr, const void * dat, const void * dat_two)
268 {
269 T* p = static_cast<T*>(object_ptr);
270 const DataT & data = *(static_cast<const DataT *>(dat));
271 const DataTwo & data_two = *(static_cast<const DataTwo *>(dat_two));
272 (p->*TMethod)(data, data_two);
273 }
274 };
275
276 inline std::ostream & operator<<(std::ostream & os, const SpartaHandler & md) {
277 os << md.name_;
278 return os;
279 }
280#endif
281
317#define CREATE_SPARTA_HANDLER(clname, meth) \
318 sparta::SpartaHandler::from_member<clname, &clname::meth> \
319 (this, #clname"::"#meth"()")
320
364#define CREATE_SPARTA_HANDLER_WITH_CLEAR(clname, meth, clear) \
365 sparta::SpartaHandler::from_member_clear<clname, &clname::meth, &clname::clear>\
366 (this, #clname"::"#meth"()")
367
401#define CREATE_SPARTA_HANDLER_WITH_OBJ(clname, obj, meth) \
402 sparta::SpartaHandler::from_member<clname, &clname::meth> \
403 (obj, #clname"::"#meth"()")
404
441#define CREATE_SPARTA_HANDLER_WITH_DATA(clname, meth, dataT) \
442 sparta::SpartaHandler::from_member_1<clname, dataT, &clname::meth> \
443 (this, #clname"::"#meth"("#dataT")")
444
480#define CREATE_SPARTA_HANDLER_WITH_TWO_DATA(clname, meth, dataOne, dataTwo) \
481 sparta::SpartaHandler::from_member_2<clname, dataOne, dataTwo, &clname::meth> \
482 (this, #clname"::"#meth"("#dataOne","#dataTwo")")
483
518#define CREATE_SPARTA_HANDLER_WITH_DATA_WITH_OBJ(clname, obj, meth, dataT) \
519 sparta::SpartaHandler::from_member_1<clname, dataT, &clname::meth> \
520 (obj, #clname"::"#meth"("#dataT")")
521}
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo