Hi All,
I'm currently busy with the device_access code and not really happy
with how many nested if statements are needed when using error
handling (celix_status_t).
See the following snipper for an example:
char *bsn = NULL;
MODULE module = NULL;
BUNDLE bundle = NULL;
SERVICE_REGISTRATION registration = NULL;
SERVICE_REGISTRY registry = NULL;
substatus = serviceReference_getBundle(ref, &bundle);
if (substatus == CELIX_SUCCESS) {
substatus = bundle_getCurrentModule(bundle, &module);
if (substatus == CELIX_SUCCESS) {
substatus = module_getSymbolicName(module, &bsn);
if (substatus == CELIX_SUCCESS) {
substatus =
serviceReference_getServiceRegistration(ref, ®istration);
if (substatus == CELIX_SUCCESS) {
substatus =
serviceRegistration_getRegistry(registration, ®istry);
if (substatus == CELIX_SUCCESS) {
...
In my opinion this makes the code very unclear and I am thinking about
adding the following macro to address this problem:
#define DO_IF_SUCCESS(status, call_func) ((status) == CELIX_SUCCESS) ?
(call_func) : (status)
The previous snippet could then be rewritten to:
char *bsn = NULL;
MODULE module = NULL;
BUNDLE bundle = NULL;
SERVICE_REGISTRATION registration = NULL;
SERVICE_REGISTRY registry = NULL;
substatus = serviceReference_getBundle(ref, &bundle);
substatus = DO_IF_SUCCESS(substatus, bundle_getCurrentModule(bundle, &module));
substatus = DO_IF_SUCCESS(substatus, module_getSymbolicName(module, &bsn));
substatus = DO_IF_SUCCESS(substatus,
serviceReference_getServiceRegistration(ref, ®istration));
substatus = DO_IF_SUCCESS(substatus,
serviceRegistration_getRegistry(registration, ®istry));
if (substatus == CELIX_SUCCESS) {
...
I would like to hear some opinions about this solution and if it is
something which can be added to celix_errno.h ?
Greetings,
Pepijn