A controversial idea is to change the implementation to C++ and make the
driver a proper class with inheritance and utilize polymorphism with
dynamic casts.

/Joakim

Den tor 27 dec. 2018 21:51 skrev Kees Bakker <k...@sodaq.com>:

> On 27-12-18 13:54, Kaspar Schleiser wrote:
> > Hi,
> >
> > On 12/26/18 11:16 PM, Kees Bakker wrote:
> >> Suppose I make a Pull Request to eliminate casts, would that be picked
> up?
> > Always welcome! +1 on Joakim's hint to keep the PR's small.
> Sure
> >> void at86rf2xx_tx_exec(const at86rf2xx_t *dev)
> >> {
> >>      netdev_t *netdev = (netdev_t *)dev;
> > What would be the way to go here? 'netdev_t netdev = &dev->netdev;'?
> Yes, please. It's already done in several drivers.
> > In this case, it won't work. Would need to recurse into
> > netdev_ieee802154_t, like 'netdev_t *netdev = &dev->netdev.netdev;'.
> >
> > That might be less error.prone, but more confusing. Unfortunately C is
> > not much help here.
> Is it more confusing? I don't think so. Personally, I prefer the
> &dev->netdev.netdev
> above the (netdev_t *)dev.
> >> Casts
> >> * Try to avoid casts (a bit vague, but it should get people's attention)
> >> * Introduce helper variables to avoid multiple casts within a function
> >> * Don't cast a const pointer into a non-const pointer without an
> >> explanation in a comment.
> >> * Don't cast a pointer to a volatile object dropping the volatile
> >> without an
> >> explanation in a comment.
> >> * ...
> > * use dereferenced superclass field instead of "blind" cast
> >
> > (the netdev case above).
> >
> > Kaspar
> > _______________________________________________
> > devel mailing list
> > devel@riot-os.org
> > https://lists.riot-os.org/mailman/listinfo/devel
>
> _______________________________________________
> devel mailing list
> devel@riot-os.org
> https://lists.riot-os.org/mailman/listinfo/devel
>
_______________________________________________
devel mailing list
devel@riot-os.org
https://lists.riot-os.org/mailman/listinfo/devel

Reply via email to