Marcus Brinkmann | 6 Sep 01:04 2002
Picon

iterator macros and requiring gcc >= 3.0 and -std=c9x

Hi,

I would like some iterator macros for the driver plugin code in the console
client.  I have written this so far:

#define driver_iterate(drv)                                             \
  for (mutex_lock (&driver_list_lock), drv = &driver_list[0];           \
       drv <= &driver_list[driver_list_len - 1];                        \
       drv++, (drv == &driver_list[driver_list_len - 1]                 \
               ? mutex_unlock (&driver_list_lock) : 0)

Which can be used like this:

   driver_t driver;
   driver_iterate (driver)
     {
       printf ("%s\n", driver->ops->name);
     }

Neal pointed out that C99 allows to define variables in statements, so the
following would work:

#define driver_iterate                                                    \
  for (mutex_lock (&driver_list_lock), driver_t driver = &driver_list[0]; \
       driver <= &driver_list[driver_list_len - 1];                       \
       driver++, (driver == &driver_list[driver_list_len - 1]             \
                  ? mutex_unlock (&driver_list_lock) : 0)

Which can be used like this:

   driver_iterate
     {
       printf ("%s\n", driver->ops->name);
     }

eliminating the need for a user-defined variable.  It's no big deal, but if
you prefer the second version, we need to use -std=c9x in our CFLAGS.  What
do you think?

Thanks,
Marcus

--

-- 
`Rhubarb is no Egyptian god.' GNU      http://www.gnu.org    marcus <at> gnu.org
Marcus Brinkmann              The Hurd http://www.gnu.org/software/hurd/
Marcus.Brinkmann <at> ruhr-uni-bochum.de
http://www.marcus-brinkmann.de/

Gmane