OMNeT++ Simulation Library  5.4.1
cgate.h
1 //==========================================================================
2 // CGATE.H - header for
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_CGATE_H
17 #define __OMNETPP_CGATE_H
18 
19 #include <set>
20 #include <map>
21 #include "cobject.h"
22 #include "cstringpool.h"
23 #include "opp_string.h"
24 #include "simtime_t.h"
25 #include "cexception.h"
26 
27 namespace omnetpp {
28 
29 class cGate;
30 class cModule;
31 class cMessage;
32 class cChannelType;
33 class cChannel;
34 class cProperties;
35 class cDisplayString;
36 class cIdealChannel;
37 class cDatarateChannel;
38 
39 
40 //
41 // internal: gateId bitfield macros.
42 // See note in cgate.cc
43 //
44 #define GATEID_LBITS 20
45 #define GATEID_HBITS (8*sizeof(int)-GATEID_LBITS) // default 12
46 #define GATEID_HMASK ((~0U)<<GATEID_LBITS) // default 0xFFF00000
47 #define GATEID_LMASK (~GATEID_HMASK) // default 0x000FFFFF
48 
49 #define MAX_VECTORGATES ((1<<(GATEID_HBITS-1))-2) // default 2046
50 #define MAX_SCALARGATES ((1<<(GATEID_LBITS-1))-2) // default ~500000
51 #define MAX_VECTORGATESIZE ((1<<(GATEID_LBITS-1))-1) // default ~500000
52 
63 class SIM_API cGate : public cObject, noncopyable
64 {
65  friend class cModule;
66  friend class cModuleGates;
67  friend class cPlaceholderModule;
68 
69  public:
73  enum Type {
74  NONE = 0,
75  INPUT = 'I',
76  OUTPUT = 'O',
77  INOUT = 'B'
78  };
79 
80  protected:
81  // internal
82  struct SIM_API Name
83  {
84  opp_string name; // "foo"
85  opp_string namei; // "foo$i"
86  opp_string nameo; // "foo$o"
87  Type type;
88  Name(const char *name, Type type);
89  bool operator<(const Name& other) const;
90  };
91 
92  public:
93  // Internal data structure, only public for technical reasons (GateIterator).
94  // One instance per module and per gate vector/gate pair/gate.
95  // Note: gate name and type are factored out to a global pool.
96  // Note2: to reduce sizeof(Desc), "size" might be stored in input.gatev[0],
97  // although it might not be worthwhile the extra complication and CPU cycles.
98  //
99  struct Desc
100  {
101  cModule *owner;
102  Name *name; // pooled (points into cModule::namePool)
103  int vectorSize; // gate vector size, or -1 if scalar gate; actually allocated size is capacityFor(size)
104  union Gates { cGate *gate; cGate **gatev; };
105  Gates input;
106  Gates output;
107 
108  Desc() {owner=nullptr; vectorSize=-1; name=nullptr; input.gate=output.gate=nullptr;}
109  bool inUse() const {return name!=nullptr;}
110  Type getType() const {return name->type;}
111  bool isVector() const {return vectorSize>=0;}
112  const char *nameFor(Type t) const {return (t==INOUT||name->type!=INOUT) ? name->name.c_str() : t==INPUT ? name->namei.c_str() : name->nameo.c_str();}
113  int indexOf(const cGate *g) const {return (g->pos>>2)==-1 ? 0 : g->pos>>2;}
114  bool deliverOnReceptionStart(const cGate *g) const {return g->pos&2;}
115  Type getTypeOf(const cGate *g) const {return (g->pos&1)==0 ? INPUT : OUTPUT;}
116  bool isInput(const cGate *g) const {return (g->pos&1)==0;}
117  bool isOutput(const cGate *g) const {return (g->pos&1)==1;}
118  int gateSize() const {return vectorSize>=0 ? vectorSize : 1;}
119  void setInputGate(cGate *g) {ASSERT(getType()!=OUTPUT && !isVector()); input.gate=g; g->desc=this; g->pos=(-(1<<2));}
120  void setOutputGate(cGate *g) {ASSERT(getType()!=INPUT && !isVector()); output.gate=g; g->desc=this; g->pos=(-(1<<2))|1;}
121  void setInputGate(cGate *g, int index) {ASSERT(getType()!=OUTPUT && isVector()); input.gatev[index]=g; g->desc=this; g->pos=(index<<2);}
122  void setOutputGate(cGate *g, int index) {ASSERT(getType()!=INPUT && isVector()); output.gatev[index]=g; g->desc=this; g->pos=(index<<2)|1;}
123  static int capacityFor(int size) {return size<8 ? (size+1)&~1 : size<32 ? (size+3)&~3 : size<256 ? (size+15)&~15 : (size+63)&~63;}
124  };
125 
126  protected:
127  Desc *desc; // descriptor of the gate or gate vector, stored in cModule
128  int pos; // b0: input(0) or output(1); b1: deliverOnReceptionStart bit;
129  // rest (pos>>2): array index, or -1 if scalar gate
130 
131  int connectionId; // uniquely identifies the connection between *this and *nextgatep; -1 if unconnected
132  cChannel *channel; // channel object (if exists)
133  cGate *prevGate; // previous and next gate in the path
134  cGate *nextGate;
135 
136  static int lastConnectionId;
137 
138  protected:
139  // internal: constructor is protected because only cModule is allowed to create instances
140  explicit cGate();
141 
142  // also protected: only cModule is allowed to delete gates
143  virtual ~cGate();
144 
145  // internal
146  static void clearFullnamePool();
147 
148  // internal
149  void installChannel(cChannel *chan);
150 
151  // internal
152  void checkChannels() const;
153 
154 #ifdef SIMFRONTEND_SUPPORT
155  // internal
156  virtual bool hasChangedSince(int64_t lastRefreshSerial);
157 #endif
158 
159  public:
165  virtual const char *getName() const override;
166 
172  virtual const char *getFullName() const override;
173 
178  virtual void forEachChild(cVisitor *v) override;
179 
184  virtual std::string str() const override;
185 
189  virtual cObject *getOwner() const override; // note: cannot return cModule* (covariant return type) due to declaration order
191 
199  virtual bool deliver(cMessage *msg, simtime_t at);
200 
223  cChannel *connectTo(cGate *gate, cChannel *channel=nullptr, bool leaveUninitialized=false);
224 
232  void disconnect();
233 
240  cChannel *reconnectWith(cChannel *channel, bool leaveUninitialized=false);
242 
248  const char *getBaseName() const;
249 
253  const char *getNameSuffix() const;
254 
259  cProperties *getProperties() const;
260 
266  Type getType() const {return desc->getTypeOf(this);}
267 
271  static const char *getTypeName(Type t);
272 
276  cModule *getOwnerModule() const;
277 
291  int getId() const;
292 
296  bool isVector() const {return desc->isVector();}
297 
302  int getBaseId() const;
303 
308  int getIndex() const {return desc->indexOf(this);}
309 
316  int getVectorSize() const {return desc->gateSize();}
317 
321  int size() const {return getVectorSize();}
322 
328  cChannel *getChannel() const {return channel;}
329 
340  void setDeliverOnReceptionStart(bool d);
341 
349  bool getDeliverOnReceptionStart() const {return pos&2;}
351 
372  cChannel *getTransmissionChannel() const;
373 
378  cChannel *findTransmissionChannel() const;
379 
388  cChannel *getIncomingTransmissionChannel() const;
389 
395  cChannel *findIncomingTransmissionChannel() const;
397 
400 
406  cGate *getPreviousGate() const {return prevGate;}
407 
413  cGate *getNextGate() const {return nextGate;}
414 
422  int getConnectionId() const {return connectionId;}
423 
428  cGate *getPathStartGate() const;
429 
434  cGate *getPathEndGate() const;
435 
439  bool pathContains(cModule *module, int gateId=-1);
440 
448  bool isConnectedOutside() const;
449 
457  bool isConnectedInside() const;
458 
464  bool isConnected() const;
465 
470  bool isPathOK() const;
472 
481  cDisplayString& getDisplayString();
482 
486  void setDisplayString(const char *dispstr);
488 };
489 
490 } // namespace omnetpp
491 
492 #endif
int getConnectionId() const
Definition: cgate.h:422
Lightweight string class, used internally in some parts of OMNeT++.
Definition: opp_string.h:39
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
Represents a module gate.
Definition: cgate.h:63
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
int getIndex() const
Definition: cgate.h:308
bool isVector() const
Definition: cgate.h:296
cChannel * getChannel() const
Definition: cgate.h:328
This class represents modules in the simulation.
Definition: cmodule.h:47
cGate * getPreviousGate() const
Definition: cgate.h:406
bool getDeliverOnReceptionStart() const
Definition: cgate.h:349
Type getType() const
Definition: cgate.h:266
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:311
cGate * getNextGate() const
Definition: cgate.h:413
Definition: cabstracthistogram.h:21
A collection of properties (cProperty).
Definition: cproperties.h:34
Represents a display string.
Definition: cdisplaystring.h:58
Base class for channels.
Definition: cchannel.h:34
int size() const
Definition: cgate.h:321
Type
Definition: cgate.h:73
int getVectorSize() const
Definition: cgate.h:316