getting symbols from dynamic modules



Felix Kater writes:
g_module_* is a great thing -- I just wonder if there is a way to not
create function pointers for each single function I need but just
import all of them at once?

Define a "suite" struct containing all the function pointers for your
module, and then export just one function that returns a pointer to
such a struct? Please have in the struct as its first member an int
with the size of the struct, for the version of the module in
question. When adding function pointers to your struct, add them to
the end, keeping the previous fields intact. You *will* be glad later
when you have different versions of your module and different versions
of client apps deployed in the wild that you did that ;)

I.e., somethihg like this (completely untested, just typing into this
mail):

typedef struct
{
  gsize size;
  void (*do_dishes)();
  int (*make_coffee)(gdouble strength, gint cups);
} MyModuleSuite;

in the module:

G_MODULE_EXPORT MyModuleSuite *
module_info (void)
{
  MyModuleSuite *retval = g_new (MyModuleSuite, 1);

  retval->size = sizeof (MyModuleSuite);
  retval->do_dishes = NULL; /* No way */
  retval->make_coffee = my_make_coffee;

  return retval;
}

in the client app:

  if (g_module_symbol (module, "module_info", &info))
    {
      MyModuleSuite *suite = (MyModuleSuite*) (*info)();

      if (suite->size > G_STRUCT_OFFSET (MyModuleSuite, do_dishes) &&
          suite->do_dishes != NULL)
        {
           /* It does the dishes, yay! */
           ...
        }
     
      if (suite->size > G_STRUCT_OFFSET (MyModuleSuite, make_coffee) &&
          suite->make_coffee != NULL)
        {
           ...
        }
    }

--tml




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]