问题
I am using omnet++, and was wondering how do I get a parameter in a c++ file of a compound module.
module server {
parameters:
int server;
submodule:
//Queue
// processor
}
calling the par
function in c++ is working for me.
回答1:
Compound module doesn't have C++ code. Only simple modules have a C++ code. To get the value of a compound module's parameter from code of simple module one can use: getParentModule()->par("foo");
An example.
NED file:
module Server {
parameters:
int cpuNumber;
submodules:
mod1 : Queue;
}
simple Queue {
// ...
}
To obtain a value of cpuNumber
in C++ code of Queue class one should write:
int cpu = getParentModule()->par("cpuNumber"); // OK
An attempt of use the following code:
int cpu = par("cpuNumber"); // Wrong!
leads to error: unknown parameter 'cpuNumber'
, because Queue
module doesn't have that parameter.
回答2:
Accessing a parameter outside of your own module is an anti-pattern. You should avoid this as it prevents the reuse of the Queue component. The best approach is to have the parameter in both the compound and the submodule and make them to havethe same value.
module Server {
parameters:
int cpuNumber;
submodules:
mod1 : Queue {
privateCpuNumber = cpuNumber;
};
}
simple Queue {
int privateCpuNumbers;
}
and then access your own paremeter:
int cpu = par("privateCpuNumber");
This way you can reuse the Queue module in any compound module.
Note that this seems to be trivial and unnecessary first, but as your model grows and you start rearranging things component reuse is a MUST.
来源:https://stackoverflow.com/questions/33503266/how-do-i-call-a-parameter-for-a-compound-module