[libcamera-devel,v8,3/4] libcamera: controls: Generate fixed- and variable-sized Span Controls
diff mbox series

Message ID 20220610120338.96883-4-Rauch.Christian@gmx.de
State Superseded
Headers show
Series
  • generate and use fixed-sized Span Control types
Related show

Commit Message

Christian Rauch June 10, 2022, 12:03 p.m. UTC
This defines Controls with a 'size' as either variable-sized Span<T> or as
fixed-sized Span<T,N>.

Signed-off-by: Christian Rauch <Rauch.Christian@gmx.de>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
---
 utils/gen-controls.py | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

--
2.34.1

Comments

Laurent Pinchart July 4, 2022, 11:52 p.m. UTC | #1
Hi Christian,

Thank you for the patch.

On Fri, Jun 10, 2022 at 01:03:37PM +0100, Christian Rauch via libcamera-devel wrote:
> This defines Controls with a 'size' as either variable-sized Span<T> or as
> fixed-sized Span<T,N>.

This patch alone breaks compilation, it needs to be squashed with 4/4.

> Signed-off-by: Christian Rauch <Rauch.Christian@gmx.de>
> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
> ---
>  utils/gen-controls.py | 32 ++++++++++++++++++++++----------
>  1 file changed, 22 insertions(+), 10 deletions(-)
> 
> diff --git a/utils/gen-controls.py b/utils/gen-controls.py
> index 3f99b5e2..46ba4394 100755
> --- a/utils/gen-controls.py
> +++ b/utils/gen-controls.py
> @@ -7,6 +7,8 @@
>  # gen-controls.py - Generate control definitions from YAML
> 
>  import argparse
> +from functools import reduce
> +import operator
>  import string
>  import sys
>  import yaml
> @@ -22,6 +24,24 @@ def format_description(description):
>      return '\n'.join([(line and ' * ' or ' *') + line for line in description])
> 
> 
> +def get_ctrl_type(ctrl):
> +    ctrl_type = ctrl['type']
> +    ctrl_size_arr = ctrl.get('size')
> +
> +    if ctrl_type == 'string':
> +        return 'std::string'
> +    elif ctrl_size_arr is not None:
> +        if len(ctrl_size_arr) > 0:
> +            # fixed-sized Span
> +            ctrl_span_size = reduce(operator.mul, ctrl_size_arr)
> +            return f"Span<const {ctrl_type}, {ctrl_span_size}>"
> +        else:
> +            # variable-sized Span
> +            return f"Span<const {ctrl_type}>"
> +    else:
> +        return ctrl_type
> +
> +
>  def generate_cpp(controls):
>      enum_doc_start_template = string.Template('''/**
>   * \\enum ${name}Enum
> @@ -50,11 +70,7 @@ ${description}
>          name, ctrl = ctrl.popitem()
>          id_name = snake_case(name).upper()
> 
> -        ctrl_type = ctrl['type']
> -        if ctrl_type == 'string':
> -            ctrl_type = 'std::string'
> -        elif ctrl.get('size'):
> -            ctrl_type = 'Span<const %s>' % ctrl_type
> +        ctrl_type = get_ctrl_type(ctrl)
> 
>          info = {
>              'name': name,
> @@ -135,11 +151,7 @@ def generate_h(controls):
> 
>          ids.append('\t' + id_name + ' = ' + str(id_value) + ',')
> 
> -        ctrl_type = ctrl['type']
> -        if ctrl_type == 'string':
> -            ctrl_type = 'std::string'
> -        elif ctrl.get('size'):
> -            ctrl_type = 'Span<const %s>' % ctrl_type
> +        ctrl_type = get_ctrl_type(ctrl)
> 
>          info = {
>              'name': name,

Patch
diff mbox series

diff --git a/utils/gen-controls.py b/utils/gen-controls.py
index 3f99b5e2..46ba4394 100755
--- a/utils/gen-controls.py
+++ b/utils/gen-controls.py
@@ -7,6 +7,8 @@ 
 # gen-controls.py - Generate control definitions from YAML

 import argparse
+from functools import reduce
+import operator
 import string
 import sys
 import yaml
@@ -22,6 +24,24 @@  def format_description(description):
     return '\n'.join([(line and ' * ' or ' *') + line for line in description])


+def get_ctrl_type(ctrl):
+    ctrl_type = ctrl['type']
+    ctrl_size_arr = ctrl.get('size')
+
+    if ctrl_type == 'string':
+        return 'std::string'
+    elif ctrl_size_arr is not None:
+        if len(ctrl_size_arr) > 0:
+            # fixed-sized Span
+            ctrl_span_size = reduce(operator.mul, ctrl_size_arr)
+            return f"Span<const {ctrl_type}, {ctrl_span_size}>"
+        else:
+            # variable-sized Span
+            return f"Span<const {ctrl_type}>"
+    else:
+        return ctrl_type
+
+
 def generate_cpp(controls):
     enum_doc_start_template = string.Template('''/**
  * \\enum ${name}Enum
@@ -50,11 +70,7 @@  ${description}
         name, ctrl = ctrl.popitem()
         id_name = snake_case(name).upper()

-        ctrl_type = ctrl['type']
-        if ctrl_type == 'string':
-            ctrl_type = 'std::string'
-        elif ctrl.get('size'):
-            ctrl_type = 'Span<const %s>' % ctrl_type
+        ctrl_type = get_ctrl_type(ctrl)

         info = {
             'name': name,
@@ -135,11 +151,7 @@  def generate_h(controls):

         ids.append('\t' + id_name + ' = ' + str(id_value) + ',')

-        ctrl_type = ctrl['type']
-        if ctrl_type == 'string':
-            ctrl_type = 'std::string'
-        elif ctrl.get('size'):
-            ctrl_type = 'Span<const %s>' % ctrl_type
+        ctrl_type = get_ctrl_type(ctrl)

         info = {
             'name': name,