I have a situation that occurs 5 times in my code, and continues to grow. Basically I have the same switch statement structure every time I need to perform an operation that involves my matrix and its type (which is set dynamically at runtime from files and user input). Here is one such example:
//QTableWidget m_matrix_table;
//cv::Mat m_matrix;
MatrixBoxWidget::MatrixBoxWidget(QWidget *parent) : QWidget(
parent) {
...
connect(m_matrix_table, &QTableWidget::cellChanged, this,
&MatrixBoxWidget::updateMatrixValue);
...
}
void MatrixBoxWidget::updateMatrixValue(int row, int column) {
QTableWidgetItem item = m_matrix_table->item(row, column);
QVariant qvariant = item->data(0);
int type = m_matrix.type();
switch (type) {
case CV_8U:
m_matrix.at<std::uint8_t>(row, column)
= qvariant.value<std::uint8_t>();
break;
case CV_8S:
m_matrix.at<std::int8_t>(row, column)
= qvariant.value<std::int8_t>();
break;
case CV_16U:
m_matrix.at<std::uint16_t>(row, column)
= qvariant.value<std::uint16_t>();
break;
case CV_16S:
m_matrix.at<std::int16_t>(row, column)
= qvariant.value<std::int16_t>();
break;
case CV_32S:
m_matrix.at<std::int32_t>(row, column)
= qvariant.value<std::int32_t>();
break;
case CV_32F:
m_matrix.at<float>(row, column)
= qvariant.value<float>();
break;
case CV_64F:
m_matrix.at<double>(row, column)
= qvariant.value<double>();
break;
default:
assert((false, "unknown type format"));
break;
}
}
usually the function signatures (or what ever else I decide to do in the switch cases) are homogenous except for the types in most circumstances, I'm only aiming to fix these cases.
Opencv uses these integers to change the type of a matrix. This is what I'm switching on. What can I do to mitigate this? Or is this kind of boiler plate necessary? Template functions that take another function don't appear to work since they would require all the templates versions of the function anyway, which doesn't get rid of the boiler plate code.
EDIT:
The marked duplicate has actually zero to do with my question. There's not a single template mentioned in either answer, and neither solves my problem.
This is what I'd like to do:
foo<T>(int type, std::function bar)
where bar would be templated inside of foo, but that is clearly not possible with current C++ semantics. the problem isn't that I'm using switch statements, its that the pattern is homogeneous across multiple use cases with the same cases.
foo<T>(type, bar)
where bar is templated inside of foo.at<T>
)int main() { auto type = getType(); switch (type) { case CV_8U: new_main<uint_8t>(); ... } };