{"id":8992,"url":"https://patchwork.libcamera.org/api/1.1/patches/8992/?format=json","web_url":"https://patchwork.libcamera.org/patch/8992/","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":"<20200724230841.27838-2-laurent.pinchart@ideasonboard.com>","date":"2020-07-24T23:08:40","name":"[libcamera-devel,1/2] libcamera: flags: Add type-safe enum-based flags","commit_ref":null,"pull_url":null,"state":"changes-requested","archived":false,"hash":"59cc07b13ea443e17c429a4aedc4ca8941125a04","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/8992/mbox/","series":[{"id":1143,"url":"https://patchwork.libcamera.org/api/1.1/series/1143/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1143","date":"2020-07-24T23:08:39","name":"libcamera: Add type-safe enum-based flags","version":1,"mbox":"https://patchwork.libcamera.org/series/1143/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8992/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8992/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id B856ABD878\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 24 Jul 2020 23:08:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 83457612F3;\n\tSat, 25 Jul 2020 01:08:56 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AB7A160496\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 25 Jul 2020 01:08:55 +0200 (CEST)","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 0D0F3585\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 25 Jul 2020 01:08:52 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"oRX65YH4\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1595632132;\n\tbh=mahSFIkd1bUlQaerddez9bd+4fMR5OX+MFXVHFSr3Bo=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=oRX65YH4OoN6FHftO+tvQu8XePGSDuOc1ddP2bDIANGVzVaXHQYlA0XS5IZInUN1g\n\tEf0s2OG6z3RgEF1+OC8z1J+DxMJBqvyUAi2xG1oGBs5Pjnetosqa6lkme2Npw6l1b+\n\tcba1iPGbfoarldAGFg2UUqnEbJksvIfSW4JZ1xIw=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 25 Jul 2020 02:08:40 +0300","Message-Id":"<20200724230841.27838-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20200724230841.27838-1-laurent.pinchart@ideasonboard.com>","References":"<20200724230841.27838-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH 1/2] libcamera: flags: Add type-safe\n\tenum-based flags","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a Flags template class that provide type-safe bitwise operators on\nenum values. This allows using enum types for bit fields, without giving\naway type-safety as usually done when storing combined flags in integer\nvariables.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/flags.h     | 195 ++++++++++++++++++++++++++++++++++\n include/libcamera/meson.build |   1 +\n src/libcamera/flags.cpp       | 192 +++++++++++++++++++++++++++++++++\n src/libcamera/meson.build     |   1 +\n 4 files changed, 389 insertions(+)\n create mode 100644 include/libcamera/flags.h\n create mode 100644 src/libcamera/flags.cpp","diff":"diff --git a/include/libcamera/flags.h b/include/libcamera/flags.h\nnew file mode 100644\nindex 000000000000..74525ccb1050\n--- /dev/null\n+++ b/include/libcamera/flags.h\n@@ -0,0 +1,195 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * flags.h - Type-safe enum-based bitfields\n+ */\n+#ifndef __LIBCAMERA_FLAGS_H__\n+#define __LIBCAMERA_FLAGS_H__\n+\n+#include <type_traits>\n+\n+namespace libcamera {\n+\n+template<typename E>\n+class Flags\n+{\n+public:\n+\tstatic_assert(std::is_enum<E>::value,\n+\t\t      \"Flags<> template parameter must be an enum\");\n+\n+\tusing Type = std::underlying_type_t<E>;\n+\n+\tconstexpr Flags()\n+\t\t: value_(0)\n+\t{\n+\t}\n+\n+\tconstexpr Flags(E flag)\n+\t\t: value_(static_cast<Type>(flag))\n+\t{\n+\t}\n+\n+\tconstexpr Flags &operator&=(E flag)\n+\t{\n+\t\tvalue_ &= static_cast<Type>(flag);\n+\t\treturn *this;\n+\t}\n+\n+\tconstexpr Flags &operator&=(Flags other)\n+\t{\n+\t\tvalue_ &= other.value_;\n+\t\treturn *this;\n+\t}\n+\n+\tconstexpr Flags &operator|=(E flag)\n+\t{\n+\t\tvalue_ |= static_cast<Type>(flag);\n+\t\treturn *this;\n+\t}\n+\n+\tconstexpr Flags &operator|=(Flags other)\n+\t{\n+\t\tvalue_ |= other.value_;\n+\t\treturn *this;\n+\t}\n+\n+\tconstexpr Flags &operator^=(E flag)\n+\t{\n+\t\tvalue_ ^= static_cast<Type>(flag);\n+\t\treturn *this;\n+\t}\n+\n+\tconstexpr Flags &operator^=(Flags other)\n+\t{\n+\t\tvalue_ ^= other.value_;\n+\t\treturn *this;\n+\t}\n+\n+\tconstexpr bool operator==(E flag)\n+\t{\n+\t\treturn value_ == static_cast<Type>(flag);\n+\t}\n+\n+\tconstexpr bool operator==(Flags other)\n+\t{\n+\t\treturn value_ == static_cast<Type>(other);\n+\t}\n+\n+\tconstexpr bool operator!=(E flag)\n+\t{\n+\t\treturn value_ != static_cast<Type>(flag);\n+\t}\n+\n+\tconstexpr bool operator!=(Flags other)\n+\t{\n+\t\treturn value_ != static_cast<Type>(other);\n+\t}\n+\n+\tconstexpr explicit operator Type() const\n+\t{\n+\t\treturn value_;\n+\t}\n+\n+\tconstexpr explicit operator bool() const\n+\t{\n+\t\treturn !!value_;\n+\t}\n+\n+\tconstexpr Flags operator&(E flag) const\n+\t{\n+\t\treturn Flags(static_cast<E>(value_ & static_cast<Type>(flag)));\n+\t}\n+\n+\tconstexpr Flags operator&(Flags other) const\n+\t{\n+\t\treturn Flags(static_cast<E>(value_ & other.value_));\n+\t}\n+\n+\tconstexpr Flags operator|(E flag) const\n+\t{\n+\t\treturn Flags(static_cast<E>(value_ | static_cast<Type>(flag)));\n+\t}\n+\n+\tconstexpr Flags operator|(Flags other) const\n+\t{\n+\t\treturn Flags(static_cast<E>(value_ | other.value_));\n+\t}\n+\n+\tconstexpr Flags operator^(E flag) const\n+\t{\n+\t\treturn Flags(static_cast<E>(value_ ^ static_cast<Type>(flag)));\n+\t}\n+\n+\tconstexpr Flags operator^(Flags other) const\n+\t{\n+\t\treturn Flags(static_cast<E>(value_ ^ other.value_));\n+\t}\n+\n+\tconstexpr Flags operator~() const\n+\t{\n+\t\treturn Flags(static_cast<E>(~value_));\n+\t}\n+\n+\tconstexpr bool operator!() const\n+\t{\n+\t\treturn !value_;\n+\t}\n+\n+private:\n+\tType value_;\n+};\n+\n+#ifndef __DOXYGEN__\n+template<typename E>\n+struct flags_enable_operators {\n+\tstatic const bool enable = false;\n+};\n+\n+template<typename E>\n+typename std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>\n+operator|(E lhs, E rhs)\n+{\n+\tusing type = std::underlying_type_t<E>;\n+\treturn Flags<E>(static_cast<E>(static_cast<type>(lhs) | static_cast<type>(rhs)));\n+}\n+\n+template<typename E>\n+typename std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>\n+operator&(E lhs, E rhs)\n+{\n+\tusing type = std::underlying_type_t<E>;\n+\treturn Flags<E>(static_cast<E>(static_cast<type>(lhs) & static_cast<type>(rhs)));\n+}\n+\n+template<typename E>\n+typename std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>\n+operator^(E lhs, E rhs)\n+{\n+\tusing type = std::underlying_type_t<E>;\n+\treturn Flags<E>(static_cast<E>(static_cast<type>(lhs) ^ static_cast<type>(rhs)));\n+}\n+\n+template<typename E>\n+typename std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>\n+operator~(E rhs)\n+{\n+\tusing type = std::underlying_type_t<E>;\n+\treturn Flags<E>(static_cast<E>(~static_cast<type>(rhs)));\n+}\n+\n+#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)\t\t\t\t\\\n+template<>\t\t\t\t\t\t\t\t\\\n+struct flags_enable_operators<_enum> {\t\t\t\t\t\\\n+\tstatic const bool enable = true;\t\t\t\t\\\n+};\n+\n+#else /* __DOXYGEN__ */\n+\n+#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)\n+\n+#endif /* __DOXYGEN__ */\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_FLAGS_H__ */\ndiff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\nindex cdb8e0372e77..cd51a1f09ffb 100644\n--- a/include/libcamera/meson.build\n+++ b/include/libcamera/meson.build\n@@ -9,6 +9,7 @@ libcamera_public_headers = files([\n     'event_dispatcher.h',\n     'event_notifier.h',\n     'file_descriptor.h',\n+    'flags.h',\n     'framebuffer_allocator.h',\n     'geometry.h',\n     'logging.h',\ndiff --git a/src/libcamera/flags.cpp b/src/libcamera/flags.cpp\nnew file mode 100644\nindex 000000000000..d0521755a83e\n--- /dev/null\n+++ b/src/libcamera/flags.cpp\n@@ -0,0 +1,192 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * flags.cpp - Type-safe enum-based bitfields\n+ */\n+\n+#include <libcamera/flags.h>\n+\n+/**\n+ * \\file flags.h\n+ * \\brief Enum-based bit fields\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class Flags\n+ * \\brief Type-safe container for enum-based bitfields\n+ *\n+ * The Flags template class provides type-safe bitwise operators on enum values.\n+ * It allows using enum types for bitfields, while preventing unsafe casts from\n+ * integer types and mixing of flags from different enum types.\n+ *\n+ * To use the Flags class, declare an enum containing the desired bit flags, and\n+ * use the Flags<enum> class to store bitfields based on the enum. If bitwise\n+ * operators on the underlying enum are also desired, they can be enabled with\n+ * the LIBCAMERA_FLAGS_ENABLE_OPERATORS(enum) macro.\n+ */\n+\n+/**\n+ * \\typedef Flags::Type\n+ * \\brief The underlying data type of the enum\n+ */\n+\n+/**\n+ * \\fn Flags::Flags()\n+ * \\brief Construct a Flags instance with a zero value\n+ */\n+\n+/**\n+ * \\fn Flags::Flags(E flag)\n+ * \\brief Construct a Flags instance storing the \\a flag\n+ * \\param[in] flag The initial value\n+ */\n+\n+/**\n+ * \\fn Flags &Flags::operator&=(E flag)\n+ * \\brief Store the bitwise AND of this Flags and the \\a flag in this Flags\n+ * \\param[in] flag The second operand\n+ * \\return A reference to this Flags\n+ */\n+\n+/**\n+ * \\fn Flags &Flags::operator&=(Flags other)\n+ * \\brief Store the bitwise AND of this Flags and the \\a other Flags in this Flags\n+ * \\param[in] other The second operand\n+ * \\return A reference to this Flags\n+ */\n+\n+/**\n+ * \\fn Flags &Flags::operator|=(E flag)\n+ * \\brief Store the bitwise OR of this Flags and the \\a flag in this Flags\n+ * \\param[in] flag The second operand\n+ * \\return A reference to this Flags\n+ */\n+\n+/**\n+ * \\fn Flags &Flags::operator|=(Flags other)\n+ * \\brief Store the bitwise OR of this Flags and the \\a other Flags in this Flags\n+ * \\param[in] other The second operand\n+ * \\return A reference to this Flags\n+ */\n+\n+/**\n+ * \\fn Flags &Flags::operator^=(E flag)\n+ * \\brief Store the bitwise XOR of this Flags and the \\a flag in this Flags\n+ * \\param[in] flag The second operand\n+ * \\return A reference to this Flags\n+ */\n+\n+/**\n+ * \\fn Flags &Flags::operator^=(Flags other)\n+ * \\brief Store the bitwise XOR of this Flags and the \\a other Flags in this Flags\n+ * \\param[in] other The second operand\n+ * \\return A reference to this Flags\n+ */\n+\n+/**\n+ * \\fn bool Flags::operator==(E flag)\n+ * \\brief Compare flags for equality\n+ * \\param[in] flag The second operand\n+ * \\return True if the Flags and \\a flag are equal, false otherwise\n+ */\n+\n+/**\n+ * \\fn bool Flags::operator==(Flags other)\n+ * \\brief Compare flags for equality\n+ * \\param[in] other The second operand\n+ * \\return True if the Flags and \\a other are equal, false otherwise\n+ */\n+\n+/**\n+ * \\fn bool Flags::operator!=(E flag)\n+ * \\brief Compare flags for non-equality\n+ * \\param[in] flag The second operand\n+ * \\return True if the Flags and \\a flag are not equal, false otherwise\n+ */\n+\n+/**\n+ * \\fn bool Flags::operator!=(Flags other)\n+ * \\brief Compare flags for non-equality\n+ * \\param[in] other The second operand\n+ * \\return True if the Flags and \\a other are not equal, false otherwise\n+ */\n+\n+/**\n+ * \\fn Flags::operator Type() const\n+ * \\brief Convert the Flags to the underlying integer type\n+ * \\return The Flags value as an integer\n+ */\n+\n+/**\n+ * \\fn Flags::operator bool() const\n+ * \\brief Convert the Flags to a boolean\n+ * \\return True if at least one flag is set, false otherwise\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator&(E flag) const\n+ * \\brief Compute the bitwise AND of this Flags and the \\a flag\n+ * \\param[in] flag The second operand\n+ * \\return A Flags containing the result of the AND operation\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator&(Flags other) const\n+ * \\brief Compute the bitwise AND of this Flags and the \\a other Flags\n+ * \\param[in] other The second operand\n+ * \\return A Flags containing the result of the AND operation\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator|(E flag) const\n+ * \\brief Compute the bitwise OR of this Flags and the \\a flag\n+ * \\param[in] flag The second operand\n+ * \\return A Flags containing the result of the OR operation\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator|(Flags other) const\n+ * \\brief Compute the bitwise OR of this Flags and the \\a other Flags\n+ * \\param[in] other The second operand\n+ * \\return A Flags containing the result of the OR operation\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator^(E flag) const\n+ * \\brief Compute the bitwise XOR of this Flags and the \\a flag\n+ * \\param[in] flag The second operand\n+ * \\return A Flags containing the result of the XOR operation\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator^(Flags other) const\n+ * \\brief Compute the bitwise XOR of this Flags and the \\a other Flags\n+ * \\param[in] other The second operand\n+ * \\return A Flags containing the result of the XOR operation\n+ */\n+\n+/**\n+ * \\fn Flags Flags::operator~() const\n+ * \\brief Compute the bitwise NOT of this Flags\n+ * \\return A Flags containing the result of the NOT operation\n+ */\n+\n+/**\n+ * \\fn bool Flags::operator!() const\n+ * \\brief Check if flags are set\n+ * \\return True if no flags is set, false otherwise\n+ */\n+\n+/**\n+ * \\def LIBCAMERA_FLAGS_ENABLE_OPERATORS(enum)\n+ * \\brief Enable bitwise operations on the \\a enum enumeration\n+ *\n+ * This macro enables the bitwise AND, OR, XOR and NOT operators on the given\n+ * \\a enum. This allows the enum values to be safely used in bitwise operations\n+ * with the Flags<> class.\n+ */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 3aad4386ffc2..2461200fa772 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -18,6 +18,7 @@ libcamera_sources = files([\n     'event_notifier.cpp',\n     'file.cpp',\n     'file_descriptor.cpp',\n+    'flags.cpp',\n     'formats.cpp',\n     'framebuffer_allocator.cpp',\n     'geometry.cpp',\n","prefixes":["libcamera-devel","1/2"]}