2

Out of curiosity, I thought I'd try and write a basic C++ class that mimics C#'s multiple delegate pattern. The code below mostly does the job, with the nasty sacrifice of losing almost all type-safety, but having to use the initial dummy parameter to set up the va_list really seems a bit off. Is there a way to use va_list without this? I do realize there are ways to do this with (for example) boost, but I was aiming for something dead simple that used just the standard library.

#include <vector>
#include <iostream>
#include <string>
#include <stdarg.h>
#include <algorithm>

using namespace std;

class CDelegate
{
public:
 virtual bool operator()(va_list params) = 0;
};

class CMultipleDelegateCaller
{
public:
 typedef vector<CDelegate*> CDelegateVector;

 CMultipleDelegateCaller& operator+=(CDelegate &rDelegate)
 {
  m_apDelegates.push_back(&rDelegate);
  return (*this);
 }

 CMultipleDelegateCaller& operator-=(CDelegate &rDelegate)
 {
  CDelegateVector::iterator iter = 
   find(m_apDelegates.begin(), m_apDelegates.end(), &rDelegate);
   if (m_apDelegates.end() != iter) m_apDelegates.erase(iter);
  return (*this);
 }

 bool Call(int iDummy, ...)
 {
  va_list params;
  CDelegate* pDelegate;
  CDelegateVector::iterator iter;
  for (iter = m_apDelegates.begin(); iter != m_apDelegates.end(); ++iter)
  {
   pDelegate = *iter;
   va_start(params, iDummy);
   if (!(*pDelegate)(params)) return false;
   va_end(params);
  }
  return true;
 }

private:
 CDelegateVector m_apDelegates;
};

class CTestDelegate:
 public CDelegate
{
public:
 CTestDelegate():m_iId(++s_iCount) {}
 virtual bool operator()(va_list params)
 {
  int iIntParam = va_arg(params, int);
    char* szCharPtrParam = va_arg(params, char*);
   string* psStringParam = va_arg(params, string*);
  cout<<m_iId<<"{"
   <<iIntParam<<", "
   <<szCharPtrParam<<", "
   <<*psStringParam<<"}"<<endl;

  return true;
 }
 int m_iId;
 static int s_iCount;
};
int CTestDelegate::s_iCount = 0;

int main(int argc, char* argv[])
{
 CMultipleDelegateCaller cDelegateCaller;
 CTestDelegate cTestDelegate1;
 CTestDelegate cTestDelegate2;

 cout<<"--------------------"<<endl;
 cDelegateCaller += cTestDelegate1;
 cDelegateCaller += cTestDelegate2;
 string sString("World");
 cDelegateCaller.Call(1, 2, "Hello", &sString);
 cout<<"--------------------"<<endl;
 cDelegateCaller -= cTestDelegate1;
 cDelegateCaller.Call(1, 2, "Hello", &sString);
 cout<<"--------------------"<<endl;
 cDelegateCaller -= cTestDelegate2;
 cDelegateCaller.Call(1, 2, "Hello", &sString);
 cout<<"--------------------"<<endl;

 cin>>sString;
 return 0;
}
4

2 回答 2

4

C++ 中带有省略号的函数只是为了与 C 兼容。使用 C++,我会在Call函数中返回临时帮助对象并添加模板operator%以传递可变数量的参数。通过以下方式使用它:

cDelegateCaller.Call() % 2 % "Hello" % sString; // dummy argument isn't required

至于您的问题,标准要求va_start在访问未命名参数之前调用。并且va_start需要第二个参数,它是函数定义中可变参数列表中最右边参数的标识符。

于 2009-09-17T06:52:20.007 回答
0

根据基里尔的回答,您可以得出结论,可以使用模板参数组合函数创建类型安全的委托。这个函数需要一个虚拟的起点,但有类型安全的好处。

FastFormat 库使用这个,boost 使用这个,我曾经在另一个问题的答案中提供了另一个示例。

于 2009-09-17T07:25:35.963 回答