// Compile-time error if trying to call without specialization.
template<typename>
const auto fieldsToParametersMap;
// default mapping
template<typename T, typename Field>
std::function<void(QJsonValue from, T* to)> getFieldMappingFunction(
const std::tuple<Field T::*, QString>& fieldToParameter)
{
return
[&](QJsonValue from, T* to)
{
const auto& [fieldPtr, _] = fieldToParameter;
auto& field = std::mem_fn(fieldPtr)(to);
QJson::deserialize(from, &field);
};
}
template <typename T, typename Field, typename Callable>
std::function<void(QJsonValue from, T* to)> getFieldMappingFunction(
const std::tuple<Field T::*, QString, Callable>& fieldToParameter)
{
return std::get<Callable>(fieldToParameter);
}
template<>
const auto fieldsToParametersMap<DeviceModelV3Base> = std::tuple{
/*Field, Key, (opt) Mapping Function*/
std::tuple{&DeviceModelV3Base::compatibleAnalyticsEngineIds,
device_properties::kCompatibleAnalyticsEnginesProperty},
};
template<>
const auto fieldsToParametersMap<DeviceModelV4> = std::tuple{
/*Field, Key, (opt) Mapping Function*/
std::tuple{&DeviceModelV4::dts, device_properties::kDts},
std::tuple{&DeviceModelV4::hasRtspSettings, device_properties::kHasRtspSettings},
std::tuple{&DeviceModelV4::ioConfigCapability, device_properties::kIoConfigCapability},
std::tuple{&DeviceModelV4::ioSettings, device_properties::kIoSettings},
};
template<typename T>
void extractParametersToFields(std::map<QString, QJsonValue>* fromParameters, T* toStruct)
{
const auto extractParameter =
[&fromParameters = *fromParameters, toStruct](const auto& fieldToParameter)
{
const auto& key = std::get<QString>(fieldToParameter);
if (auto extracted = fromParameters.extract(key))
getFieldMappingFunction(fieldToParameter)(std::move(extracted.mapped()), toStruct);
};
std::apply(
[extractParameter](const auto&... fieldToParam)
{
(extractParameter(fieldToParam), ...);
},
fieldsToParametersMap<std::decay_t<T>>);
}
// Compile-time error if trying to call without specialization.
template<typename>
const auto fieldsToParametersMap;
// default mapping
template<typename T, typename Field>
std::function<void(QJsonValue from, T* to)> getFieldMappingFunction(
const std::tuple<Field T::*, QString>& fieldToParameter)
{
return
[&](QJsonValue from, T* to)
{
const auto& [fieldPtr, _] = fieldToParameter;
auto& field = std::mem_fn(fieldPtr)(to);
QJson::deserialize(from, &field);
};
}
template <typename T, typename Field, typename Callable>
std::function<void(QJsonValue from, T* to)> getFieldMappingFunction(
const std::tuple<Field T::*, QString, Callable>& fieldToParameter)
{
return std::get<Callable>(fieldToParameter);
}
template<>
const auto fieldsToParametersMap<DeviceModelV3Base> = std::make_tuple(
/*Field, Key, (opt) Mapping Function*/
std::make_tuple(&DeviceModelV3Base::compatibleAnalyticsEngineIds,
device_properties::kCompatibleAnalyticsEnginesProperty)
);
template<>
const auto fieldsToParametersMap<DeviceModelV4> = std::make_tuple(
/*Field, Key, (opt) Mapping Function*/
std::make_tuple(&DeviceModelV4::dts, device_properties::kDts),
std::make_tuple(&DeviceModelV4::hasRtspSettings, device_properties::kHasRtspSettings),
std::make_tuple(&DeviceModelV4::ioConfigCapability, device_properties::kIoConfigCapability),
std::make_tuple(&DeviceModelV4::ioSettings, device_properties::kIoSettings)
);
template<typename T>
void extractParametersToFields(std::map<QString, QJsonValue>* fromParameters, T* toStruct)
{
const auto extractParameter =
[&fromParameters = *fromParameters, toStruct](const auto& fieldToParameter)
{
const auto& key = std::get<QString>(fieldToParameter);
if (auto extracted = fromParameters.extract(key))
getFieldMappingFunction(fieldToParameter)(std::move(extracted.mapped()), toStruct);
};
std::apply(
[extractParameter](const auto&... fieldToParam)
{
(extractParameter(fieldToParam), ...);
},
fieldsToParametersMap<std::decay_t<T>>);
}