1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 12:44:25 +01:00

Added tins_cast as a replacement for dynamic_cast when using it on PDU classes.

This commit is contained in:
Matias Fontanini
2013-10-18 09:28:43 -03:00
parent 87fdd62b57
commit a507355e27
9 changed files with 59 additions and 9 deletions

View File

@@ -49,7 +49,7 @@ namespace Crypto {
/**
* \cond
*/
class RC4Key;
struct RC4Key;
#ifdef HAVE_WPA2_DECRYPTION
namespace WPA2 {
class invalid_handshake : public std::exception {

View File

@@ -145,6 +145,16 @@ public:
return "Malformed option";
}
};
/**
* \brief Exception thrown when a call to tins_cast fails.
*/
class bad_tins_cast : public std::exception {
public:
const char *what() const throw() {
return "Bad Tins cast";
}
};
}
#endif // TINS_EXCEPTIONS_H

View File

@@ -456,6 +456,34 @@ namespace Tins {
*lop /= rop;
return lop;
}
namespace Internals {
template<typename T>
struct remove_pointer {
typedef T type;
};
template<typename T>
struct remove_pointer<T*> {
typedef T type;
};
}
template<typename T, typename U>
T tins_cast(U *pdu) {
typedef typename Internals::remove_pointer<T>::type TrueT;
return pdu && (TrueT::pdu_flag == pdu->pdu_type()) ?
static_cast<T>(pdu) :
0;
}
template<typename T, typename U>
T &tins_cast(U &pdu) {
T *ptr = tins_cast<T*>(&pdu);
if(!ptr)
throw bad_tins_cast();
return *ptr;
}
}
#endif // TINS_PDU_H