LearnMore
LearnMore

Reputation: 745

How to call a function(only known at runtime) with parameter values(only known at runtime)?

At runtime a single test is made to determine which of two functions to call and also to establish parameter value(s).

The function is then called every few seconds.

void Foo::func( std::string s);
void Foo::func( std::string s1 std::string s2, std::string s2);

Obviously, its inefficient to test( which function to call ) for every call, especially when the function and its parameter value(s) will not change once established.

I need something like this:

Test once which function to call and established parameter value(s).

Assign initialised function to some kind of callable variable.

funcVar = void Foo::func( "blah", "blah", "woof" );

or

funcVar = void Foo::func( "blah" );

Then call function;

for( every few seconds )
{
call FuncVar;
}

Is there some kind of callable variable I can use to do this?

Upvotes: 0

Views: 574

Answers (4)

SaurabhS
SaurabhS

Reputation: 663

std::function should work here.

std:: function<void()> fun = [&s] () {/* do  work*/};
if (some_condition)
{
   fun = [&s1,&s2,&s3] () {/* do your work*/} ;
}

if(every few seconds)
{
    fun();
}

Upvotes: 0

PirklW
PirklW

Reputation: 484

If you want to std::bind overloaded functions, you have to specify which one you want to use:

#include <string>
#include <functional>
#include <iostream>
#include <map>


struct Foo {
    void b() { std::cout << name << ": function 1" << std::endl; }
    void b(int i) {std::cout << name << ": function 2" << std::endl; }

    std::string name;
};


void with_map() {
    Foo f;
    f.name = "with_map";

    std::map<const int, std::function<void()>> map({
        {10, std::bind<void(Foo::*)(), Foo&>(&Foo::b, f)},
        {20, std::bind<void(Foo::*)(int), Foo&, int>(&Foo::b, f, 1)},
    });

    map[20]();
    map[10]();
}


void without_map() {
    Foo f;
    f.name = "without_map";

    std::function<void()> x = std::bind<void(Foo::*)(), Foo&>(&Foo::b, f);
    x();

    x = std::bind<void(Foo::*)(int), Foo&, int>(&Foo::b, f, 1);
    x();
}


int main() {

    without_map();

    with_map();

    return 0;
}

Upvotes: 0

Klaus
Klaus

Reputation: 25613

You can simply use a lambda function object ( closure ) which you can call as often as you like.

Example:

struct Foo 
{
    void func( std::string s){ std::cout << "single" << s << std::endl; };
    void func( std::string s1, std::string s2, std::string s3) 
    {   
        std::cout << "tripple " << s1 << " " << s2 << " " << s3 << std::endl;
    }   

    // Select which function to call
    std::function<void()> Select( int what )
    {   
        if ( what == 1 )
        {   
            // Create a callable object and put in also parameters
            return [this]() { func("Hallo"); };
        }
        else
        {   
            // The same for the other selection
            return [this]() { func("This","is","tripple!"); };
        }
    }   
};  

int main()
{   
    Foo foo;
 
    // Pick what you want to select
    auto action = foo.Select( 2 );

    for ( ;; )
    {   
        // and call it as often you like
        action();
    }   
}

Upvotes: 1

Jakub Piskorz
Jakub Piskorz

Reputation: 1073

You can use std::bind for creating a functor with stored arguments and common call operator for both your use cases. Check this snippet:

#include <string>
#include <functional>

struct Foo {
    static void a(std::string) {}
    static void b(std::string, std::string) {}
};

int main() {
    std::function<void()> x;

    if(true) {
        x = std::bind(&Foo::b, "a", "b");
    }

    x();

    return 0;
}

Upvotes: 0

Related Questions