Gooday2die
Gooday2die

Reputation: 109

Dynamic Function in Cpp

I have been working on a cpp project and I would like to make my function dynamically. I have a function which has a bout 9 options when I am running.

Let capital letter be a condition as boolean type, and small letter be a function. And let's assume that my function has its conditions(A-I) outside the function as a global scope. And the conditions would not be changed during runtime. It would be just set one time before running this function, and would not be changed during this function.

void Myfunction(){
if(A) a(); // I would not use else if or switch since I should check all conditions.
if(B) b();
...
if(I) i();
return;
}

And the function would be called in a infinite-looped manner

int main(void){
//Conditions A - I is declared here
while (1) Myfunction;
return 0;
}

I do know that using if statement is quite slow and also checking non-variable conditions is kind of nonsense. In order for me to save computing resources and save time while running this function, I would like to make my function dynamically. Meaning that if first checking its conditions (A-I) then the function decides which expression to use.

For example, if we have conditions A, B, C as true and all others(D - I) as false. I would like to make the function automatically become.

void Myfunction(){
a();
b();
c();
return;
}

I have searched the internet, however wasn't able to find any. I found some articles about templates in cpp however it was not the case I was looking for.

Thank you for reading this article. Since I am quite a new to Stackoverflow, I might have made some mistakes in this post. If there are anything in this post that is against Stackoverflow's rules, please please let me know. I would be more than happy to modify my post.

Thank you.

Upvotes: 1

Views: 886

Answers (2)

Gooday2die
Gooday2die

Reputation: 109

So I have found a solution to my project using Function Pointers. First I made my functions as and then saved those function pointers to an function pointer array. While saving those function pointers to function pointer array, I counted how many items would be saved in my function pointer array. So by using those counted value, I can iterate each element of my function array. Actually all my functions were in a class. so my functions were actually member functions.

In order for me to use my member function pointers, I needed to make a struct type of function pointer. And also declare a new function pointer variable in my class named CC to use it in its whole class scope.

typedef void(CC::FunctionPointers)();
FunctionPointers* FunctionPointerArray;
FunctionPointerArray = (FunctionPointers*)malloc(sizeof(FunctionPointers) * 4); // I will just say 4 conditions right ere. 
// Malloc-ing the FunctionPointers type can be possible, or there is another option 
// like making direct array (Both two are the same in terms of operation)

And, then I have function declaration and definition in my class named CC.

void aFunc() { cout << "in a()" << endl; }
void bFunc() { cout << "in b()" << endl; }
void cFunc() { cout << "in c()" << endl; }
void dFunc() { cout << "in d()" << endl; }

Then I make the function pointer array assignment member function. And lets save this returned value as totalIterCounts

int GenerateFunctionPointers(void){
    if(a) FunctionPointerArray[0] = &CC::aFunc();
    if(b) FunctionPointerArray[1] = &CC::bFunc();
    if(c) FunctionPointerArray[2] = &CC::cFunc();
    if(d) FunctionPointerArray[3] = &CC::dFunc();
    return a + b + c + d; // To set exact iteration counts;
}

Then When I need to call my functions, I will make this loop to call my functions.

void callFunctions(){
for (int i = 0 ; i < totalIterCounts ; i++){
    (this->*FunctionPointerArray[i])();
}

So in summary my whole class would be something like this.

class CC{
Public:
    typedef void(CC::FunctionPointers)();
    FunctionPointers* FunctionPointerArray = NULL;
    // I do know that using non-variable in malloc is kind of meaningless.
    int totalIterCounts; // To count how many iterations I need.

    CC(){
        totalIterCounts = GenerateFunctionPointers();
    }

    void aFunc() { cout << "in a()" << endl; }
    void bFunc() { cout << "in b()" << endl; }
    void cFunc() { cout << "in c()" << endl; }
    void dFunc() { cout << "in d()" << endl; }

    int GenerateFunctionPointers(void){
        FunctionPointerArray = (FunctionPointers*)malloc(sizeof(FunctionPointers) * 4) 
        //I have not initialized every element here, Assigning NULL would be safer.
        if(a) FunctionPointerArray[0] = &CC::aFunc();
        if(b) FunctionPointerArray[1] = &CC::bFunc();
        if(c) FunctionPointerArray[2] = &CC::cFunc();
        if(d) FunctionPointerArray[3] = &CC::dFunc();
        return a + b + c + d; // To set exact iteration counts;
    }

    void CallFunctions(){
        for (int i = 0 ; i < totalIterCounts ; i++){
        (this->*FunctionPointerArray[i])();
    }
}

And the function CallFunctions() can be called from outside in the future. This solution worked for me last night. Special thanks to everyone who helped me with this question!

This is my first time with Answering my own question in Stackoverflow, so I might have some problems in this answer. Please let me know if I should modify or edit somethings. Again Thanks alot.

Upvotes: 0

Insaf K
Insaf K

Reputation: 366

I tried to create a solution using function templates.

The two main functions are GetMyFunction() and MyFunctionTemplate().

MyFunctionTemplate() is a function template which would accept all of your expected params as bool template arguments(Non-type Template Arguments).

GetMyFunction() function would return pointer to the required specialization of MyFunctionTemplate() during run time.

GetMyFunction() also does one more thing, it must check for all of the combinations of the params, and return the corresponding function.

These MyFunctionTemplate() specialization would be created during compile time, and I believe those if() checks within MyFunctionTemplate() would be removed since those are time compile-time constants(Someone please confirm this).

#include <iostream>

using namespace std;

void aFunc() { cout << "in a()" << endl; }
void bFunc() { cout << "in b()" << endl; }
void cFunc() { cout << "in c()" << endl; }
void dFunc() { cout << "in d()" << endl; }

template <bool a, bool b, bool c, bool d>
void MyFunctionTemplate()
{
    if (a) aFunc();
    if (b) bFunc();
    if (c) cFunc();
    if (d) dFunc();
}

void (*GetMyFunction(bool a, bool b, bool c, bool d))()
{
    if (a && b && c && d)
        return &MyFunctionTemplate<true, true, true, true>;
    if (a && b && c && !d)
        return &MyFunctionTemplate<true, true, true, false>;
    // And all other combinations follows....
}

int main(void)
{
    // Conditions A - I is declared here
    bool a = true, b = true, c = true, d = true;
    // auto MyFunction = GetMyFunction(a, b, c, d);
    void (*MyFunction)(void) = GetMyFunction(a, b, c, d);
    MyFunction();

    return 0;
}

Upvotes: 1

Related Questions