OMNeT++ Simulation Library  6.0.3
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 "cownedobject.h"
22 #include "cclassdescriptor.h"
23 
24 namespace omnetpp {
25 
26 
34 class SIM_API cWatchBase : public cNoncopyableOwnedObject
35 {
36  public:
42  cWatchBase(const char *name) : cNoncopyableOwnedObject(name) {}
44 
50  virtual bool supportsAssignment() const = 0;
51 
56  virtual void assign(const char *s) {}
58 };
59 
60 
65 template<typename T>
67 {
68  private:
69  const T& r;
70  public:
71  cGenericReadonlyWatch(const char *name, const T& x) : cWatchBase(name), r(x) {}
72  virtual const char *getClassName() const override {return omnetpp::opp_typename(typeid(T));}
73  virtual bool supportsAssignment() const override {return false;}
74  virtual std::string str() const override
75  {
76  std::stringstream out;
77  out << r;
78  return out.str();
79  }
80 };
81 
82 
88 template<typename T>
90 {
91  private:
92  T& r;
93  public:
94  cGenericAssignableWatch(const char *name, T& x) : cWatchBase(name), r(x) {}
95  virtual const char *getClassName() const override {return omnetpp::opp_typename(typeid(T));}
96  virtual bool supportsAssignment() const override {return true;}
97  virtual std::string str() const override
98  {
99  std::stringstream out;
100  out << r;
101  return out.str();
102  }
103  virtual void assign(const char *s) override
104  {
105  std::stringstream in(s);
106  in >> r;
107  }
108 };
109 
114 class SIM_API cWatch_bool : public cWatchBase
115 {
116  private:
117  bool& r;
118  public:
119  cWatch_bool(const char *name, bool& x) : cWatchBase(name), r(x) {}
120  virtual const char *getClassName() const override {return "bool";}
121  virtual bool supportsAssignment() const override {return true;}
122  virtual std::string str() const override
123  {
124  return r ? "true" : "false";
125  }
126  virtual void assign(const char *s) override
127  {
128  r = *s!='0' && *s!='n' && *s!='N' && *s!='f' && *s!='F';
129  }
130 };
131 
136 class SIM_API cWatch_char : public cWatchBase
137 {
138  private:
139  char& r;
140  public:
141  cWatch_char(const char *name, char& x) : cWatchBase(name), r(x) {}
142  virtual const char *getClassName() const override {return "char";}
143  virtual bool supportsAssignment() const override {return true;}
144  virtual std::string str() const override
145  {
146  std::stringstream out;
147  out << "'" << ((unsigned char)r<32 ? ' ' : r) << "' (" << int(r) << ")";
148  return out.str();
149  }
150  virtual void assign(const char *s) override
151  {
152  if (s[0]=='\'')
153  r = s[1];
154  else
155  r = atoi(s);
156  }
157 };
158 
163 class SIM_API cWatch_uchar : public cWatchBase
164 {
165  private:
166  unsigned char& r;
167  public:
168  cWatch_uchar(const char *name, unsigned char& x) : cWatchBase(name), r(x) {}
169  virtual const char *getClassName() const override {return "unsigned char";}
170  virtual bool supportsAssignment() const override {return true;}
171  virtual std::string str() const override
172  {
173  std::stringstream out;
174  out << "'" << (char)(r<' ' ? ' ' : r) << "' (" << int(r) << ")";
175  return out.str();
176  }
177  virtual void assign(const char *s) override
178  {
179  if (s[0]=='\'')
180  r = s[1];
181  else
182  r = atoi(s);
183  }
184 };
185 
190 class SIM_API cWatch_stdstring : public cWatchBase
191 {
192  private:
193  std::string& r;
194  public:
195  cWatch_stdstring(const char *name, std::string& x) : cWatchBase(name), r(x) {}
196  virtual const char *getClassName() const override {return "std::string";}
197  virtual bool supportsAssignment() const override {return true;}
198  virtual std::string str() const override;
199  virtual void assign(const char *s) override;
200 };
201 
206 class SIM_API cWatch_cObject : public cWatchBase
207 {
208  friend class cWatchProxyDescriptor;
209  private:
210  cObject& r;
211  cClassDescriptor *desc; // for this watch object, not the one being watched
212  std::string typeName;
213  public:
214  cWatch_cObject(const char *name, const char *typeName, cObject& ref);
215  ~cWatch_cObject() { dropAndDelete(desc); }
216  virtual const char *getClassName() const override {return typeName.c_str();}
217  virtual std::string str() const override {return std::string("-> ") + r.getClassName() + ")" + r.getFullName() + " " + r.str();}
218  virtual bool supportsAssignment() const override {return false;}
219  virtual cClassDescriptor *getDescriptor() const override {return desc;}
220  virtual void forEachChild(cVisitor *visitor) override;
221  cObject *getObjectPtr() const {return &r;}
222 };
223 
228 class SIM_API cWatch_cObjectPtr : public cWatchBase
229 {
230  friend class cWatchProxyDescriptor;
231  private:
232  cObject *&rp;
233  cClassDescriptor *desc; // for this watch object, not the one being watched
234  std::string typeName;
235  public:
236  cWatch_cObjectPtr(const char *name, const char* typeName, cObject *&ptr);
237  ~cWatch_cObjectPtr() { dropAndDelete(desc); }
238  virtual const char *getClassName() const override {return typeName.c_str();}
239  virtual std::string str() const override {return rp ? ( std::string("-> (") + rp->getClassName() + ")" + rp->getFullName() + " " + rp->str()) : "<null>";}
240  virtual bool supportsAssignment() const override {return false;}
241  virtual cClassDescriptor *getDescriptor() const override {return desc;}
242  virtual void forEachChild(cVisitor *visitor) override;
243  cObject *getObjectPtr() const {return rp;}
244 };
245 
246 
247 inline cWatchBase *createWatch(const char *varname, short& d) {
248  return new cGenericAssignableWatch<short>(varname, d);
249 }
250 
251 inline cWatchBase *createWatch(const char *varname, unsigned short& d) {
252  return new cGenericAssignableWatch<unsigned short>(varname, d);
253 }
254 
255 inline cWatchBase *createWatch(const char *varname, int& d) {
256  return new cGenericAssignableWatch<int>(varname, d);
257 }
258 
259 inline cWatchBase *createWatch(const char *varname, unsigned int& d) {
260  return new cGenericAssignableWatch<unsigned int>(varname, d);
261 }
262 
263 inline cWatchBase *createWatch(const char *varname, long& d) {
264  return new cGenericAssignableWatch<long>(varname, d);
265 }
266 
267 inline cWatchBase *createWatch(const char *varname, unsigned long& d) {
268  return new cGenericAssignableWatch<unsigned long>(varname, d);
269 }
270 
271 inline cWatchBase *createWatch(const char *varname, float& d) {
272  return new cGenericAssignableWatch<float>(varname, d);
273 }
274 
275 inline cWatchBase *createWatch(const char *varname, double& d) {
276  return new cGenericAssignableWatch<double>(varname, d);
277 }
278 
279 inline cWatchBase *createWatch(const char *varname, bool& d) {
280  return new cWatch_bool(varname, d);
281 }
282 
283 inline cWatchBase *createWatch(const char *varname, char& d) {
284  return new cWatch_char(varname, d);
285 }
286 
287 inline cWatchBase *createWatch(const char *varname, unsigned char& d) {
288  return new cWatch_uchar(varname, d);
289 }
290 
291 inline cWatchBase *createWatch(const char *varname, signed char& d) {
292  return new cWatch_char(varname, *(char *)&d);
293 }
294 
295 inline cWatchBase *createWatch(const char *varname, std::string& v) {
296  return new cWatch_stdstring(varname, v);
297 }
298 
299 // this is the fallback, if none of the above match
300 template<typename T>
301 inline cWatchBase *createWatch(const char *varname, T& d) {
302  return new cGenericReadonlyWatch<T>(varname, d);
303 }
304 
305 // to be used if T also has op>> defined
306 template<typename T>
307 inline cWatchBase *createWatch_genericAssignable(const char *varname, T& d) {
308  return new cGenericAssignableWatch<T>(varname, d);
309 }
310 
311 // for objects
312 inline cWatchBase *createWatch_cObject(const char *varname, const char *typeName, cObject& obj) {
313  return new cWatch_cObject(varname, typeName, obj);
314 }
315 
316 // for pointers to objects.
317 // NOTE: this is a bit tricky. C++ thinks that (cObject*&) and
318 // (SomeDerivedType*&) are unrelated, so we have to force the cast
319 // in the WATCH_PTR() macro. But to stay type-safe, we include a 3rd arg
320 // of type cObject*: the compiler has to be able to cast that
321 // implicitly from SomeDerivedType* -- this way we do not accept pointers
322 // that are REALLY unrelated.
323 inline cWatchBase *createWatch_cObjectPtr(const char *varname, const char *typeName, cObject *&refp, cObject *p) {
324  ASSERT(refp==p);
325  return new cWatch_cObjectPtr(varname, typeName, refp);
326 }
327 
328 
341 #define WATCH(variable) omnetpp::createWatch(#variable,(variable))
342 
349 #define WATCH_RW(variable) omnetpp::createWatch_genericAssignable(#variable,(variable))
350 
357 #define WATCH_OBJ(variable) omnetpp::createWatch_cObject(#variable, omnetpp::opp_typename(typeid(variable)), (variable))
358 
365 #define WATCH_PTR(variable) omnetpp::createWatch_cObjectPtr(#variable, omnetpp::opp_typename(typeid(variable)), (cObject*&)(variable),(variable))
366 
369 } // namespace omnetpp
370 
371 
372 #endif
373 
374 
omnetpp::cClassDescriptor
Abstract base class for class descriptors.
Definition: cclassdescriptor.h:39
omnetpp::cGenericReadonlyWatch
Template Watch class, for any type that supports operator<<.
Definition: cwatch.h:66
omnetpp::cGenericAssignableWatch::assign
virtual void assign(const char *s) override
Definition: cwatch.h:103
omnetpp::cGenericAssignableWatch::str
virtual std::string str() const override
Definition: cwatch.h:97
omnetpp::cWatch_char
Watch class, specifically for char.
Definition: cwatch.h:136
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:72
omnetpp::cGenericReadonlyWatch::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:73
omnetpp::cWatch_cObject::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:218
omnetpp::cGenericAssignableWatch::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:95
omnetpp::cWatch_cObject::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:216
omnetpp::cWatch_cObject
Watch class, specifically for objects subclassed from cObject.
Definition: cwatch.h:206
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:169
omnetpp::cWatch_cObjectPtr
Watch class, specifically for pointers to objects subclassed from cObject.
Definition: cwatch.h:228
omnetpp::cWatch_cObjectPtr::str
virtual std::string str() const override
Definition: cwatch.h:239
omnetpp::cWatchBase::assign
virtual void assign(const char *s)
Definition: cwatch.h:56
omnetpp::cWatch_cObjectPtr::getDescriptor
virtual cClassDescriptor * getDescriptor() const override
Definition: cwatch.h:241
omnetpp::cGenericReadonlyWatch::str
virtual std::string str() const override
Definition: cwatch.h:74
omnetpp::cObject::str
virtual std::string str() const
omnetpp::cWatchBase::cWatchBase
cWatchBase(const char *name)
Definition: cwatch.h:42
omnetpp::cWatch_char::assign
virtual void assign(const char *s) override
Definition: cwatch.h:150
omnetpp::cGenericAssignableWatch::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:96
omnetpp::cWatch_bool::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:121
omnetpp::cWatch_cObject::str
virtual std::string str() const override
Definition: cwatch.h:217
omnetpp::cWatch_uchar::assign
virtual void assign(const char *s) override
Definition: cwatch.h:177
omnetpp::cWatch_stdstring
Watch class, specifically for std::string.
Definition: cwatch.h:190
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:122
omnetpp::cGenericAssignableWatch
Template Watch class, for any type that supports operator<<, and operator>> for assignment.
Definition: cwatch.h:89
omnetpp::cWatch_bool::assign
virtual void assign(const char *s) override
Definition: cwatch.h:126
omnetpp::cWatch_bool
Watch class, specifically for bool.
Definition: cwatch.h:114
omnetpp::cWatch_cObject::getDescriptor
virtual cClassDescriptor * getDescriptor() const override
Definition: cwatch.h:219
omnetpp::cWatch_cObjectPtr::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:238
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:196
omnetpp::cWatch_char::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:143
omnetpp::cWatch_uchar::str
virtual std::string str() const override
Definition: cwatch.h:171
omnetpp::cWatch_uchar
Watch class, specifically for unsigned char.
Definition: cwatch.h:163
omnetpp::cWatch_cObjectPtr::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:240
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:142
omnetpp::cWatch_uchar::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:170
omnetpp::cWatch_stdstring::supportsAssignment
virtual bool supportsAssignment() const override
Definition: cwatch.h:197
omnetpp::cWatch_char::str
virtual std::string str() const override
Definition: cwatch.h:144
omnetpp::cWatch_bool::getClassName
virtual const char * getClassName() const override
Definition: cwatch.h:120
omnetpp::cWatchBase
Helper class to make primitive types and non-cOwnedObject objects inspectable in Qtenv....
Definition: cwatch.h:34