libcamera: yaml_parser: Replace ok flag to get() with std::optional

The YamlObject::get() function takes a default value and an optional
bool ok flag to handle parsing errors. This ad-hoc mechanism complicates
error handling in callers.

A better API is possible by dropping the default value and ok flag and
returning an std::optional. Not only does it simplify the calls, it also
lets callers handle errors through the standard std::optional class
instead of the current ad-hoc mechanism.

Provide a get() wrapper around std::optional::value_or() to further
simplify callers that don't need any specific error handling.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
Tested-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart 2022-07-26 04:33:19 +03:00
parent 22ffeae04d
commit feb8c9be78
3 changed files with 96 additions and 122 deletions

View file

@ -9,6 +9,7 @@
#include <iterator>
#include <map>
#include <optional>
#include <string>
#include <vector>
@ -165,7 +166,13 @@ public:
#else
template<typename T>
#endif
T get(const T &defaultValue, bool *ok = nullptr) const;
std::optional<T> get() const;
template<typename T>
T get(const T &defaultValue) const
{
return get<T>().value_or(defaultValue);
}
DictAdapter asDict() const { return DictAdapter{ dictionary_ }; }
ListAdapter asList() const { return ListAdapter{ list_ }; }