diff --git a/include/util/math.hpp b/include/util/math.hpp index 0978c69c..27651821 100644 --- a/include/util/math.hpp +++ b/include/util/math.hpp @@ -15,24 +15,42 @@ using std::isnormal; namespace rack { //////////////////// -// basic integer functions +// generic functions //////////////////// -inline int min(int a, int b) { +/** Returns the minimum of `a` and `b` */ +template +inline T min(T a, T b) { return (a < b) ? a : b; } -inline int max(int a, int b) { +/** Returns the maximum of `a` and `b` */ +template +inline T max(T a, T b) { return (a > b) ? a : b; } /** Limits `x` between `a` and `b` Assumes a <= b */ -inline int clamp(int x, int a, int b) { +template +inline T clamp(T x, T a, T b) { return min(max(x, a), b); } +/** Limits `x` between `a` and `b` +If a > b, switches the two values +*/ +template +inline T clamp2(T x, T a, T b) { + return clamp(x, min(a, b), max(a, b)); +} + +//////////////////// +// basic integer functions +//////////////////// + + /** Euclidean modulus, always returns 0 <= mod < base for positive base. */ inline int eucmod(int a, int base) { @@ -58,14 +76,6 @@ inline bool ispow2(int n) { // basic float functions //////////////////// -inline float min(float a, float b) { - return (a < b) ? a : b; -} - -inline float max(float a, float b) { - return (a > b) ? a : b; -} - /** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */ inline float sgn(float x) { return copysignf(1.0f, x); @@ -80,20 +90,6 @@ inline bool isNear(float a, float b, float epsilon = 1.0e-6f) { return fabsf(a - b) <= epsilon; } -/** Limits `x` between `a` and `b` -Assumes a <= b -*/ -inline float clamp(float x, float a, float b) { - return min(max(x, a), b); -} - -/** Limits `x` between `a` and `b` -If a > b, switches the two values -*/ -inline float clamp2(float x, float a, float b) { - return clamp(x, min(a, b), max(a, b)); -} - /** If the magnitude of x if less than eps, return 0 */ inline float chop(float x, float eps) { return (-eps < x && x < eps) ? 0.0f : x; diff --git a/src/app/SVGSwitch.cpp b/src/app/SVGSwitch.cpp index d399cd06..08e594bf 100644 --- a/src/app/SVGSwitch.cpp +++ b/src/app/SVGSwitch.cpp @@ -21,7 +21,7 @@ void SVGSwitch::addFrame(std::shared_ptr svg) { void SVGSwitch::onChange(EventChange &e) { assert(frames.size() > 0); float valueScaled = rescale(value, minValue, maxValue, 0, frames.size() - 1); - int index = clamp((int) roundf(valueScaled), 0, frames.size() - 1); + int index = clamp((int) roundf(valueScaled), 0, (int) frames.size() - 1); sw->setSVG(frames[index]); dirty = true; ParamWidget::onChange(e); diff --git a/src/audio.cpp b/src/audio.cpp index 1596c8b7..f5365b42 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -138,11 +138,11 @@ std::string AudioIO::getDeviceDetail(int device, int offset) { if (getDeviceInfo(device, &deviceInfo)) { std::string deviceDetail = stringf("%s (", deviceInfo.name.c_str()); if (offset < (int) deviceInfo.inputChannels) - deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, deviceInfo.inputChannels)); + deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, (int) deviceInfo.inputChannels)); if (offset < (int) deviceInfo.inputChannels && offset < (int) deviceInfo.outputChannels) deviceDetail += ", "; if (offset < (int) deviceInfo.outputChannels) - deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, deviceInfo.outputChannels)); + deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, (int) deviceInfo.outputChannels)); deviceDetail += ")"; return deviceDetail; } diff --git a/src/settings.cpp b/src/settings.cpp index 78737b0f..1c29c334 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -105,7 +105,7 @@ static void settingsFromJson(json_t *rootJ) { // zoom json_t *zoomJ = json_object_get(rootJ, "zoom"); if (zoomJ) { - gRackScene->zoomWidget->setZoom(clamp(json_number_value(zoomJ), 0.25f, 4.0f)); + gRackScene->zoomWidget->setZoom(clamp((float) json_number_value(zoomJ), 0.25f, 4.0f)); gToolbar->zoomSlider->setValue(json_number_value(zoomJ) * 100.0); } diff --git a/src/ui/TextField.cpp b/src/ui/TextField.cpp index dcbf550d..765cd18b 100644 --- a/src/ui/TextField.cpp +++ b/src/ui/TextField.cpp @@ -166,8 +166,8 @@ void TextField::onKey(EventKey &e) { } break; } - cursor = clamp(cursor, 0, text.size()); - selection = clamp(selection, 0, text.size()); + cursor = clamp(cursor, 0, (int) text.size()); + selection = clamp(selection, 0, (int) text.size()); e.consumed = true; }