MiXiM  2.3
FindModule.h
00001 #ifndef FIND_MODULE_H
00002 #define FIND_MODULE_H
00003 
00004 #include <omnetpp.h>
00005 
00012 template<typename T = cModule * const >
00013 class FindModule
00014 {
00015     public:
00022         static T findSubModule(const cModule * const top)
00023         {
00024             for (cModule::SubmoduleIterator i(top); !i.end(); i++)
00025             {
00026                 cModule * const sub = i();
00027                 // this allows also a return type of read only pointer: const cModule *const
00028                 T dCastRet = dynamic_cast<T>(sub);
00029                 if (dCastRet != NULL)
00030                     return dCastRet;
00031                 // this allows also a return type of read only pointer: const cModule *const
00032                 T recFnd = findSubModule(sub);
00033                 if (recFnd != NULL)
00034                     return recFnd;
00035             }
00036             return NULL;
00037         }
00038 
00045         static T findGlobalModule()
00046         {
00047             return findSubModule(simulation.getSystemModule());
00048         }
00049 
00056         static cModule* findHost(cModule * const m)
00057         {
00058             cModule* parent = m != NULL ? m->getParentModule() : NULL;
00059             cModule* node = m;
00060 
00061             // all nodes should be a sub module of the simulation which has no parent module!!!
00062             while (parent != NULL && parent->getParentModule() != NULL)
00063             {
00064                 node = parent;
00065                 parent = node->getParentModule();
00066             }
00067             return node;
00068         }
00069         static cModule* findNetwork(cModule * const m)
00070         {
00071             cModule* node = findHost(m);
00072 
00073             return node ? node->getParentModule() : node;
00074         }
00075         // the constness version
00076         static const cModule* findHost(const cModule * const m)
00077         {
00078             const cModule* parent = m != NULL ? m->getParentModule() : NULL;
00079             const cModule *node = m;
00080 
00081             // all nodes should be a sub module of the simulation which has no parent module!!!
00082             while (parent != NULL && parent->getParentModule() != NULL)
00083             {
00084                 node = parent;
00085                 parent = node->getParentModule();
00086             }
00087             return node;
00088         }
00089         static const cModule* findNetwork(const cModule * const m)
00090         {
00091             const cModule* node = findHost(m);
00092 
00093             return node ? node->getParentModule() : node;
00094         }
00095 };
00096 
00101 template<typename T = cModule>
00102 class AccessModuleWrap
00103 {
00104     public:
00105         typedef T wrapType;
00106     private:
00107         T* pModule;
00110         AccessModuleWrap(const AccessModuleWrap<T>&);
00113         AccessModuleWrap<T>& operator=(const AccessModuleWrap<T>&);
00114 
00115     public:
00116         AccessModuleWrap() :
00117                 pModule(NULL)
00118         {
00119         }
00120 
00121         virtual ~AccessModuleWrap()
00122         {
00123         }
00124         ;
00125 
00126         T* get(cModule * const from = NULL)
00127         {
00128             if (!pModule)
00129             {
00130                 pModule = FindModule<T*>::findSubModule(
00131                         FindModule<>::findHost(from != NULL ? from : simulation.getContextModule()));
00132             }
00133             return pModule;
00134         }
00135 };
00136 
00137 #endif