From patchwork Mon Jan 25 17:22:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10988 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 1D0A1BD808 for ; Mon, 25 Jan 2021 17:22:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8A937682D2; Mon, 25 Jan 2021 18:22:34 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="GIk0LEhN"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 44D5D682CB for ; Mon, 25 Jan 2021 18:22:33 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id AB9B0331 for ; Mon, 25 Jan 2021 18:22:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1611595352; bh=+Qmrh31NsxTa8X3jWsgKQtfszaUOz8sDR4FfvAlCTys=; h=From:To:Subject:Date:From; b=GIk0LEhN1iPtrCR9pEGRCKpzeveZBwN2dH7ExtQRoAOicCTT72rWZ4CJlLanysUWd dGNRYOPZd16H2L7sspGJRCxOghVpT/8c7FCNM0bLmrP8quq5bqQUUZPTM2mkuALdr0 04QXsnfuqkNWyyPjhKRtoYniEvgQl9nXhodLyU8I= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 25 Jan 2021 19:22:10 +0200 Message-Id: <20210125172210.31681-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH/RFC] libcamera: geometry: Mark const functions with [[nodiscard]] X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Geometry classes generally have two sets of functions, one that operates on the object and modifies it (e.g. Rectangle::scaleBy()), and one that performs the same operations by instead return a modified copy of the object, leaving the original untouched (e.g.Rectangle::scaledBy()). As the names are close, they can easily be mistaken, with the const version used instead of the in-place version. To catch these errors at compile time, mark the const versions with [[nodiscard]], as there is no use case for calling them without using the result of the call. Signed-off-by: Laurent Pinchart --- include/libcamera/geometry.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) This patch came as a result of catching one of the issues described in the commit message during review of another patch. [[nodiscard]] is only available in C++17, which would make it impossible to use libcamera in C++14 code (this can for instance for a problem with Chromium - the web browser, not the OS). An alternative would be to define a custom attribute, for instance #define __nodiscard [[nodiscard]] in a public header (compile.h ? util.h ?), conditionally on the C++ version. The check would thus be disabled in application compiled with C++14, but would be enabled for the compilation of libcamera itself. Is this worth it ? diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 2f3a82e2afc3..0a9d61f45874 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -92,8 +92,8 @@ public: return *this; } - constexpr Size alignedDownTo(unsigned int hAlignment, - unsigned int vAlignment) const + [[nodiscard]] constexpr Size alignedDownTo(unsigned int hAlignment, + unsigned int vAlignment) const { return { width / hAlignment * hAlignment, @@ -101,8 +101,8 @@ public: }; } - constexpr Size alignedUpTo(unsigned int hAlignment, - unsigned int vAlignment) const + [[nodiscard]] constexpr Size alignedUpTo(unsigned int hAlignment, + unsigned int vAlignment) const { return { (width + hAlignment - 1) / hAlignment * hAlignment, @@ -110,7 +110,7 @@ public: }; } - constexpr Size boundedTo(const Size &bound) const + [[nodiscard]] constexpr Size boundedTo(const Size &bound) const { return { std::min(width, bound.width), @@ -118,7 +118,7 @@ public: }; } - constexpr Size expandedTo(const Size &expand) const + [[nodiscard]] constexpr Size expandedTo(const Size &expand) const { return { std::max(width, expand.width), @@ -126,10 +126,10 @@ public: }; } - Size boundedToAspectRatio(const Size &ratio) const; - Size expandedToAspectRatio(const Size &ratio) const; + [[nodiscard]] Size boundedToAspectRatio(const Size &ratio) const; + [[nodiscard]] Size expandedToAspectRatio(const Size &ratio) const; - Rectangle centeredTo(const Point ¢er) const; + [[nodiscard]] Rectangle centeredTo(const Point ¢er) const; Size operator*(float factor) const; Size operator/(float factor) const; @@ -247,10 +247,11 @@ public: Rectangle &scaleBy(const Size &numerator, const Size &denominator); Rectangle &translateBy(const Point &point); - Rectangle boundedTo(const Rectangle &bound) const; - Rectangle enclosedIn(const Rectangle &boundary) const; - Rectangle scaledBy(const Size &numerator, const Size &denominator) const; - Rectangle translatedBy(const Point &point) const; + [[nodiscard]] Rectangle boundedTo(const Rectangle &bound) const; + [[nodiscard]] Rectangle enclosedIn(const Rectangle &boundary) const; + [[nodiscard]] Rectangle scaledBy(const Size &numerator, + const Size &denominator) const; + [[nodiscard]] Rectangle translatedBy(const Point &point) const; }; bool operator==(const Rectangle &lhs, const Rectangle &rhs);