Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Develop a plugin in C++: Using FuncItem._pFunc()

2013-05-12
2013-05-26
  • Ami Friedman
    Ami Friedman
    2013-05-12

    <<I meant="" to="" raise="" this="" issue="" in="" THIS="" forum="" as="" opposed="" to="" "OpenForum"="">>

    I'm a veteran C programmer but very new to C++ (and I insist on using c++ right now :))
    From reading around some forums it seems that using a function member as a callback is "problematic". It seems that static functions is the way to go. The problem with that is accessing all my class members from within the static function.
    I guess my question is, did anyone here develop plugins in C++ and if yes how did you get around passing the command functions to FuncItem.
    Thanks

     
  • dail8859
    dail8859
    2013-05-12

    If you haven't seen the plugin template yet I would highly suggest looking through it. It was very helpful for me. :)

    Yes, pointers to non-static class methods can be problematic. If you do make the class methods static, you would have to make the class members static also so you can access them in the static method. Or, you could create a wrapper function around each of your class methods, see here for a good example.

    Hope this helps!

     
  • In general, when you register a callback in C, you pass a function pointer, and sometimes a parameter to pass to that function. When you can register a parameter with the function pointer, you can pass the object pointer as parameter and a simple trampoline like trampoline_method(TheClass* obj) { obj->method(); }

    If you can only pass a function pointer, and not some parameter to it, you can use what is called a wrapper in the above post if you have an upper limit on the number of objects on which you might want to register callbacks at the same time. In the fully dynamic case, where you do not know at compile time how many objects/callbacks will have to be registered, you need to generate machine code at execution time. This is how I did my "Python Plugin Square", used by my "Modeline Parser", to register plugin menu functions at execution time (the number of functions is not known at compile time).

     
    Last edit: François-R Boyer 2013-05-12
  • @François, have you got the code for the C element of the Python Plugin Square, I'd really like to see how this is done, I think it would be a really welcome addition to my plugin template I've been working on since the dawn of time. I couldn't find the code under the SF project.

    Cheers,
    Dave.

     
  • @Dave:
    I was a bit busy, but I finally put online the C source for my Python Plugin Square. It is now available in the Code tab of the SF project. I made it "self contained", and should thus compile easily. I hope the code is readable... if you have questions, don't hesitate to ask.

    Cheers,
    François