marcelnabeck / TinyFramePlusPlus

Object-oriented implementation of TinyFrame in C++.
MIT License
5 stars 1 forks source link

Object oriented callback calls for Listeners #1

Open marcelnabeck opened 2 years ago

marcelnabeck commented 2 years ago

Include an object on which the Listener callbacks are called:

asmwarrior commented 4 months ago

Hi, currently, the listener function is still a "global function", for example, see the below code:

https://github.com/asmwarrior/TinyFrame/blob/b51f0c52ab53b67bd9670ed201608d8a1aa83f9d/demo/simple/test.cpp#L51-L106

So, in this issue, you mentioned that "an object" could be used. Do you mean the listener could be a std::function?

marcelnabeck commented 4 months ago

Hi,

yes, std::function would be an option to implement this.

Unfortunately std::function is very expensive in terms of memory consumption and execution time, and also potentially allocates memory in the heap. This makes it unsuitable for most embedded applications...

As alternative I was thinking about using etl::delegate from the Embedded Template Library, which is much more lightweight. This would add a dependency to a big external library which I also didn't want.

Probably the best option is the creation of an interface class which the user has to implement. Then an object of this class could be used to replace current function pointers and it's object oriented.

asmwarrior commented 4 months ago

Hi, thanks for the quick response and explanation.

I'm using this tinyframe library in either a embedded application running in a MCU, also I use this library in a PC C++ program.

I'm OK about the direction.

BTW: you solved the problem I encountered several years ago, see here: multiple instances with different frame structure?.

Thanks.