libcamera: camera_manager: Make the class a singleton

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 <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-05 12:58:20 +02:00
parent f3829b5745
commit 8b0de29c41
3 changed files with 22 additions and 8 deletions

View file

@ -19,15 +19,19 @@ class PipelineHandler;
class CameraManager class CameraManager
{ {
public: public:
CameraManager();
int start(); int start();
void stop(); void stop();
std::vector<std::string> list() const; std::vector<std::string> list() const;
Camera *get(const std::string &name); Camera *get(const std::string &name);
static CameraManager *instance();
private: private:
CameraManager();
CameraManager(const CameraManager &) = delete;
void operator=(const CameraManager &) = delete;
DeviceEnumerator *enumerator_; DeviceEnumerator *enumerator_;
std::vector<PipelineHandler *> pipes_; std::vector<PipelineHandler *> pipes_;
}; };

View file

@ -161,4 +161,19 @@ Camera *CameraManager::get(const std::string &name)
return nullptr; 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 */ } /* namespace libcamera */

View file

@ -19,10 +19,7 @@ class ListTest : public Test
protected: protected:
int init() int init()
{ {
cm = new CameraManager(); cm = CameraManager::instance();
if (!cm)
return -ENOMEM;
cm->start(); cm->start();
return 0; return 0;
@ -43,8 +40,6 @@ protected:
void cleanup() void cleanup()
{ {
cm->stop(); cm->stop();
delete cm;
} }
private: private: