[v2] libcamera: yaml_parser: Make default value templated in `get()`
diff mbox series

Message ID 20240612224614.35807-1-pobrn@protonmail.com
State Accepted
Headers show
Series
  • [v2] libcamera: yaml_parser: Make default value templated in `get()`
Related show

Commit Message

Barnabás Pőcze June 12, 2024, 10:46 p.m. UTC
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` and the default value comes
from a string literal, as the default value string would always
be constructed otherwise, even if not needed.

Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
---
Changes in v2:
 * update documentation

---
 include/libcamera/internal/yaml_parser.h | 6 +++---
 src/libcamera/yaml_parser.cpp            | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

--
2.45.2

Comments

Laurent Pinchart June 12, 2024, 11:11 p.m. UTC | #1
Hi Barnabás,

Thank you for the patch.

On Wed, Jun 12, 2024 at 10:46:16PM +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` and the default value comes
> from a string literal, as the default value string would always
> be constructed otherwise, even if not needed.
> 
> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
> Changes in v2:
>  * update documentation
> 
> ---
>  include/libcamera/internal/yaml_parser.h | 6 +++---
>  src/libcamera/yaml_parser.cpp            | 2 +-
>  2 files changed, 4 insertions(+), 4 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__
> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
> index aac9a2bd..b68a44c1 100644
> --- a/src/libcamera/yaml_parser.cpp
> +++ b/src/libcamera/yaml_parser.cpp
> @@ -104,7 +104,7 @@ std::size_t YamlObject::size() const
>   */
> 
>  /**
> - * \fn template<typename T> YamlObject::get<T>(const T &defaultValue) const
> + * \fn template<typename T, typename U> YamlObject::get<T>(U &&defaultValue) const
>   * \brief Parse the YamlObject as a \a T value
>   * \param[in] defaultValue The default value when failing to parse
>   *
Kieran Bingham June 12, 2024, 11:13 p.m. UTC | #2
Quoting Barnabás Pőcze (2024-06-12 23:46:16)
> 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` and the default value comes
> from a string literal, as the default value string would always
> be constructed otherwise, even if not needed.
> 
> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>
> ---
> Changes in v2:
>  * update documentation

Thanks - fully green:
 - https://gitlab.freedesktop.org/camera/libcamera/-/pipelines/1199993

I had already provided a tag for this on v1, so I think it still holds:

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
 
> ---
>  include/libcamera/internal/yaml_parser.h | 6 +++---
>  src/libcamera/yaml_parser.cpp            | 2 +-
>  2 files changed, 4 insertions(+), 4 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__
> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
> index aac9a2bd..b68a44c1 100644
> --- a/src/libcamera/yaml_parser.cpp
> +++ b/src/libcamera/yaml_parser.cpp
> @@ -104,7 +104,7 @@ std::size_t YamlObject::size() const
>   */
> 
>  /**
> - * \fn template<typename T> YamlObject::get<T>(const T &defaultValue) const
> + * \fn template<typename T, typename U> YamlObject::get<T>(U &&defaultValue) const
>   * \brief Parse the YamlObject as a \a T value
>   * \param[in] defaultValue The default value when failing to parse
>   *
> --
> 2.45.2
>

Patch
diff mbox series

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__
diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp
index aac9a2bd..b68a44c1 100644
--- a/src/libcamera/yaml_parser.cpp
+++ b/src/libcamera/yaml_parser.cpp
@@ -104,7 +104,7 @@  std::size_t YamlObject::size() const
  */

 /**
- * \fn template<typename T> YamlObject::get<T>(const T &defaultValue) const
+ * \fn template<typename T, typename U> YamlObject::get<T>(U &&defaultValue) const
  * \brief Parse the YamlObject as a \a T value
  * \param[in] defaultValue The default value when failing to parse
  *