Re: Potential Design Pattern bug in Chibi Abstract peripheral interfaces
Posted: Wed Feb 21, 2018 12:07 pm
A possible solution would be to make the first pointer in an object (after the VMT pointer) a pointer to the object instance data. This would require a double indirection for data access.
For example:
The first field of _base_sensor_data and "_base_accelerometer_data" and "_base_accelerometer_data" would point back to the object instance.
Accessing data in the super-classes must be done using the interface pointer (to the VMT field), accessing data in "_lis302dl_data" should be done using a double indirection.
This back-pointer could be added to the root class type.
I don't like particularly, it is complex and requires more fields in those classes that could be used in multiple inheritance.
Edit:
Alternatively, the first element of the VMT could be, by convention, an offset to be added to the the interface pointer to get back to the object base. This would be not slower and save RAM in objects. In the previous example the offset for BaseSensor would be 0, for BaseAccelerometer it would be -sizeof(BaseSensor).
Giovanni
For example:
Code: Select all
struct LIS302DLDriver {
/** @brief BaseSensor Virtual Methods Table. */
const struct BaseSensorVMT *vmt_sensor;
_base_sensor_data
/** @brief BaseAccelerometer Virtual Methods Table. */
const struct LIS302DLAccelerometerVMT *vmt_accelerometer;
_base_accelerometer_data
_lis302dl_data
};
The first field of _base_sensor_data and "_base_accelerometer_data" and "_base_accelerometer_data" would point back to the object instance.
Accessing data in the super-classes must be done using the interface pointer (to the VMT field), accessing data in "_lis302dl_data" should be done using a double indirection.
This back-pointer could be added to the root class type.
I don't like particularly, it is complex and requires more fields in those classes that could be used in multiple inheritance.
Edit:
Alternatively, the first element of the VMT could be, by convention, an offset to be added to the the interface pointer to get back to the object base. This would be not slower and save RAM in objects. In the previous example the offset for BaseSensor would be 0, for BaseAccelerometer it would be -sizeof(BaseSensor).
Giovanni