OMNeT++ Simulation Library  5.6.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 
252  bool isGateHalf() const;
253 
259  cGate *getOtherHalf() const;
260 
264  const char *getBaseName() const;
265 
269  const char *getNameSuffix() const;
270 
275  cProperties *getProperties() const;
276 
282  Type getType() const {return desc->getTypeOf(this);}
283 
287  static const char *getTypeName(Type t);
288 
292  cModule *getOwnerModule() const;
293 
307  int getId() const;
308 
312  bool isVector() const {return desc->isVector();}
313 
318  int getBaseId() const;
319 
324  int getIndex() const {return desc->indexOf(this);}
325 
332  int getVectorSize() const {return desc->gateSize();}
333 
337  int size() const {return getVectorSize();}
338 
344  cChannel *getChannel() const {return channel;}
345 
356  void setDeliverOnReceptionStart(bool d);
357 
365  bool getDeliverOnReceptionStart() const {return pos&2;}
367 
388  cChannel *getTransmissionChannel() const;
389 
394  cChannel *findTransmissionChannel() const;
395 
404  cChannel *getIncomingTransmissionChannel() const;
405 
411  cChannel *findIncomingTransmissionChannel() const;
413 
416 
422  cGate *getPreviousGate() const {return prevGate;}
423 
429  cGate *getNextGate() const {return nextGate;}
430 
438  int getConnectionId() const {return connectionId;}
439 
444  cGate *getPathStartGate() const;
445 
450  cGate *getPathEndGate() const;
451 
455  bool pathContains(cModule *module, int gateId=-1);
456 
464  bool isConnectedOutside() const;
465 
473  bool isConnectedInside() const;
474 
480  bool isConnected() const;
481 
486  bool isPathOK() const;
488 
497  cDisplayString& getDisplayString();
498 
502  void setDisplayString(const char *dispstr);
504 };
505 
506 } // namespace omnetpp
507 
508 #endif
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
int getVectorSize() const
Definition: cgate.h:332
Type getType() const
Definition: cgate.h:282
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
cGate * getNextGate() const
Definition: cgate.h:429
int size() const
Definition: cgate.h:337
bool getDeliverOnReceptionStart() const
Definition: cgate.h:365
This class represents modules in the simulation.
Definition: cmodule.h:47
bool isVector() const
Definition: cgate.h:312
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
Definition: cabstracthistogram.h:21
int getIndex() const
Definition: cgate.h:324
A collection of properties (cProperty).
Definition: cproperties.h:34
cChannel * getChannel() const
Definition: cgate.h:344
Represents a display string.
Definition: cdisplaystring.h:58
cGate * getPreviousGate() const
Definition: cgate.h:422
Base class for channels.
Definition: cchannel.h:34
Type
Definition: cgate.h:73
int getConnectionId() const
Definition: cgate.h:438