Mour_Ka Mour_Ka - 26 days ago 18
C++ Question

Omnet++: C++ simple condition

What might be the reason that this condition is not true and else is executed

IPassiveQueue *queueModule = nullptr;
queueModule = nullptr;
if (queueModule == nullptr) {
...
}
else {...
}


Based on the answer below I am adding here the reason behind why I am trying to force the condition to be true

The code below is always checking the first condition and executing else. It is in initialize so it doesnt depend on any previous executed class and the queueModule member is set in class definition as protected member

class INET_API EtherMACBase : public MACBase
{virtual void initializeQueueModule();
IPassiveQueue *queueModule = nullptr;
...}

void EtherMACBase::initializeQueueModule()
{
queueModule = nullptr;
if (par("queueModule").stringValue()[0]) {
cModule *module = getModuleFromPar<cModule>(par("queueModule"), this);
if (module->isSimple())
queueModule = check_and_cast<IPassiveQueue *>(module);
else {
cGate *queueOut = module->gate("out")->getPathStartGate();
queueModule = check_and_cast<IPassiveQueue *>(queueOut->getOwnerModule());
}

EV_DETAIL << "Requesting first frame from queue module\n";
txQueue.setExternalQueue(queueModule);

if (txQueue.extQueue->getNumPendingRequests() == 0)
txQueue.extQueue->requestPacket();
}
else {
txQueue.setInternalQueue("txQueue", par("txQueueLimit").longValue());
}
}


While in this case the condition is true although the parameter is set to be the same

class INET_API PPP : public MACBase
{
protected:
IPassiveQueue *queueModule = nullptr;
virtual void initialize(int stage) override;
...}

void PPP::initialize(int stage)
{
queueModule = nullptr;

if (par("queueModule").stringValue()[0]) {
cModule *mod = getModuleFromPar<cModule>(par("queueModule"), this);
if (mod->isSimple())
queueModule = check_and_cast<IPassiveQueue *>(mod);
else {
cGate *queueOut = mod->gate("out")->getPathStartGate();
queueModule = check_and_cast<IPassiveQueue *>(queueOut->getOwnerModule());
}
}
...
}

Answer

Your problem is a consequence of different declarations of default queue type for PPP and Ethernet interfaces in NED files in INET.
Look at these two files: src/inet/linklayer/ppp/PPPInterface.ned and src/inet/linklayer/ethernet/EthernetInterface.ned. The crucial parts of these files are below.

module PPPInterface like IWiredNic
{
    parameters:
        string queueType = default("DropTailQueue"); 
    submodules:
        queue: <queueType> like IOutputQueue if queueType != "" {
        }
        ppp: PPP {
            parameters:
                queueModule = (queueType == "" ? "" : "^.queue");
        }
}

module EthernetInterface like IWiredNic
{
    parameters:
        string queueType = default(""); 
    submodules:
        queue: EtherQoSQueue if queueType != "" {
        }
        mac: <macType> like IEtherMAC {
            parameters:
                queueModule = (queueType == "" ? "" : "^.queue");
        }
}

One can notice that PPPInterface has an assigned default value for queueType parameter, and therefore there is a queue submodule as well as parameter queueModule is not empty.
However, EthernetInterface does not have a value of queueType, so there is no queue submodule, and parameter queueModule is an empty string. Reading of [0] element of empty string results in 0 (false).