cgate.h Source File¶

API: cgate.h Source File
API
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
26namespace omnetpp {
27
28class cGate;
29class cModule;
30class cMessage;
31class cChannelType;
32class cChannel;
33class cProperties;
34class cDisplayString;
35class cIdealChannel;
37struct 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
62class 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 // internal
154 void doDisconnect(bool shouldCallPreDelete);
155
156#ifdef SIMFRONTEND_SUPPORT
157 // internal
158 virtual bool hasChangedSince(int64_t lastRefreshSerial);
159#endif
160
161 public:
164
167 virtual const char *getName() const override;
168
174 virtual const char *getFullName() const override;
175
180 virtual void forEachChild(cVisitor *v) override;
181
186 virtual std::string str() const override;
187
191 virtual cObject *getOwner() const override; // note: cannot return cModule* (covariant return type) due to declaration order
193
201 virtual bool deliver(cMessage *msg, const SendOptions& options, simtime_t at);
202
205
228 cChannel *connectTo(cGate *gate, cChannel *channel=nullptr, bool leaveUninitialized=false);
229
240
247 cChannel *reconnectWith(cChannel *channel, bool leaveUninitialized=false);
249
252
259 bool isGateHalf() const;
260
266 cGate *getOtherHalf() const;
267
271 const char *getBaseName() const;
272
276 const char *getNameSuffix() const;
277
283
289 Type getType() const {return desc->getTypeOf(this);}
290
294 static const char *getTypeName(Type t);
295
299 cModule *getOwnerModule() const;
300
314 int getId() const;
315
319 bool isVector() const {return desc->isVector();}
320
325 int getBaseId() const;
326
331 int getIndex() const;
332
339 int getVectorSize() const;
340
344 int size() const {return getVectorSize();}
345
351 cChannel *getChannel() const {return channel;}
352
362
389 bool getDeliverImmediately() const {return pos&2;}
390
394 [[deprecated("Renamed to setDeliverImmediately() -- please use the new name")]]
396
400 [[deprecated("Renamed to getDeliverImmediately() -- please use the new name")]]
403
406
425
431
441
449
452
458 cGate *getPreviousGate() const {return prevGate;}
459
465 cGate *getNextGate() const {return nextGate;}
466
474 int getConnectionId() const {return connectionId;}
475
480 cGate *getPathStartGate() const;
481
486 cGate *getPathEndGate() const;
487
491 bool pathContains(cModule *module, int gateId=-1);
492
500 bool isConnectedOutside() const;
501
509 bool isConnectedInside() const;
510
516 bool isConnected() const;
517
522 bool isPathOK() const;
524
527
534
538 void setDisplayString(const char *dispstr);
540};
541
542} // namespace omnetpp
543
544#endif
Abstract base class for creating a channel of a given type.
Definition ccomponenttype.h:332
Base class for channels.
Definition cchannel.h:47
A transmission channel model that supports propagation delay, transmission duration computed from a d...
Definition cdataratechannel.h:70
Represents a display string.
Definition cdisplaystring.h:69
Represents a module gate.
Definition cgate.h:63
const char * getNameSuffix() const
cChannel * reconnectWith(cChannel *channel, bool leaveUninitialized=false)
void setDeliverOnReceptionStart(bool d)
Definition cgate.h:395
cGate * getNextGate() const
Definition cgate.h:465
int getIndex() const
Type
Definition cgate.h:72
cGate * getOtherHalf() const
cGate * getPreviousGate() const
Definition cgate.h:458
bool getDeliverImmediately() const
Definition cgate.h:389
cModule * getOwnerModule() const
cChannel * findTransmissionChannel() const
cGate * getPathStartGate() const
virtual cObject * getOwner() const override
bool isVector() const
Definition cgate.h:319
int getId() const
int getVectorSize() const
bool isPathOK() const
virtual const char * getFullName() const override
void setDisplayString(const char *dispstr)
bool isConnectedInside() const
int getConnectionId() const
Definition cgate.h:474
bool isGateHalf() const
cChannel * getChannel() const
Definition cgate.h:351
cChannel * getIncomingTransmissionChannel() const
void setDeliverImmediately(bool d)
bool pathContains(cModule *module, int gateId=-1)
cChannel * findIncomingTransmissionChannel() const
bool isConnected() const
cProperties * getProperties() const
const char * getBaseName() const
cDisplayString & getDisplayString()
Type getType() const
Definition cgate.h:289
bool isConnectedOutside() const
virtual const char * getName() const override
int getBaseId() const
cChannel * getTransmissionChannel() const
static const char * getTypeName(Type t)
virtual bool deliver(cMessage *msg, const SendOptions &options, simtime_t at)
virtual void forEachChild(cVisitor *v) override
virtual std::string str() const override
cGate * getPathEndGate() const
cChannel * connectTo(cGate *gate, cChannel *channel=nullptr, bool leaveUninitialized=false)
bool getDeliverOnReceptionStart() const
Definition cgate.h:401
int size() const
Definition cgate.h:344
Channel with zero propagation delay, zero transmission delay (infinite datarate), and always enabled.
Definition cchannel.h:327
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition cmessage.h:96
This class represents modules in the simulation.
Definition cmodule.h:49
cObject()
Definition cobject.h:124
A collection of properties (cProperty).
Definition cproperties.h:35
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition cvisitor.h:57
Lightweight string class, used internally in some parts of OMNeT++.
Definition opp_string.h:40
SimTime simtime_t
Represents simulation time.
Definition simtime_t.h:40
Options for the cSimpleModule::send() and cSimpleModule::sendDirect() calls.
Definition csimplemodule.h:82