Devices and Factories

4 views
Skip to first unread message

Harald Scheirich

unread,
May 18, 2015, 10:19:21 AM5/18/15
to openSurgSim
There were two smaller merge requests that where accepted in the last two weeks that move the device hierarchy to a more dynamic runtime oriented access. Currently when you need to find out if a device is built and fall back to other devices if it isnt, there is really no good solution except to construct a series of #ifdef checks on the cmake variables, this works reasonably well but is not very flexible and does not lend itself to reusability. For example a new series of checks has to be constructed for each new device chain that needs to be implemented. 

First to make including devices easier we added a common Devices/Devices.h file, that includes all the devices that are currently being built. This removes the need to create #ifdef constructs for the device inlcudes (yes at the cost of including all the devices). 

Second we added a factory class to InterfaceDevice some devices already register themselves here, this enables a dynamic query and construction of the devices without the need to resort to strict types. We can now write a generic device factory like this 

std::array<std::string, 2> devices =
{
"SurgSim::Device::OculusDevice",
"SurgSim::Device::IdentityPoseDevice"
};

std::shared_ptr<SurgSim::Input::DeviceInterface> device;
for (auto deviceName : devices)
{
try
{
device = SurgSim::Input::DeviceInterface::getFactory().create(deviceName, "Tracker");
}
catch (std::exception e)
{
continue;
}
if (device->initialize())
{
break;
}
else
{
device = nullptr;
}
}
SURGSIM_ASSERT(device != nullptr) << "Could not initialize any kind of tracking device.";

As next steps we will be adding all other devices to the factory, and enabling untyped access (deriving from Accessible) on them, this would make the device configuration completely dynamic and independent of the static types.

Harry 


--
Harald Scheirich
Principal Software Engineer
Simquest Solutions Inc. 
Reply all
Reply to author
Forward
0 new messages