[libcamera-devel,2/9] utils: ipc: Add support for enums in function parameters
diff mbox series

Message ID 20220803112150.3040287-3-paul.elder@ideasonboard.com
State Superseded
Headers show
Series
  • utils: ipc: Add support for enums and Flags
Related show

Commit Message

Paul Elder Aug. 3, 2022, 11:21 a.m. UTC
There is already support for enums as struct members, but there was no
support for enums in function parameters. Add it.

This does not add support for returning enums as direct return values.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 .../libcamera_templates/proxy_functions.tmpl        | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Laurent Pinchart Aug. 9, 2022, 1:04 a.m. UTC | #1
Hi Paul,

Thank you for the patch.

On Wed, Aug 03, 2022 at 08:21:43PM +0900, Paul Elder via libcamera-devel wrote:
> There is already support for enums as struct members, but there was no
> support for enums in function parameters. Add it.
> 
> This does not add support for returning enums as direct return values.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
> ---
>  .../libcamera_templates/proxy_functions.tmpl        | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl b/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl
> index bac826a7..dc35620f 100644
> --- a/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl
> +++ b/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl
> @@ -59,7 +59,11 @@
>  {%- else %}
>  	std::tie({{param.mojom_name}}Buf, std::ignore) =
>  {%- endif %}
> +{%- if param|is_enum %}
> +		IPADataSerializer<uint32_t>::serialize(static_cast<uint32_t>({{param.mojom_name}})

C++ has a concept of underlying type for enumerations (see
https://en.cppreference.com/w/cpp/language/enum and
https://en.cppreference.com/w/cpp/types/underlying_type). We could use
that to serialize to an integer type suitable for the enum, but I think
that's overkill. However, it would be good to make sure we don't
silently truncate the value here. A

static_assert(sizeof({{param|name_full}}) <= 4);

would work. It can be placed in either the serialization or
deserializatin code, once should be enough. With that,

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

> +{%- else %}
>  		IPADataSerializer<{{param|name}}>::serialize({{param.mojom_name}}
> +{% endif -%}
>  {{- ", &controlSerializer_" if param|needs_control_serializer -}}
>  );
>  {%- endfor %}
> @@ -97,7 +101,12 @@
>   # This code is meant to be used by macro deserialize_call.
>   #}
>  {%- macro deserialize_param(param, pointer, loop, buf, fds, iter, data_size) -%}
> -{{"*" if pointer}}{{param.mojom_name}} = IPADataSerializer<{{param|name}}>::deserialize(
> +{{"*" if pointer}}{{param.mojom_name}} =
> +{%- if param|is_enum %}
> +static_cast<{{param|name_full}}>(IPADataSerializer<uint32_t>::deserialize(
> +{%- else %}
> +IPADataSerializer<{{param|name}}>::deserialize(
> +{%- endif %}
>  	{{buf}}{{- ".cbegin()" if not iter}} + {{param.mojom_name}}Start,
>  {%- if loop.last and not iter %}
>  	{{buf}}.cend()
> @@ -121,7 +130,7 @@
>  {%- if param|needs_control_serializer %}
>  	&controlSerializer_
>  {%- endif -%}
> -);
> +){{")" if param|is_enum}};
>  {%- endmacro -%}
>  
>

Patch
diff mbox series

diff --git a/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl b/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl
index bac826a7..dc35620f 100644
--- a/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl
+++ b/utils/ipc/generators/libcamera_templates/proxy_functions.tmpl
@@ -59,7 +59,11 @@ 
 {%- else %}
 	std::tie({{param.mojom_name}}Buf, std::ignore) =
 {%- endif %}
+{%- if param|is_enum %}
+		IPADataSerializer<uint32_t>::serialize(static_cast<uint32_t>({{param.mojom_name}})
+{%- else %}
 		IPADataSerializer<{{param|name}}>::serialize({{param.mojom_name}}
+{% endif -%}
 {{- ", &controlSerializer_" if param|needs_control_serializer -}}
 );
 {%- endfor %}
@@ -97,7 +101,12 @@ 
  # This code is meant to be used by macro deserialize_call.
  #}
 {%- macro deserialize_param(param, pointer, loop, buf, fds, iter, data_size) -%}
-{{"*" if pointer}}{{param.mojom_name}} = IPADataSerializer<{{param|name}}>::deserialize(
+{{"*" if pointer}}{{param.mojom_name}} =
+{%- if param|is_enum %}
+static_cast<{{param|name_full}}>(IPADataSerializer<uint32_t>::deserialize(
+{%- else %}
+IPADataSerializer<{{param|name}}>::deserialize(
+{%- endif %}
 	{{buf}}{{- ".cbegin()" if not iter}} + {{param.mojom_name}}Start,
 {%- if loop.last and not iter %}
 	{{buf}}.cend()
@@ -121,7 +130,7 @@ 
 {%- if param|needs_control_serializer %}
 	&controlSerializer_
 {%- endif -%}
-);
+){{")" if param|is_enum}};
 {%- endmacro -%}