config: Add configuration retrieval helpers
Let's add some helpers to make accessing simple configuration values simpler. The helpers are used in the followup patches. GlobalConfiguration::option ensures that no value is returned rather than a value of YamlObject::empty. Signed-off-by: Milan Zamazal <mzamazal@redhat.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
committed by
Laurent Pinchart
parent
94236f64ff
commit
17febd7bb3
@@ -8,6 +8,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <libcamera/base/utils.h>
|
||||
|
||||
#include "libcamera/internal/yaml_parser.h"
|
||||
|
||||
@@ -23,6 +28,29 @@ public:
|
||||
unsigned int version() const;
|
||||
Configuration configuration() const;
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> option(
|
||||
const std::initializer_list<std::string_view> confPath) const
|
||||
{
|
||||
const YamlObject *c = &configuration();
|
||||
for (auto part : confPath) {
|
||||
c = &(*c)[part];
|
||||
if (!*c)
|
||||
return {};
|
||||
}
|
||||
return c->get<T>();
|
||||
}
|
||||
|
||||
std::optional<std::vector<std::string>> listOption(
|
||||
const std::initializer_list<std::string_view> confPath) const;
|
||||
std::optional<std::string> envOption(
|
||||
const char *const envVariable,
|
||||
const std::initializer_list<std::string_view> confPath) const;
|
||||
std::optional<std::vector<std::string>> envListOption(
|
||||
const char *const envVariable,
|
||||
const std::initializer_list<std::string_view> confPath,
|
||||
const std::string delimiter = ":") const;
|
||||
|
||||
private:
|
||||
bool loadFile(const std::filesystem::path &fileName);
|
||||
void load();
|
||||
|
||||
Reference in New Issue
Block a user