Message ID | 20200120173816.31829-4-laurent.pinchart@ideasonboard.com |
---|---|
State | RFC |
Delegated to: | Laurent Pinchart |
Headers | show |
Series |
|
Related | show |
Hi Laurent, Thanks for your work. On 2020-01-20 19:38:16 +0200, Laurent Pinchart wrote: > Replace the C-style arrays with std::array wherever the ARRAY_SIZE macro > is used, removing the need for the macro completely. std::array combines > the performance and accessibility of C-style arrays with the benefits of > a standard container, which is shown here through the ability to carry > its size. > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> > --- > src/libcamera/camera.cpp | 13 +++++++------ > src/libcamera/include/utils.h | 2 -- > src/libcamera/ipa_module.cpp | 11 ++++++----- > src/libcamera/log.cpp | 23 ++++++++++++----------- > src/libcamera/utils.cpp | 5 ----- > src/libcamera/v4l2_videodevice.cpp | 7 ++++--- > test/ipc/unixsocket.cpp | 8 ++++---- > 7 files changed, 33 insertions(+), 36 deletions(-) > > diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp > index 79a5f994f9bb..769f16c9ceab 100644 > --- a/src/libcamera/camera.cpp > +++ b/src/libcamera/camera.cpp > @@ -7,6 +7,7 @@ > > #include <libcamera/camera.h> > > +#include <array> > #include <iomanip> > > #include <libcamera/framebuffer_allocator.h> > @@ -404,20 +405,20 @@ Camera::~Camera() > LOG(Camera, Error) << "Removing camera while still in use"; > } > > -static const char *const camera_state_names[] = { > +static constexpr auto camera_state_names = utils::make_array( > "Available", > "Acquired", > "Configured", > - "Running", > -}; > + "Running" > +); > > bool Camera::stateBetween(State low, State high) const > { > if (state_ >= low && state_ <= high) > return true; > > - ASSERT(static_cast<unsigned int>(low) < ARRAY_SIZE(camera_state_names) && > - static_cast<unsigned int>(high) < ARRAY_SIZE(camera_state_names)); > + ASSERT(static_cast<unsigned int>(low) < camera_state_names.size() && > + static_cast<unsigned int>(high) < camera_state_names.size()); > > LOG(Camera, Debug) << "Camera in " << camera_state_names[state_] > << " state trying operation requiring state between " > @@ -432,7 +433,7 @@ bool Camera::stateIs(State state) const > if (state_ == state) > return true; > > - ASSERT(static_cast<unsigned int>(state) < ARRAY_SIZE(camera_state_names)); > + ASSERT(static_cast<unsigned int>(state) < camera_state_names.size()); > > LOG(Camera, Debug) << "Camera in " << camera_state_names[state_] > << " state trying operation requiring state " > diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h > index 6e9b9259456a..9057f8b0de84 100644 > --- a/src/libcamera/include/utils.h > +++ b/src/libcamera/include/utils.h > @@ -18,8 +18,6 @@ > #include <sys/time.h> > #include <type_traits> > > -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) > - > #ifndef __DOXYGEN__ > > /* uClibc and uClibc-ng don't provide O_TMPFILE */ > diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp > index 2c355ea8b5e5..6d9ff77d5bd4 100644 > --- a/src/libcamera/ipa_module.cpp > +++ b/src/libcamera/ipa_module.cpp > @@ -480,7 +480,7 @@ bool IPAModule::match(PipelineHandler *pipe, > */ > bool IPAModule::isOpenSource() const > { > - static const char *osLicenses[] = { > + static constexpr auto osLicenses = utils::make_array( > "GPL-2.0-only", > "GPL-2.0-or-later", > "GPL-3.0-only", > @@ -488,12 +488,13 @@ bool IPAModule::isOpenSource() const > "LGPL-2.1-only", > "LGPL-2.1-or-later", > "LGPL-3.0-only", > - "LGPL-3.0-or-later", > - }; > + "LGPL-3.0-or-later" > + ); > > - for (unsigned int i = 0; i < ARRAY_SIZE(osLicenses); i++) > - if (!strcmp(osLicenses[i], info_.license)) > + for (const char *license : osLicenses) { > + if (!strcmp(license, info_.license)) > return true; > + } > > return false; > } > diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp > index 1c82dc68fb0f..fec48c9e7175 100644 > --- a/src/libcamera/log.cpp > +++ b/src/libcamera/log.cpp > @@ -7,6 +7,7 @@ > > #include "log.h" > > +#include <array> > #if HAVE_BACKTRACE > #include <execinfo.h> > #endif > @@ -83,15 +84,15 @@ static int log_severity_to_syslog(LogSeverity severity) > > static const char *log_severity_name(LogSeverity severity) > { > - static const char *const names[] = { > + static constexpr auto names = utils::make_array<const char *>( > " DBG", > " INFO", > " WARN", > " ERR", > - "FATAL", > - }; > + "FATAL" > + ); > > - if (static_cast<unsigned int>(severity) < ARRAY_SIZE(names)) > + if (static_cast<unsigned int>(severity) < names.size()) > return names[severity]; > else > return "UNKWN"; > @@ -405,9 +406,9 @@ void Logger::backtrace() > if (!output) > return; > > - void *buffer[32]; > - int num_entries = ::backtrace(buffer, ARRAY_SIZE(buffer)); > - char **strings = backtrace_symbols(buffer, num_entries); > + std::array<void *, 32> buffer; > + int num_entries = ::backtrace(buffer.data(), buffer.size()); > + char **strings = backtrace_symbols(buffer.data(), num_entries); > if (!strings) > return; > > @@ -603,13 +604,13 @@ void Logger::parseLogLevels() > */ > LogSeverity Logger::parseLogLevel(const std::string &level) > { > - static const char *const names[] = { > + static constexpr auto names = utils::make_array<const char *>( > "DEBUG", > "INFO", > "WARN", > "ERROR", > - "FATAL", > - }; > + "FATAL" > + ); > > int severity; > > @@ -620,7 +621,7 @@ LogSeverity Logger::parseLogLevel(const std::string &level) > severity = LogInvalid; > } else { > severity = LogInvalid; > - for (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) { > + for (unsigned int i = 0; i < names.size(); ++i) { > if (names[i] == level) { > severity = i; > break; > diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp > index b639cfa83d0c..dfd140ca464a 100644 > --- a/src/libcamera/utils.cpp > +++ b/src/libcamera/utils.cpp > @@ -22,11 +22,6 @@ namespace libcamera { > > namespace utils { > > -/** > - * \def ARRAY_SIZE(array) > - * \brief Determine the number of elements in the static array. > - */ > - > /** > * \brief Strip the directory prefix from the path > * \param[in] path The path to process > diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp > index 82267730289d..234d8123c7b4 100644 > --- a/src/libcamera/v4l2_videodevice.cpp > +++ b/src/libcamera/v4l2_videodevice.cpp > @@ -7,6 +7,7 @@ > > #include "v4l2_videodevice.h" > > +#include <array> > #include <fcntl.h> > #include <iomanip> > #include <sstream> > @@ -992,13 +993,13 @@ int V4L2VideoDevice::exportBuffers(unsigned int count, > > for (unsigned i = 0; i < count; ++i) { > struct v4l2_buffer buf = {}; > - struct v4l2_plane planes[VIDEO_MAX_PLANES] = {}; > + std::array<struct v4l2_plane, VIDEO_MAX_PLANES> planes = {}; > > buf.index = i; > buf.type = bufferType_; > buf.memory = memoryType_; > - buf.length = ARRAY_SIZE(planes); > - buf.m.planes = planes; > + buf.length = planes.size(); > + buf.m.planes = planes.data(); > > ret = ioctl(VIDIOC_QUERYBUF, &buf); > if (ret < 0) { > diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp > index f53042b88720..5bf543c197fa 100644 > --- a/test/ipc/unixsocket.cpp > +++ b/test/ipc/unixsocket.cpp > @@ -6,6 +6,7 @@ > */ > > #include <algorithm> > +#include <array> > #include <fcntl.h> > #include <iostream> > #include <stdlib.h> > @@ -21,7 +22,6 @@ > #include "ipc_unixsocket.h" > #include "test.h" > #include "thread.h" > -#include "utils.h" > > #define CMD_CLOSE 0 > #define CMD_REVERSE 1 > @@ -303,13 +303,13 @@ protected: > IPCUnixSocket::Payload message, response; > int ret; > > - static const char *strings[2] = { > + static constexpr std::array<const char *, 2> strings = { > "Foo", > "Bar", > }; > int fds[2]; > > - for (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) { > + for (unsigned int i = 0; i < strings.size(); i++) { > unsigned int len = strlen(strings[i]); > > fds[i] = open("/tmp", O_TMPFILE | O_RDWR, > @@ -331,7 +331,7 @@ protected: > if (ret) > return ret; > > - for (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) { > + for (unsigned int i = 0; i < strings.size(); i++) { > unsigned int len = strlen(strings[i]); > char buf[len]; > > -- > Regards, > > Laurent Pinchart > > _______________________________________________ > libcamera-devel mailing list > libcamera-devel@lists.libcamera.org > https://lists.libcamera.org/listinfo/libcamera-devel
diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 79a5f994f9bb..769f16c9ceab 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -7,6 +7,7 @@ #include <libcamera/camera.h> +#include <array> #include <iomanip> #include <libcamera/framebuffer_allocator.h> @@ -404,20 +405,20 @@ Camera::~Camera() LOG(Camera, Error) << "Removing camera while still in use"; } -static const char *const camera_state_names[] = { +static constexpr auto camera_state_names = utils::make_array( "Available", "Acquired", "Configured", - "Running", -}; + "Running" +); bool Camera::stateBetween(State low, State high) const { if (state_ >= low && state_ <= high) return true; - ASSERT(static_cast<unsigned int>(low) < ARRAY_SIZE(camera_state_names) && - static_cast<unsigned int>(high) < ARRAY_SIZE(camera_state_names)); + ASSERT(static_cast<unsigned int>(low) < camera_state_names.size() && + static_cast<unsigned int>(high) < camera_state_names.size()); LOG(Camera, Debug) << "Camera in " << camera_state_names[state_] << " state trying operation requiring state between " @@ -432,7 +433,7 @@ bool Camera::stateIs(State state) const if (state_ == state) return true; - ASSERT(static_cast<unsigned int>(state) < ARRAY_SIZE(camera_state_names)); + ASSERT(static_cast<unsigned int>(state) < camera_state_names.size()); LOG(Camera, Debug) << "Camera in " << camera_state_names[state_] << " state trying operation requiring state " diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 6e9b9259456a..9057f8b0de84 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -18,8 +18,6 @@ #include <sys/time.h> #include <type_traits> -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - #ifndef __DOXYGEN__ /* uClibc and uClibc-ng don't provide O_TMPFILE */ diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index 2c355ea8b5e5..6d9ff77d5bd4 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -480,7 +480,7 @@ bool IPAModule::match(PipelineHandler *pipe, */ bool IPAModule::isOpenSource() const { - static const char *osLicenses[] = { + static constexpr auto osLicenses = utils::make_array( "GPL-2.0-only", "GPL-2.0-or-later", "GPL-3.0-only", @@ -488,12 +488,13 @@ bool IPAModule::isOpenSource() const "LGPL-2.1-only", "LGPL-2.1-or-later", "LGPL-3.0-only", - "LGPL-3.0-or-later", - }; + "LGPL-3.0-or-later" + ); - for (unsigned int i = 0; i < ARRAY_SIZE(osLicenses); i++) - if (!strcmp(osLicenses[i], info_.license)) + for (const char *license : osLicenses) { + if (!strcmp(license, info_.license)) return true; + } return false; } diff --git a/src/libcamera/log.cpp b/src/libcamera/log.cpp index 1c82dc68fb0f..fec48c9e7175 100644 --- a/src/libcamera/log.cpp +++ b/src/libcamera/log.cpp @@ -7,6 +7,7 @@ #include "log.h" +#include <array> #if HAVE_BACKTRACE #include <execinfo.h> #endif @@ -83,15 +84,15 @@ static int log_severity_to_syslog(LogSeverity severity) static const char *log_severity_name(LogSeverity severity) { - static const char *const names[] = { + static constexpr auto names = utils::make_array<const char *>( " DBG", " INFO", " WARN", " ERR", - "FATAL", - }; + "FATAL" + ); - if (static_cast<unsigned int>(severity) < ARRAY_SIZE(names)) + if (static_cast<unsigned int>(severity) < names.size()) return names[severity]; else return "UNKWN"; @@ -405,9 +406,9 @@ void Logger::backtrace() if (!output) return; - void *buffer[32]; - int num_entries = ::backtrace(buffer, ARRAY_SIZE(buffer)); - char **strings = backtrace_symbols(buffer, num_entries); + std::array<void *, 32> buffer; + int num_entries = ::backtrace(buffer.data(), buffer.size()); + char **strings = backtrace_symbols(buffer.data(), num_entries); if (!strings) return; @@ -603,13 +604,13 @@ void Logger::parseLogLevels() */ LogSeverity Logger::parseLogLevel(const std::string &level) { - static const char *const names[] = { + static constexpr auto names = utils::make_array<const char *>( "DEBUG", "INFO", "WARN", "ERROR", - "FATAL", - }; + "FATAL" + ); int severity; @@ -620,7 +621,7 @@ LogSeverity Logger::parseLogLevel(const std::string &level) severity = LogInvalid; } else { severity = LogInvalid; - for (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) { + for (unsigned int i = 0; i < names.size(); ++i) { if (names[i] == level) { severity = i; break; diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index b639cfa83d0c..dfd140ca464a 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -22,11 +22,6 @@ namespace libcamera { namespace utils { -/** - * \def ARRAY_SIZE(array) - * \brief Determine the number of elements in the static array. - */ - /** * \brief Strip the directory prefix from the path * \param[in] path The path to process diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 82267730289d..234d8123c7b4 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -7,6 +7,7 @@ #include "v4l2_videodevice.h" +#include <array> #include <fcntl.h> #include <iomanip> #include <sstream> @@ -992,13 +993,13 @@ int V4L2VideoDevice::exportBuffers(unsigned int count, for (unsigned i = 0; i < count; ++i) { struct v4l2_buffer buf = {}; - struct v4l2_plane planes[VIDEO_MAX_PLANES] = {}; + std::array<struct v4l2_plane, VIDEO_MAX_PLANES> planes = {}; buf.index = i; buf.type = bufferType_; buf.memory = memoryType_; - buf.length = ARRAY_SIZE(planes); - buf.m.planes = planes; + buf.length = planes.size(); + buf.m.planes = planes.data(); ret = ioctl(VIDIOC_QUERYBUF, &buf); if (ret < 0) { diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp index f53042b88720..5bf543c197fa 100644 --- a/test/ipc/unixsocket.cpp +++ b/test/ipc/unixsocket.cpp @@ -6,6 +6,7 @@ */ #include <algorithm> +#include <array> #include <fcntl.h> #include <iostream> #include <stdlib.h> @@ -21,7 +22,6 @@ #include "ipc_unixsocket.h" #include "test.h" #include "thread.h" -#include "utils.h" #define CMD_CLOSE 0 #define CMD_REVERSE 1 @@ -303,13 +303,13 @@ protected: IPCUnixSocket::Payload message, response; int ret; - static const char *strings[2] = { + static constexpr std::array<const char *, 2> strings = { "Foo", "Bar", }; int fds[2]; - for (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) { + for (unsigned int i = 0; i < strings.size(); i++) { unsigned int len = strlen(strings[i]); fds[i] = open("/tmp", O_TMPFILE | O_RDWR, @@ -331,7 +331,7 @@ protected: if (ret) return ret; - for (unsigned int i = 0; i < ARRAY_SIZE(strings); i++) { + for (unsigned int i = 0; i < strings.size(); i++) { unsigned int len = strlen(strings[i]); char buf[len];
Replace the C-style arrays with std::array wherever the ARRAY_SIZE macro is used, removing the need for the macro completely. std::array combines the performance and accessibility of C-style arrays with the benefits of a standard container, which is shown here through the ability to carry its size. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> --- src/libcamera/camera.cpp | 13 +++++++------ src/libcamera/include/utils.h | 2 -- src/libcamera/ipa_module.cpp | 11 ++++++----- src/libcamera/log.cpp | 23 ++++++++++++----------- src/libcamera/utils.cpp | 5 ----- src/libcamera/v4l2_videodevice.cpp | 7 ++++--- test/ipc/unixsocket.cpp | 8 ++++---- 7 files changed, 33 insertions(+), 36 deletions(-)