函数生成库? [英] Function generating library?

查看:108
本文介绍了函数生成库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找一个可以生成(无参数的void)函数的类/库,这些函数由带有const的函数调用组成.用法就像

Hi,

I am looking for a class/library that can generate (parameterless void) functions that consist of function calls with consts. Usage would be something like

generator.startFunction();
generator.addCall(funcPtr);
generator.addParam(1);
generator.addParam(&someVar);
... //More calls
funcPtr newFunc = generator.compile();
newFunc();



我同意用户负责将函数签名与参数正确匹配,并使所有函数的调用约定相同.我也希望能够调用类成员函数,但这不是必须的.

有人知道具有这种功能的图书馆吗?如果不是,那么有谁知道具有执行此操作所需的ASM知识的良好资源吗?

谢谢!



I''m ok with the user being responsible for correctly matching function signatures with parameters, and for the calling conventions to be the same for all functions. I would like to be able to call class member functions as well, but it isn''t a must.

Does anyone know of a library with this kind of functionality? If not, does anyone know a good resource with the ASM knowledge required to do this?

Thanks!

推荐答案

您能解释一下为什么要这样吗?您的问题可能有解决方案,但是如果您提供上下文,这将大有帮助.
您在此处描述的内容没有多大意义,因为您编写的代码可以被直接调用该函数代替.我想您是在寻找某种脚本,对吗?
Could you explain why you would like something like that ? There are probably solutions to your problem but it would help a lot if you provide the context.
What you described here doesn''t make a lot of sense because the code you wrote could be replaced by the direct call to the function. I guess you are instead looking for some kind of scripting, am I right ?


看到您对Cedric答案的评论,您有两种选择.

第一个是通过boost :: function奋斗并使用它.您应该能够使用它来绑定函数,对象,成员函数及其参数.如果您想在运行时完成绑定(即当您漫游到一个函数时不知道什么类型的函数以及参数的类型/值),那么这将行不通.

不使用boost的更肮脏的方法是自己提出小功能对象.当您执行此类操作时,这些类通常很小,在某些情况下甚至可以使用标准库.

因此,如果我有一个带有带一个int参数的函数b的类A,并且我想在具有相同参数的A集合上调用b:

Seeing your comment to Cedric''s answer, you''ve got a couple of options.

The first one is fight your way through boost::function and use that. You should be able to bind functions, objects, member functions and their parameters using that. If you want complete binding at run time (i.e. when you wander into a function you don''t know what type of function and types/values of parameters) then this isn''t going to work.

A dirtier approach that doesn''t use boost would be to come up with small function objects yourself. The classes are usually fairly small when you do this sort of thing and in some cases you can even use the standard library.

So if I''ve got a class A with a function b that takes one int parameter and I want to call b on a collection of A''s with the same parameter:

class A
{
    public:
        void b( int n )
        {
            std::cout << "b called with..." << n << std::endl;
        }
};



您可以使用常量参数调用A :: b来收集A:s的集合:



you can call A::b with a constant parameter for a collection of A''s:

std::vector<A> a_array( 16 );
std::for_each( a_array.begin(), a_array.end(), std::bind2nd( std::mem_fun_ref( &A::b ), 5  ) );



希望这会有所帮助,

干杯,

Ash



Hope this helps,

Cheers,

Ash


我采用了一种稍微不同的方法.类似于boost :: function,但允许我在呼叫准备期间烘烤参数.这是我写的一个小型测试应用程序:

I went for a slightly different approach. Something similar to boost::function but allowing me to bake the parameters in during call preparation. Here is a small test app I wrote :

#include <iostream>
#include <vector>
typedef void (*VoidFunc)();
int callMe() { std::cout << "callMe()\n"; return 1; }
void callMeWithInt(int a) { std::cout << "callMeWithInt(" << a << ")\n"; }
void callMeWithFloat(float a) { std::cout << "callMeWithFloat(" << a << ")\n"; }
class FunctionCaller { public : virtual void call() = 0; };
template <typename RetValT> class NoParamCaller : public FunctionCaller
{
    typedef RetValT (*NoParamFunc)();
    NoParamFunc theFunc;
public:
    NoParamCaller(NoParamFunc func) : theFunc(func) {};
    void call() { theFunc(); }
};
template <typename RetValT, typename Param1T> class OneParamCaller : public FunctionCaller
{
public:
    typedef RetValT (*OneParamFunc)(Param1T);
    OneParamFunc theFunc;
    Param1T param1;
    OneParamCaller(OneParamFunc _func, Param1T _param1) : theFunc(_func), param1(_param1) {};
    void call() { theFunc(param1); }
};
int main(int argc, char* argv[])
{
    std::vector<FunctionCaller*> calls;
    calls.push_back(new NoParamCaller<int>(callMe));
    calls.push_back(new OneParamCaller<void, int>(callMeWithInt, 1));
    calls.push_back(new OneParamCaller<void, float>(callMeWithFloat, 5.5));
    for (size_t i=0; i<calls.size(); i++)
    {
        calls[i]->call();
    }
    for (size_t i=0; i<calls.size(); i++)
    {
        delete calls[i];
    }
    calls.clear();
    std::cout << "Finished" << std::endl;
    return 0;
}



此代码可以编译并正确运行.我有FunctionCaller接口,然后由模板类实现.在这种情况下,我只处理0或1个参数,但是为2,3,4种情况添加相同的模板(应该足够了),也许成员函数的排列也不会那么困难.

然后,我将拥有一个需要调用的FunctionCallers数组,而不是具有生成的函数.开销可能是程序集编写路线(必须访问数组和另一个虚拟函数调用)的2-3倍,但我可以控制该开销.

这对我想做的事情似乎还行吗?有人看到它固有的问题吗?



This code compiles and runs correctly. I have the FunctionCaller interface, which then gets implemented by the template classes. In this case, I only handle 0 or 1 parameters, but adding the same templates for the 2,3,4 cases (should be enough) and maybe the member function permutation as well will not be that hard.

Then, instead of having the generated function, i will have an array of FunctionCallers that i will need to call. The overhead is probably 2-3 times bigger than the assembly writing route (have to access the array and another virtual function call), but I can manage that cost.

Does this seem OK for what I''m trying to do? Anybody see something inherently wrong with it?


这篇关于函数生成库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆