Arduino and dynamic_cast<WiFiEspUDP*>(pSerialSrc);

Is there any way to use this with Arduino or has anyone come up with an Arduino compatible alternative?

(deleted)

spycatcher2k:
What is it? Context, Description of function, or even a little bit of a clue would help.

Are you familiar with Windows MFC programming with their DECLARE_DYANMIC(class), IMPLMENT_DYNAMIC(class, base class> and IsKindOf(RUNTIME_CLASS(class name)) system?

class HardwareSerial and class WiFiEspUDP are both derived from Stream.
I would like to pass in a pointer to either a HardwareSerial object or a WiFiEspUDP object.
And inside my function I would like to be able to tell which object it is so I know whether or not to do this:

bool CSerialManager::writeWord(Stream *pSerialSrc, const __FlashStringHelper *strBuff)
{
	WiFiEspUDP* pUDPServer = dynamic_cast<WiFiEspUDP*>(pSerialSrc);
	if (pUDPServer)
		pUDPServer->beginPacket(pUDPServer->remoteIP(), pUDPServer->remotePort());

	pSerialSrc->print(strBuff);
	pSerialSrc->print("#");

	if (pUDPServer)
		pUDPServer->endPacket();
}

The alternative is to have serparate functions for class HardwareSerial and class WiFiEspUDP, which is rather inconvenient given that there are a number of similar functions.

Normally, UDP classes are derived from Arduino UDP class.

So, you can do something like this:

bool CSerialManager::writeWord(UDP *pUdpSrc, const __FlashStringHelper *strBuff)
{
	WiFiEspUDP* pUDPServer = dynamic_cast<WiFiEspUDP*>(pUdpSrc);
	if (pUDPServer)
		pUDPServer->beginPacket(pUDPServer->remoteIP(), pUDPServer->remotePort());

	writeWord((Stream*) pUdpSrc, strBuff);

	if (pUDPServer)
		pUDPServer->endPacket();
}

bool CSerialManager::writeWord(Stream *pSerialSrc, const __FlashStringHelper *strBuff)
{
	pSerialSrc->print(strBuff);
	pSerialSrc->print("#");
}

Sorry I did not make it clear. dynamic_cast generates a compile error along the lines of you can't use it with compiler option -f......

What about:

bool CSerialManager::writeWord(UDP *pUdpSrc, const __FlashStringHelper *strBuff)
{
	pUdpSrc->beginPacket(pUdpSrc->remoteIP(), pUdpSrc->remotePort());
	
	writeWord((Stream*) pUdpSrc, strBuff);
	
	pUdpSrc->endPacket();
}

bool CSerialManager::writeWord(Stream *pSerialSrc, const __FlashStringHelper *strBuff)
{
	pSerialSrc->print(strBuff);
	pSerialSrc->print("#");
}

and calling:

...
WiFiEspUDP* pUDPServer;
...
myCSerialManager.writeWord(pUDPServer,F("just a test"));
...