[c++] function pointers to switch output write function?
Posted: Mon Jan 18, 2016 4:31 pm
Greetings, this may be a p.o.c. for advanced coders, I however seem to be stuck. I simply want a methodology to direct certain text in an uint8_t *buffer of size uint16_t length to different write(const uint8_t *, size_t) functions. Unfortunately, the write functions are member functions of several different classes, the Serial-Class, the Wifi-Client-Class and an asynchronous TCP-Client-Class (https://github.com/me-no-dev/ESPAsyncTCP).
All write functions share the same signature:
So I'd define my function pointer like so:
And call it like so:
That, however, failes at compile time. Does anybody know how I would do it correctly?
All write functions share the same signature:
Code: Select all
size_t write(const uint8_t *buf, size_t len);
So I'd define my function pointer like so:
Code: Select all
typedef size_t (* writePtr) (const uint8_t *, size_t);
writePtr outputWriteChannel;
And call it like so:
Code: Select all
WiFiClient testWifiClient;
// populate wifi client obviously
AsyncClient testAsyncClient;
// populate async client obviously
uint8_t buf[100];
strcpy(buf, "Hello World!");
outputWriteChannel = &Serial.write;
outputWriteChannel(buf, len); // Goes to Serial
outputWriteChannel = &testWifiClient.write;
outputWriteChannel(buf, len); // Goes to Synchronous TCP client
outputWriteChannel = &testAsyncClient.write;
outputWriteChannel(buf, len); // Goes to aSynchronous TCP client
That, however, failes at compile time. Does anybody know how I would do it correctly?