OMNeT++ API 6.2.0
Discrete Event Simulation Library
cwatch.h
1 //==========================================================================
2 // CWATCH.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CWATCH_H
17 #define __OMNETPP_CWATCH_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include <functional>
22 #include "cownedobject.h"
23 #include "cclassdescriptor.h"
24 
25 namespace omnetpp {
26 
27 
35 class SIM_API cWatchBase : public cNoncopyableOwnedObject
36 {
37  public:
43  cWatchBase(const char *name) : cNoncopyableOwnedObject(name) {}
45 
51  virtual bool supportsAssignment() const = 0;
52 
57  virtual void assign(const char *s) {}
59 };
60 
61 
66 template<typename T>
68 {
69  private:
70  const T& r;
71  public:
72  cGenericReadonlyWatch(const char *name, const T& x) : cWatchBase(name), r(x) {}
73  virtual const char *getClassName() const override {return omnetpp::opp_typename(typeid(T));}
74  virtual bool supportsAssignment() const override {return false;}
75  virtual std::string str() const override
76  {
77  std::stringstream out;
78  out << r;
79  return out.str();
80  }
81 };
82 
83 
89 template<typename T>
91 {
92  private:
93  T& r;
94  public:
95  cGenericAssignableWatch(const char *name, T& x) : cWatchBase(name), r(x) {}
96  virtual const char *getClassName() const override {return omnetpp::opp_typename(typeid(T));}
97  virtual bool supportsAssignment() const override {return true;}
98  virtual std::string str() const override
99  {
100  std::stringstream out;
101  out << r;
102  return out.str();
103  }
104  virtual void assign(const char *s) override
105  {
106  std::stringstream in(s);
107  in >> r;
108  }
109 };
110 
115 class SIM_API cWatch_bool : public cWatchBase
116 {
117  private:
118  bool& r;
119  public:
120  cWatch_bool(const char *name, bool& x) : cWatchBase(name), r(x) {}
121  virtual const char *getClassName() const override {return "bool";}
122  virtual bool supportsAssignment() const override {return true;}
123  virtual std::string str() const override
124  {
125  return r ? "true" : "false";
126  }
127  virtual void assign(const char *s) override
128  {
129  r = *s!='0' && *s!='n' && *s!='N' && *s!='f' && *s!='F';
130  }
131 };
132 
137 class SIM_API cWatch_char : public cWatchBase
138 {
139  private:
140  char& r;
141  public:
142  cWatch_char(const char *name, char& x) : cWatchBase(name), r(x) {}
143  virtual const char *getClassName() const override {return "char";}
144  virtual bool supportsAssignment() const override {return true;}
145  virtual std::string str() const override
146  {
147  std::stringstream out;
148  out << "'" << ((unsigned char)r<32 ? ' ' : r) << "' (" << int(r) << ")";
149  return out.str();
150  }
151  virtual void assign(const char *s) override
152  {
153  if (s[0]=='\'')
154  r = s[1];
155  else
156  r = atoi(s);
157  }
158 };
159 
164 class SIM_API cWatch_uchar : public cWatchBase
165 {
166  private:
167  unsigned char& r;
168  public:
169  cWatch_uchar(const char *name, unsigned char& x) : cWatchBase(name), r(x) {}
170  virtual const char *getClassName() const override {return "unsigned char";}
171  virtual bool supportsAssignment() const override {return true;}
172  virtual std::string str() const override
173  {
174  std::stringstream out;
175  out << "'" << (char)(r<' ' ? ' ' : r) << "' (" << int(r) << ")";
176  return out.str();
177  }
178  virtual void assign(const char *s) override
179  {
180  if (s[0]=='\'')
181  r = s[1];
182  else
183  r = atoi(s);
184  }
185 };
186 
191 class SIM_API cWatch_stdstring : public cWatchBase
192 {
193  private:
194  std::string& r;
195  public:
196  cWatch_stdstring(const char *name, std::string& x) : cWatchBase(name), r(x) {}
197  virtual const char *getClassName() const override {return "std::string";}
198  virtual bool supportsAssignment() const override {return true;}
199  virtual std::string str() const override;
200  virtual void assign(const char *s) override;
201 };
202 
207 class SIM_API cWatch_cObject : public cWatchBase
208 {
209  friend class cWatchProxyDescriptor;
210  private:
211  cObject& r;
212  cClassDescriptor *desc; // for this watch object, not the one being watched
213  std::string typeName;
214  public:
215  cWatch_cObject(const char *name, const char *typeName, cObject& ref);
216  ~cWatch_cObject() { dropAndDelete(desc); }
217  virtual const char *getClassName() const override {return typeName.c_str();}
218  virtual std::string str() const override {return std::string("-> ") + r.getClassName() + ")" + r.getFullName() + " " + r.str();}
219  virtual bool supportsAssignment() const override {return false;}
220  virtual cClassDescriptor *getDescriptor() const override {return desc;}
221  virtual void forEachChild(cVisitor *visitor) override;
222  cObject *getObjectPtr() const {return &r;}
223 };
224 
229 class SIM_API cWatch_cObjectPtr : public cWatchBase
230 {
231  friend class cWatchProxyDescriptor;
232  private:
233  cObject *&rp;
234  cClassDescriptor *desc; // for this watch object, not the one being watched
235  std::string typeName;
236  public:
237  cWatch_cObjectPtr(const char *name, const char* typeName, cObject *&ptr);
238  ~cWatch_cObjectPtr() { dropAndDelete(desc); }
239  virtual const char *getClassName() const override {return typeName.c_str();}
240  virtual std::string str() const override {return rp ? ( std::string("-> (") + rp->getClassName() + ")" + rp->getFullName() + " " + rp->str()) : "<null>";}
241  virtual bool supportsAssignment() const override {return false;}
242  virtual cClassDescriptor *getDescriptor() const override {return desc;}
243  virtual void forEachChild(cVisitor *visitor) override;
244  cObject *getObjectPtr() const {return rp;}
245 };
246 
252 template<typename T>
254 {
255  private:
256  std::function<T()> f;
257  public:
258  cComputedExpressionWatch(const char *name, std::function<T()> f) : cWatchBase(name), f(f) {}
259  virtual const char *getClassName() const override {return omnetpp::opp_typename(typeid(T));}
260  virtual bool supportsAssignment() const override {return false;}
261  virtual std::string str() const override { std::stringstream out; out << f(); return out.str(); }
262 };
263 
264 inline cWatchBase *createWatch(const char *varname, short& d) {
265  return new cGenericAssignableWatch<short>(varname, d);
266 }
267 
268 inline cWatchBase *createWatch(const char *varname, unsigned short& d) {
269  return new cGenericAssignableWatch<unsigned short>(varname, d);
270 }
271 
272 inline cWatchBase *createWatch(const char *varname, int& d) {
273  return new cGenericAssignableWatch<int>(varname, d);
274 }
275 
276 inline cWatchBase *createWatch(const char *varname, unsigned int& d) {
277  return new cGenericAssignableWatch<unsigned int>(varname, d);
278 }
279 
280 inline cWatchBase *createWatch(const char *varname, long& d) {
281  return new cGenericAssignableWatch<long>(varname, d);
282 }
283 
284 inline cWatchBase *createWatch(const char *varname, unsigned long& d) {
285  return new cGenericAssignableWatch<unsigned long>(varname, d);
286 }
287 
288 inline cWatchBase *createWatch(const char *varname, float& d) {
289  return new cGenericAssignableWatch<float>(varname, d);
290 }
291 
292 inline cWatchBase *createWatch(const char *varname, double& d) {
293  return new cGenericAssignableWatch<double>(varname, d);
294 }
295 
296 inline cWatchBase *createWatch(const char *varname, bool& d) {
297  return new cWatch_bool(varname, d);
298 }
299 
300 inline cWatchBase *createWatch(const char *varname, char& d) {
301  return new cWatch_char(varname, d);
302 }
303 
304 inline cWatchBase *createWatch(const char *varname, unsigned char& d) {
305  return new cWatch_uchar(varname, d);
306 }
307 
308 inline cWatchBase *createWatch(const char *varname, signed char& d) {
309  return new cWatch_char(varname, *(char *)&d);
310 }
311 
312 inline cWatchBase *createWatch(const char *varname, std::string& v) {
313  return new cWatch_stdstring(varname, v);
314 }
315 
316 // this is the fallback, if none of the above match
317 template<typename T>
318 inline cWatchBase *createWatch(const char *varname, T& d) {
319  return new cGenericReadonlyWatch<T>(varname, d);
320 }
321 
322 // to be used if T also has op>> defined
323 template<typename T>
324 inline cWatchBase *createWatch_genericAssignable(const char *varname, T& d) {
325  return new cGenericAssignableWatch<T>(varname, d);
326 }
327 
328 // for objects
329 inline cWatchBase *createWatch_cObject(const char *varname, const char *typeName, cObject& obj) {
330  return new cWatch_cObject(varname, typeName, obj);
331 }
332 
333 template<typename T>
334 inline cWatchBase *createComputedExpressionWatch(const char *name, std::function<T()> f) {
335  return new cComputedExpressionWatch<T>(name, f);
336 }
337 
338 // for pointers to objects.
339 // NOTE: this is a bit tricky. C++ thinks that (cObject*&) and
340 // (SomeDerivedType*&) are unrelated, so we have to force the cast
341 // in the WATCH_PTR() macro. But to stay type-safe, we include a 3rd arg
342 // of type cObject*: the compiler has to be able to cast that
343 // implicitly from SomeDerivedType* -- this way we do not accept pointers
344 // that are REALLY unrelated.
345 inline cWatchBase *createWatch_cObjectPtr(const char *varname, const char *typeName, cObject *&refp, cObject *p) {
346  ASSERT(refp==p);
347  return new cWatch_cObjectPtr(varname, typeName, refp);
348 }
349 
350 
363 #define WATCH(variable) omnetpp::createWatch(#variable,(variable))
364 
371 #define WATCH_RW(variable) omnetpp::createWatch_genericAssignable(#variable,(variable))
372 
379 #define WATCH_OBJ(variable) omnetpp::createWatch_cObject(#variable, omnetpp::opp_typename(typeid(variable)), (variable))
380 
387 #define WATCH_PTR(variable) omnetpp::createWatch_cObjectPtr(#variable, omnetpp::opp_typename(typeid(variable)), (cObject*&)(variable),(variable))
388 
405 #define WATCH_EXPR(name, expression) omnetpp::createComputedExpressionWatch(name, std::function([=]() {return (expression);}))
406 
415 #define WATCH_LAMBDA(name, lambdaFunction) omnetpp::createComputedExpressionWatch(name, std::function(lambdaFunction))
416 
419 } // namespace omnetpp
420 
421 
422 #endif
423 
424 
omnetpp::cClassDescriptor
Abstract base class for class descriptors.
Definition: cclassdescriptor.h:39
omnetpp::cComputedExpressionWatch::str
virtual std::string str() const override
Definition: cwatch.h:261
omnetpp::cGenericReadonlyWatch
Template Watch class, for any type that supports operator<<.
Definition: cwatch.h:67
omnetpp::cGenericAssignableWatch::assign
virtual void assign(const char *s) override
Definition: cwatch.h:104
omnetpp::cGenericAssignableWatch::str
virtual std::string str() const override
Definition: cwatch.h:98
omnetpp::cWatch_char
Watch class, specifically for char.
Definition: cwatch.h:137
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cGenericReadonlyWatch::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:73
omnetpp::cGenericReadonlyWatch::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:74
omnetpp::cWatch_cObject::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:219
omnetpp::cGenericAssignableWatch::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:96
omnetpp::cWatch_cObject::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:217
omnetpp::cWatch_cObject
Watch class, specifically for objects subclassed from cObject.
Definition: cwatch.h:207
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cWatch_uchar::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:170
omnetpp::cWatch_cObjectPtr
Watch class, specifically for pointers to objects subclassed from cObject.
Definition: cwatch.h:229
omnetpp::cWatch_cObjectPtr::str
virtual std::string str() const override
Definition: cwatch.h:240
omnetpp::cWatchBase::assign
virtual void assign(const char *s)
Definition: cwatch.h:57
omnetpp::cWatch_cObjectPtr::getDescriptor
virtual cClassDescriptor * getDescriptor() const override
Definition: cwatch.h:242
omnetpp::cGenericReadonlyWatch::str
virtual std::string str() const override
Definition: cwatch.h:75
omnetpp::cObject::str
virtual std::string str() const
omnetpp::cWatchBase::cWatchBase
cWatchBase(const char *name)
Definition: cwatch.h:43
omnetpp::cWatch_char::assign
virtual void assign(const char *s) override
Definition: cwatch.h:151
omnetpp::cGenericAssignableWatch::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:97
omnetpp::cWatch_bool::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:122
omnetpp::cWatch_cObject::str
virtual std::string str() const override
Definition: cwatch.h:218
omnetpp::cComputedExpressionWatch::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:259
omnetpp::cWatch_uchar::assign
virtual void assign(const char *s) override
Definition: cwatch.h:178
omnetpp::cWatch_stdstring
Watch class, specifically for std::string.
Definition: cwatch.h:191
omnetpp::cObject::getFullName
virtual const char * getFullName() const
Definition: cobject.h:169
omnetpp::cWatch_bool::str
virtual std::string str() const override
Definition: cwatch.h:123
omnetpp::cGenericAssignableWatch
Template Watch class, for any type that supports operator<<, and operator>> for assignment.
Definition: cwatch.h:90
omnetpp::cWatch_bool::assign
virtual void assign(const char *s) override
Definition: cwatch.h:127
omnetpp::cWatch_bool
Watch class, specifically for bool.
Definition: cwatch.h:115
omnetpp::cWatch_cObject::getDescriptor
virtual cClassDescriptor * getDescriptor() const override
Definition: cwatch.h:220
omnetpp::cWatch_cObjectPtr::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:239
omnetpp::cNoncopyableOwnedObject
Base class for cOwnedObject-based classes that do not wish to support assignment and duplication.
Definition: cownedobject.h:242
omnetpp::cWatch_stdstring::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:197
omnetpp::cWatch_char::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:144
omnetpp::cWatch_uchar::str
virtual std::string str() const override
Definition: cwatch.h:172
omnetpp::cWatch_uchar
Watch class, specifically for unsigned char.
Definition: cwatch.h:164
omnetpp::cWatch_cObjectPtr::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:241
omnetpp::cObject::getClassName
virtual const char * getClassName() const
omnetpp::opp_typename
const SIM_API char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.
omnetpp::cWatch_char::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:143
omnetpp::cWatch_uchar::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:171
omnetpp::cWatch_stdstring::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:198
omnetpp::cWatch_char::str
virtual std::string str() const override
Definition: cwatch.h:145
omnetpp::cWatch_bool::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:121
omnetpp::cComputedExpressionWatch::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:260
omnetpp::cWatchBase
Helper class to make primitive types and non-cOwnedObject objects inspectable in Qtenv....
Definition: cwatch.h:35
omnetpp::cComputedExpressionWatch
Watch class for computed expressions. The watch will display the result of calling an encapsulated fu...
Definition: cwatch.h:253