{"id":2715,"url":"https://patchwork.libcamera.org/api/1.1/patches/2715/?format=json","web_url":"https://patchwork.libcamera.org/patch/2715/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200120173816.31829-4-laurent.pinchart@ideasonboard.com>","date":"2020-01-20T17:38:16","name":"[libcamera-devel,3/3] libcamera: Replace ARRAY_SIZE with std::array","commit_ref":null,"pull_url":null,"state":"rfc","archived":false,"hash":"bc194318abba64830d25a20cd896392a3ff2ae65","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":{"id":14,"url":"https://patchwork.libcamera.org/api/1.1/users/14/?format=json","username":"pinchartl","first_name":"Laurent","last_name":"Pinchart","email":"laurent.pinchart@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/2715/mbox/","series":[{"id":644,"url":"https://patchwork.libcamera.org/api/1.1/series/644/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=644","date":"2020-01-20T17:38:13","name":"Use std::array where applicable","version":1,"mbox":"https://patchwork.libcamera.org/series/644/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2715/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2715/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C04EF6081F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 18:38:21 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 62C5410D6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Jan 2020 18:38:21 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1579541901;\n\tbh=WWLXvwTHQaiX6BmRPdeYbn5mDIBJ2XbviXSOEyuI/gw=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=okGHnI15CtEfz0NWpH9DR4e/O5ZqWXkyMqnLLS3Ay3ykG3Rvkx5sp/K63CUAfbZOt\n\txugc9cCWFsesCkZhX3hNJHs/q4EJdSpAY61okj3pegtzBpY8FT5DNgSzH0OfWasVvJ\n\tiOrLxO0/Ixy55qnqALUI5JeH7KFCD9o5K7uawOu8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 20 Jan 2020 19:38:16 +0200","Message-Id":"<20200120173816.31829-4-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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 3/3] libcamera: Replace ARRAY_SIZE with\n\tstd::array","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 20 Jan 2020 17:38:21 -0000"},"content":"Replace the C-style arrays with std::array wherever the ARRAY_SIZE macro\nis used, removing the need for the macro completely. std::array combines\nthe performance and accessibility of C-style arrays with the benefits of\na standard container, which is shown here through the ability to carry\nits size.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/camera.cpp           | 13 +++++++------\n src/libcamera/include/utils.h      |  2 --\n src/libcamera/ipa_module.cpp       | 11 ++++++-----\n src/libcamera/log.cpp              | 23 ++++++++++++-----------\n src/libcamera/utils.cpp            |  5 -----\n src/libcamera/v4l2_videodevice.cpp |  7 ++++---\n test/ipc/unixsocket.cpp            |  8 ++++----\n 7 files changed, 33 insertions(+), 36 deletions(-)","diff":"diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp\nindex 79a5f994f9bb..769f16c9ceab 100644\n--- a/src/libcamera/camera.cpp\n+++ b/src/libcamera/camera.cpp\n@@ -7,6 +7,7 @@\n \n #include <libcamera/camera.h>\n \n+#include <array>\n #include <iomanip>\n \n #include <libcamera/framebuffer_allocator.h>\n@@ -404,20 +405,20 @@ Camera::~Camera()\n \t\tLOG(Camera, Error) << \"Removing camera while still in use\";\n }\n \n-static const char *const camera_state_names[] = {\n+static constexpr auto camera_state_names = utils::make_array(\n \t\"Available\",\n \t\"Acquired\",\n \t\"Configured\",\n-\t\"Running\",\n-};\n+\t\"Running\"\n+);\n \n bool Camera::stateBetween(State low, State high) const\n {\n \tif (state_ >= low && state_ <= high)\n \t\treturn true;\n \n-\tASSERT(static_cast<unsigned int>(low) < ARRAY_SIZE(camera_state_names) &&\n-\t       static_cast<unsigned int>(high) < ARRAY_SIZE(camera_state_names));\n+\tASSERT(static_cast<unsigned int>(low) < camera_state_names.size() &&\n+\t       static_cast<unsigned int>(high) < camera_state_names.size());\n \n \tLOG(Camera, Debug) << \"Camera in \" << camera_state_names[state_]\n \t\t\t   << \" state trying operation requiring state between \"\n@@ -432,7 +433,7 @@ bool Camera::stateIs(State state) const\n \tif (state_ == state)\n \t\treturn true;\n \n-\tASSERT(static_cast<unsigned int>(state) < ARRAY_SIZE(camera_state_names));\n+\tASSERT(static_cast<unsigned int>(state) < camera_state_names.size());\n \n \tLOG(Camera, Debug) << \"Camera in \" << camera_state_names[state_]\n \t\t\t   << \" state trying operation requiring state \"\ndiff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\nindex 6e9b9259456a..9057f8b0de84 100644\n--- a/src/libcamera/include/utils.h\n+++ b/src/libcamera/include/utils.h\n@@ -18,8 +18,6 @@\n #include <sys/time.h>\n #include <type_traits>\n \n-#define ARRAY_SIZE(a)\t(sizeof(a) / sizeof(a[0]))\n-\n #ifndef __DOXYGEN__\n \n /* uClibc and uClibc-ng don't provide O_TMPFILE */\ndiff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp\nindex 2c355ea8b5e5..6d9ff77d5bd4 100644\n--- a/src/libcamera/ipa_module.cpp\n+++ b/src/libcamera/ipa_module.cpp\n@@ -480,7 +480,7 @@ bool IPAModule::match(PipelineHandler *pipe,\n  */\n bool IPAModule::isOpenSource() const\n {\n-\tstatic const char *osLicenses[] = {\n+\tstatic constexpr auto osLicenses = utils::make_array(\n \t\t\"GPL-2.0-only\",\n \t\t\"GPL-2.0-or-later\",\n \t\t\"GPL-3.0-only\",\n@@ -488,12 +488,13 @@ bool IPAModule::isOpenSource() const\n \t\t\"LGPL-2.1-only\",\n \t\t\"LGPL-2.1-or-later\",\n \t\t\"LGPL-3.0-only\",\n-\t\t\"LGPL-3.0-or-later\",\n-\t};\n+\t\t\"LGPL-3.0-or-later\"\n+\t);\n \n-\tfor (unsigned int i = 0; i < ARRAY_SIZE(osLicenses); i++)\n-\t\tif (!strcmp(osLicenses[i], info_.license))\n+\tfor (const char *license : osLicenses) {\n+\t\tif (!strcmp(license, info_.license))\n \t\t\treturn true;\n+\t}\n \n \treturn false;\n }\ndiff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp\nindex 1c82dc68fb0f..fec48c9e7175 100644\n--- a/src/libcamera/log.cpp\n+++ b/src/libcamera/log.cpp\n@@ -7,6 +7,7 @@\n \n #include \"log.h\"\n \n+#include <array>\n #if HAVE_BACKTRACE\n #include <execinfo.h>\n #endif\n@@ -83,15 +84,15 @@ static int log_severity_to_syslog(LogSeverity severity)\n \n static const char *log_severity_name(LogSeverity severity)\n {\n-\tstatic const char *const names[] = {\n+\tstatic constexpr auto names = utils::make_array<const char *>(\n \t\t\"  DBG\",\n \t\t\" INFO\",\n \t\t\" WARN\",\n \t\t\"  ERR\",\n-\t\t\"FATAL\",\n-\t};\n+\t\t\"FATAL\"\n+\t);\n \n-\tif (static_cast<unsigned int>(severity) < ARRAY_SIZE(names))\n+\tif (static_cast<unsigned int>(severity) < names.size())\n \t\treturn names[severity];\n \telse\n \t\treturn \"UNKWN\";\n@@ -405,9 +406,9 @@ void Logger::backtrace()\n \tif (!output)\n \t\treturn;\n \n-\tvoid *buffer[32];\n-\tint num_entries = ::backtrace(buffer, ARRAY_SIZE(buffer));\n-\tchar **strings = backtrace_symbols(buffer, num_entries);\n+\tstd::array<void *, 32> buffer;\n+\tint num_entries = ::backtrace(buffer.data(), buffer.size());\n+\tchar **strings = backtrace_symbols(buffer.data(), num_entries);\n \tif (!strings)\n \t\treturn;\n \n@@ -603,13 +604,13 @@ void Logger::parseLogLevels()\n  */\n LogSeverity Logger::parseLogLevel(const std::string &level)\n {\n-\tstatic const char *const names[] = {\n+\tstatic constexpr auto names = utils::make_array<const char *>(\n \t\t\"DEBUG\",\n \t\t\"INFO\",\n \t\t\"WARN\",\n \t\t\"ERROR\",\n-\t\t\"FATAL\",\n-\t};\n+\t\t\"FATAL\"\n+\t);\n \n \tint severity;\n \n@@ -620,7 +621,7 @@ LogSeverity Logger::parseLogLevel(const std::string &level)\n \t\t\tseverity = LogInvalid;\n \t} else {\n \t\tseverity = LogInvalid;\n-\t\tfor (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) {\n+\t\tfor (unsigned int i = 0; i < names.size(); ++i) {\n \t\t\tif (names[i] == level) {\n \t\t\t\tseverity = i;\n \t\t\t\tbreak;\ndiff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\nindex b639cfa83d0c..dfd140ca464a 100644\n--- a/src/libcamera/utils.cpp\n+++ b/src/libcamera/utils.cpp\n@@ -22,11 +22,6 @@ namespace libcamera {\n \n namespace utils {\n \n-/**\n- * \\def ARRAY_SIZE(array)\n- * \\brief Determine the number of elements in the static array.\n- */\n-\n /**\n  * \\brief Strip the directory prefix from the path\n  * \\param[in] path The path to process\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 82267730289d..234d8123c7b4 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -7,6 +7,7 @@\n \n #include \"v4l2_videodevice.h\"\n \n+#include <array>\n #include <fcntl.h>\n #include <iomanip>\n #include <sstream>\n@@ -992,13 +993,13 @@ int V4L2VideoDevice::exportBuffers(unsigned int count,\n \n \tfor (unsigned i = 0; i < count; ++i) {\n \t\tstruct v4l2_buffer buf = {};\n-\t\tstruct v4l2_plane planes[VIDEO_MAX_PLANES] = {};\n+\t\tstd::array<struct v4l2_plane, VIDEO_MAX_PLANES> planes = {};\n \n \t\tbuf.index = i;\n \t\tbuf.type = bufferType_;\n \t\tbuf.memory = memoryType_;\n-\t\tbuf.length = ARRAY_SIZE(planes);\n-\t\tbuf.m.planes = planes;\n+\t\tbuf.length = planes.size();\n+\t\tbuf.m.planes = planes.data();\n \n \t\tret = ioctl(VIDIOC_QUERYBUF, &buf);\n \t\tif (ret < 0) {\ndiff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp\nindex f53042b88720..5bf543c197fa 100644\n--- a/test/ipc/unixsocket.cpp\n+++ b/test/ipc/unixsocket.cpp\n@@ -6,6 +6,7 @@\n  */\n \n #include <algorithm>\n+#include <array>\n #include <fcntl.h>\n #include <iostream>\n #include <stdlib.h>\n@@ -21,7 +22,6 @@\n #include \"ipc_unixsocket.h\"\n #include \"test.h\"\n #include \"thread.h\"\n-#include \"utils.h\"\n \n #define CMD_CLOSE\t0\n #define CMD_REVERSE\t1\n@@ -303,13 +303,13 @@ protected:\n \t\tIPCUnixSocket::Payload message, response;\n \t\tint ret;\n \n-\t\tstatic const char *strings[2] = {\n+\t\tstatic constexpr std::array<const char *, 2> strings = {\n \t\t\t\"Foo\",\n \t\t\t\"Bar\",\n \t\t};\n \t\tint fds[2];\n \n-\t\tfor (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) {\n+\t\tfor (unsigned int i = 0; i < strings.size(); i++) {\n \t\t\tunsigned int len = strlen(strings[i]);\n \n \t\t\tfds[i] = open(\"/tmp\", O_TMPFILE | O_RDWR,\n@@ -331,7 +331,7 @@ protected:\n \t\tif (ret)\n \t\t\treturn ret;\n \n-\t\tfor (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) {\n+\t\tfor (unsigned int i = 0; i < strings.size(); i++) {\n \t\t\tunsigned int len = strlen(strings[i]);\n \t\t\tchar buf[len];\n \n","prefixes":["libcamera-devel","3/3"]}