[Xcb] [PATCH] Add macros for automatically loading lists of atoms
William Swanson
swansontec at gmail.com
Sun Nov 18 23:48:46 PST 2012
Hello All,
I am attaching a fairly hefty patch for the util library. This patch
might be a bit controversial, since I see that the util library
contained similar functionality at one point in the past, which has
since been removed. This patch takes a somewhat different approach,
and is about 90% preprocessor macros.
To use these macros, first create a list of atoms:
#define MY_ATOMS WM_PROTOCOLS, WM_DELETE_WINDOW
Next, declare a structure to hold these atoms:
XCB_ATOM_LIST(my_atoms_t, MY_ATOMS);
Now create a function to automatically initialize the structure:
XCB_ATOM_LIST_FN(my_atom_init, my_atoms_t, MY_ATOMS);
Finally, in your main function or other suitable location, simply call
the initialization function:
my_atoms_t atoms;
atoms = my_atom_init(connection, &error);
if (error) { ... }
At this point, all the atoms in the list now exist and are available
by name, like `atoms.WM_PROTOCOLS`.
There is also an optional `XCB_ATOM_LIST_DECLARE_FN` macro to create a
function declaration suitable for placing in a header file.
As you can see, creating two atoms is about 8 lines of code including
error handling. This is not much of a savings compared to doing things
by hand, apart from the fact that the atom list is only declared once,
which avoids a lot of repetition.
These macros obviously become more useful as the atom list grows
longer. For example, xcb_ewmh.h currently uses m4 to produce a long
list of atoms, but these macros are now capable of doing something
similar in pure C. I've set the recursion limit to 365 items, so the
lists can be quite long indeed.
One possible criticism is that the macros generate a single, blocking
initialization function. I have no problem splitting this function in
two if folks think that would be more xcb-like.
These macros rely on variadic parameters, which are a C99 feature. I
have tested them on GCC, clang, and tcc, and they seem to work
perfectly. I don't know if using C99 features is a problem or not.
I would like to hear what people think about this.
-William
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Create-the-atom-list-sub-library.patch
Type: application/octet-stream
Size: 15983 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/xcb/attachments/20121118/3f1de508/attachment.obj>
More information about the Xcb
mailing list