-
Notifications
You must be signed in to change notification settings - Fork 480
/
ModuleAccess.h
129 lines (112 loc) · 4.15 KB
/
ModuleAccess.h
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
118
119
120
121
122
123
124
125
126
127
128
//
// Copyright (C) 2004 Andras Varga
//
// 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 __INET_MODULEACCESS_H
#define __INET_MODULEACCESS_H
#include "inet/common/INETDefs.h"
#include "inet/networklayer/contract/IInterfaceTable.h"
namespace inet {
/**
* Returns true if the given module is a network node, i.e. a module
* with the @networkNode property set.
*/
INET_API bool isNetworkNode(const cModule *mod);
/**
* 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.
*/
INET_API cModule *findModuleSomewhereUp(const char *name, const cModule *from);
/**
* Find the node containing the given module.
* Returns nullptr, if no containing node.
*/
INET_API cModule *findContainingNode(const cModule *from);
/**
* Find the node containing the given module.
* throws error if no containing node.
*/
INET_API cModule *getContainingNode(const cModule *from);
/**
* Find the ancestor module under the node containing the given module.
* Returns nullptr, if no such node found.
*/
INET_API cModule *findModuleUnderContainingNode(const cModule *from);
/**
* Finds a module in the module tree, given by its absolute or relative path
* defined by 'par' parameter.
* Returns nullptr if the 'par' parameter is empty.
* Returns the pointer to a module of type T or throws an error if module not found
* or type mismatch.
*/
template<typename T>
INET_API T *findModuleFromPar(cPar& par, const cModule *from, bool required = true);
template<typename T>
T *findModuleFromPar(cPar& par, const cModule *from, bool required)
{
const char *path = par;
if (path && *path) {
cModule *mod = from->getModuleByPath(path);
if (!mod) {
if (required)
throw cRuntimeError("Module not found on path '%s' defined by par '%s'", path, par.getFullPath().c_str());
else
return nullptr;
}
T *m = dynamic_cast<T *>(mod);
if (!m)
throw cRuntimeError("Module can not cast to '%s' on path '%s' defined by par '%s'", opp_typename(typeid(T)), path, par.getFullPath().c_str());
return m;
}
return nullptr;
}
/**
* Gets a module in the module tree, given by its absolute or relative path
* defined by 'par' parameter.
* Returns the pointer to a module of type T or throws an error if module not found
* or type mismatch.
*/
template<typename T>
INET_API T *getModuleFromPar(cPar& par, const cModule *from, bool required = true);
template<typename T>
T *getModuleFromPar(cPar& par, const cModule *from, bool required)
{
const char *path = par;
cModule *mod = from->getModuleByPath(path);
if (!mod) {
if (required)
throw cRuntimeError("Module not found on path '%s' defined by par '%s'", path, par.getFullPath().c_str());
else
return nullptr;
}
T *m = dynamic_cast<T *>(mod);
if (!m)
throw cRuntimeError("Module can not cast to '%s' on path '%s' defined by par '%s'", opp_typename(typeid(T)), path, par.getFullPath().c_str());
return m;
}
/**
* Find the nic module (inside the networkNode) containing the given module.
* Returns nullptr, if no containing nic module.
*/
INET_API InterfaceEntry *findContainingNicModule(const cModule *from);
/**
* Find the nic module (inside the networkNode) containing the given module.
* throws error if no containing nic module.
*/
INET_API InterfaceEntry *getContainingNicModule(const cModule *from);
} // namespace inet
#endif // ifndef __INET_MODULEACCESS_H