From patchwork Sat Jul 13 11:39:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 1679 Return-Path: Received: from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net [195.74.38.227]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4C2FC60E40 for ; Sat, 13 Jul 2019 13:39:58 +0200 (CEST) X-Halon-ID: ebdafb60-a562-11e9-8d05-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from localhost.localdomain (unknown [126.209.254.147]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id ebdafb60-a562-11e9-8d05-005056917f90; Sat, 13 Jul 2019 13:39:55 +0200 (CEST) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Sat, 13 Jul 2019 20:39:46 +0900 Message-Id: <20190713113946.25744-1-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.22.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: utils: Add clamp() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 13 Jul 2019 11:39:58 -0000 C++11 does not support std::clamp(), add a custom implementation in utils. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/libcamera/include/utils.h | 7 +++++++ src/libcamera/utils.cpp | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 97bd470a45b050ef..beb2ce3ff7da9352 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_UTILS_H__ #define __LIBCAMERA_UTILS_H__ +#include #include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -45,6 +46,12 @@ unsigned int set_overlap(InputIt1 first1, InputIt1 last1, return count; } +/* C++11 doesn't provide std::clamp */ +template +T clamp(const T& v, const T& lo, const T& hi) { + return std::max(lo, std::min(v, hi)); +} + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index ef365366f29b426e..b0ca3565ee5a0d83 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -85,6 +85,15 @@ char *secure_getenv(const char *name) * \return The number of elements in the intersection of the two ranges */ +/** + * \fn libcamera::utils::clamp(const T& v, const T& lo, const T& hi) + * \param[in] v The value to clamp + * \param[in] lo The lower boundary to clamp v to + * \param[in] hi The higher boundary to clamp v to + * + * \return lo if v is less than lo, hi if v is greater than hi, otherwise v + */ + } /* namespace utils */ } /* namespace libcamera */