afw-org / afw

Adaptive Framework
https://afw.tools
MIT License
4 stars 1 forks source link

Make change so that interface implementations do not need to assign instance to self #52

Open mike000000000 opened 1 year ago

mike000000000 commented 1 year ago

Change the generated '_impl.h' to support a '#define <interface_name.upper()>_SELF_H ' to specify the name of the 'self' typedef.

Currently, the impl_ function for each method has a first parameter that is a 'const _t instance' and most function implementations have as their first line an assign to a self variable that is not const and is a cast of instance to this self typedef. The typedef for self always have `_t pub' as it's first member followed by any implementation specific members.

Example, in typedef for self in internal header:

typedef struct afw_pool_internal_singlethreaded_self_s
afw_pool_internal_singlethreaded_self_t;

struct afw_pool_internal_singlethreaded_self_s {

    afw_pool_t pub;

   /* Other instance specific members. */
  ...

};

Then in the implementation of a interface:

/* Declares and rti/inf defines for interface afw_pool. */
#define AFW_IMPLEMENTATION_ID "afw_pool_singlethreaded"
#include "afw_pool_impl_declares.h"

...

/*
 * Implementation of method malloc for interface afw_pool.
 */
void *
impl_afw_pool_malloc(
    const afw_pool_t * instance,
    afw_size_t size,
    afw_xctx_t *xctx)
{
    afw_pool_internal_singlethreaded_self_t*self =
        (afw_pool_internal_singlethreaded_self_t*)instance;

  /* Implementation code */
}

This change will allow a <interface_name.upper()>_SELF_T macro to be defined before the include of the _impl_declares.h to specify the name of the implementation specific self typedef.

The above can change to this:

/* Declares and rti/inf defines for interface afw_pool. */
#define AFW_IMPLEMENTATION_ID "afw_pool_singlethreaded"
#define AFW_POOL_SELF_T afw_pool_internal_singlethreaded_self_t
#include "afw_pool_impl_declares.h"

...

/*
 * Implementation of method malloc for interface afw_pool.
 */
void *
impl_afw_pool_malloc(
    AFW_POOL_SELF_T *self,
    afw_size_t size,
    afw_xctx_t *xctx)
{
  /* Implementation code */
}
...

This issue will also change the interface skeletons to include this new define and remove the commented out assign in each method skeleton.

This change will remove an unnecessary assignment when calling each method at both execution time and coding time.

As part of this issue, change some or all of the existing implementations to use the new #define.

mike000000000 commented 1 year ago

This is left open for a while longer. Implementations will be converted when it's helpful and convenient.