C++ Templates based common devices

Related to the the forum.
Post Reply
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

C++ Templates based common devices

Post by ag123 »

I'm just thinking aloud.
I think we can maintain a separate tree (in a separate repository, quite like a 'library') for c++ templates based common devices e.g. spi, uart, i2c, adc etc, they wouldn't fit right in in the Arduino API, but can be deemed an alternative API. This would allow us to experiment with ideas. And maybe at some point if it turns out it is mature and that they can be integrated, perhaps they could be merged 'in future'.

A good thing about c++ templates, say vs pure virtual classes is that templates are just like #defines, i.e. that they do not consume additional flash and memory and is a standard c++ feature.

using c++ templates would allow alternative implementations, e.g. with DMA vs 'vanilla' implementations which may be just similar with the SPI api, etc.
And that given the separation of templates and implementations, it may make possible implementations say by stm32 series, hence allowing for leaner codes and binaries instead of patching if-defs to cater for cross series differences.
Normally, pure virtual classes and implementations does the job, but that pure virtual classes consume some flash and sram even for the 'empty' stub.
I'm not sure if templates would consume extra flash and ram though, even though the concept of templates is they work 'like macros'

incidentally, i think even 'variants' can be defined with templates.

well, this idea is 'not tested', but that c++ templates may be deemed a solution sort of. and that it would detour from 'Arduino' api as that is more 'C' like. A facade can be nevertheless made for it, but I think the syntax and declarations part would at least be different.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: C++ Templates based common devices

Post by fpiSTM »

Yes, it is a nice idea. I thought about this some years ago but don't have time to POC.
jacobli
Posts: 42
Joined: Fri Jun 11, 2021 3:40 am

Re: C++ Templates based common devices

Post by jacobli »

Actually, C++and Arduino are still very similar.
Post Reply

Return to “Ideas & suggestions”