cmodule.h

00001 //==========================================================================
00002 //   CMODULE.H  -  header for
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //==========================================================================
00007 
00008 /*--------------------------------------------------------------*
00009   Copyright (C) 1992-2008 Andras Varga
00010   Copyright (C) 2006-2008 OpenSim Ltd.
00011 
00012   This file is distributed WITHOUT ANY WARRANTY. See the file
00013   `license' for details on this and other legal matters.
00014 *--------------------------------------------------------------*/
00015 
00016 #ifndef __CMODULE_H
00017 #define __CMODULE_H
00018 
00019 #include <vector>
00020 #include "ccomponent.h"
00021 #include "globals.h"
00022 #include "cgate.h"
00023 #include "csimulation.h"
00024 
00025 NAMESPACE_BEGIN
00026 
00027 
00028 class  cMessage;
00029 class  cGate;
00030 class  cSimulation;
00031 class  cModuleType;
00032 class  cCanvas;
00033 
00048 class SIM_API cModule : public cComponent //implies noncopyable
00049 {
00050     friend class cGate;
00051     friend class cSimulation;
00052     friend class cModuleType;
00053     friend class cChannelType;
00054 
00055   public:
00068     class SIM_API GateIterator
00069     {
00070       private:
00071         const cModule *module;
00072         int descIndex;
00073         bool isOutput;
00074         int index;
00075 
00076       private:
00077         void advance();
00078         cGate *current() const;
00079 
00080       public:
00084         GateIterator(const cModule *m)  {init(m);}
00085 
00089         void init(const cModule *m);
00090 
00095         cGate *operator()() const {cGate *result=current(); ASSERT(result||end()); return result;}
00096 
00100         bool end() const;
00101 
00107         cGate *operator++(int);
00108 
00113         cGate *operator+=(int k);
00114     };
00115 
00128     class SIM_API SubmoduleIterator
00129     {
00130       private:
00131         cModule *p;
00132 
00133       public:
00137         SubmoduleIterator(const cModule *m)  {init(m);}
00138 
00142         void init(const cModule *m)  {p = m ? const_cast<cModule *>(m->firstsubmodp) : NULL;}
00143 
00149         cModule *operator()() const {return p;}
00150 
00154         bool end() const  {return (bool)(p==NULL);}
00155 
00161         cModule *operator++(int)  {if (!p) return NULL; cModule *t=p; p=p->nextp; return t;}
00162     };
00163 
00169     class SIM_API ChannelIterator
00170     {
00171       private:
00172         std::vector<cChannel *> channels;
00173         int k;
00174 
00175       public:
00179         ChannelIterator(const cModule *parentmodule) {init(parentmodule);}
00180 
00184         void init(const cModule *parentmodule);
00185 
00190         cChannel *operator()() const {return k < (int)channels.size() ? channels[k] : NULL;}
00191 
00195         bool end() const {return k == (int)channels.size();}
00196 
00203         cChannel *operator++(int) {return end() ? NULL : channels[k++];}
00204     };
00205 
00206   private:
00207     static std::string lastmodulefullpath; // cached result of last getFullPath() call
00208     static const cModule *lastmodulefullpathmod; // module of lastmodulefullpath
00209 
00210   private:
00211     enum {
00212         FL_BUILDINSIDE_CALLED = 128, // whether buildInside() has been called
00213         FL_RECORD_EVENTS = 256, // enables recording events in this module
00214     };
00215 
00216   protected:
00217     mutable char *fullpath; // cached fullPath string (caching is optional, so it may be NULL)
00218     mutable char *fullname; // buffer to store full name of object
00219     int mod_id;             // id (subscript into cSimulation)
00220     static bool cachefullpath; // whether to cache the fullPath string or not
00221 
00222     // Note: parent module is stored in ownerp -- a module is always owned by its parent
00223     // module. If ownerp cannot be cast to a cModule, the module has no parent module
00224     // (e.g. the system module which is owned by the global object 'simulation').
00225     cModule *prevp, *nextp; // pointers to sibling submodules
00226     cModule *firstsubmodp;  // pointer to first submodule
00227     cModule *lastsubmodp;   // pointer to last submodule (needed for efficient append operation)
00228 
00229     typedef std::set<cGate::Name> NamePool;
00230     static NamePool namePool;
00231     int descvSize;    // size of the descv array
00232     cGate::Desc *descv; // array with one element per gate or gate vector
00233 
00234     int idx;      // index if module vector, 0 otherwise
00235     int vectsize; // vector size, -1 if not a vector
00236 
00237     cCanvas *canvas;  // NULL when unused
00238 
00239   public:
00240     // internal: currently used by init
00241     void setRecordEvents(bool e)  {setFlag(FL_RECORD_EVENTS,e);}
00242     bool isRecordEvents() const  {return flags&FL_RECORD_EVENTS;}
00243 
00244   protected:
00245     // internal: called from destructor, recursively unsubscribes all listeners
00246     void releaseListeners();
00247 
00248     // internal: has initialize() been called?
00249     bool buildInsideCalled() const {return flags&FL_BUILDINSIDE_CALLED;}
00250 
00251     // internal: called from callInitialize(). Does one stage for this submodule
00252     // tree, and returns true if there are more stages to do
00253     virtual bool initializeModules(int stage);
00254 
00255     // internal: called from callInitialize(). Does one stage for channels in this
00256     // submodule tree, and returns true if there are more stages to do
00257     virtual bool initializeChannels(int stage);
00258 
00259     // internal: called when a message arrives at a gate which is no further
00260     // connected (that is, getNextGate() is NULL)
00261     virtual void arrived(cMessage *msg, cGate *ongate, simtime_t t);
00262 
00263     // internal: sets the module ID. Called as part of the module creation process.
00264     virtual void setId(int n);
00265 
00266     // internal: sets module name and its index within vector (if module is
00267     // part of a module vector). Called as part of the module creation process.
00268     virtual void setNameAndIndex(const char *s, int i, int n);
00269 
00270     // internal: called from setName() and setIndex()
00271     void updateFullName();
00272 
00273     // internal: called from setName() and setIndex()
00274     void updateFullPath();
00275 
00276     // internal: inserts a submodule. Called as part of the module creation process.
00277     void insertSubmodule(cModule *mod);
00278 
00279     // internal: removes a submodule
00280     void removeSubmodule(cModule *mod);
00281 
00282     // internal: "virtual ctor" for cGate, because in cPlaceholderModule we need
00283     // different gate objects; type should be INPUT or OUTPUT, but not INOUT
00284     virtual cGate *createGateObject(cGate::Type type);
00285 
00286     // internal: called from deleteGate()
00287     void disposeGateDesc(cGate::Desc *desc, bool checkConnected);
00288 
00289     // internal: called from deleteGate()
00290     void disposeGateObject(cGate *gate, bool checkConnected);
00291 
00292     // internal: add a new gatedesc by expanding gatedescv[]
00293     cGate::Desc *addGateDesc(const char *name, cGate::Type type, bool isVector);
00294 
00295     // internal: finds a gate descriptor with the given name in gatedescv[];
00296     // ignores (but returns) potential "$i"/"$o" suffix in gatename
00297     int findGateDesc(const char *gatename, char& suffix) const;
00298 
00299     // internal: like findGateDesc(), but throws an error if the gate does not exist
00300     cGate::Desc *gateDesc(const char *gatename, char& suffix) const;
00301 
00302     // internal: helper for setGateSize()
00303     void adjustGateDesc(cGate *g, cGate::Desc *newvec);
00304 
00305     // internal: called as part of the destructor
00306     void clearGates();
00307 
00308     // internal: builds submodules and internal connections for this module
00309     virtual void doBuildInside();
00310 
00311   public:
00312     // internal: may only be called between simulations, when no modules exist
00313     static void clearNamePools();
00314 
00315     // internal utility function. Takes O(n) time as it iterates on the gates
00316     int gateCount() const;
00317 
00318     // internal utility function. Takes O(n) time as it iterates on the gates
00319     cGate *gateByOrdinal(int k) const;
00320 
00321     // internal: return the canvas if exists, or NULL if not (i.e. no create-on-demand)
00322     cCanvas *getCanvasIfExists() {return canvas;}
00323 
00324   public:
00334     cModule();
00335 
00339     virtual ~cModule();
00341 
00344 
00349     virtual void forEachChild(cVisitor *v);
00350 
00354     virtual void setName(const char *s);
00355 
00361     virtual const char *getFullName() const;
00362 
00368     virtual std::string getFullPath() const;
00369 
00373     std::string info() const;
00375 
00378 
00389     virtual cGate *addGate(const char *gatename, cGate::Type type, bool isvector=false);
00390 
00397     virtual void setGateSize(const char *gatename, int size);
00398 
00410     virtual cGate *getOrCreateFirstUnconnectedGate(const char *gatename, char suffix,
00411                                                    bool inside, bool expand);
00412 
00419     virtual void getOrCreateFirstUnconnectedGatePair(const char *gatename,
00420                                                      bool inside, bool expand,
00421                                                      cGate *&gatein, cGate *&gateout);
00422 
00440     virtual void finalizeParameters();
00441 
00451     virtual int buildInside();
00453 
00456 
00461     virtual bool isSimple() const;
00462 
00466     virtual bool isModule() const  {return true;}
00467 
00472     virtual bool isPlaceholder() const  {return false;}
00473 
00478     virtual cModule *getParentModule() const;
00479 
00483     cModuleType *getModuleType() const  {return (cModuleType *)getComponentType();}
00484 
00489     virtual cProperties *getProperties() const;
00490 
00500     int getId() const  {return mod_id;}
00501 
00505     bool isVector() const  {return vectsize>=0;}
00506 
00510     int getIndex() const  {return idx;}
00511 
00516     int getVectorSize() const  {return vectsize<0 ? 1 : vectsize;}
00517 
00521     int size() const  {return getVectorSize();}
00523 
00530     bool hasSubmodules() const {return firstsubmodp!=NULL;}
00531 
00537     int findSubmodule(const char *submodname, int idx=-1);
00538 
00544     cModule *getSubmodule(const char *submodname, int idx=-1);
00545 
00553     _OPPDEPRECATED cModule *getModuleByRelativePath(const char *path);
00554 
00573     cModule *getModuleByPath(const char *path);
00575 
00578 
00585     virtual cGate *gate(const char *gatename, int index=-1);
00586 
00593     const cGate *gate(const char *gatename, int index=-1) const {
00594         return const_cast<cModule *>(this)->gate(gatename, index);
00595     }
00596 
00604     virtual cGate *gateHalf(const char *gatename, cGate::Type type, int index=-1);
00605 
00613     const cGate *gateHalf(const char *gatename, cGate::Type type, int index=-1) const {
00614         return const_cast<cModule *>(this)->gateHalf(gatename, type, index);
00615     }
00616 
00624     virtual bool hasGate(const char *gatename, int index=-1) const;
00625 
00633     virtual int findGate(const char *gatename, int index=-1) const;
00634 
00642     virtual cGate *gate(int id);
00643 
00651     const cGate *gate(int id) const {return const_cast<cModule *>(this)->gate(id);}
00652 
00658     virtual void deleteGate(const char *gatename);
00659 
00670     virtual std::vector<const char *> getGateNames() const;
00671 
00677     virtual cGate::Type gateType(const char *gatename) const;
00678 
00684     virtual bool isGateVector(const char *gatename) const;
00685 
00696     virtual int gateSize(const char *gatename) const;
00697 
00707     virtual int gateBaseId(const char *gatename) const;
00708 
00715     bool checkInternalConnections() const;
00717 
00724     cPar& getAncestorPar(const char *parname);
00725 
00729     virtual cCanvas *getCanvas();
00731 
00741     virtual void callInitialize();
00742 
00747     virtual bool callInitialize(int stage);
00748 
00752     virtual void callFinish();
00754 
00757 
00762     virtual void scheduleStart(simtime_t t);
00763 
00769     virtual void deleteModule();
00770 
00792     virtual void changeParentTo(cModule *mod);
00794 };
00795 
00796 
00800 class SIM_API _OPPDEPRECATED cSubModIterator : public cModule::SubmoduleIterator
00801 {
00802   public:
00803     cSubModIterator(const cModule& m) : cModule::SubmoduleIterator(&m) {}
00804     void init(const cModule& m) {cModule::SubmoduleIterator::init(&m);}
00805 };
00806 
00807 NAMESPACE_END
00808 
00809 
00810 #endif
00811 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3