[xkbcommon] Use an integer type for modifiers bit mask.

Wander Lairson Costa wander.lairson at gmail.com
Fri Oct 4 05:09:32 PDT 2013


2013/10/4 Thiago Macieira <thiago.macieira at intel.com>:
> On quinta-feira, 3 de outubro de 2013 23:03:44, Wander Lairson Costa wrote:
>> If we combine two enum values, the result is not a valid enum value
>> anymore, so it cannot be attributed to an enum variable.
>>
>> C++ compilers will complain if such an assigment is done.
>
> This is done all the time in C++. The enum must be backed by an integer with
> at least as many bits as the enum possesses.
>

The issued raised when I took code from window.c in the weston clients:

        mask = xkb_state_serialize_mods(input->xkb.state,
                                        XKB_STATE_DEPRESSED |
                                        XKB_STATE_LATCHED);

g++ gave me a build error because I was passing an integer to enum
parameter. C++ is a bit more nit-picky than C regarding implicit
conversions. Therefore I had to use a cast.

> With the ABI that GCC uses, it's always at least 4 bytes.

Personally, I don't like enum's in the ABI at all, because according
to C/C++ standards, the compiler is free to choose whatever type it
likes, and indeed I had problems with that in the past. C++11 fixed
that [1]. But nevermind.

> The only thing is that you need to cast it from integer back to the enum type.
>

That's what the patch is about: avoid casts. Whenever you use a cast,
you are giving up the help the compiler may give to you regarding
invalid type conversions. So, I always use the rule of thumb of
avoiding casts whenever I can. IMO, this is not a harmful patch and
will make the C++ programmers a little bit easier.

Ofcourse the libxkbcommon maintainers may have another view and reject
the patch.

[1] http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html

-- 
Best Regards,
Wander Lairson Costa


More information about the wayland-devel mailing list