From patchwork Thu Feb 4 03:59:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 11128 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 2C054BD160 for ; Thu, 4 Feb 2021 04:00:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9D866613F8; Thu, 4 Feb 2021 05:00:18 +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="nRHx/Xuv"; 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 23A5E613DA for ; Thu, 4 Feb 2021 05:00:17 +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 9B1155A5 for ; Thu, 4 Feb 2021 05:00:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1612411216; bh=JKkU5tDdpjA+IoDcO3tP0tc2j9O4b4X99oYhJ30Vyf4=; h=From:To:Subject:Date:From; b=nRHx/XuvRiGWAMNon9oTbbkZq3BsVF50H9iw0NAZhwO6Lam+/UFtoLbTJ3EpAkh2C PwLgshv+wCVkZSRDJCfANyMw9kRlKj3W07lZurP0UeQlIDZejMjqc38muzaM4e9Py+ fnfkcezZC5DrjbQZssroDIO3g3b0ITVa+9UrvkR8= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 4 Feb 2021 05:59:50 +0200 Message-Id: <20210204035951.23751-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] libcamera: Add macro to conditionally use [[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" The [[nodiscard]] attribute has been added to C++17. It can thus be used inside libcamera, but would prevent applications compiled for C++14 to use libcamera if the attribute was used in public headers. To offer this feature when the application is compiled with a recent-enough C++ version, as well as for compiling libcamera itself, add a __nodiscard macro that expands as [[nodiscard]] when using C++17 or newer. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Kieran Bingham --- include/libcamera/compiler.h | 16 ++++++++++++++++ include/libcamera/meson.build | 1 + 2 files changed, 17 insertions(+) create mode 100644 include/libcamera/compiler.h diff --git a/include/libcamera/compiler.h b/include/libcamera/compiler.h new file mode 100644 index 000000000000..dc56dbb8b792 --- /dev/null +++ b/include/libcamera/compiler.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * compiler.h - Compiler support + */ +#ifndef __LIBCAMERA_COMPILER_H__ +#define __LIBCAMERA_COMPILER_H__ + +#if __cplusplus >= 201703L +#define __nodiscard [[nodiscard]] +#else +#define __nodiscard +#endif + +#endif /* __LIBCAMERA_COMPILER_H__ */ diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index cf2935f1ee95..13e9eeb6d6ad 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -5,6 +5,7 @@ libcamera_public_headers = files([ 'buffer.h', 'camera.h', 'camera_manager.h', + 'compiler.h', 'controls.h', 'extensible.h', 'file_descriptor.h', From patchwork Thu Feb 4 03:59:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 11129 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 B08EABD160 for ; Thu, 4 Feb 2021 04:00:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0A624613FA; Thu, 4 Feb 2021 05:00:19 +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="Df3Jep0I"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5FC66613F2 for ; Thu, 4 Feb 2021 05:00:17 +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 EE623813 for ; Thu, 4 Feb 2021 05:00:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1612411217; bh=Kehm5PjOVwnrdjAiLiVtzALkQ8lrlm1054uXr//PqQs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Df3Jep0IqL5nXHWJGNa9x+UfRAXSmuBFQDthB/NLz0p/CPXrOueIlk1qHK3kCSuwp W23JrdbOCeqrJCe4KlbTKWPy2T6ldPkMIEkAS/bjt6hqv8C6R02SBbF7RnXg97cUZJ 7JIpiwb30+YJl1knOlZ0HZ8d8hSV6wb+RrH71sGs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 4 Feb 2021 05:59:51 +0200 Message-Id: <20210204035951.23751-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210204035951.23751-1-laurent.pinchart@ideasonboard.com> References: <20210204035951.23751-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] 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 Reviewed-by: Niklas Söderlund Reviewed-by: Kieran Bingham --- include/libcamera/geometry.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 2f3a82e2afc3..fdd1b4678074 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -11,6 +11,8 @@ #include #include +#include + namespace libcamera { class Rectangle; @@ -92,8 +94,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 +103,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 +112,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 +120,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 +128,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 +249,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);