Writing C(++) code that is easy to add (as a module) to micropython
Posted: Mon Dec 31, 2018 8:02 pm
I'd like to write a library in C (or C++) that is easy to use in C++ and also reasonably easy to port to micropython. There are some topics that I'd like to sort out before I start designing my interface.
- As I understand it, micropython works on structs that are initialized and modified using normal functions (as described here: https://micropython-dev-docs.readthedoc ... odule.html). Does that rule out any C++ class and template trickery?
- passing objects to functions or methods: can I use references or is a pointer easier to port? Within the C code it's a no-brainer, but there will also be methods exposed to micropython that need to pass around references (or pointers) to objects. If I'm limited to plain C, then I'll have to use pointers anyway. However, since modules can throw exceptions (can't they?), C++ is apparently ok.
- abstraction: some kind of abstract interface will be needed. It's easy enough to do this with function pointers in a struct (I had to do this for AVRs and it worked just fine), but is it necessary?
If I just had to write it in C++, my abstract class would have the following virtual methods (How can I format as code? I couldn't find an option for that):
class MyAbstractClass
- bool at_end()
- SomeStruct& next()
- void reset()
- MyAbstractClass& applyTo(MyAbstractClass& other)
Used primarily like this: somethingEntirelyDifferent.digest(myDerivedClassA()).applyTo(myDerivedClassB()). ... (I want to chain different implementations of MyClass)
The classes inheriting from myclass would just have custom constructors and maybe some setters, so nothing out of the ordinary.
So to sum it up: What should I look out for when designing an interface in C/C++
- As I understand it, micropython works on structs that are initialized and modified using normal functions (as described here: https://micropython-dev-docs.readthedoc ... odule.html). Does that rule out any C++ class and template trickery?
- passing objects to functions or methods: can I use references or is a pointer easier to port? Within the C code it's a no-brainer, but there will also be methods exposed to micropython that need to pass around references (or pointers) to objects. If I'm limited to plain C, then I'll have to use pointers anyway. However, since modules can throw exceptions (can't they?), C++ is apparently ok.
- abstraction: some kind of abstract interface will be needed. It's easy enough to do this with function pointers in a struct (I had to do this for AVRs and it worked just fine), but is it necessary?
If I just had to write it in C++, my abstract class would have the following virtual methods (How can I format as code? I couldn't find an option for that):
class MyAbstractClass
- bool at_end()
- SomeStruct& next()
- void reset()
- MyAbstractClass& applyTo(MyAbstractClass& other)
Used primarily like this: somethingEntirelyDifferent.digest(myDerivedClassA()).applyTo(myDerivedClassB()). ... (I want to chain different implementations of MyClass)
The classes inheriting from myclass would just have custom constructors and maybe some setters, so nothing out of the ordinary.
So to sum it up: What should I look out for when designing an interface in C/C++