summaryrefslogtreecommitdiff
path: root/src/libcamera/camera.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-01-17 16:23:25 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-01-21 11:13:53 +0200
commit21ff749a790386f87e767a690c77948a6474ceaa (patch)
treeb50d8edf0846336f5cf85f69fe653fa32c695059 /src/libcamera/camera.cpp
parentf3695e9b09ce4a88d6e0480d9e5058673af34a49 (diff)
libcamera: camera: Handle camera objects through shared pointers
The Camera class is explicitly reference-counted to manage the lifetime of camera objects. Replace this open-coded implementation with usage of the std::shared_ptr<> class. This API change prevents pipeline handlers from subclassing the Camera class. This isn't deemed to be an issue. Mark the class final to make this explicit. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/libcamera/camera.cpp')
-rw-r--r--src/libcamera/camera.cpp46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp
index 6da2b201..acf912be 100644
--- a/src/libcamera/camera.cpp
+++ b/src/libcamera/camera.cpp
@@ -41,19 +41,36 @@ namespace libcamera {
* streams from a single image source. It provides the main interface to
* configuring and controlling the device, and capturing image streams. It is
* the central object exposed by libcamera.
+ *
+ * To support the central nature of Camera objects, libcamera manages the
+ * lifetime of camera instances with std::shared_ptr<>. Instances shall be
+ * created with the create() function which returns a shared pointer. The
+ * Camera constructors and destructor are private, to prevent instances from
+ * being constructed and destroyed manually.
*/
/**
- * \brief Construct a named camera device
+ * \brief Create a camera instance
+ * \param[in] name The name of the camera device
*
- * \param[in] name The name to set on the camera device
+ * The caller is responsible for guaranteeing unicity of the camera name.
*
- * The caller is responsible for guaranteeing unicity of the camera
- * device name.
+ * \return A shared pointer to the newly created camera object
*/
-Camera::Camera(const std::string &name)
- : ref_(1), name_(name)
+std::shared_ptr<Camera> Camera::create(const std::string &name)
{
+ struct Allocator : std::allocator<Camera> {
+ void construct(void *p, const std::string &name)
+ {
+ ::new(p) Camera(name);
+ }
+ void destroy(Camera *p)
+ {
+ p->~Camera();
+ }
+ };
+
+ return std::allocate_shared<Camera>(Allocator(), name);
}
/**
@@ -66,24 +83,13 @@ const std::string &Camera::name() const
return name_;
}
-/**
- * \brief Acquire a reference to the camera
- */
-void Camera::get()
+Camera::Camera(const std::string &name)
+ : name_(name)
{
- ref_++;
}
-/**
- * \brief Release a reference to the camera
- *
- * When the last reference is released the camera device is deleted. Callers
- * shall not access the camera device after calling this function.
- */
-void Camera::put()
+Camera::~Camera()
{
- if (--ref_ == 0)
- delete this;
}
} /* namespace libcamera */