I've been planning on writing an API abstraction layer for my rendering engine. The two APIs I want to include are D3D11 and D3D12. So I started by writing some interfaces and their respective implementation for each API.
The following code snippet examplifies this:
class IDevice
{
//... (pure) virtual methods
};
class CD3D11Device : public IDevice
{
//... virtual method implementations
};
class CD3D12Device : public IDevice
{
//... virtual method implementations
};
So far so good. Now to the actual problem:
If I have another interface with a method that requires a IDevice*
as parameter, how can I ensure that the "right" device is passed?
class ISomeClass
{
public:
virtual void foo(IDevice* pDev) = 0;
};
class CD3D11SomeClass : public ISomeClass
{
public:
virtual void foo(IDevice* pDev) override
{
// should only be passed CD3D11Device
}
};
class CD3D12SomeClass : public ISomeClass
{
public:
virtual void foo(IDevice* pDev) override
{
// should only be passed CD3D12Device
}
};
I know that I could call dynamic_cast
on the IDevice*
pointers every time and check for nullptr
but that is tedious as well as expensive in respect to performance.
Is there some elegant solution to this problem? Does anybody of you guys know how professional/commercial game engines cope with that?