mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 09:38:19 +00:00
Add templated As() method to Val, use in various places we were using dynamic_cast
This commit is contained in:
parent
a7b5915b1a
commit
a94fcad957
7 changed files with 28 additions and 14 deletions
14
src/Val.h
14
src/Val.h
|
@ -12,6 +12,7 @@
|
|||
#include "zeek/Type.h"
|
||||
#include "zeek/Timer.h"
|
||||
#include "zeek/Notifier.h"
|
||||
#include "zeek/Reporter.h"
|
||||
#include "zeek/net_util.h"
|
||||
|
||||
// We have four different port name spaces: TCP, UDP, ICMP, and UNKNOWN.
|
||||
|
@ -209,6 +210,19 @@ UNDERLYING_ACCESSOR_DECL(TypeVal, zeek::Type*, AsType)
|
|||
|
||||
StringValPtr ToJSON(bool only_loggable=false, RE_Matcher* re=nullptr);
|
||||
|
||||
template<typename T>
|
||||
T As()
|
||||
{
|
||||
// Since we're converting from "this", make sure the type requested is a pointer.
|
||||
static_assert(std::is_pointer<T>());
|
||||
|
||||
auto v = dynamic_cast<T>(this);
|
||||
if ( ! v )
|
||||
reporter->InternalError("Failed dynamic_cast between Val types");
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
// Friends with access to Clone().
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue