Search code examples
c++function-pointerspriority-queueomnet++

How to implement a cQueue as a priority Queue in Omnet++?


I want to use the Omnet++ based container cQueue as a priority Queue.
As it is explained in the API reference and in the manual - I need to define it
as follows:
cQueue queue("Name of queue", someCompareFunc)

When someCompareFunc is of type CompareFunc which is defined by omnet as:
typedef int (*CompareFunc)(cObject *a, cObject *b);

So, I wanted to implement this comparative function, but didn't manage to write something that will compile.
I admit I didn't work with function pointers for some time now, but after a small research, I think I do understand and did write some test codes with eclipse IDE (c++).

I'm trying to write the code at a simpleModule file.cc.
So for every function I declare at the header file in the "regular way", in the .cc file I need to add the module name with "::" before the function's name.
So in my header file I declared:
int compareByNodes (cObject *a, cObject *b);

And in the .cc file:

int JobScheduler::compareByNodes (cObject *a, cObject *b){
       return 1;
    };

My first try to define the cQueue was:

cQueue queue("job_Buffer",&compareByNodes);

But I received a compilation error of: 'compareByNodes' was not declared in this scope.
So I figured it must have something to do with the 'JobScheduler::' that is missing.
The second try was:

CompareFunc tmp = (CompareFunc)&JobScheduler::compareByNodes;
cQueue queue("job_Buffer",tmp);

This time my errors were:
"Multiple markers at this line
- within this context
- converting from ‘int (JobScheduler::)(omnetpp::cObject, omnetpp::cObject*)’ to ‘omnetpp::CompareFunc {aka int () (omnetpp::cObject, omnetpp::cObject*)}’ [-Wpmf-conversions]"

Adding parenthesis after the 'ampersand' didn't help either.

It would really help if I could use this method of Omnet and I guess I'm missing something since it's a well defined "feature" of the software.
I also tried to google these subjects, searched in the google groups section, and didn't find any answers.
Would appreciate any help


Solution

  • Try to declare a compare method as static. For example:

    //...
    class Txc1 : public cSimpleModule {
      protected:
        virtual void initialize() override;
        virtual void handleMessage(cMessage *msg) override;
    
      public:
        static int MyCompareFunc (cObject *a, cObject *b);
    };
    
    Define_Module(Txc1);
    
    int Txc1::MyCompareFunc (cObject *a, cObject *b) {
        return 1;
    }
    
    void Txc1::initialize() {
        cQueue q1("queue1", MyCompareFunc);
        // ...
    }