Hello, i am developing a TTY terminal parser library for arduino *1, so i got the parser for the escaped commands(tty console codes), and the related hardware specific display operations.
The idea is to create an easy swappable hardware-related display class/library.
The way i am now trying is to create empty pointers to display operations, and execute them only if previously assigned.
SH1106_DisplayOperations.CUP(int argc,int *argv[]) <-- the code is here
But i am not nailing the declaration or the assign, or i need some typedef... IDK, the compiler and linker are getting really mad, the usual error goes like: Cannot convert 'void (MyClass::)()' to 'void ()()
I shall confess i am not a senior c++ dev :(, so i praise c++ seniors 4 an answer.
How shall be done?
If it's too hard?
Is there a better way?
Pointers method is useful for partial implementations, so, only developed and assigned functions are called.
'Cause there are MANY display operations, and only a subset will be developed in any case, pointers provide an automatic way to discriminate via nullPtr.
Also, the terminalParser class can be developed independently mantaining full compatibility providing support for future implemented code for escape codes, so can be published only parsing the most common subset.
You better declare a common functions (API) base class and use multiple inheritance to derive classes with specific displays. Like the Stream class provides several methods that can work only in derived classes for Serial, Wire etc.
The base class by itself can do almost nothing, but as soon as you instantiate a derived class for your current display, based also upon that display library, your code can propagate all commands to that library.
A base class would be used in OOP. Pointers to non static member functions are messy.
Note that the optimizer will likely remove calls to functions doing nothing, so your performance will possibly be better than checking for a null pointer if that can be done at compile time
Already tried declaring the methods pointing to an external class, but did not succeed.
Maybe wrong declaration, don't remember exactly, been 100+ days trying, tried many stuff.
static void (SH1106_DisplayOperations::*moveCursorX)(unsigned int,int[]); // function pointer declaration
class TerminalParser {
public:
virtual void whoAmI() {
Serial.println(F("not implemented"));
}
};
class HardwareDisplay : public TerminalParser {
void whoAmI() {
Serial.println(F("HardwareDisplay"));
}
};
class SoftwareDisplay : public TerminalParser {
void whoAmI() {
Serial.println(F("SoftwareDisplay"));
}
};
class PartialDisplay : public TerminalParser {
// whoAmI is not implemented, the default function will be called in the parent class
};
HardwareDisplay h;
SoftwareDisplay s;
PartialDisplay p;
TerminalParser* myDisplays[] = {&h, &s, &p};
void setup() {
Serial.begin(115200); Serial.println();
for (auto & aDisplay : myDisplays) aDisplay->whoAmI();
}
void loop() {}
you can run it here in the simulator
the serial monitor will print
HardwareDisplay
SoftwareDisplay
not implemented
showing that child member functions were call despite the fact that the array was holding TerminalParser instances pointers ➜ the method that was called was actually based on the real instance type.
If you are pointing to class member function you have to pass instance, otherwise declare function static or point to global function or pass lambda. As you have shown no specifics, a non specific answer you shall get