[PATCH v1] libcamera: yaml_parser: Make default value templated in `get()`

Laurent Pinchart laurent.pinchart at ideasonboard.com
Tue May 21 16:18:35 CEST 2024


Hi Barnabás,

Thank you for the patch.

On Mon, May 20, 2024 at 04:02:50AM +0000, Barnabás Pőcze wrote:
> This way the construction of the default value of type `T`
> can be delayed until it is really needed, which is useful,
> for example when `T == std::string`, as the default value
> string would always be constructed otherwise.

That's interesting. Is that all it takes to defer the construction of
the default value ?

I implemented a different solution a while ago, which was merged in
48c106429a19 ("libcamera: base: utils: Provide defopt to simplify
std::optional::value_or() usage") but had to be reverted due to
compilation issues with gcc 8.0.0 to 8.3.0 (which we care about due to
Debian 10 still being supported, although we may drop that once it
reaches EOL at end of June this year).

> Signed-off-by: Barnabás Pőcze <pobrn at protonmail.com>
> ---
>  include/libcamera/internal/yaml_parser.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
> index b6979d73..3ac27e06 100644
> --- a/include/libcamera/internal/yaml_parser.h
> +++ b/include/libcamera/internal/yaml_parser.h
> @@ -179,10 +179,10 @@ public:
>  #endif
>  	std::optional<T> get() const;
>  
> -	template<typename T>
> -	T get(const T &defaultValue) const
> +	template<typename T, typename U>
> +	T get(U &&defaultValue) const
>  	{
> -		return get<T>().value_or(defaultValue);
> +		return get<T>().value_or(std::forward<U>(defaultValue));
>  	}
>  
>  #ifndef __DOXYGEN__

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list