From patchwork Mon Jan 20 17:38:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2713 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: 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 1B11F6081F for ; Mon, 20 Jan 2020 18:38:21 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id ADE3610D6 for ; Mon, 20 Jan 2020 18:38:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579541900; bh=PNjzR3WxiO+vO/1ZND9WK69/l54MBwae0rrQ0g3uCKA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Wwx2ObC2g9hPxoZM5YP75ohtDiPc3aBqzIE8l6VH1yHwyW6zjY+Bcb25OJqBcd6Iq 89u5ipoNkQFJbs3SdkcclfgAB3Az2u5GvB72FFgqZiieTVoCFFhjeVjftuNfUH12Dn Kd5cvLNr0LLJk+B6m92XWTm5e2yf7QqrQAGhdk9o= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 20 Jan 2020 19:38:14 +0200 Message-Id: <20200120173816.31829-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200120173816.31829-1-laurent.pinchart@ideasonboard.com> References: <20200120173816.31829-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/3] libcamera: utils: Add a new make_array() function 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: , X-List-Received-Date: Mon, 20 Jan 2020 17:38:21 -0000 Add a custom implementation of std::make_array() as defined by the library fundamentals TS v2 ([1]). The function is useful to initialize static arrays without requiring manually specifying the number of elements, which could lead to bugs when the specified size is larger than the number of initializers. This is an experimental feature in the C++ standard, and while available in in stdlibc++ from g++-6 onwards, it is not provided by libc++. [1] https://en.cppreference.com/w/cpp/experimental/make_array Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- Documentation/Doxyfile.in | 1 + src/libcamera/include/utils.h | 42 +++++++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 14 ++++++++++++ 3 files changed, 57 insertions(+) diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in index 8e6fbdbb92b6..6de59d407e7b 100644 --- a/Documentation/Doxyfile.in +++ b/Documentation/Doxyfile.in @@ -877,6 +877,7 @@ EXCLUDE_SYMBOLS = libcamera::BoundMethodArgs \ libcamera::BoundMethodPackBase \ libcamera::BoundMethodStatic \ libcamera::SignalBase \ + *::details::* \ std::* # The EXAMPLE_PATH tag can be used to specify one or more files or directories diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index e467eb21c518..6e9b9259456a 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -8,12 +8,15 @@ #define __LIBCAMERA_UTILS_H__ #include +#include #include +#include #include #include #include #include #include +#include #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) @@ -108,6 +111,45 @@ inline _hex hex(uint64_t value, unsigned int width) size_t strlcpy(char *dst, const char *src, size_t size); +namespace details { + +namespace make_array { + + template + struct negation : std::integral_constant {}; + + template struct conjunction : std::true_type {}; + template struct conjunction : B1 {}; + template + struct conjunction + : std::conditional_t, B1> {}; + + template struct is_ref_wrapper : std::false_type {}; + template struct is_ref_wrapper> : std::true_type {}; + + template + struct return_type_helper { + using type = D; + }; + template + struct return_type_helper : std::common_type { + static_assert(conjunction>>...>::value, + "Types cannot contain reference_wrappers when D is void"); + }; + + template + using return_type = std::array::type, + sizeof...(Types)>; +} /* namespace make_array */ + +} /* namespace details */ + +template +constexpr details::make_array::return_type make_array(Types&&... t) +{ + return {std::forward(t)... }; +} + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 4beffdab5eb6..b639cfa83d0c 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -199,6 +199,20 @@ size_t strlcpy(char *dst, const char *src, size_t size) return strlen(src); } +/** + * \fn template libcamera::utils::make_array(Types&&... t) + * \brief Create a std::array with automatic deduction of element type and count + * \param[in] t The initialization values for the array elements + * + * This function creates and returns an instance of std::array whose size is + * equal to the number of arguments. If the template argument \a D is void, the + * array element type is deduced from the elements through + * std::common_types_t. Otherwise it is set to D. The elements are + * initialized from the corresponding arguments. + * + * \return A std::array initialized from the arguments + */ + } /* namespace utils */ } /* namespace libcamera */