C - Exposing an interface which declares some methods to developer. Later call the implementations(defined by the developer) to run the system -


I'm trying to do some interesting (automation) in C. what I have to do.

    Expose the external interface (the developer)
  1. This interface declares some logic based work.
  2. Invite the developer to implement those functions.
  3. The backend system will calculate some parameters.
  4. Then, passing parameters (something like callback) will call those implementations.

    This should not be a big thing. But after going through the loop "try to think" I did not get any success many times, I am getting more confused every time.

    I am unable to write the code to get the functionality. Here is some example code.

    Assume that I have created the interface below.

      MyInterface.h int doSomething (int x, int y); Int doSomethingElseNow (int x, int y);   

    And the developer asked to include the header file and implement its definition for the above mentioned tasks. (Something like "interface" in Java) assume that he did something like this (could be wrong)

      developer 1.c # included & lt; "MyInterface.h" & gt; Int doSomething (int x, int y) {return x * y; } int doSomethingElseNow (int x, int y) {return x + y; }   

    Now, my question is how do I call these functions (defined) from any other class? What is my backend code? This code is normal, it will be written before the developer has been asked to define tasks. Want to do something. (This is wrong to make sure)

      Includes Backend C # & lt; "MyInterface.h" & gt; Int main () {int x, y; Calling values ​​of // x and y // have some intense results = dots (x, y); // Calculating something on the result variable / calling the grouping itself an init result 2 = doSomethingElseNow (x, the result); // Do something with result2 ...}   

    Please show me some direction to move forward.

    You can definitely do by declaring your callback functions only And then can not apply , because it will force them to implement the developer (if they do not, it will get a linker error). This is what e G. The SDL library does this when it is again #define s main () function (Eug!)

    However, this is not a very intelligent solution. ("You can" does not mean "you".) Here's why:

    • On some systems, you will not be able to create a dynamic library with your code because the linkage Undefined symbols are unsuccessful.
    • Only one callback function can be executed per executable. Sometimes it is insufficient.
    • The developer can not call his callback function which he wants to call it. It may also introduce name collisions.
    • It's ugly and behind the concept.

      Therefore, rather than complying with the implementation of some arbitrarily designated functions, a clear indicator in one of your interfaces callback function. This is more flexible and elegant than your current perspective.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -