libcamera: controls: Use std::optional to handle invalid control values

Previously, ControlList::get<T>() would use default constructed objects to
indicate that a ControlList does not have the requested Control. This has
several disadvantages: 1) It requires types to be default constructible,
2) it does not differentiate between a default constructed object and an
object that happens to have the same state as a default constructed object.

std::optional<T> additionally stores the information if the object is valid
or not, and therefore is more expressive than a default constructed object.

Signed-off-by: Christian Rauch <Rauch.Christian@gmx.de>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Christian Rauch 2022-07-05 10:55:48 +01:00 committed by Laurent Pinchart
parent ef77e26379
commit 1c4d480185
10 changed files with 44 additions and 44 deletions

View file

@ -8,6 +8,7 @@
#pragma once
#include <assert.h>
#include <optional>
#include <set>
#include <stdint.h>
#include <string>
@ -373,11 +374,11 @@ public:
bool contains(unsigned int id) const;
template<typename T>
T get(const Control<T> &ctrl) const
std::optional<T> get(const Control<T> &ctrl) const
{
const ControlValue *val = find(ctrl.id());
if (!val)
return T{};
return std::nullopt;
return val->get<T>();
}