这种C++技法最先学自ATL的架构,而后却总是记不住,备之.
class DmGameCamera;
struct ISceneNode
{
virtual void Release() = 0;
virtual void Render() = 0;
virtual bool IsVisible() = 0;
virtual bool RayPickup(const vec3_type& vBegin, const vec3_type& vDir) = 0;
virtual vec3_type getPositionToParent() = 0;
virtual void UpdateFrame(uint32 time, DmGameCamera& camera) = 0;
virtual void AddChildSceneNode(ISceneNode& node) = 0;
};
struct IModelSceneNode : public ISceneNode
{
virtual void MakeVisible(bool bVisible) = 0;
};
template<class T, class I = ISceneNode>
struct ISceneNodeImpl : public I
{
void UpdateFrame(uint32 time, DmGameCamera& camera)
{
}
bool RayPickup(const vec3_type& vBegin, const vec3_type& vDir)
{
return false;
}
vec3_type getPositionToParent()
{
return vec3_type(0, 0, 0);
}
void AddChildSceneNode(ISceneNode& node)
{
}
bool IsVisible()
{
return true;
}
void Render()
{
}
void Release()
{
T* pThis = static_cast<T*>(this);
delete pThis;
}
};
template<class T>
struct IModelSceneNodeImpl : public ISceneNodeImpl<T, IModelSceneNode>
{
IModelSceneNodeImpl()
{
m_bOutOfCamera = true;
}
void MakeVisible(bool bVisible)
{
m_bOutOfCamera = !bVisible;
}
bool m_bOutOfCamera;
}; 关键在于将高级接口作为模板参数下传至基本接口的实现类模板.