6

我基本上希望为通用 C 函数生成一个包装器,而无需手动指定类型。所以我有一个带有固定原型的回调,但我需要根据包装函数的类型在包装器中执行一些特殊代码......所以基本上我正在考虑在类模板中使用静态方法将我的函数包装到符合标准的接口,例如:

// this is what we want the wrapped function to look like
typedef void (*callback)(int); 
void foobar( float x ); // wrappee

// doesn't compile
template< T (*f)(S) > // non-type template param, it's a function ptr
struct Wrapper
{
  static void wrapped(int x)
  {
     // do a bunch of other stuff here
     f(static_cast<S>(x)); // call wrapped function, ignore result

  }
}

然后我想做类似的事情:

AddCallback( Wrapper<foobar>::wrapped );

但是,问题是我不能直接在 Wrapper 模板中的函数参数中使用“S”,我必须首先将其列为参数:

template< class T, class S, T (*f)(S) >
struct Wrapper
// ...

但这意味着使用 ( ) 会更痛苦Wrapper<void,float,foobar>::wrapped,理想情况下,我只想在那里传递函数指针并让它自动计算出参数的类型(和返回类型)。需要明确的是,在包装函数内部,我需要引用函数指针的类型(所以我确实需要一些等价的 S 或 T)。

有没有办法做到这一点?

4

4 回答 4

5

您可能希望考虑的一件事是使用 LLVM 或类似方法在运行时生成适当的蹦床函数。或者这是一个静态解决方案:

#include <iostream>

void f(float f) { std::cout << f << std::endl; }

template<typename T, typename S> struct static_function_adapter {
        template<T(*f)(S)> struct adapt_container {
                static void callback(int v) {
                        f(static_cast<S>(v));
                }
        };

        template<T(*f)(S)> adapt_container<f> adapt() const {
                return adapt_container<f>();
        }
};

template<typename T, typename S> struct static_function_adapter<T, S> get_adapter(T (*)(S)) {
        return static_function_adapter<T, S>();
}

#define ADAPTED_FUNCTION(f) (&get_adapter(f).adapt<f>().callback)

int main() {
        void (*adapted)(int) = ADAPTED_FUNCTION(f);
        adapted(42);
        return 0;
}

get_adapter 函数允许我们推断参数和返回类型;adapt() 然后将其转换为在实际函数上参数化的类型,最后我们在回调中得到一个静态函数。

于 2009-07-15T19:06:45.070 回答
0

如果您使用返回“包装”而不是直接引用它的函数,编译器将尝试自动匹配函数调用的模板参数。

编辑:这个呢?

int foobar( float x ); // wrappee

template <typename T, typename S>
struct Wrapper {
    typedef T (*F)(S);
    F f;

    Wrapper(F f) : f(f) { }

    void wrapped(S x) {
        // do a bunch of other stuff here
        f(x); // call wrapped function, ignore result
    }
};

template <typename T, typename S>
Wrapper<T,S> getWrapper(T (*f)(S)) {
    return Wrapper<T,S>(f);
}

...
getWrapper(foobar).wrapped(7);
于 2009-07-15T18:24:39.540 回答
0

我会看boost。初读您的问题,在我看来,它<boost/function_types/parameter_types.hpp>提供了您的需求。

于 2009-07-15T18:37:59.683 回答
0

编辑:全新的答案

好的,我已经完全重新考虑了这个问题,并相信我得到了你想要的。我之前实际上已经这样做了:-P。

这就是想法,我有一个重载 operator() 的基类,然后我有一个用于每个“arity”函数的子类。最后我有一个工厂函数,它将返回其中之一。代码很大(可能有点矫枉过正)但运行良好。许多library_function重载是为了支持不同的语法,大部分是不必要的。它还支持boost::bind函数、成员函数等,远远超出您的需要。

http://pastebin.com/m35af190

示例,用法:

// map of library functions which will return an int.
std::map<std::string, LibraryFunction<int> > functions;

// function to register stuff in the map
void registerFunction(const std::string &name, LibraryFunction<int> func) {
    functions.insert(std::make_pair(name, func));
}

稍后你可以这样做:

// the this param is so the function has access to the scripting engine and can pop off the parameters, you can easily chop it out

// register 2 functions, one with no params, one with 1 param
registerFunction("my_function", library_function1(*this, call_my_function));
registerFunction("my_function2", library_function0(*this, call_my_function2));

functions["my_function"]();
functions["my_function2"]();
于 2009-07-15T18:28:42.667 回答