For much the same reason as optional hooks are desirable, it is also nice to be able to call a function that may not be there. You might think that DSOs provide the answer,[4] and you’d be half right. But they don’t quite, for two reasons — first, not every platform supports DSOs, and second, when the function is not missing, it may be statically linked. Forcing everyone to use DSOs for all modules just to support optional functions is going too far. Particularly since we have a better plan!
An optional function is pretty much what it sounds like. It is a function that may turn out, at runtime, not to be implemented (or not to exist at all, more to the point). So, there are five parts to an optional function: a declaration, an implementation, a registration, a retrieval, and a call. The export of the optional function declares it:
APR_DECLARE_OPTIONAL_FN(int,some_fn,(const char *thing))
This is pretty much like a hook declaration: you have the return type, the name of the function, and the argument declaration. Like a hook declaration, it would normally appear in a header.
Next it has to be implemented:
int some_fn(const char *thing) { /* do stuff */ }
Note that the function name must be the same as in the declaration.
The next step is to register the function (note that optional functions are a bit like optional hooks in a distorting mirror — some parts switch role from the exporter of the function to the importer, and this is one of them):
APR_REGISTER_OPTIONAL_FN(some_fn);
Again, the function name must be the same as the declaration. This is normally called in the hook registration process.[5]
Next, the user of the function must retrieve it. Because it is
registered during hook registration, it can’t be
reliably retrieved at that point. However, there is a hook for
retrieving optional functions (called, obviously enough,
optional_fn_retrieve
). Or it can be done by
keeping a flag that says whether it has been retrieved and retrieving
it when it is needed. (Although it is tempting to use the pointer to
function as the flag, it is a bad idea — if it is not registered,
then you will attempt to retrieve it every time instead of just
once). In either case, the actual retrieval looks like this:
APR_OPTIONAL_FN_TYPE(some_fn) *pfn; pfn=APR_RETRIEVE_OPTIONAL_FN(some_fn);
From there on in, pfn
gets used just like any
other pointer to a function. Remember that it may be
NULL
, of course!