Add downcast methods to NetworkInterface

As we've introduced virtual inheritance to support EMACInterface, we can
no longer use C-style casts or static_cast to downcast from
NetworkInterface to more specific types. RTTI is disabled in the
toolchains, so dynamic_cast is unavailables.

Add virtual downcast methods to permit conversions to the 6 derived
classes. Probably only needed for EMACInterface, WiFiInterface and
EthInterface, but handles the set.
pull/6847/head
Kevin Bracey 2017-12-19 16:05:35 +02:00
parent 6dffe6e4e7
commit 74dfe3423a
6 changed files with 47 additions and 2 deletions

View File

@ -101,6 +101,9 @@ public:
*/ */
virtual const char *get_gateway() = 0; virtual const char *get_gateway() = 0;
virtual CellularBase *cellularBase() {
return this;
}
}; };
#endif //CELLULAR_BASE_H #endif //CELLULAR_BASE_H

View File

@ -150,6 +150,10 @@ public:
*/ */
EMAC &get_emac() const { return _emac; } EMAC &get_emac() const { return _emac; }
virtual EMACInterface *emacInterface() {
return this;
}
protected: protected:
/** Provide access to the underlying stack /** Provide access to the underlying stack
* *

View File

@ -29,6 +29,9 @@
*/ */
class EthInterface : public virtual NetworkInterface class EthInterface : public virtual NetworkInterface
{ {
virtual EthInterface *ethInterface() {
return this;
}
}; };

View File

@ -29,6 +29,9 @@
*/ */
class MeshInterface : public NetworkInterface class MeshInterface : public NetworkInterface
{ {
virtual MeshInterface *meshInterface() {
return this;
}
}; };

View File

@ -21,9 +21,13 @@
#include "netsocket/SocketAddress.h" #include "netsocket/SocketAddress.h"
#include "Callback.h" #include "Callback.h"
// Predeclared class // Predeclared classes
class NetworkStack; class NetworkStack;
class EthInterface;
class WiFiInterface;
class MeshInterface;
class CellularBase;
class EMACInterface;
/** NetworkInterface class /** NetworkInterface class
* *
@ -153,6 +157,30 @@ public:
*/ */
virtual nsapi_error_t set_blocking(bool blocking); virtual nsapi_error_t set_blocking(bool blocking);
/** Dynamic downcast to an EthInterface */
virtual EthInterface *ethInterface() {
return 0;
}
/** Dynamic downcast to a WiFiInterface */
virtual WiFiInterface *wifiInterface() {
return 0;
}
/** Dynamic downcast to a MeshInterface */
virtual MeshInterface *meshInterface() {
return 0;
}
/** Dynamic downcast to a CellularBase */
virtual CellularBase *cellularBase() {
return 0;
}
/** Dynamic downcast to an EMACInterface */
virtual EMACInterface *emacInterface() {
return 0;
}
protected: protected:
friend class Socket; friend class Socket;

View File

@ -99,6 +99,10 @@ public:
* negative on error see @a nsapi_error * negative on error see @a nsapi_error
*/ */
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count) = 0; virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count) = 0;
virtual WiFiInterface *wifiInterface() {
return this;
}
}; };
#endif #endif