summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/include/private/base/SkTPin.h
diff options
context:
space:
mode:
Diffstat (limited to 'gfx/skia/skia/include/private/base/SkTPin.h')
-rw-r--r--gfx/skia/skia/include/private/base/SkTPin.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/gfx/skia/skia/include/private/base/SkTPin.h b/gfx/skia/skia/include/private/base/SkTPin.h
new file mode 100644
index 0000000000..c824c44640
--- /dev/null
+++ b/gfx/skia/skia/include/private/base/SkTPin.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2020 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkTPin_DEFINED
+#define SkTPin_DEFINED
+
+#include <algorithm>
+
+/** @return x pinned (clamped) between lo and hi, inclusively.
+
+ Unlike std::clamp(), SkTPin() always returns a value between lo and hi.
+ If x is NaN, SkTPin() returns lo but std::clamp() returns NaN.
+*/
+template <typename T>
+static constexpr const T& SkTPin(const T& x, const T& lo, const T& hi) {
+ return std::max(lo, std::min(x, hi));
+}
+
+#endif