Download this file

ModuleAccess.h    119 lines (101 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Copyright (C) 2004 Andras Varga, 2014 Vladimir Vesely
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//
#ifndef MODULEACCESS_H_
#define MODULEACCESS_H_
#include <omnetpp.h>
//TODO Add methods to limit find scope for a single DIF, single DAF and single ComputerSystem
/**
* Returns true if the given module is a network node, i.e. a module
* with the @node property set.
*/
bool isNode(cModule *mod);
/**
* Find a module with given name, and "closest" to module "from".
*
* Operation: gradually rises in the module hierarchy, and searches
* recursively among all submodules at every level.
*/
cModule *findModuleWherever(const char *name, cModule *from);
/**
* Find a module with given name, and "closest" to module "from".
*
* Operation: gradually rises in the module hierarchy up to the @node
* module, and searches recursively among all submodules at every level.
*/
cModule *findModuleWhereverInNode(const char *name, cModule *from);
/**
* Find a module with given name, and "closest" to module "from".
*
* Operation: gradually rises in the module hierarchy, and looks for a submodule
* of the given name.
*/
cModule *findModuleSomewhereUp(const char *name, cModule *from);
/**
* Checks if the module is node, i.e. it has a @node attribute.
*/
bool isNode(cModule *mod);
/**
* Find the node containing the given module.
* Returns NULL, if no containing node.
*/
cModule *findContainingNode(cModule *from);
/**
* Find the ancestor module under the node containing the given module.
* Returns NULL, if no such node found.
*/
cModule *findModuleUnderContainingNode(cModule *from);
/**
* Finds and returns the pointer to a module of type T and name N.
* Uses findModuleWhereverInNode(). See usage e.g. at RoutingTableAccess.
*/
template<typename T>
class ModuleAccess
{
// Note: MSVC 6.0 doesn't like const char *N as template parameter,
// so we have to pass it via the ctor...
private:
const char *name;
T *p;
public:
ModuleAccess(const char *n) {name = n; p = NULL;}
virtual ~ModuleAccess() {}
virtual T *get()
{
if (!p)
p = get(simulation.getContextModule());
return p;
}
virtual T *get(cModule *from)
{
T *m = getIfExists(from);
if (!m) opp_error("Module (%s)%s not found", opp_typename(typeid(T)), name);
return m;
}
virtual T *getIfExists()
{
if (!p)
p = getIfExists(simulation.getContextModule());
return p;
}
virtual T *getIfExists(cModule *from)
{
if (!from) opp_error("Invalid argument: module must not be NULL");
cModule *m = findModuleWhereverInNode(name, from);
return dynamic_cast<T*>(m);
}
};
#endif