[Xcb] [PATCH libxcb 1/1] support switch case in the generator

Ran Benita ran234 at gmail.com
Sat Aug 23 09:15:10 PDT 2014


On Tue, Aug 19, 2014 at 03:57:34PM +0200, Christian Linhart wrote:
> The implementation is rather simple:
> When a <case> is used instead of a <bitcase>
> then operator "==" is used instead of "&" in the if-condition.
> 
> So it creates a series of "if" statements
> (instead of a switch-case statement in C )
> 
> In practice this does not matter because a good
> optimizing compiler will create the same code
> as for a switch-case.
> 
> With this simple implementation we get additional
> flexibility in the following forms:
> * a case value may appear in multiple case branches.
>   for example:
> 	case C1 will be selected by values 1, 4, or 5
> 	case C2 will be selected by values 3, 4, or 7
> 
> * mixing of bitcase and case is possible
> 	(this will usually make no sense but there may
> 	be protocol specs where this is needed)
> 
> details of the impl:
> * replaced "is_bitcase" with "is_case_or_bitcase" in all places
>   so that cases are treated like bitcases.
> 
> * In function "_c_serialize_helper_switch": write operator "=="
>   instead of operator "&" if it is a case.

Nice and straight-forward:

Reviewed-by: Ran Benita <ran234 at gmail.com>

Just one minor suggestion below.

> ---
>  src/c_client.py | 55 +++++++++++++++++++++++++++++++++----------------------
>  1 file changed, 33 insertions(+), 22 deletions(-)
> 
> diff --git a/src/c_client.py b/src/c_client.py
> index 54e56c4..87f268b 100644

[...]

> @@ -685,36 +685,47 @@ def _c_serialize_helper_switch(context, self, complex_name,
>                                 code_lines, temp_vars,
>                                 space, prefix):
>      count = 0
>      switch_expr = _c_accessor_get_expr(self.expr, None)
>  
>      for b in self.bitcases:
>          len_expr = len(b.type.expr)
> +
> +        compare_operator = '&'

I'd drop this line. You can also write

    compare_operator = '==' if b.type.is_case else '&'

if you'd like.

Ran


More information about the Xcb mailing list