Qt: Add clamp function, using stl if available
Vicki Pfau vi@endrift.com
Sat, 16 Jan 2021 00:47:41 -0800
4 files changed,
17 insertions(+),
7 deletions(-)
M
src/platform/qt/InputController.cpp
→
src/platform/qt/InputController.cpp
@@ -10,6 +10,7 @@ #include "GamepadAxisEvent.h"
#include "GamepadButtonEvent.h" #include "InputProfile.h" #include "LogController.h" +#include "utils.h" #include <QApplication> #include <QTimer>@@ -733,7 +734,7 @@ }
void InputController::setLuminanceLevel(int level) { int value = 0x16; - level = std::max(0, std::min(10, level)); + level = clamp(level, 0, 10); if (level > 0) { value += GBA_LUX_LEVELS[level - 1]; }
M
src/platform/qt/MemoryModel.cpp
→
src/platform/qt/MemoryModel.cpp
@@ -9,6 +9,7 @@ #include "GBAApp.h"
#include "CoreController.h" #include "LogController.h" #include "VFileDevice.h" +#include "utils.h" #include <QAction> #include <QApplication>@@ -628,11 +629,7 @@ viewport()->update();
} void MemoryModel::boundsCheck() { - if (m_top < 0) { - m_top = 0; - } else if (m_top > (m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight) { - m_top = (m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight; - } + m_top = clamp(m_top, 0, static_cast<int32_t>(m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight); } bool MemoryModel::isInSelection(uint32_t address) {
M
src/platform/qt/SensorView.cpp
→
src/platform/qt/SensorView.cpp
@@ -8,6 +8,7 @@
#include "CoreController.h" #include "GamepadAxisEvent.h" #include "InputController.h" +#include "utils.h" #include <mgba/core/core.h> #include <mgba/internal/gba/gba.h>@@ -129,7 +130,7 @@ }
} void SensorView::setLuminanceValue(int value) { - value = std::max(0, std::min(value, 255)); + value = clamp(value, 0, 255); if (m_input) { m_input->setLuminanceValue(value); }
M
src/platform/qt/utils.h
→
src/platform/qt/utils.h
@@ -11,6 +11,8 @@ #include <QRect>
#include <QSize> #include <QString> +#include <algorithm> + namespace QGBA { QString niceSizeFormat(size_t filesize);@@ -44,5 +46,14 @@ }
QPoint origin = QPoint((size.width() - ds.width()) / 2, (size.height() - ds.height()) / 2); return QRect(origin, ds); } + +#if __cplusplus >= 201703L +using std::clamp; +#else +template<class T> +constexpr const T& clamp(const T& v, const T& lo, const T& hi) { + return std::max(lo, std::min(hi, v)); +} +#endif }