[英]How to find, from which Types is object composed of?
ok, yesterday I posted almost identical question here , but I wasn't able to modify the answer(working) to my needs... I did not want to mess the other topic, so I have started new one.好的,昨天我在这里发布了几乎相同的问题,但我无法根据我的需要修改答案(工作)......我不想弄乱另一个话题,所以我开始了新的话题。
So, I have 2 (actually about 15) structs, which can composed an object所以,我有 2 个(实际上大约 15 个)结构,它们可以组成一个 object
class MyBase{};
template <typename Super, typename T1, typename T2>
struct A : public Super
{
void doStuffA() { cout<<"doing something in A"; }
};
template <typename Super, typename T1, typename T2>
struct B : public Super
{
void doStuffB() { cout<<"doing something in B"; }
};
then I have:然后我有:
template <typename ComposedType, typename T1, typename T2>
class Combined
{
ComposedType m_cT;
public:
Combined(const ComposedType & c) : m_cT(c) { }
typedef A<null, T1, T2> anull;
typedef B<null, T1, T2> bnull;
void update()
{
typedef typename split<ComposedType>::Ct Ct;
typedef typename split<ComposedType>::At At;
//this I want
if( composed of A )
m_cT.doStuffA();
if( composed of B )
m_cT.doStuffB();
}
};
and I want to use it like:我想像这样使用它:
int main()
{
typedef A<B<MyBase,int,int>,int,int> ComposedType1;
typedef B<MyBase,int,int> ComposedType2;
ComposedType1 ct1;
ComposedType2 ct2;
Combined<ComposedType1, int, int> cb1(ct1);
cb1.update();
Combined<ComposedType2, int, int> cb2(ct2);
cb2.update();
}
(ints are just for example purposes) (整数仅用于示例目的)
So I have some template magic:所以我有一些模板魔法:
struct null{};
template<typename>
struct split
{
typedef null Ct;
typedef null At;
};
template<template<typename> class C, typename T>
struct split<C<T> >
{
typedef C<null> Ct; //class template
typedef T At; //argument type
};
template<template<typename> class C>
struct split<C<MyBase> >
{
typedef C<null> Ct; //class template
typedef MyBase At; //argument type
};
but I can not make it works:(但我不能让它工作:(
I know there is a lot of code, but this is actually minimal example... I have posted this code to ideone , to make it better for reading.我知道有很多代码,但这实际上是最小的示例...我已将此代码发布到ideone ,以使其更好地阅读。
Thank you!谢谢!
EDIT: (to ask questions in comments)编辑:(在评论中提问)
I am building system for AI and want to solve as much thing in compile time as I can.我正在为 AI 构建系统,并希望在编译时尽可能多地解决问题。 In this case, I am building system for movement behavior.
在这种情况下,我正在构建运动行为系统。 My code supply many types of behavior like "Go to point", "Evade from", "Avoid obstacles" etc. This behaviors are in example above mentioned as A a, B. Each of this behavior has method like "performBehavior" and its return type can be combined with other "performBehavior".
我的代码提供了许多类型的行为,如“前往点”、“避开”、“避开障碍物”等。这些行为在上面的示例中被称为 A a、B。每个行为都有类似“performBehavior”的方法及其返回类型可以与其他“performBehavior”组合。
So I want to put together specific behavior at compile time.所以我想在编译时把特定的行为放在一起。 eg.
例如。 just A or A+C+D+F etc...
只是 A 或 A+C+D+F 等...
and then in my update do something like:然后在我的更新中执行以下操作:
if behavior is consisted of "Go to point", than "performBehaviorGoTo"如果行为由“Go to point”组成,则比“performBehaviorGoTo”
if behavior is consisted of "Evade from", than "performBehaviorEvade"如果行为由“Evade from”组成,则比“performBehaviorEvade”
... ...
this is very very short explanation, but hope I have made my point这是非常简短的解释,但希望我已经表达了我的观点
You can do that with function overloading:您可以通过 function 重载来做到这一点:
template <typename Super, typename T1, typename T2>
void doStuff(A<Super, T1, T2>& a) { a.doStaffA(); }
template <typename Super, typename T1, typename T2>
void doStuff(B<Super, T1, T2>& b) { b.doStaffB(); }
And then:接着:
// ...
void update()
{
//this I want
//if( composed of A )
// m_cT.doStuffA();
//if( composed of B )
// m_cT.doStuffB();
doStuff(m_cT);
}
It is not clear, whether you want to chain calls for A<B<...> >
.目前尚不清楚,是否要链接
A<B<...> >
调用。 If you do, then something like the following would do:如果你这样做,那么类似以下的事情会做:
template <class T>
void doStuff(T&) { /* do nothing */ }
template <typename Super, typename T1, typename T2>
void doStuff(A<Super, T1, T2>& a) {
a.doStaffA();
doStuff(static_cast<Super&>(a));
}
template <typename Super, typename T1, typename T2>
void doStuff(B<Super, T1, T2>& b) {
b.doStaffB();
doStuff(static_cast<Super&>(b));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.