From patchwork Mon Jan 7 23:11:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 167 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D2A1B60B31 for ; Tue, 8 Jan 2019 00:10:48 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 707A8E4E for ; Tue, 8 Jan 2019 00:10:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1546902648; bh=UJI6UaC1EAZPRNdZguGohhk0zzUbkT6HuCDJAn3gmjM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=l8YXNdCfEDginB8ahFaBQ6kQExpj3/sKlrKqCrwj+1K46iJp6ByonRzTG2reQMca9 yOkpzVACUvYCoaBy8sEJjZxkALGLEw/pauhbyXrHIoHZqV3qCHoVhpDA5i6agQkDq3 ltlFuYVHmf2OWWdxdWDTOnUuZh5sBVuqkU1hzZHk= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 8 Jan 2019 01:11:44 +0200 Message-Id: <20190107231151.23291-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> References: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 04/11] libcamera: camera_manager: Make the class a singleton X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Jan 2019 23:10:49 -0000 There can only be a single camera manager instance in the application. Creating it as a singleton helps avoiding mistakes. It also allows the camera manager to be used as a storage of global data, such as the future event dispatcher. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- Changes since v1: - Delete copy constructor and assignment operator - Fix documentation style issue --- include/libcamera/camera_manager.h | 8 ++++++-- src/libcamera/camera_manager.cpp | 15 +++++++++++++++ test/list.cpp | 7 +------ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h index 2768a5bd2384..e14da0f893b3 100644 --- a/include/libcamera/camera_manager.h +++ b/include/libcamera/camera_manager.h @@ -19,15 +19,19 @@ class PipelineHandler; class CameraManager { public: - CameraManager(); - int start(); void stop(); std::vector list() const; Camera *get(const std::string &name); + static CameraManager *instance(); + private: + CameraManager(); + CameraManager(const CameraManager &) = delete; + void operator=(const CameraManager &) = delete; + DeviceEnumerator *enumerator_; std::vector pipes_; }; diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index 50a805fc665c..1a9d2f38e3b9 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -161,4 +161,19 @@ Camera *CameraManager::get(const std::string &name) return nullptr; } +/** + * \brief Retrieve the camera manager instance + * + * The CameraManager is a singleton and can't be constructed manually. This + * function shall instead be used to retrieve the single global instance of the + * manager. + * + * \return The camera manager instance + */ +CameraManager *CameraManager::instance() +{ + static CameraManager manager; + return &manager; +} + } /* namespace libcamera */ diff --git a/test/list.cpp b/test/list.cpp index 39b8a41d1fef..e2026c99c5b8 100644 --- a/test/list.cpp +++ b/test/list.cpp @@ -19,10 +19,7 @@ class ListTest : public Test protected: int init() { - cm = new CameraManager(); - if (!cm) - return -ENOMEM; - + cm = CameraManager::instance(); cm->start(); return 0; @@ -43,8 +40,6 @@ protected: void cleanup() { cm->stop(); - - delete cm; } private: