OMNeT++ Simulation Library  6.0.3
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 "opp_string.h"
23 #include "simtime_t.h"
24 #include "cexception.h"
25 
26 namespace omnetpp {
27 
28 class cGate;
29 class cModule;
30 class cMessage;
31 class cChannelType;
32 class cChannel;
33 class cProperties;
34 class cDisplayString;
35 class cIdealChannel;
36 class cDatarateChannel;
37 struct SendOptions;
38 
39 //
40 // internal: gateId bitfield macros.
41 // See note in cgate.cc
42 //
43 #define GATEID_LBITS 20
44 #define GATEID_HBITS (8*sizeof(int)-GATEID_LBITS) // default 12
45 #define GATEID_HMASK ((~0U)<<GATEID_LBITS) // default 0xFFF00000
46 #define GATEID_LMASK (~GATEID_HMASK) // default 0x000FFFFF
47 
48 #define MAX_VECTORGATES ((1<<(GATEID_HBITS-1))-2) // default 2046
49 #define MAX_SCALARGATES ((1<<(GATEID_LBITS-1))-2) // default ~500000
50 #define MAX_VECTORGATESIZE ((1<<(GATEID_LBITS-1))-1) // default ~500000
51 
62 class SIM_API cGate : public cObject, noncopyable
63 {
64  friend class cModule;
65  friend class cModuleGates;
66  friend class cPlaceholderModule;
67 
68  public:
72  enum Type {
73  NONE = 0,
74  INPUT = 'I',
75  OUTPUT = 'O',
76  INOUT = 'B'
77  };
78 
79  protected:
80  // internal
81  struct SIM_API Name
82  {
83  opp_string name; // "foo"
84  opp_string namei; // "foo$i"
85  opp_string nameo; // "foo$o"
86  Type type;
87  Name(const char *name, Type type);
88  bool operator<(const Name& other) const;
89  };
90 
91  public:
92  // Internal data structure, only public for technical reasons (GateIterator).
93  // One instance per module and per gate vector/gate pair/gate.
94  // Note: gate name and type are factored out to a global pool.
95  // Note2: to reduce sizeof(Desc), "size" might be stored in input.gatev[0],
96  // although it might not be worthwhile the extra complication and CPU cycles.
97  //
98  struct Desc
99  {
100  cModule *owner;
101  Name *name; // pooled (points into cModule::namePool)
102  int vectorSize; // gate vector size, or -1 if scalar gate; actually allocated size is capacityFor(size)
103  union Gates { cGate *gate; cGate **gatev; };
104  Gates input;
105  Gates output;
106 
107  Desc() {owner=nullptr; vectorSize=-1; name=nullptr; input.gate=output.gate=nullptr;}
108  bool inUse() const {return name!=nullptr;}
109  Type getType() const {return name->type;}
110  bool isVector() const {return vectorSize>=0;}
111  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();}
112  int indexOf(const cGate *g) const {ASSERT(isVector()); return g->pos >> 2;}
113  bool deliverOnReceptionStart(const cGate *g) const {return g->pos&2;}
114  Type getTypeOf(const cGate *g) const {return (g->pos&1)==0 ? INPUT : OUTPUT;}
115  bool isInput(const cGate *g) const {return (g->pos&1)==0;}
116  bool isOutput(const cGate *g) const {return (g->pos&1)==1;}
117  int gateSize() const {ASSERT(isVector()); return vectorSize;}
118  void setInputGate(cGate *g) {ASSERT(getType()!=OUTPUT && !isVector()); input.gate=g; g->desc=this; g->pos=(-(1<<2));}
119  void setOutputGate(cGate *g) {ASSERT(getType()!=INPUT && !isVector()); output.gate=g; g->desc=this; g->pos=(-(1<<2))|1;}
120  void setInputGate(cGate *g, int index) {ASSERT(getType()!=OUTPUT && isVector()); input.gatev[index]=g; g->desc=this; g->pos=(index<<2);}
121  void setOutputGate(cGate *g, int index) {ASSERT(getType()!=INPUT && isVector()); output.gatev[index]=g; g->desc=this; g->pos=(index<<2)|1;}
122  static int capacityFor(int size) {return size<8 ? (size+1)&~1 : size<32 ? (size+3)&~3 : size<256 ? (size+15)&~15 : (size+63)&~63;}
123  };
124 
125  protected:
126  Desc *desc = nullptr; // descriptor of the gate or gate vector, stored in cModule
127  int pos = 0; // b0: input(0) or output(1); b1: deliverOnReceptionStart bit;
128  // rest (pos>>2): array index, or -1 if scalar gate
129 
130  int connectionId = -1; // uniquely identifies the connection between *this and *nextgatep; -1 if unconnected
131  cChannel *channel = nullptr; // channel object (if exists)
132  cGate *prevGate = nullptr; // previous and next gate in the path
133  cGate *nextGate = nullptr;
134 
135  static int lastConnectionId;
136 
137  protected:
138  // internal: constructor is protected because only cModule is allowed to create instances
139  explicit cGate() {}
140 
141  // also protected: only cModule is allowed to delete gates
142  virtual ~cGate();
143 
144  // internal
145  static void clearFullnamePool();
146 
147  // internal
148  void installChannel(cChannel *chan);
149 
150  // internal
151  void checkChannels() const;
152 
153 #ifdef SIMFRONTEND_SUPPORT
154  // internal
155  virtual bool hasChangedSince(int64_t lastRefreshSerial);
156 #endif
157 
158  public:
164  virtual const char *getName() const override;
165 
171  virtual const char *getFullName() const override;
172 
177  virtual void forEachChild(cVisitor *v) override;
178 
183  virtual std::string str() const override;
184 
188  virtual cObject *getOwner() const override; // note: cannot return cModule* (covariant return type) due to declaration order
190 
198  virtual bool deliver(cMessage *msg, const SendOptions& options, simtime_t at);
199 
225  cChannel *connectTo(cGate *gate, cChannel *channel=nullptr, bool leaveUninitialized=false);
226 
236  void disconnect();
237 
244  cChannel *reconnectWith(cChannel *channel, bool leaveUninitialized=false);
246 
256  bool isGateHalf() const;
257 
263  cGate *getOtherHalf() const;
264 
268  const char *getBaseName() const;
269 
273  const char *getNameSuffix() const;
274 
279  cProperties *getProperties() const;
280 
286  Type getType() const {return desc->getTypeOf(this);}
287 
291  static const char *getTypeName(Type t);
292 
296  cModule *getOwnerModule() const;
297 
311  int getId() const;
312 
316  bool isVector() const {return desc->isVector();}
317 
322  int getBaseId() const;
323 
328  int getIndex() const;
329 
336  int getVectorSize() const;
337 
341  int size() const {return getVectorSize();}
342 
348  cChannel *getChannel() const {return channel;}
349 
358  void setDeliverImmediately(bool d);
359 
386  bool getDeliverImmediately() const {return pos&2;}
387 
391  [[deprecated("Renamed to setDeliverImmediately() -- please use the new name")]]
392  void setDeliverOnReceptionStart(bool d) {setDeliverImmediately(d);}
393 
397  [[deprecated("Renamed to getDeliverImmediately() -- please use the new name")]]
398  bool getDeliverOnReceptionStart() const {return getDeliverImmediately();}
400 
421  cChannel *getTransmissionChannel() const;
422 
427  cChannel *findTransmissionChannel() const;
428 
437  cChannel *getIncomingTransmissionChannel() const;
438 
444  cChannel *findIncomingTransmissionChannel() const;
446 
449 
455  cGate *getPreviousGate() const {return prevGate;}
456 
462  cGate *getNextGate() const {return nextGate;}
463 
471  int getConnectionId() const {return connectionId;}
472 
477  cGate *getPathStartGate() const;
478 
483  cGate *getPathEndGate() const;
484 
488  bool pathContains(cModule *module, int gateId=-1);
489 
497  bool isConnectedOutside() const;
498 
506  bool isConnectedInside() const;
507 
513  bool isConnected() const;
514 
519  bool isPathOK() const;
521 
530  cDisplayString& getDisplayString();
531 
535  void setDisplayString(const char *dispstr);
537 };
538 
539 } // namespace omnetpp
540 
541 #endif
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::noncopyable
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:415
omnetpp::cGate::getDeliverImmediately
bool getDeliverImmediately() const
Definition: cgate.h:386
omnetpp::cGate::getChannel
cChannel * getChannel() const
Definition: cgate.h:348
omnetpp::opp_string
Lightweight string class, used internally in some parts of OMNeT++.
Definition: opp_string.h:39
omnetpp::cGate::Type
Type
Definition: cgate.h:72
omnetpp::cChannel
Base class for channels.
Definition: cchannel.h:46
omnetpp::cGate
Represents a module gate.
Definition: cgate.h:62
omnetpp::cGate::getType
Type getType() const
Definition: cgate.h:286
omnetpp::cGate::getConnectionId
int getConnectionId() const
Definition: cgate.h:471
omnetpp::simtime_t
SimTime simtime_t
Represents simulation time.
Definition: simtime_t.h:40
omnetpp::cGate::setDeliverOnReceptionStart
void setDeliverOnReceptionStart(bool d)
Definition: cgate.h:392
omnetpp::cGate::getPreviousGate
cGate * getPreviousGate() const
Definition: cgate.h:455
omnetpp::cGate::getDeliverOnReceptionStart
bool getDeliverOnReceptionStart() const
Definition: cgate.h:398
omnetpp::cGate::getNextGate
cGate * getNextGate() const
Definition: cgate.h:462
omnetpp::cGate::size
int size() const
Definition: cgate.h:341
omnetpp::cGate::isVector
bool isVector() const
Definition: cgate.h:316
omnetpp::cDisplayString
Represents a display string.
Definition: cdisplaystring.h:58