OMNeT++ Simulation Library  6.0.3
cdisplaystring.h
1 //==========================================================================
2 // CDISPLAYSTRING.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_CDISPLAYSTRING_H
17 #define __OMNETPP_CDISPLAYSTRING_H
18 
19 #include "cobject.h"
20 #include "cgate.h"
21 #include "cmodule.h"
22 
23 namespace omnetpp {
24 
25 
58 class SIM_API cDisplayString
59 {
60  private:
61  enum { MAXARGS = 16 }; // maximum number of arguments per tag
62 
63  // holds one tag
64  struct Tag {
65  char *name;
66  int numArgs;
67  char *args[MAXARGS];
68  Tag() {name=nullptr; numArgs=0;}
69  };
70 
71  char *buffer = nullptr; // holds pieces of display string (sliced with zeroes)
72  char *bufferEnd = nullptr; // points to last byte of buffer allocated
73  Tag *tags = nullptr; // table of tags
74  int numTags = 0; // number of tags
75 
76  mutable char *assembledString = nullptr; // cached copy of assembled display string
77  mutable bool assembledStringValid = true; // if cached copy is up-to-date
78 
79  // needed for notifications
80  cComponent *ownerComponent = nullptr;
81 
82  private:
83  void copy(const cDisplayString& other) {parse(other.str());}
84 
85  // helper functions
86  void doParse(const char *s);
87  void doUpdateWith(const cDisplayString& ds);
88  bool doSetTagArg(int tagindex, int index, const char *value);
89  bool doSetTagArg(const char *tagname, int index, const char *value);
90  int doInsertTag(const char *tagname, int atindex=0);
91  bool doRemoveTag(int tagindex);
92 
93  void doParse();
94  void assemble() const;
95  void clearTags();
96  bool pointsIntoBuffer(char *s) const {return s>=buffer && s<=bufferEnd;}
97  static void strcatescaped(char *d, const char *s);
98 
99  // internal: called before the stored display string changes
100  void beforeChange();
101  // internal: called when the stored display string changes, and notifies Envir in turn.
102  void afterChange();
103 
104  public:
105  // internal:
106  void setHostObject(cComponent *o) {ownerComponent=o;}
107  void dump(std::ostream& out) const;
108 
109  public:
112 
117 
123  cDisplayString(const char *dispstr);
124 
128  cDisplayString(const cDisplayString& ds);
129 
133  ~cDisplayString();
135 
141  cDisplayString& operator=(const cDisplayString& ds);
142 
146  cDisplayString& operator=(const char *s) {parse(s); return *this;}
147 
151  operator const char *() const {return str();}
153 
156 
160  const char *str() const;
161 
165  void set(const char *displaystr) {parse(displaystr);}
166 
178  void parse(const char *displaystr);
179 
184  void updateWith(const cDisplayString& ds);
185 
190  void updateWith(const char *s);
192 
195 
199  bool containsTag(const char *tagname) const;
200 
208  int getNumArgs(const char *tagname) const;
209 
215  const char *getTagArg(const char *tagname, int index) const;
216 
226  bool setTagArg(const char *tagname, int index, const char *value);
227 
232  bool setTagArg(const char *tagname, int index, long value);
233 
239  bool removeTag(const char *tagname);
241 
244 
249  int getNumTags() const;
250 
255  const char *getTagName(int tagindex) const;
256 
261  int getTagIndex(const char *tagname) const;
262 
270  int getNumArgs(int tagindex) const;
271 
277  const char *getTagArg(int tagindex, int index) const;
278 
288  bool setTagArg(int tagindex, int index, const char *value);
289 
297  int insertTag(const char *tagname, int atindex=0);
298 
304  bool removeTag(int tagindex);
306 };
307 
308 
309 } // namespace omnetpp
310 
311 
312 #endif
313 
314 
omnetpp::cDisplayString::set
void set(const char *displaystr)
Definition: cdisplaystring.h:165
omnetpp::cDisplayString::cDisplayString
cDisplayString()
Definition: cdisplaystring.h:116
omnetpp::cComponent
Common base for module and channel classes.
Definition: ccomponent.h:49
omnetpp::cDisplayString::str
const char * str() const
omnetpp::cDisplayString
Represents a display string.
Definition: cdisplaystring.h:58
omnetpp::cDisplayString::operator=
cDisplayString & operator=(const char *s)
Definition: cdisplaystring.h:146