Add templated As() method to Val, use in various places we were using dynamic_cast

This commit is contained in:
Tim Wojtulewicz 2020-12-03 14:25:18 -07:00
parent a7b5915b1a
commit a94fcad957
7 changed files with 28 additions and 14 deletions

View file

@ -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().