简体   繁体   English

为可变数量的参数的不同函数编写通用包装器

[英]Writing a generalized wrapper for different functions with variable number of arguments

I want to write a C++ wrapper for a bunch of functions which are all similar in what they do. 我想为一堆函数编写C ++包装器,这些函数在功能上都相似。 But they all have a different number of arguments. 但他们都有不同数量的参数。 For example (assume typeA, typeB, etc. are different typedef'd types): 例如(假设typeA,typeB等是不同的typedef类型):

typeA func1 (typeB b, typeC c);

typeA func2 (typeB b, typeD d, typeE e);

These are the functions I want to write a wrapper for (notice that both have return type typeA and first argument of type typeB). 这些是我要为其编写包装器的函数(注意,它们都具有返回类型typeA和类型typeB的第一个参数)。 So I intend to create a general wrapper class and store a list of arguments in the constructor. 因此,我打算创建一个通用包装器类,并将参数列表存储在构造函数中。 And I want to be able to call a function which looks like 'func' in the following: 我希望能够在下面调用类似于“ func”的函数:

class wrapper {
  public:
    wrapper (/*args - a list of arguments, including 
               a pointer to the actual function, and 
               the remaining arguments depending 
               on the function. */);
    void func (typeB b, typeA &a);
}

So, I want to write func in such a way that it calls whatever function (either func1 or func2) with the arguments passed into the constructor. 因此,我想以这样一种方式编写func:它使用传入构造函数的参数调用任何函数(func1或func2)。

I want to know if there is a way I can treat func1 or func2 as though they have a variable number of arguments. 我想知道是否可以将func1或func2视为具有可变数量的参数。 Or at least, if there is a way in C++ in which I can feed in a function and a list of arguments and get eval (function, argument_list). 或者至少,如果在C ++中有一种方法,我可以输入一个函数和一个参数列表并获取eval(函数,argument_list)。

The alternative is writing a wrapper for every single function, which I want to avoid but am not averse to. 另一种方法是为每个函数编写一个包装器,我想避免但并不反对。 (Also, I don't mind if a solution involves me not using a wrapper class but a wrapper function) (此外,我不介意解决方案是否涉及到我不是使用包装器类而是包装器函数)

Here is something I have done in C, to wrap around any function. 这是我在C语言中完成的操作,以包装所有功能。 I hope it will help/inspire you. 我希望它能帮助/启发您。 The main restriction is that you can wrap only around user defined functions, since you will have to modify the signature of the function. 主要限制是只能包装用户定义的函数,因为您必须修改函数的签名。

My solution uses the va_arg structure. 我的解决方案使用va_arg结构。

#include <stdarg.h>
void * super_wrapper( void * (*fn)(), void * ret, int n, ...)
{
  va_list = my_list;
  va_start(my_list, n);
  ret = (*fn)(ret, n, my_list);
  va_end(my_list);
  return ret
}

The signature of the wrapper takes the pointer to the function you want to execute, a pointer to give to the function to retrieve your return value, the number of arguments of youe call to the function, and then the arguments of the function you want to call. 包装器的签名使用指向您要执行的函数的指针,赋予该函数的指针以检索您的返回值,调用该函数的参数数量,然后是您想要执行的函数的参数呼叫。

The va_list is a list of arguments. va_list是参数列表。 You initialize it with va_start. 您可以使用va_start对其进行初始化。 The first argument of va_start is the va_list you want to initialize. va_start的第一个参数是要初始化的va_list。 The second argument is the last known parameter of the current function (in this example, it's 'n'). 第二个参数是当前函数的最后一个已知参数(在本例中为“ n”)。

Now, instead of the ellipsis in your user defined function, you will put an argument va_list. 现在,您将放置参数va_list而不是用户定义函数中的省略号。

Two examples I had: 我有两个例子:

double sum (int n, ...)
{
  int i;
  double res = 0;
  double tmp;
  va_list = my_list;
  va_start(my_list, n);
  for(i=0; i<n; i++)
  {
    tmp = va_arg(my_list, double);
    res += tmp;
  }
  va_end(my_list);
  return res;
}

void my_print(int n, ...)
{
  int i;
  va_list my_list;
  va_start(my_list, n);
  char * tmp;
  for(i=0; i<n; i++)
  {
    tmp = va_arg(my_list, char *);
    printf("%s ", tmp);
  }
  printf("\n");
  va_end(my_list);
}

I managed to use the same wrapper to call this two different functions with some modifications in the signature and in the function bodies. 我设法使用相同的包装器来调用这两个不同的函数,并对签名和函数主体进行了一些修改。 The thing is that in arguments and in returns, I only give pointers. 关键是在参数和返回中,我只给出指针。 So I have to dereference the pointers. 因此,我必须取消引用指针。

void * sum (void * ret, int n, va_list my_list)
{
  int i;
  double res = 0;
  double *tmp;
  for(i=0; i<n; i++)
  {
    tmp = va_arg(my_list, double);
    res += *tmp;
  }
  ret = &res;
  return ret;
}

void* my_print(int n, ...)
{
  int i;
  char ** tmp;
  for(i=0; i<n; i++)
  {
    tmp = va_arg(my_list, char **);
    printf("%s ", *tmp);
  }
  printf("\n");
  return ret;
}

Now I am able to use the same wrapper function in my main to call these two functions: 现在,我可以在主程序中使用相同的包装器函数来调用这两个函数:

void main()
{
  double two = 2.0;
  double three = 3.0;
  double fifteen = 15.0;

  char *I, *am, *a, *great, *wrapper;
  I = malloc(sizeof(char) * 2);
  am = malloc(sizeof(char) * 3);
  a = malloc(sizeof(char)*2);
  great = malloc(sizeof(char) * 6;
  wrapper = malloc(sizeof(char) * 8);

  I = strcpy(I, "I\0");
  am = strcpy(am, "am\0");
  a = strcpy(a,"a\0");
  great = strcpy(great, "great\0");
  wrapper = strcpy(wrapper, "wrapper\0");

  void * ret;

  printf("Sum = %f\n", *( (double*)super_wrapper(sum, ret, 3, &two, &three, &fifteen) ) );
  super_wrapper(my_print, ret, 5, &I, &am, &a, &great, &wrapper);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有不同数量参数的模板函数 - template functions with different number of arguments 使用可变编号 arguments 的宏编写 function - Writing a function using a macro with variable number of arguments 简洁地用可变数量的参数重写一组函数 - Succinctly rewrite a set of functions with variable number of arguments 将向量解压缩为参数以调用具有可变数量参数的函数 - Unpack vector into arguments to call functions that have a variable number of arguments 编写通用包装:有条件地将模板参数中的不同类型映射到单个类内部类型 - Writing a Generic Wrapper: Conditionally Map different Types from Template Arguments onto a Single Class-Internal Type 具有不同参数数量的函数中的 C++ 代码重复 - C++ code duplication in functions with different number of arguments 从指针映射调用函数,该函数具有可变数字参数的函数 - Calling a function from a map of pointers to functions having a variable number arguments C ++:函数指向具有可变数量参数的函数 - C++: Function pointer to functions with variable number of arguments c ++传递带有可变数量参数的函数作为其他函数的参数 - c++ passing functions with variable number of arguments as argument to other function Python C ++ API-使用可变数量的参数重载函数 - Python C++ API - overloading functions with variable number of arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM