cmvr-es/cmvr-es/devices/touch/touch_factory.h

48 lines
1.6 KiB
C++

#ifndef CMVR_ES_TOUCH_FACTORY_H
#define CMVR_ES_TOUCH_FACTORY_H
#include <memory>
#include "cmvr/config/touch_config/touch_config.pb.h"
#include "devices/touch/px_6ax_gen3/include/px_6ax_gen3.h"
#include "devices/touch/px_6ax_genx_can/include/px_6ax_genx_can.h"
#include "devices/touch/zero_sim_touch/include/zero_sim_touch.h"
namespace cmvr::device {
class TouchFactory {
public:
static std::shared_ptr<AbstractTouch> create(const config::TouchDeviceConfig& cfg) {
if (cfg.id().empty()) {
CMVR_LOG(ERROR) << "[TouchFactory] Touch id is empty";
return nullptr;
}
switch (cfg.backend_case()) {
case config::TouchDeviceConfig::kPx6AxGen3:
return createBackend_<PX6AXGen3>(cfg.id(), cfg.px_6ax_gen3());
case config::TouchDeviceConfig::kZeroSim:
return createBackend_<ZeroSimTouch>(cfg.id(), cfg.zero_sim());
case config::TouchDeviceConfig::kPx6AxGenxCan:
return createBackend_<PX6AXGenXCan>(cfg.id(), cfg.px_6ax_genx_can());
default:
CMVR_LOG(ERROR) << "[TouchFactory] Touch backend is not configured: " << cfg.id();
return nullptr;
}
}
private:
template <typename Device, typename Config>
static std::shared_ptr<AbstractTouch> createBackend_(const std::string& id, Config cfg) {
if (!cfg.id().empty() && cfg.id() != id) {
CMVR_LOG(ERROR) << "[TouchFactory] Touch id does not match backend id: " << id;
return nullptr;
}
cfg.set_id(id);
return std::make_shared<Device>(cfg);
}
};
} // namespace cmvr::device
#endif // CMVR_ES_TOUCH_FACTORY_H