I am trying to manage devices on an embedded device. The devices are managed by the device manager. Its responsibility is to ensure the lifetime of the devices and supply the other parts of the code with devices.
A device has certain methods (init, check status, etc.) For simplicity, I only added IsReady(). In order to make the code more flexible, I like to use interfaces. For example, I have an IO expander that has UART support. So this device implements IDevice buit also the IStream interface.
I can ask for all devices that are compatible with IStream. Since I don’t want to rely on RTTI, I use keys for this. (In the example these are hardcoded) I would prefer if this wasn’t required, but without RTTI I don’t see that happen.
The problem arises when I try to get devices with a certain interface. Since this interface itself doenst inherit from IDevice, I don’t have access to the IDevice methods.
Here is the stripped down version of what I try to achieve:
class IDevice {
public:
virtual bool IsReady() = 0;
};
class IStream {
public:
virtual size_t ReadStream(uint8_t* buffer, size_t bufferSize) = 0;
};
class DeviceManager {
public:
template<typename T>
std::vector<std::shared_ptr<T>> GetDevicesByCompatibility(const std::string& compatibility);
};
class MAX14830_Uart : public IDevice, public IStream {
public:
bool IsReady() override;
size_t ReadStream(uint8_t* buffer, size_t bufferSize) override;
};
void Test()
{
DeviceManager deviceManager; // Not included in example, but the devices are instantiated and registered to the device manager
auto devices = deviceManager.GetDevicesByCompatibility<IStream>("IStream");
for (auto& device: devices)
{
// Here is the problem, I want to check if the device is ready, but my device is of type IStream, not IDevice
if (device->IsReady())
{
}
}
}