libcamera: camera_manager: Use std::unique_ptr to store event dispatcher

The CameraManager takes ownership of the dispatcher passed to the
setEventDispatcher() function. Enforces this by using std::unique_ptr<>.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart 2019-01-18 01:49:17 +02:00
parent 0908669834
commit 32bf7ef239
2 changed files with 9 additions and 7 deletions

View file

@ -29,7 +29,7 @@ public:
static CameraManager *instance(); static CameraManager *instance();
void setEventDispatcher(EventDispatcher *dispatcher); void setEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher);
EventDispatcher *eventDispatcher(); EventDispatcher *eventDispatcher();
private: private:
@ -41,7 +41,7 @@ private:
std::unique_ptr<DeviceEnumerator> enumerator_; std::unique_ptr<DeviceEnumerator> enumerator_;
std::vector<PipelineHandler *> pipes_; std::vector<PipelineHandler *> pipes_;
EventDispatcher *dispatcher_; std::unique_ptr<EventDispatcher> dispatcher_;
}; };
} /* namespace libcamera */ } /* namespace libcamera */

View file

@ -12,6 +12,7 @@
#include "event_dispatcher_poll.h" #include "event_dispatcher_poll.h"
#include "log.h" #include "log.h"
#include "pipeline_handler.h" #include "pipeline_handler.h"
#include "utils.h"
/** /**
* \file camera_manager.h * \file camera_manager.h
@ -58,7 +59,6 @@ CameraManager::CameraManager()
CameraManager::~CameraManager() CameraManager::~CameraManager()
{ {
delete dispatcher_;
} }
/** /**
@ -209,14 +209,14 @@ CameraManager *CameraManager::instance()
* The CameraManager takes ownership of the event dispatcher and will delete it * The CameraManager takes ownership of the event dispatcher and will delete it
* when the application terminates. * when the application terminates.
*/ */
void CameraManager::setEventDispatcher(EventDispatcher *dispatcher) void CameraManager::setEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher)
{ {
if (dispatcher_) { if (dispatcher_) {
LOG(Warning) << "Event dispatcher is already set"; LOG(Warning) << "Event dispatcher is already set";
return; return;
} }
dispatcher_ = dispatcher; dispatcher_ = std::move(dispatcher);
} }
/** /**
@ -226,14 +226,16 @@ void CameraManager::setEventDispatcher(EventDispatcher *dispatcher)
* If no dispatcher has been set, a default poll-based implementation is created * If no dispatcher has been set, a default poll-based implementation is created
* and returned, and no custom event dispatcher may be installed anymore. * and returned, and no custom event dispatcher may be installed anymore.
* *
* The returned event dispatcher is valid until the camera manager is destroyed.
*
* \return Pointer to the event dispatcher * \return Pointer to the event dispatcher
*/ */
EventDispatcher *CameraManager::eventDispatcher() EventDispatcher *CameraManager::eventDispatcher()
{ {
if (!dispatcher_) if (!dispatcher_)
dispatcher_ = new EventDispatcherPoll(); dispatcher_ = utils::make_unique<EventDispatcherPoll>();
return dispatcher_; return dispatcher_.get();
} }
} /* namespace libcamera */ } /* namespace libcamera */