00001 // 00002 // Copyright (C) 2004 Andras Varga 00003 // 00004 // This program is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public License 00006 // as published by the Free Software Foundation; either version 2 00007 // of the License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program; if not, see <http://www.gnu.org/licenses/>. 00016 // 00017 00018 #ifndef __INET_MODULEACCESS_H 00019 #define __INET_MODULEACCESS_H 00020 00021 #include <omnetpp.h> 00022 #include "INETDefs.h" 00023 00024 00031 INET_API cModule *findModuleWherever(const char *name, cModule *from); 00032 00039 INET_API cModule *findModuleWhereverInNode(const char *name, cModule *from); 00040 00047 INET_API cModule *findModuleSomewhereUp(const char *name, cModule *from); 00048 00053 template<typename T> 00054 class ModuleAccess 00055 { 00056 // Note: MSVC 6.0 doesn't like const char *N as template parameter, 00057 // so we have to pass it via the ctor... 00058 private: 00059 const char *name; 00060 T *p; 00061 public: 00062 ModuleAccess(const char *n) {name = n; p=NULL;} 00063 virtual ~ModuleAccess() {} 00064 00065 virtual T *get() 00066 { 00067 if (!p) 00068 { 00069 cModule *m = findModuleWhereverInNode(name, simulation.getContextModule()); 00070 if (!m) opp_error("Module (%s)%s not found",opp_typename(typeid(T)),name); 00071 p = check_and_cast<T*>(m); 00072 } 00073 return p; 00074 } 00075 00076 virtual T *getIfExists() 00077 { 00078 if (!p) 00079 { 00080 cModule *m = findModuleWhereverInNode(name, simulation.getContextModule()); 00081 p = dynamic_cast<T*>(m); 00082 } 00083 return p; 00084 } 00085 }; 00086 00087 #endif 00088