From patchwork Wed Nov 20 14:27:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22035 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 CB55AC330B for ; Wed, 20 Nov 2024 14:28:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3AB1A65F6C; Wed, 20 Nov 2024 15:28:27 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="tmKdgyiM"; 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 8CE2F65F67 for ; Wed, 20 Nov 2024 15:28:23 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:bbd:82cc:f3f3:e12e]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6EAB0AB5; Wed, 20 Nov 2024 15:28:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1732112885; bh=ijGutbxbl2X9YnUKzdIB7SeDg9SnLHwr/1Zvs4bqR60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tmKdgyiMxeCR7y4MvPPxfrdDNO4P+DMtHkiJwxzyrYwnlRdzdhvMp/978Gb6gGtUb 1ZZqMrr0AszWfNXq5wm2HSC8UbixuamEP18DhM8kcc3gcBrnUuuF71YKX2geWFCwkW zupBwo3ssk9hCq4vKatv0kaFMl3s3E2hqbynZoSA= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Laurent Pinchart Subject: [PATCH v3 4/9] libcamera: internal: matrix: Replace vector with array in constructor Date: Wed, 20 Nov 2024 15:27:51 +0100 Message-ID: <20241120142807.2093301-5-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241120142807.2093301-1-stefan.klug@ideasonboard.com> References: <20241120142807.2093301-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 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 Matrix constructor that takes a std::vector is meant and only used to initialize a Matrix from an initializer list. Using a std::vector is problematic for two reasons. First, it requires constructing a vector, copying the data from the initializer list, which is an expensive operation. Then, the vector size can't be verified at compile time, making the constructor unsafe. The first issue could be solved by replacing the vector with a std::initializer_list or a Span. The second issue would require checking the initializer list size with a static assertion, or restricting usage of the constructor to fixed-extent spans. Unfortunately, even if the size of initializer lists is always known at compile time, the std::initializer_list::size() function is a compile-time constant only for constant initializer lists. Using a span would work better, but construction of a fixed extent span from an initializer list must be explicit, making the API cumbersome. We can solve all those issues by passing an std::array to the constructor. Construction of an array from an initializer list can be implicit and doesn't involve a copy, and the array size is a template parameter and therefore guaranteed to be a compile-time constant. Signed-off-by: Stefan Klug Reviewed-by: Laurent Pinchart --- Changes in v3: - Collect tags - Update commit msg --- include/libcamera/internal/matrix.h | 2 +- src/libcamera/matrix.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h index 3701d0ee980b..7a71028c473a 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -33,7 +33,7 @@ public: data_.fill(static_cast(0)); } - Matrix(const std::vector &data) + Matrix(const std::array &data) { std::copy(data.begin(), data.end(), data_.begin()); } diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp index 55359aa206ee..4d95a19bfbb9 100644 --- a/src/libcamera/matrix.cpp +++ b/src/libcamera/matrix.cpp @@ -32,7 +32,7 @@ LOG_DEFINE_CATEGORY(Matrix) */ /** - * \fn Matrix::Matrix(const std::vector &data) + * \fn Matrix::Matrix(const std::array &data) * \brief Construct a matrix from supplied data * \param[in] data Data from which to construct a matrix *