boost::any、变体、基于它们的数组调用函数
给定一组函数,例如: <代码>
template<class A1>
Void Go(A1 a);
模板
无效 Go(A1 a1, A2 a2);
模板
无效 Go(A1 a1, A2 a2, A3 a3);
Is it possible to take an array of some variant type and given its contents, fire the correct function? My application for this is that I want to pass a set of parameters X, to another process, where I only have the option of passing a single pointer. My idea was to send a pointer to a std::vector<boost::any> and then to somehow work out which of the above methods to fire given its contents.这涉及我对跨线程事件和通信的实验,因此它可能看起来不必要地深奥!
编辑:好的,例如,这就是意图。显然它无法编译(模板解析发生在编译时,但我想确定在运行时调用哪个函数!):
#include <boost\any.hpp>
#include <vector>
#include <iostream>
#include <string>
class A
{
public:
void Go()
{
std::cout << L"(0 params)\n";
}
template
void Go(U0 u0)
{
std::cout << L"1 param " << u0 << L"\n";
}
template
void Go(U0 u0, U1 u1)
{
std::cout << L"2 params " << u0 << L" " << u1 << L"\n";
}
template
void Go(U0 u0, U1 u1, U2 u2)
{
std::cout << L"3 params " << u0 << L" " << u1 << L" " << u2 << L"\n";
}
};
class B
{
public:
void Whatever() {}
};
int main(int argc, wchar_t* argv[])
{
// Create a collection of variants.
std::vector<boost::any> myVariants;
B myB;
myVariants.push_back(123);
myVariants.push_back(std::wstring(L"Test"));
myVariants.push_back(&myB);
// Take a void pointer to them.
void *variants = &myVariants;
// Convert back into an array.
std::vector<boost::any>& myConverted = *(std::vector<boost::any> *)(variants);
// Fire the correct event on A.
A myA;
switch(myConverted.size())
{
case 0:
myA.Go();
break;
case 1:
myA.Go(myConverted[0]);
break;
case 2:
myA.Go(myConverted[0], myConverted[1]);
break;
case 3:
myA.Go(myConverted[0], myConverted[1], myConverted[2]);
break;
default: ;
// throw
}
}
Given a set of functions, such as:
template<class A1> Void Go(A1 a);template<class A1, class A2> Void Go(A1 a1, A2 a2);
template<class A1, class A2, class A3> Void Go(A1 a1, A2 a2, A3 a3);
Is it possible to take an array of some variant type and given its contents, fire the correct function? My application for this is that I want to pass a set of parameters X, to another process, where I only have the option of passing a single pointer. My idea was to send a pointer to a std::vector<boost::any> and then to somehow work out which of the above methods to fire given its contents.
This concerns my experiments with cross-thread eventing and communication, hence it may seem unnecessarily esoteric!
Edit: ok, for example, this is the intention. Obviously it doesn't compile (the template resolution occurs at compile-time, but I want to determine which function to call at run-time!):
#include <boost\any.hpp>
#include <vector>
#include <iostream>
#include <string>
class A
{
public:
void Go()
{
std::cout << L"(0 params)\n";
}
template
void Go(U0 u0)
{
std::cout << L"1 param " << u0 << L"\n";
}
template
void Go(U0 u0, U1 u1)
{
std::cout << L"2 params " << u0 << L" " << u1 << L"\n";
}
template
void Go(U0 u0, U1 u1, U2 u2)
{
std::cout << L"3 params " << u0 << L" " << u1 << L" " << u2 << L"\n";
}
};
class B
{
public:
void Whatever() {}
};
int main(int argc, wchar_t* argv[])
{
// Create a collection of variants.
std::vector<boost::any> myVariants;
B myB;
myVariants.push_back(123);
myVariants.push_back(std::wstring(L"Test"));
myVariants.push_back(&myB);
// Take a void pointer to them.
void *variants = &myVariants;
// Convert back into an array.
std::vector<boost::any>& myConverted = *(std::vector<boost::any> *)(variants);
// Fire the correct event on A.
A myA;
switch(myConverted.size())
{
case 0:
myA.Go();
break;
case 1:
myA.Go(myConverted[0]);
break;
case 2:
myA.Go(myConverted[0], myConverted[1]);
break;
case 3:
myA.Go(myConverted[0], myConverted[1], myConverted[2]);
break;
default: ;
// throw
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,boost::variant 知道它当前存储的值类型。它允许访问并调用正确的重载
operator()
。boost::any
使用一种根本不同的技术,无法告诉您它当前存储的内容。Yes,
boost::variant
knows the value type that it currently stores. It allows visitation and calls the correct overloadedoperator()
.boost::any
uses a fundamental different technique and can't tell you what it currently stores.好的,我在这方面取得了一些进展。如果我使用 boost::any 数组,我可以在 void * 之间进行转换(因此将其作为自定义窗口消息中的 lParam 传递给 msgProc)。解决方案是发送者类和接收者类都具有相同的模板参数。也就是说,类似这样的东西(应该在 2010 年编译为控制台项目):
上面演示了从 boost::any 向量到 void * 然后回到 boost::any 向量,调用函数在具有正确数量和类型的某些对象上。
Ok, I made some progress with this. If I use an array of boost::any, I can convert to and from a void * (and hence pass it as an lParam in a custom window message to a msgProc). The solution is if both sender and receiver classes have the same template parameters. That is to say, something like this (should compile as a console project in 2010):
The above demonstrates going from a vector of boost::any to a void * and then back to a vector of boost::any, calling a function on some object with the correct arity and types.