FindModule.h

00001 #ifndef FIND_MODULE_H
00002 #define FIND_MODULE_H
00003 
00004 #include <omnetpp.h>
00005 
00012 template<typename T = cModule*>
00013 class FindModule
00014 {
00015   public:
00022     static T findSubModule(cModule *top)
00023     {
00024       T ret;
00025       for (cModule::SubmoduleIterator i(top); !i.end(); i++)
00026       {
00027         cModule *sub = i();
00028         ret = dynamic_cast<T>(sub);
00029         if (ret!=NULL)
00030           return ret;
00031         ret = findSubModule(sub);
00032         if (ret!=NULL)
00033           return ret;
00034       }
00035       return NULL;
00036     }
00037 
00044     static T findGlobalModule() {return findSubModule(simulation.getSystemModule());}
00045 
00052     static cModule* findHost(cModule *m) {
00053        cModule *parent = m->getParentModule();
00054       cModule *node = m;
00055 
00056       // all nodes should be a sub module of the simulation which has no parent module!!!
00057       while( parent->getParentModule() != NULL ){
00058       node = parent;
00059       parent = node->getParentModule();
00060       }
00061 
00062       return node;
00063     }
00064 };
00065 
00066 #endif