Skip to content

Commit 6bbaf75

Browse files
committed
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.
1 parent bf76abf commit 6bbaf75

File tree

6 files changed

+27
-2
lines changed

6 files changed

+27
-2
lines changed

features/netsocket/CellularBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class CellularBase: public NetworkInterface {
101101
*/
102102
virtual const char *get_gateway() = 0;
103103

104+
virtual CellularBase *cellularBase() { return this; }
104105
};
105106

106107
#endif //CELLULAR_BASE_H

features/netsocket/EMACInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class EMACInterface : public virtual NetworkInterface
150150
*/
151151
EMAC &get_emac() const { return _emac; }
152152

153+
virtual EMACInterface *emacInterface() { return this; }
154+
153155
protected:
154156
/** Provide access to the underlying stack
155157
*

features/netsocket/EthInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
class EthInterface : public virtual NetworkInterface
3131
{
32+
virtual EthInterface *ethInterface() { return this; }
3233
};
3334

3435

features/netsocket/MeshInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
class MeshInterface : public NetworkInterface
3131
{
32+
virtual MeshInterface *meshInterface() { return this; }
3233
};
3334

3435

features/netsocket/NetworkInterface.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
#include "netsocket/SocketAddress.h"
2222
#include "Callback.h"
2323

24-
// Predeclared class
24+
// Predeclared classes
2525
class NetworkStack;
26-
26+
class EthInterface;
27+
class WiFiInterface;
28+
class MeshInterface;
29+
class CellularBase;
30+
class EMACInterface;
2731

2832
/** NetworkInterface class
2933
*
@@ -153,6 +157,20 @@ class NetworkInterface {
153157
*/
154158
virtual nsapi_error_t set_blocking(bool blocking);
155159

160+
/** Dynamic downcast to an EthInterface */
161+
virtual EthInterface *ethInterface() { return 0; }
162+
163+
/** Dynamic downcast to a WiFiInterface */
164+
virtual WiFiInterface *wifiInterface() { return 0; }
165+
166+
/** Dynamic downcast to a MeshInterface */
167+
virtual MeshInterface *meshInterface() { return 0; }
168+
169+
/** Dynamic downcast to a CellularBase */
170+
virtual CellularBase *cellularBase() { return 0; }
171+
172+
/** Dynamic downcast to an EMACInterface */
173+
virtual EMACInterface *emacInterface() { return 0; }
156174

157175
protected:
158176
friend class Socket;

features/netsocket/WiFiInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class WiFiInterface: public virtual NetworkInterface
9999
* negative on error see @a nsapi_error
100100
*/
101101
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count) = 0;
102+
103+
virtual WiFiInterface *wifiInterface() { return this; }
102104
};
103105

104106
#endif

0 commit comments

Comments
 (0)