如何查找,对象由哪些类型组成? [英] 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.
所以,我有 2 个(实际上大约 15 个)结构,它们可以组成一个对象
So, I have 2 (actually about 15) structs, which can composed an 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"; }
};
然后我有:
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();
}
};
我想像这样使用它:
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
};
但我不能让它工作:(
我知道有很多代码,但这实际上是最小的例子...我已将此代码发布到 ideone,更好地阅读.
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.
谢谢!
(在评论中提问)
我正在为 AI 构建系统并希望在编译中解决尽可能多的问题时间尽可能.在这种情况下,我正在构建运动行为系统.我的代码提供了许多类型的行为,例如转到点"、逃避"、避开障碍物"等.这种行为在上面的例子中被称为 A a, B. 每个行为都有类似performBehavior"的方法并且它的返回类型可以与其他performBehavior"结合使用.
I am building system for AI and want to solve as much thing in compile time as I can. 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+C+D+F 等等...
So I want to put together specific behavior at compile time. eg. just A or A+C+D+F etc...
然后在我的更新中执行以下操作:
and then in my update do something like:
如果行为由转到点"组成,则performBehaviorGoTo"
if behavior is consisted of "Go to point", than "performBehaviorGoTo"
如果行为由逃避"组成,则performBehaviorEvade"
if behavior is consisted of "Evade from", than "performBehaviorEvade"
...
这是非常非常简短的解释,但希望我已经表达了我的观点
this is very very short explanation, but hope I have made my point
推荐答案
你可以通过函数重载来做到这一点:
You can do that with function overloading:
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(); }
然后:
// ...
void update()
{
//this I want
//if( composed of A )
// m_cT.doStuffA();
//if( composed of B )
// m_cT.doStuffB();
doStuff(m_cT);
}
目前还不清楚,是否要对 A<B<...> 的调用进行链式调用.>代码>.如果你这样做,那么类似下面的事情会做:
It is not clear, whether you want to chain calls for 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));
}
这篇关于如何查找,对象由哪些类型组成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!