cgate.h

00001 //==========================================================================
00002 //   CGATE.H  -  header for
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cGate       : module gate
00009 //
00010 //==========================================================================
00011 
00012 /*--------------------------------------------------------------*
00013   Copyright (C) 1992-2008 Andras Varga
00014   Copyright (C) 2006-2008 OpenSim Ltd.
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
00018 *--------------------------------------------------------------*/
00019 
00020 #ifndef __CGATE_H
00021 #define __CGATE_H
00022 
00023 #include <set>
00024 #include <map>
00025 #include "cobject.h"
00026 #include "cstringpool.h"
00027 #include "opp_string.h"
00028 #include "simtime_t.h"
00029 
00030 NAMESPACE_BEGIN
00031 
00032 class cGate;
00033 class cModule;
00034 class cMessage;
00035 class cChannelType;
00036 class cChannel;
00037 class cProperties;
00038 class cDisplayString;
00039 class cIdealChannel;
00040 class cDatarateChannel;
00041 
00042 
00043 //
00044 // internal: gateId bitfield macros.
00045 // See note in cgate.cc
00046 //
00047 #define GATEID_LBITS  20
00048 #define GATEID_HBITS  (8*sizeof(int)-GATEID_LBITS)   // usually 12
00049 #define GATEID_HMASK  ((~0)<<GATEID_LBITS)           // usually 0xFFF00000
00050 #define GATEID_LMASK  (~GATEID_HMASK)                // usually 0x000FFFFF
00051 
00052 #define MAX_VECTORGATES  ((1<<(GATEID_HBITS-1))-2)   // usually 2046
00053 #define MAX_SCALARGATES  ((1<<(GATEID_LBITS-1))-2)   // usually ~500000
00054 #define MAX_VECTORGATESIZE ((1<<(GATEID_LBITS-1))-1) // usually ~500000
00055 
00064 class SIM_API cGate : public cObject, noncopyable
00065 {
00066     friend class cModule;
00067     friend class cModuleGates;
00068     friend class cPlaceholderModule;
00069 
00070   public:
00074     enum Type {
00075         NONE = 0,
00076         INPUT = 'I',
00077         OUTPUT = 'O',
00078         INOUT = 'B'
00079     };
00080 
00081   protected:
00082     // internal
00083     struct SIM_API Name
00084     {
00085         opp_string name;  // "foo"
00086         opp_string namei; // "foo$i"
00087         opp_string nameo; // "foo$o"
00088         Type type;
00089         Name(const char *name, Type type);
00090         bool operator<(const Name& other) const;
00091     };
00092 
00093   public:
00094     // Internal data structure, only public for technical reasons (GateIterator).
00095     // One instance per module and per gate vector/gate pair/gate.
00096     // Note: gate name and type are factored out to a global pool.
00097     // Note2: to reduce sizeof(Desc), "size" might be stored in input.gatev[0],
00098     // although it might not be worthwhile the extra complication and CPU cycles.
00099     //
00100     struct Desc
00101     {
00102         cModule *ownerp;
00103         Name *namep;  // pooled (points into cModule::namePool)
00104         int size; // gate vector size, or -1 if scalar gate; actually allocated size is capacityFor(size)
00105         union Gates { cGate *gate; cGate **gatev; };
00106         Gates input;
00107         Gates output;
00108 
00109         Desc() {ownerp=NULL; size=-1; namep=NULL; input.gate=output.gate=NULL;}
00110         bool inUse() const {return namep!=NULL;}
00111         Type getType() const {return namep->type;}
00112         bool isVector() const {return size>=0;}
00113         const char *nameFor(Type t) const {return (t==INOUT||namep->type!=INOUT) ? namep->name.c_str() : t==INPUT ? namep->namei.c_str() : namep->nameo.c_str();}
00114         int indexOf(const cGate *g) const {return (g->pos>>2)==-1 ? 0 : g->pos>>2;}
00115         bool deliverOnReceptionStart(const cGate *g) const {return g->pos&2;}
00116         Type getTypeOf(const cGate *g) const {return (g->pos&1)==0 ? INPUT : OUTPUT;}
00117         bool isInput(const cGate *g) const {return (g->pos&1)==0;}
00118         bool isOutput(const cGate *g) const {return (g->pos&1)==1;}
00119         int gateSize() const {return size>=0 ? size : 1;}
00120         void setInputGate(cGate *g) {ASSERT(getType()!=OUTPUT && !isVector()); input.gate=g; g->desc=this; g->pos=(-1<<2);}
00121         void setOutputGate(cGate *g) {ASSERT(getType()!=INPUT && !isVector()); output.gate=g; g->desc=this; g->pos=(-1<<2)|1;}
00122         void setInputGate(cGate *g, int index) {ASSERT(getType()!=OUTPUT && isVector()); input.gatev[index]=g; g->desc=this; g->pos=(index<<2);}
00123         void setOutputGate(cGate *g, int index) {ASSERT(getType()!=INPUT && isVector()); output.gatev[index]=g; g->desc=this; g->pos=(index<<2)|1;}
00124         static int capacityFor(int size) {return size<8 ? (size+1)&~1 : size<32 ? (size+3)&~3 : size<256 ? (size+15)&~15 : (size+63)&~63;}
00125     };
00126 
00127   protected:
00128     Desc *desc; // descriptor of gate/gate vector, stored in cModule
00129     int pos;    // b0: input(0) or output(1); b1: deliverOnReceptionStart bit;
00130                 // rest (pos>>2): array index, or -1 if scalar gate
00131 
00132     cChannel *channelp; // channel object (if exists)
00133     cGate *prevgatep;   // previous and next gate in the path
00134     cGate *nextgatep;
00135 
00136   protected:
00137     // internal: constructor is protected because only cModule is allowed to create instances
00138     explicit cGate();
00139 
00140     // also protected: only cModule is allowed to delete gates
00141     virtual ~cGate();
00142 
00143     // internal
00144     static void clearFullnamePool();
00145 
00146     // internal
00147     void installChannel(cChannel *chan);
00148 
00149     // internal
00150     void checkChannels() const;
00151 
00152   public:
00158     virtual const char *getName() const;
00159 
00165     virtual const char *getFullName() const;
00166 
00171     virtual void forEachChild(cVisitor *v);
00172 
00177     virtual std::string info() const;
00178 
00182     virtual cObject *getOwner() const;
00184 
00192     virtual bool deliver(cMessage *msg, simtime_t at);
00193 
00216     cChannel *connectTo(cGate *gate, cChannel *channel=NULL, bool leaveUninitialized=false);
00217 
00225     void disconnect();
00226 
00233     cChannel *reconnectWith(cChannel *channel, bool leaveUninitialized=false);
00234 
00238     _OPPDEPRECATED void setChannel(cChannel *channel) {reconnectWith(channel);}
00240 
00246     const char *getBaseName() const;
00247 
00251     const char *getNameSuffix() const;
00252 
00257     cProperties *getProperties() const;
00258 
00264     Type getType() const  {return desc->getTypeOf(this);}
00265 
00269     static const char *getTypeName(Type t);
00270 
00274     cModule *getOwnerModule() const;
00275 
00289     int getId() const;
00290 
00294     bool isVector() const  {return desc->isVector();}
00295 
00300     int getBaseId() const;
00301 
00306     int getIndex() const  {return desc->indexOf(this);}
00307 
00314     int getVectorSize() const  {return desc->gateSize();}
00315 
00319     int size() const  {return getVectorSize();}
00320 
00326     cChannel *getChannel() const  {return channelp;}
00327 
00338     void setDeliverOnReceptionStart(bool d);
00339 
00347     bool getDeliverOnReceptionStart() const  {return pos&2;}
00349 
00370     cChannel *getTransmissionChannel() const;
00371 
00376     cChannel *findTransmissionChannel() const;
00377 
00386     cChannel *getIncomingTransmissionChannel() const;
00387 
00393     cChannel *findIncomingTransmissionChannel() const;
00395 
00398 
00404     cGate *getPreviousGate() const {return prevgatep;}
00405 
00411     cGate *getNextGate() const   {return nextgatep;}
00412 
00417     cGate *getPathStartGate() const;
00418 
00423     cGate *getPathEndGate() const;
00424 
00428     bool pathContains(cModule *module, int gateId=-1);
00429 
00437     bool isConnectedOutside() const;
00438 
00446     bool isConnectedInside() const;
00447 
00453     bool isConnected() const;
00454 
00459     bool isPathOK() const;
00461 
00470     cDisplayString& getDisplayString();
00471 
00475     void setDisplayString(const char *dispstr);
00477 };
00478 
00479 NAMESPACE_END
00480 
00481 #endif
00482 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3