libcamera: camera_manager: Turn enumerator into a unique_ptr<>

Convey the fact that the CameraManager class owns the DeviceEnumerator
instance it creates by using std::unique_ptr<> to store the pointer.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart 2019-01-15 15:32:59 +02:00
parent a2f095947f
commit 62eae99ed2
4 changed files with 10 additions and 13 deletions

View file

@ -7,6 +7,7 @@
#ifndef __LIBCAMERA_CAMERA_MANAGER_H__ #ifndef __LIBCAMERA_CAMERA_MANAGER_H__
#define __LIBCAMERA_CAMERA_MANAGER_H__ #define __LIBCAMERA_CAMERA_MANAGER_H__
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -37,7 +38,7 @@ private:
void operator=(const CameraManager &) = delete; void operator=(const CameraManager &) = delete;
~CameraManager(); ~CameraManager();
DeviceEnumerator *enumerator_; std::unique_ptr<DeviceEnumerator> enumerator_;
std::vector<PipelineHandler *> pipes_; std::vector<PipelineHandler *> pipes_;
EventDispatcher *dispatcher_; EventDispatcher *dispatcher_;

View file

@ -73,7 +73,6 @@ CameraManager::~CameraManager()
*/ */
int CameraManager::start() int CameraManager::start()
{ {
if (enumerator_) if (enumerator_)
return -ENODEV; return -ENODEV;
@ -95,7 +94,7 @@ int CameraManager::start()
*/ */
while (1) { while (1) {
PipelineHandler *pipe = factory->create(); PipelineHandler *pipe = factory->create();
if (!pipe->match(enumerator_)) { if (!pipe->match(enumerator_.get())) {
delete pipe; delete pipe;
break; break;
} }
@ -130,10 +129,7 @@ void CameraManager::stop()
pipes_.clear(); pipes_.clear();
if (enumerator_) enumerator_.reset(nullptr);
delete enumerator_;
enumerator_ = nullptr;
} }
/** /**

View file

@ -14,6 +14,7 @@
#include "device_enumerator.h" #include "device_enumerator.h"
#include "log.h" #include "log.h"
#include "media_device.h" #include "media_device.h"
#include "utils.h"
/** /**
* \file device_enumerator.h * \file device_enumerator.h
@ -128,20 +129,18 @@ bool DeviceMatch::match(const MediaDevice *device) const
* \return A pointer to the newly created device enumerator on success, or * \return A pointer to the newly created device enumerator on success, or
* nullptr if an error occurs * nullptr if an error occurs
*/ */
DeviceEnumerator *DeviceEnumerator::create() std::unique_ptr<DeviceEnumerator> DeviceEnumerator::create()
{ {
DeviceEnumerator *enumerator; std::unique_ptr<DeviceEnumerator> enumerator;
/** /**
* \todo Add compile time checks to only try udev enumerator if libudev * \todo Add compile time checks to only try udev enumerator if libudev
* is available. * is available.
*/ */
enumerator = new DeviceEnumeratorUdev(); enumerator = utils::make_unique<DeviceEnumeratorUdev>();
if (!enumerator->init()) if (!enumerator->init())
return enumerator; return enumerator;
delete enumerator;
/* /*
* Either udev is not available or udev initialization failed. Fall back * Either udev is not available or udev initialization failed. Fall back
* on the sysfs enumerator. * on the sysfs enumerator.

View file

@ -8,6 +8,7 @@
#define __LIBCAMERA_DEVICE_ENUMERATOR_H__ #define __LIBCAMERA_DEVICE_ENUMERATOR_H__
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -34,7 +35,7 @@ private:
class DeviceEnumerator class DeviceEnumerator
{ {
public: public:
static DeviceEnumerator *create(); static std::unique_ptr<DeviceEnumerator> create();
virtual ~DeviceEnumerator(); virtual ~DeviceEnumerator();