[libcamera-devel,v2,7/8] android: CameraDevice: Manage staticMetadata_ with std::unique_ptr
diff mbox series

Message ID 20210324070757.3530377-8-hiroh@chromium.org
State Accepted
Headers show
Series
  • Improve pointer types in android HAL adaptation layer
Related show

Commit Message

Hirokazu Honda March 24, 2021, 7:07 a.m. UTC
staticMetadata_ in CameraDevice is not necessary to be a raw
pointer. This reduces the manual new/delete code by changing the
type to std::unique_ptr.

Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/android/camera_device.cpp | 14 ++++----------
 src/android/camera_device.h   |  2 +-
 2 files changed, 5 insertions(+), 11 deletions(-)

--
2.31.0.291.g576ba9dcdaf-goog

Comments

Jacopo Mondi March 25, 2021, 8:52 a.m. UTC | #1
Hi Hiro

On Wed, Mar 24, 2021 at 04:07:56PM +0900, Hirokazu Honda wrote:
> staticMetadata_ in CameraDevice is not necessary to be a raw
> pointer. This reduces the manual new/delete code by changing the
> type to std::unique_ptr.
>
> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
  j

> ---
>  src/android/camera_device.cpp | 14 ++++----------
>  src/android/camera_device.h   |  2 +-
>  2 files changed, 5 insertions(+), 11 deletions(-)
>
> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
> index e75fd25f..8fdcb85a 100644
> --- a/src/android/camera_device.cpp
> +++ b/src/android/camera_device.cpp
> @@ -311,8 +311,7 @@ CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor() = default;
>
>  CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)
>  	: id_(id), running_(false), camera_(std::move(camera)),
> -	  staticMetadata_(nullptr), facing_(CAMERA_FACING_FRONT),
> -	  orientation_(0)
> +	  facing_(CAMERA_FACING_FRONT), orientation_(0)
>  {
>  	camera_->requestCompleted.connect(this, &CameraDevice::requestComplete);
>
> @@ -341,9 +340,6 @@ CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)
>
>  CameraDevice::~CameraDevice()
>  {
> -	if (staticMetadata_)
> -		delete staticMetadata_;
> -
>  	for (auto &it : requestTemplates_)
>  		delete it.second;
>  }
> @@ -732,11 +728,10 @@ const camera_metadata_t *CameraDevice::getStaticMetadata()
>  	uint32_t numEntries;
>  	uint32_t byteSize;
>  	std::tie(numEntries, byteSize) = calculateStaticMetadataSize();
> -	staticMetadata_ = new CameraMetadata(numEntries, byteSize);
> +	staticMetadata_ = std::make_unique<CameraMetadata>(numEntries, byteSize);
>  	if (!staticMetadata_->isValid()) {
>  		LOG(HAL, Error) << "Failed to allocate static metadata";
> -		delete staticMetadata_;
> -		staticMetadata_ = nullptr;
> +		staticMetadata_.reset();
>  		return nullptr;
>  	}
>
> @@ -1356,8 +1351,7 @@ const camera_metadata_t *CameraDevice::getStaticMetadata()
>
>  	if (!staticMetadata_->isValid()) {
>  		LOG(HAL, Error) << "Failed to construct static metadata";
> -		delete staticMetadata_;
> -		staticMetadata_ = nullptr;
> +		staticMetadata_.reset();
>  		return nullptr;
>  	}
>
> diff --git a/src/android/camera_device.h b/src/android/camera_device.h
> index a11cf243..cea82b24 100644
> --- a/src/android/camera_device.h
> +++ b/src/android/camera_device.h
> @@ -113,7 +113,7 @@ private:
>  	std::shared_ptr<libcamera::Camera> camera_;
>  	std::unique_ptr<libcamera::CameraConfiguration> config_;
>
> -	CameraMetadata *staticMetadata_;
> +	std::unique_ptr<CameraMetadata> staticMetadata_;
>  	std::map<unsigned int, const CameraMetadata *> requestTemplates_;
>  	const camera3_callback_ops_t *callbacks_;
>
> --
> 2.31.0.291.g576ba9dcdaf-goog
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch
diff mbox series

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index e75fd25f..8fdcb85a 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -311,8 +311,7 @@  CameraDevice::Camera3RequestDescriptor::~Camera3RequestDescriptor() = default;

 CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)
 	: id_(id), running_(false), camera_(std::move(camera)),
-	  staticMetadata_(nullptr), facing_(CAMERA_FACING_FRONT),
-	  orientation_(0)
+	  facing_(CAMERA_FACING_FRONT), orientation_(0)
 {
 	camera_->requestCompleted.connect(this, &CameraDevice::requestComplete);

@@ -341,9 +340,6 @@  CameraDevice::CameraDevice(unsigned int id, std::shared_ptr<Camera> camera)

 CameraDevice::~CameraDevice()
 {
-	if (staticMetadata_)
-		delete staticMetadata_;
-
 	for (auto &it : requestTemplates_)
 		delete it.second;
 }
@@ -732,11 +728,10 @@  const camera_metadata_t *CameraDevice::getStaticMetadata()
 	uint32_t numEntries;
 	uint32_t byteSize;
 	std::tie(numEntries, byteSize) = calculateStaticMetadataSize();
-	staticMetadata_ = new CameraMetadata(numEntries, byteSize);
+	staticMetadata_ = std::make_unique<CameraMetadata>(numEntries, byteSize);
 	if (!staticMetadata_->isValid()) {
 		LOG(HAL, Error) << "Failed to allocate static metadata";
-		delete staticMetadata_;
-		staticMetadata_ = nullptr;
+		staticMetadata_.reset();
 		return nullptr;
 	}

@@ -1356,8 +1351,7 @@  const camera_metadata_t *CameraDevice::getStaticMetadata()

 	if (!staticMetadata_->isValid()) {
 		LOG(HAL, Error) << "Failed to construct static metadata";
-		delete staticMetadata_;
-		staticMetadata_ = nullptr;
+		staticMetadata_.reset();
 		return nullptr;
 	}

diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index a11cf243..cea82b24 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -113,7 +113,7 @@  private:
 	std::shared_ptr<libcamera::Camera> camera_;
 	std::unique_ptr<libcamera::CameraConfiguration> config_;

-	CameraMetadata *staticMetadata_;
+	std::unique_ptr<CameraMetadata> staticMetadata_;
 	std::map<unsigned int, const CameraMetadata *> requestTemplates_;
 	const camera3_callback_ops_t *callbacks_;