{"id":10108,"url":"https://patchwork.libcamera.org/api/1.1/patches/10108/?format=json","web_url":"https://patchwork.libcamera.org/patch/10108/","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":"<20201020014005.12783-3-laurent.pinchart@ideasonboard.com>","date":"2020-10-20T01:40:02","name":"[libcamera-devel,v2,2/5] libcamera: Add a base class to implement the d-pointer design pattern","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"a0194af7b94812d3cc7cc3823a080c8fe9402ec2","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/10108/mbox/","series":[{"id":1386,"url":"https://patchwork.libcamera.org/api/1.1/series/1386/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1386","date":"2020-10-20T01:40:00","name":"libcamera: Implement d-pointer design pattern","version":2,"mbox":"https://patchwork.libcamera.org/series/1386/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10108/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10108/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 9F32ABDB1F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 20 Oct 2020 01:41:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 68A1861394;\n\tTue, 20 Oct 2020 03:41:00 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E4FE660CE6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 20 Oct 2020 03:40:56 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8633E52\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 20 Oct 2020 03:40:56 +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=\"ivY+SFIO\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1603158056;\n\tbh=HJn3opyIsMo9C4szyNA7FaeB56wPdvisJKO6yY0yO98=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=ivY+SFIOinbX0W8LViQMGuctgBGP+I97QPMcEKG+8uZkQqlZrktwCpUEkOmjZttas\n\t2RvqeLQZ1eXPIuB2XfrUOSYcpWR8tEJjC1ZWmenieaSspv5whkeb8DIR6ZH4M2CpfO\n\tk6uGJ4uq3dmPV3yfkjIamHqpbuYMKGpRp0kLhS8g=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 20 Oct 2020 04:40:02 +0300","Message-Id":"<20201020014005.12783-3-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20201020014005.12783-1-laurent.pinchart@ideasonboard.com>","References":"<20201020014005.12783-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v2 2/5] libcamera: Add a base class to\n\timplement the d-pointer design pattern","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=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The d-pointer design patterns helps creating public classes that can be\nextended without breaking their ABI. To facilitate usage of the pattern\nin libcamera, create a base Extensible class with associated macros.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\nChanges since v1:\n\n- Don't hide variable declarations in LIBCAMERA_[DO]_PTR macros\n- Extend documentation\n- Fix typos\n---\n include/libcamera/extensible.h |  86 +++++++++++++++++++++\n include/libcamera/meson.build  |   1 +\n src/libcamera/extensible.cpp   | 134 +++++++++++++++++++++++++++++++++\n src/libcamera/meson.build      |   1 +\n 4 files changed, 222 insertions(+)\n create mode 100644 include/libcamera/extensible.h\n create mode 100644 src/libcamera/extensible.cpp","diff":"diff --git a/include/libcamera/extensible.h b/include/libcamera/extensible.h\nnew file mode 100644\nindex 000000000000..ea8808ad3e3c\n--- /dev/null\n+++ b/include/libcamera/extensible.h\n@@ -0,0 +1,86 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * extensible.h - Utilities to create extensible public classes with stable ABIs\n+ */\n+#ifndef __LIBCAMERA_EXTENSIBLE_H__\n+#define __LIBCAMERA_EXTENSIBLE_H__\n+\n+#include <memory>\n+\n+namespace libcamera {\n+\n+#ifndef __DOXYGEN__\n+#define LIBCAMERA_DECLARE_PRIVATE(klass)\t\t\t\t\\\n+public:\t\t\t\t\t\t\t\t\t\\\n+\tclass Private;\t\t\t\t\t\t\t\\\n+\tfriend class Private;\n+\n+#define LIBCAMERA_DECLARE_PUBLIC(klass)\t\t\t\t\t\\\n+\tfriend class klass;\n+\n+#define LIBCAMERA_D_PTR(klass)\t\t\t\t\t\t\\\n+\t_d<klass::Private>();\n+\n+#define LIBCAMERA_O_PTR(klass)\t\t\t\t\t\t\\\n+\t_o<klass>();\n+\n+#else\n+#define LIBCAMERA_DECLARE_PRIVATE(klass)\n+#define LIBCAMERA_DECLARE_PUBLIC(klass)\n+#define LIBCAMERA_D_PTR(klass)\n+#define LIBCAMERA_O_PTR(klass)\n+#endif\n+\n+class Extensible\n+{\n+public:\n+\tclass Private\n+\t{\n+\tpublic:\n+\t\tPrivate(Extensible *o);\n+\t\tvirtual ~Private();\n+\n+#ifndef __DOXYGEN__\n+\t\ttemplate<typename T>\n+\t\tconst T *_o() const\n+\t\t{\n+\t\t\treturn static_cast<const T *>(o_);\n+\t\t}\n+\n+\t\ttemplate<typename T>\n+\t\tT *_o()\n+\t\t{\n+\t\t\treturn static_cast<T *>(o_);\n+\t\t}\n+#endif\n+\n+\tprivate:\n+\t\tExtensible * const o_;\n+\t};\n+\n+\tExtensible(Private *d);\n+\n+protected:\n+#ifndef __DOXYGEN__\n+\ttemplate<typename T>\n+\tconst T *_d() const\n+\t{\n+\t\treturn static_cast<const T *>(d_.get());\n+\t}\n+\n+\ttemplate<typename T>\n+\tT *_d()\n+\t{\n+\t\treturn static_cast<T*>(d_.get());\n+\t}\n+#endif\n+\n+private:\n+\tconst std::unique_ptr<Private> d_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_EXTENSIBLE_H__ */\ndiff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\nindex 83bc46729314..15e6b43c9585 100644\n--- a/include/libcamera/meson.build\n+++ b/include/libcamera/meson.build\n@@ -8,6 +8,7 @@ libcamera_public_headers = files([\n     'controls.h',\n     'event_dispatcher.h',\n     'event_notifier.h',\n+    'extensible.h',\n     'file_descriptor.h',\n     'flags.h',\n     'framebuffer_allocator.h',\ndiff --git a/src/libcamera/extensible.cpp b/src/libcamera/extensible.cpp\nnew file mode 100644\nindex 000000000000..1dcb0bf1b12f\n--- /dev/null\n+++ b/src/libcamera/extensible.cpp\n@@ -0,0 +1,134 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Google Inc.\n+ *\n+ * extensible.cpp - Utilities to create extensible public classes with stable ABIs\n+ */\n+\n+#include <libcamera/extensible.h>\n+\n+/**\n+ * \\file extensible.h\n+ * \\brief Utilities to create extensible public classes with stable ABIs\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\def LIBCAMERA_DECLARE_PRIVATE\n+ * \\brief Declare private data for a public class\n+ * \\param klass The public class name\n+ *\n+ * The LIBCAMERA_DECLARE_PRIVATE() macro plumbs the infrastructure necessary to\n+ * make a class manage its private data through a d-pointer. It shall be used at\n+ * the very top of the class definition, with the public class name passed as\n+ * the \\a klass parameter.\n+ */\n+\n+/**\n+ * \\def LIBCAMERA_DECLARE_PUBLIC\n+ * \\brief Declare public data for a private class\n+ * \\param klass The public class name\n+ *\n+ * The LIBCAMERA_DECLARE_PUBLIC() macro is the counterpart of\n+ * LIBCAMERA_DECLARE_PRIVATE() to be used in the private data class. It shall be\n+ * used at the very top of the private class definition, with the public class\n+ * name passed as the \\a klass parameter.\n+ */\n+\n+/**\n+ * \\def LIBCAMERA_D_PTR(klass)\n+ * \\brief Retrieve the private data pointer\n+ * \\param[in] klass The public class name\n+ *\n+ * This macro can be used in any member function of a class that inherits,\n+ * directly or indirectly, from the Extensible class, to create a local\n+ * variable named 'd' that points to the class' private data instance.\n+ */\n+\n+/**\n+ * \\def LIBCAMERA_O_PTR(klass)\n+ * \\brief Retrieve the public instance corresponding to the private data\n+ * \\param[in] klass The public class name\n+ *\n+ * This macro is the counterpart of LIBCAMERA_D_PTR() for private data classes.\n+ * It can be used in any member function of the private data class to create a\n+ * local variable named 'o' that points to the public class instance\n+ * corresponding to the private data.\n+ */\n+\n+/**\n+ * \\class Extensible\n+ * \\brief Base class to manage private data through a d-pointer\n+ *\n+ * The Extensible class provides a base class to implement the\n+ * <a href=\"https://wiki.qt.io/D-Pointer\">d-pointer</a> design pattern (also\n+ * known as <a href=\"https://en.wikipedia.org/wiki/Opaque_pointer\">opaque pointer</a>\n+ * or <a href=\"https://en.cppreference.com/w/cpp/language/pimpl\">pImpl idiom</a>).\n+ * It helps creating public classes that can be extended without breaking their\n+ * ABI. Such classes store their private data in a separate private data object,\n+ * referenced by a pointer in the public class (hence the name d-pointer).\n+ *\n+ * Classes that follow this design pattern are referred herein as extensible\n+ * classes. To be extensible, a class PublicClass shall:\n+ *\n+ * - inherit from the Extensible class or from another extensible class\n+ * - invoke the LIBCAMERA_DECLARE_PRIVATE() macro at the very top of the class\n+ *   definition\n+ * - define a private data class named PublicClass::Private that inherits from\n+ *   the Private data class of the base class\n+ * - invoke the LIBCAMERA_DECLARE_PUBLIC() macro at the very top of the Private\n+ *   data class definition\n+ * - pass a pointer to a newly allocated Private data object to the constructor\n+ *   of the base class\n+ *\n+ * Additionally, if the PublicClass is not final, it shall expose one or more\n+ * constructors that takes a pointer to a Private data instance, to be used by\n+ * derived classes.\n+ *\n+ * The Private class is fully opaque to users of the libcamera public API.\n+ * Internally, it can be kept private to the implementation of PublicClass, or\n+ * be exposed to other classes. In the latter case, the members of the Private\n+ * class need to be qualified with appropriate access specifiers. The\n+ * PublicClass and Private classes always have full access to each other's\n+ * protected and private members.\n+ */\n+\n+/**\n+ * \\brief Construct an instance of an Extensible class\n+ * \\param[in] d Pointer to the private data instance\n+ */\n+Extensible::Extensible(Extensible::Private *d)\n+\t: d_(d)\n+{\n+}\n+\n+/**\n+ * \\var Extensible::d_\n+ * \\brief Pointer to the private data instance\n+ */\n+\n+/**\n+ * \\class Extensible::Private\n+ * \\brief Base class for private data managed through a d-pointer\n+ */\n+\n+/**\n+ * \\brief Construct an instance of an Extensible class private data\n+ * \\param[in] o Pointer to the public class object\n+ */\n+Extensible::Private::Private(Extensible *o)\n+\t: o_(o)\n+{\n+}\n+\n+Extensible::Private::~Private()\n+{\n+}\n+\n+/**\n+ * \\var Extensible::Private::o_\n+ * \\brief Pointer to the public class object\n+ */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 47ddb4014a61..b9f6457433f9 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -17,6 +17,7 @@ libcamera_sources = files([\n     'event_dispatcher.cpp',\n     'event_dispatcher_poll.cpp',\n     'event_notifier.cpp',\n+    'extensible.cpp',\n     'file.cpp',\n     'file_descriptor.cpp',\n     'flags.cpp',\n","prefixes":["libcamera-devel","v2","2/5"]}