动态函数调用在运行时(va_list的) [英] Dynamic function calls at runtime (va_list)
问题描述
有在C的方式来获得与va_list的动态长度的参数列表,如下所述:
http://www.cprogramming.com/tutorial/c/lesson17.html
There is a way in C to obtain a dynamic length argument list with va_list, as described here: http://www.cprogramming.com/tutorial/c/lesson17.html
这++并不需要用C很简单,但大部分时间。我目前正在建设一个顶级包装类封装了一些Zend的功能。
That quite simple, but most times in C++ not needed. I am currently building a top level wrapper class for encapsulating some Zend functionality.
不过,我确实需要拨打电话这样的动态功能的如从一个正常的printf函数动态。
Anyway, I do need to make a call to such a dynamic function like printf from a normal function dynamically.
我的意思是上述例子相反的方式,这里啥子我有这么战,这也许解释了我的想法好一点:
I mean the reverse way of the example described above, here is waht I got so war, maybe this explains my idea a little better:
void ZendParams::setRequired( int &Var )
{
// save every var pointer to a stack
// and save the type with it. (if needed, does not seems to be)
// after call to ZendParams::fetch()
// the values are stored in the passed variables
this->_params.push_back( (void*) &Var );
this->_paramStr.append( 'i' );
}
size_t ZendParams::fetch()
{
if ( zend_parse_parameters(
ZEND_NUM_ARGS() TSRMLS_CC, ,
this->_paramStr.c_str(),
...
) !== FAILURE)
{
}
return 0;
}
所以我想拨打电话来动态zend_parse_parameters。
So I want to make the call to zend_parse_parameters dynamically.
基本思路是增加指针列表中动态地VAR和它们传递给了Zend函数(参考)。
The base idea is to add pointer to vars in a list and pass them to the zend function (as reference) dynamically.
我想一定有一种用va_list的做到这一点了。
I thought there must be a way to do this with va_list , too.
但如何?
要让它更简单,我用这个例子:
To get it more simple, I am using this example:
list<int> myList;
myList.push_back(1);
myList.push_back(5);
myList.push_back(10);
myList.push_back(37);
myList.push_back(42);
double function avg( int num, ... )
va_list arguments;
int sum = 0;
va_start ( arguments, num );
for ( int x = 0; x < num; x++ )
{
sum += va_arg ( arguments, int );
}
va_end ( arguments );
return sum / num;
}
我要打电话平均
与我在列表中得到了所有的数字。我从上面提到的教程中的例子功能,但它应该显示我的意思在一个非常简单的方法。
但是,我不能改变调用该函数,因为它是Zend框架的一部分。
I want to call avg
with all numbers I got in the list. I took the example function from the tutorial mentioned above, but it should show up what I mean in a very simple way.
However, I can not change the function called, as it is part of the zend framework.
有没有用C或C ++没有办法做到这一点?
Is there any way in C or C++ to do this?
我的第2个方法:
template <typename... Arguments>
size_t Zend::getMethodParams( string paramStr, Arguments... Args )
{
if ( zend_parse_parameters(
ZEND_NUM_ARGS() TSRMLS_CC, ,
paramStr.c_str(),
Args...
) == FAILURE)
{
return 0;
}
}
要这样调用(获取定义3参数):
To be called like this (to get the defined 3 Parameters):
string My1stParam;
int My2ndParam;
int My3rdParam;
Zend::getMethodParams<string, int, int>( "sii", &My1stParam, &My2ndParam, &My3rdParam );
我觉得应该工作,但是有一个问题,很难与:
该zend_parse_parameters函数返回值2为一个字符串(C风格的字符串!):
- 一个char指针和
- 字符串长度
I think that should work, but there is one hard issue with that: The zend_parse_parameters function returns 2 values for a string (c-style string!): - a char pointer and - the string length.
所以,我要么必须调用这种方式:
So I would either have to call it that way:
char* My1stParam_str;
int My1stParam_length
int My2ndParam;
int My3rdParam;
Zend::getMethodParams<string, int, int>( "sii", &My1stParam_str, &My1stParam_length, &My2ndParam, &My3rdParam );
string My1stParam;
My1stParam.assign(My1stParam_str, My1stParam_length);
无论如何,这是我想prevent。
Anyway, that was what I wanted to prevent.
或者,我将不得不修改传递给zend_parse_parameters参数列表功能在内部做这些额外的步骤。
Or I would have to modify the list of arguments passed to the zend_parse_parameters function to do these additional steps internally.
我希望能够以这种方式,至少把它叫做:
I am hoping to be able to call it at least in that way:
string My1stParam;
int My2ndParam;
int My3rdParam;
Zend::getMethodParams<string, int, int>( "sii", &My1stParam, &My2ndParam, &My3rdParam );
所以很明显这样说:这些参数在编译时已知,但函数调用将在更高版本的源$ C $ C所有出现在非常不同的
推荐答案
我发现了Zend框架内解决的办法。之前我确实寻找这样一个解决方案,但它似乎没有很好的记录(N.M.已经mentoined,有喜欢的va_list服用功能无Zend公司内部的方式)。
I found a way around this within the zend framework. I did indeed search for such a solution before, but it seems to be not very good documented ( n.m. already mentoined, that there is no zend internal way like a va_list taking function ).
但确实有一个!
对于其他人这个问题stucking:
For everyone else stucking with this problem:
long arg;
zval ***args;
int i, argc = ZEND_NUM_ARGS( );
if (zend_parse_parameters(1 TSRMLS_CC, "l", &arg) == FAILURE) return;
array_init(return_value);
add_index_long(return_value, 0, arg);
if(argc>1) {
args = (zval ***)emalloc(argc * sizeof(zval **));
if(zend_get_parameters_array_ex(argc, args) == FAILURE) {
efree(args);
return;
}
for(i = 1; i < argc; i++) {
zval_add_ref(args[i]);
add_index_zval(return_value,i, *args[i]);
}
efree(args);
}
即溶液)
这个片段中,在 http://docstore.mik.ua/orelly发现/webprog/php/ch14_07.htm 动态解析所有的参数到一个PHP数组的c重新presentation。
This snippet, found on http://docstore.mik.ua/orelly/webprog/php/ch14_07.htm parses all parameters dynamically into the c representation of an PHP array.
这篇关于动态函数调用在运行时(va_list的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!