Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:32:24+00:00 2026-06-04T06:32:24+00:00

I’m programming in a very static environment and would like to generalize the problem/code

  • 0

I’m programming in a very static environment and would like to generalize the problem/code in the best way possible, to make it clean and avoid any boilerplate code. This is my case; I’m developing a modification system which sits between the program and the extensions, where the extensions will be forwarded the API calls. When my modification gets loaded by the program, I receive a pointer to a table consisting entirely of function pointers.

struct APITable
{
 void (*MouseClick)(int, int);
 void (*MouseLeave)(bool);
 int (*MouseRet)(float, int);
 // And about a 200 more
 // ......
};

In this function I set the callback functions/pointers, to point at my own (i.e apiTable->MouseClick = &MyMouseClick;). I’m trying to extend this framework (since it only allows one modification at a time) to support multiple plugins. This is done by loading additional plugins (as libraries) and then forward all callbacks (such as MouseClick) to the loaded plugins. This is an example;

void MyMouseClick(int x, int y)
{
 bool blockOriginal = false;

 // Iterate over all plugins
 if(plugin_function_is_defined_pre)
  blockOriginal = call_plugin_callback_pre(x, y);

 if(!blockOriginal)
  EngineMouseClick(x, y); // This is the programs "engine" function, which I have to call if I want normal execution flow

 // Iterate over all plugins
 if(plugin_function_is_defined_post)
  call_plugin_callback_post(x, y);
}

This is a very simplified procedure but I think it tells everything that needs to be told. Since I have over 200 functions there’s no way I would copy and paste all this boilerplate code. The normal procedure would probably be a #define, one for void functions and one for functions with a return value. Although this works, I would definately prefer another solution (which might not be possible since C++ is a statically typed language).

The point is; I want to generalize this procedure in a clean and efficient way avoiding any boilerplate code. Worth mentioning is that the functions may override the original functions return value, in callbacks not declared as void.

So to strip it to the bones: Program->MyExtension->Plugins&Engine

NOTE: I do not want to use any language but C++, and will use C++11 features if necessary!

EDIT: In case of interest, this is how my modification gets loaded by the program:

extern "C" ModLoad(APITable * apiTable)
{
 apiTable->MouseClick = &MyMouseClick;
 apiTable->MouseLeave = &MyMouseLeave;
 // And so on
 // ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:32:26+00:00Added an answer on June 4, 2026 at 6:32 am

    which might not be possible since C++ is a statically typed language

    If only C++ had a statically-typed way to write generic code that can be customised for different parameters, maybe some kind of template facility? 😉

    N.B. I have no idea how you intend to handle return values or what blockOriginal or the callbacks are meant to do, so this is just a sketch.

    template<typename Res, typename... Args>
      Res call_plugin_func(Enum funcID, Res (*origFunc)(Args...), Args... args)
      {
        Res res = Res();
        bool blockOriginal = false;
    
        // Iterate over all plugins, looking for defs of funcID
        if(plugin_function_is_defined_pre)
          blockOriginal = call_plugin_callback_pre(args...);
    
        if(!blockOriginal)
          res = origFunc(args...);
    
        // Iterate over all plugins
        if(plugin_function_is_defined_post)
          call_plugin_callback_post(args...);
    
        return res;
      }
    
    template<typename... Args>
      void call_plugin_func(Enum funcID, void (*origFunc)(Args...), Args... args)
      {
        bool blockOriginal = false;
    
        // Iterate over all plugins, looking for defs of funcID
        if(plugin_function_is_defined_pre)
          blockOriginal = call_plugin_callback_pre(args...);
    
        if(!blockOriginal)
          origFunc(args...);
    
        // Iterate over all plugins
        if(plugin_function_is_defined_post)
          call_plugin_callback_post(args...);
      }
    
    void MyMouseClick(int x, int y)
    {
      call_plugin_func(ID_MyMouseClick, &EngineMouseClick, x, y);
    }
    

    (Two function templates are needed because the one returning void has a different body, it can’t declare res to be type void.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am currently running into a problem where an element is coming back from
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.