Vol*_*kan 8 c++ macros templates design-patterns proxy-classes
假设我有一个界面
class I{
public:
virtual void f(int id)=0;
virtual void g(int id, float x)=0;
}
Run Code Online (Sandbox Code Playgroud)
我需要一个代理类,为指针映射做一些id
class Proxy : I
{
I * i[5];
public:
void f(int id)
{
i[id]->f(id);
}
void g(int id, float x)
{
i[id]->g(id, x);
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我写作
Proxy *p;
p->f(1);
Run Code Online (Sandbox Code Playgroud)
在id = 1的对象上调用f
有几个这样的情况和接口相当大.所以我不想编写代理类中的所有函数.有没有办法自动完成?也许使用宏,模板,重载" - >"等.
简单的解决方案是定义一个operator->,它返回指向接口的指针.但这会打破你的封装,因为每个人都可以直接访问你的对象而你实际上不需要你的代理类(你可能只需要使用std :: map).
替代你可以做点什么
template <typename Interface>
class Proxy
{
Interface* interfaces[5];
public:
template <typename F, typename... Params>
auto operator()(F f, const int id, Params... parameters)
-> decltype((interfaces[id]->*f)(id, parameters...))
{ return (interfaces[id]->*f)(id, parameters...); }
};
Run Code Online (Sandbox Code Playgroud)
它严重依赖于C++ 11的功能,因此可能无法使用编译器进行编译.
首先,它使用Variadic模板.有关详细信息,请参阅https://en.wikipedia.org/wiki/Variadic_Templates.
接下来它使用decl_type.有关更多信息,请参阅https://en.wikipedia.org/wiki/Decltype.
你必须像这样使用它:
Proxy<I> p;
...
p(&I::f,1);
p(&I::g,3, 1.);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1030 次 |
| 最近记录: |