S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

pass variable-argument list

Posted on 2013-05-18 10:45 S.l.e!ep.¢% 阅读(654) 评论(0)  编辑 收藏 引用 所属分类: C++

Introduction

I've been looking for solution of passing variable-argument list from my va-function to another one, like TRACE for example. All solutions I saw were about using special functions that take va_list as argument. But this is a un-straight way. Why couldn't I just pass "..." to next function? C++ syntax doesn't allow this. But C++ allows to extend itself. Let me introduce you new macros from va_ set:

template<byte count>
struct SVaPassNext{
    SVaPassNext<count-1> big;
    DWORD dw;
};
template<> struct SVaPassNext<0>{};
//SVaPassNext - is generator of structure of any size at compile time.class CVaPassNext{
public:
    SVaPassNext<50> svapassnext;
    CVaPassNext(va_list & args){
		try{//to avoid access violation
			memcpy(&svapassnext, args, sizeof(svapassnext));
		} catch (...) {}
    }
};
#define va_pass(valist) CVaPassNext(valist).svapassnext#if 0 //using:void MyVA_Function(szFormat, ...){
    va_list args;
    va_start(args, szFormat);
    SomeOtherVA_Function(szFormat, va_pass(args));
    va_end(args);
}
#endif
how this works:
I just copy 50 * sizeof(DWORD) bytes of stack to my struct of this size and simply pass this struct as ... argument to next function. Compiler just copies my copy of local stack to next function stack. And that's all we need.

Enjoy!

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理