OMNeT++ Simulation Library  5.6.1
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 
23 namespace omnetpp {
24 
25 
33 class SIM_API cWatchBase : public cNoncopyableOwnedObject
34 {
35  public:
41  cWatchBase(const char *name) : cNoncopyableOwnedObject(name) {}
43 
49  virtual bool supportsAssignment() const = 0;
50 
55  virtual void assign(const char *s) {}
57 };
58 
59 
64 template<typename T>
66 {
67  private:
68  const T& r;
69  public:
70  cGenericReadonlyWatch(const char *name, const T& x) : cWatchBase(name), r(x) {}
71  virtual const char *getClassName() const override {return opp_typename(typeid(T));}
72  virtual bool supportsAssignment() const override {return false;}
73  virtual std::string str() const override
74  {
75  std::stringstream out;
76  out << r;
77  return out.str();
78  }
79 };
80 
81 
87 template<typename T>
89 {
90  private:
91  T& r;
92  public:
93  cGenericAssignableWatch(const char *name, T& x) : cWatchBase(name), r(x) {}
94  virtual const char *getClassName() const override {return opp_typename(typeid(T));}
95  virtual bool supportsAssignment() const override {return true;}
96  virtual std::string str() const override
97  {
98  std::stringstream out;
99  out << r;
100  return out.str();
101  }
102  virtual void assign(const char *s) override
103  {
104  std::stringstream in(s);
105  in >> r;
106  }
107 };
108 
113 class SIM_API cWatch_bool : public cWatchBase
114 {
115  private:
116  bool& r;
117  public:
118  cWatch_bool(const char *name, bool& x) : cWatchBase(name), r(x) {}
119  virtual const char *getClassName() const override {return "bool";}
120  virtual bool supportsAssignment() const override {return true;}
121  virtual std::string str() const override
122  {
123  return r ? "true" : "false";
124  }
125  virtual void assign(const char *s) override
126  {
127  r = *s!='0' && *s!='n' && *s!='N' && *s!='f' && *s!='F';
128  }
129 };
130 
135 class SIM_API cWatch_char : public cWatchBase
136 {
137  private:
138  char& r;
139  public:
140  cWatch_char(const char *name, char& x) : cWatchBase(name), r(x) {}
141  virtual const char *getClassName() const override {return "char";}
142  virtual bool supportsAssignment() const override {return true;}
143  virtual std::string str() const override
144  {
145  std::stringstream out;
146  out << "'" << ((unsigned char)r<32 ? ' ' : r) << "' (" << int(r) << ")";
147  return out.str();
148  }
149  virtual void assign(const char *s) override
150  {
151  if (s[0]=='\'')
152  r = s[1];
153  else
154  r = atoi(s);
155  }
156 };
157 
162 class SIM_API cWatch_uchar : public cWatchBase
163 {
164  private:
165  unsigned char& r;
166  public:
167  cWatch_uchar(const char *name, unsigned char& x) : cWatchBase(name), r(x) {}
168  virtual const char *getClassName() const override {return "unsigned char";}
169  virtual bool supportsAssignment() const override {return true;}
170  virtual std::string str() const override
171  {
172  std::stringstream out;
173  out << "'" << (char)(r<' ' ? ' ' : r) << "' (" << int(r) << ")";
174  return out.str();
175  }
176  virtual void assign(const char *s) override
177  {
178  if (s[0]=='\'')
179  r = s[1];
180  else
181  r = atoi(s);
182  }
183 };
184 
189 class SIM_API cWatch_stdstring : public cWatchBase
190 {
191  private:
192  std::string& r;
193  public:
194  cWatch_stdstring(const char *name, std::string& x) : cWatchBase(name), r(x) {}
195  virtual const char *getClassName() const override {return "std::string";}
196  virtual bool supportsAssignment() const override {return true;}
197  virtual std::string str() const override;
198  virtual void assign(const char *s) override;
199 };
200 
205 class SIM_API cWatch_cObject : public cWatchBase
206 {
207  private:
208  cObject& r;
209  public:
210  cWatch_cObject(const char *name, cObject& ref) : cWatchBase(name), r(ref) {}
211  virtual const char *getClassName() const override {return r.getClassName();}
212  virtual std::string str() const override {return r.str();}
213  virtual bool supportsAssignment() const override {return false;}
214  virtual cClassDescriptor *getDescriptor() const override {return r.getDescriptor();}
215 };
216 
221 class SIM_API cWatch_cObjectPtr : public cWatchBase
222 {
223  private:
224  cObject *&rp;
225  public:
226  cWatch_cObjectPtr(const char *name, cObject *&ptr) : cWatchBase(name), rp(ptr) {}
227  virtual const char *getClassName() const override {return rp? rp->getClassName() : "n/a";}
228  virtual std::string str() const override {return rp ? rp->str() : "<null>";}
229  virtual bool supportsAssignment() const override {return false;}
230  virtual cClassDescriptor *getDescriptor() const override {return rp ? rp->getDescriptor() : nullptr;}
231 };
232 
233 
234 inline cWatchBase *createWatch(const char *varname, short& d) {
235  return new cGenericAssignableWatch<short>(varname, d);
236 }
237 
238 inline cWatchBase *createWatch(const char *varname, unsigned short& d) {
239  return new cGenericAssignableWatch<unsigned short>(varname, d);
240 }
241 
242 inline cWatchBase *createWatch(const char *varname, int& d) {
243  return new cGenericAssignableWatch<int>(varname, d);
244 }
245 
246 inline cWatchBase *createWatch(const char *varname, unsigned int& d) {
247  return new cGenericAssignableWatch<unsigned int>(varname, d);
248 }
249 
250 inline cWatchBase *createWatch(const char *varname, long& d) {
251  return new cGenericAssignableWatch<long>(varname, d);
252 }
253 
254 inline cWatchBase *createWatch(const char *varname, unsigned long& d) {
255  return new cGenericAssignableWatch<unsigned long>(varname, d);
256 }
257 
258 inline cWatchBase *createWatch(const char *varname, float& d) {
259  return new cGenericAssignableWatch<float>(varname, d);
260 }
261 
262 inline cWatchBase *createWatch(const char *varname, double& d) {
263  return new cGenericAssignableWatch<double>(varname, d);
264 }
265 
266 inline cWatchBase *createWatch(const char *varname, bool& d) {
267  return new cWatch_bool(varname, d);
268 }
269 
270 inline cWatchBase *createWatch(const char *varname, char& d) {
271  return new cWatch_char(varname, d);
272 }
273 
274 inline cWatchBase *createWatch(const char *varname, unsigned char& d) {
275  return new cWatch_uchar(varname, d);
276 }
277 
278 inline cWatchBase *createWatch(const char *varname, signed char& d) {
279  return new cWatch_char(varname, *(char *)&d);
280 }
281 
282 inline cWatchBase *createWatch(const char *varname, std::string& v) {
283  return new cWatch_stdstring(varname, v);
284 }
285 
286 // this is the fallback, if none of the above match
287 template<typename T>
288 inline cWatchBase *createWatch(const char *varname, T& d) {
289  return new cGenericReadonlyWatch<T>(varname, d);
290 }
291 
292 // to be used if T also has op>> defined
293 template<typename T>
294 inline cWatchBase *createWatch_genericAssignable(const char *varname, T& d) {
295  return new cGenericAssignableWatch<T>(varname, d);
296 }
297 
298 // for objects
299 inline cWatchBase *createWatch_cObject(const char *varname, cObject& obj) {
300  return new cWatch_cObject(varname, obj);
301 }
302 
303 // for pointers to objects.
304 // NOTE: this is a bit tricky. C++ thinks that (cObject*&) and
305 // (SomeDerivedType*&) are unrelated, so we have to force the cast
306 // in the WATCH_PTR() macro. But to stay type-safe, we include a 3rd arg
307 // of type cObject*: the compiler has to be able to cast that
308 // implicitly from SomeDerivedType* -- this way we do not accept pointers
309 // that are REALLY unrelated.
310 inline cWatchBase *createWatch_cObjectPtr(const char *varname, cObject *&refp, cObject *p) {
311  ASSERT(refp==p);
312  return new cWatch_cObjectPtr(varname, refp);
313 }
314 
315 
318 
326 #define WATCH(variable) omnetpp::createWatch(#variable,(variable))
327 
334 #define WATCH_RW(variable) omnetpp::createWatch_genericAssignable(#variable,(variable))
335 
342 #define WATCH_OBJ(variable) omnetpp::createWatch_cObject(#variable,(variable))
343 
350 #define WATCH_PTR(variable) omnetpp::createWatch_cObjectPtr(#variable,(cObject*&)(variable),(variable))
351 
352 
353 } // namespace omnetpp
354 
355 
356 #endif
357 
358 
virtual const char * getClassName() const override
Definition: cwatch.h:227
Watch class, specifically for bool.
Definition: cwatch.h:113
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
Watch class, specifically for pointers to objects subclassed from cObject.
Definition: cwatch.h:221
virtual std::string str() const override
Definition: cwatch.h:170
virtual bool supportsAssignment() const override
Definition: cwatch.h:95
cWatchBase(const char *name)
Definition: cwatch.h:41
virtual std::string str() const override
Definition: cwatch.h:212
Watch class, specifically for std::string.
Definition: cwatch.h:189
Helper class to make primitive types and non-cOwnedObject objects inspectable in Tkenv/Qtenv. To be used only via the WATCH, WATCH_PTR, WATCH_OBJ, WATCH_VECTOR etc macros.
Definition: cwatch.h:33
Watch class, specifically for char.
Definition: cwatch.h:135
virtual const char * getClassName() const override
Definition: cwatch.h:195
virtual void assign(const char *s) override
Definition: cwatch.h:176
virtual bool supportsAssignment() const override
Definition: cwatch.h:120
virtual void assign(const char *s) override
Definition: cwatch.h:125
virtual const char * getClassName() const override
Definition: cwatch.h:168
Template Watch class, for any type that supports operator<<, and operator>> for assignment.
Definition: cwatch.h:88
virtual std::string str() const override
Definition: cwatch.h:228
Watch class, specifically for unsigned char.
Definition: cwatch.h:162
virtual const char * getClassName() const override
Definition: cwatch.h:71
virtual bool supportsAssignment() const override
Definition: cwatch.h:213
virtual std::string str() const override
Definition: cwatch.h:143
virtual bool supportsAssignment() const override
Definition: cwatch.h:169
virtual const char * getClassName() const override
Definition: cwatch.h:119
virtual bool supportsAssignment() const override
Definition: cwatch.h:196
virtual std::string str() const override
Definition: cwatch.h:121
virtual bool supportsAssignment() const override
Definition: cwatch.h:229
virtual bool supportsAssignment() const override
Definition: cwatch.h:72
Template Watch class, for any type that supports operator<<.
Definition: cwatch.h:65
virtual void assign(const char *s)
Definition: cwatch.h:55
Watch class, specifically for objects subclassed from cObject.
Definition: cwatch.h:205
virtual const char * getClassName() const override
Definition: cwatch.h:141
virtual std::string str() const
virtual std::string str() const override
Definition: cwatch.h:96
Definition: cabstracthistogram.h:21
virtual std::string str() const override
Definition: cwatch.h:73
virtual const char * getClassName() const override
Definition: cwatch.h:94
virtual void assign(const char *s) override
Definition: cwatch.h:102
Abstract base class for structure description classes, used mainly with message subclassing.
Definition: cclassdescriptor.h:44
Base class for cOwnedObject-based classes that do not wish to support assignment and duplication...
Definition: cownedobject.h:248
virtual const char * getClassName() const override
Definition: cwatch.h:211
virtual void assign(const char *s) override
Definition: cwatch.h:149
virtual bool supportsAssignment() const override
Definition: cwatch.h:142
SIM_API const char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.
virtual const char * getClassName() const