开发者

Timer Thread with passed Function* and Param

开发者 https://www.devze.com 2023-03-01 06:22 出处:网络
I\'m working on finishing up my server for my first iPhone application, and I want to implement a simple little feature.

I'm working on finishing up my server for my first iPhone application, and I want to implement a simple little feature.

I would like to run a function (perhaps method as well), if another function returns a certain value after a certain waiting period. Fairly simple concept.... right?

Here's my basic foundation.

template <typename T,class TYP>
struct funcpar{
 T (*function)(TYP);
 TYP parameter;
 funcpar(T (*func)(TYP),TYP param);
 funcpar& operator=(const funcpar& fp);
}; 

The goal here is to be able to call funcpar::function(funcpar::parameter) to run t开发者_JS百科he stored function and parameter, and not have to worry about anything else...

When I attempted to use a void* parameter instead of the template, I couldn't copy the memory as an object (because I didn't know what the end object was going to be, or the beginning for that matter) and when I tried multiple timers, every single object's parameter would change to the new parameter passed to the new timer... With the previous struct I have a

question:

Is it possible to make an all-inclusive pointer to this type of object inside a method of a class? Can I templatize a method, and not the whole class? Would it work exactly like a function template?

I have a managing class that holds a vector of these "jobs" and takes care of everything fairly well. I just don't know how to use a templatized function with the struct, or how to utilize templates on a single method in a class..

I'm also utilizing this in my custom simple threadpool, and that's working fairly well, and has the same problems...

I have another question:

Can I possibly store a function with a parameter before it's run? Something like toRun = dontrunmeyet(withThisParameter);? Is my struct even necessary?

Am I going about this whole thing incorrectly?

If this is overly ambiguous, I can set you up with my whole code for context


In order to create a class method that takes a template parameter, yes, it would work almost exactly like a function template. For example:

class A
{
        public:
                template<typename T>
                void my_function(const T& value) { }
};

int main()
{
        A test;
        test.my_function(5);

        return 0;
}

Secondly, for your structure, you can actually turn that into a functor-object that by overloading operator(), lets you call the structure as-if it were a function rather than having to actually call the specific function pointer members inside the structure. For instance, your structure could be re-written to look like this:

#include <iostream>

template <class ReturnType, class ParameterType>
class funcpar
{
    private:
        ReturnType (*function)(ParameterType);
        ParameterType parameter;

    public:
        funcpar(ReturnType (*func)(ParameterType),ParameterType param):
            function(func), parameter(param) {}

        funcpar& operator=(const funcpar& fp);

        //operator() overloaded to be a function that takes no arguments
        //and returns type ReturnType
        ReturnType operator() ()
        {
            return function(parameter);
        }
};


int sample_func(int value)
{
    return value + 1;
}


int main()
{
    funcpar<int, int> test_functor(sample_func, 5);

    //you can call any instance of funcpar just like a normal function
    std::cout << test_functor() << std::endl; 

    return 0;
}

BTW, you do need the functor object (or your structure, etc.) in order to bind a dynamic parameter to a function before the function is called in C/C++ ... you can't "store" a parameter with an actual function. Binding a parameter to a function is actually called a closure, and in C/C++, creating a closure requires a structure/class or some type of associated data-structure you can use to bind a function with a specific parameter stored in memory that is used only for a specific instance of that function call.

0

精彩评论

暂无评论...
验证码 换一张
取 消