mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +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
|
@ -1853,7 +1853,7 @@ ValPtr EqExpr::Fold(Val* v1, Val* v2) const
|
||||||
{
|
{
|
||||||
if ( op1->GetType()->Tag() == TYPE_PATTERN )
|
if ( op1->GetType()->Tag() == TYPE_PATTERN )
|
||||||
{
|
{
|
||||||
auto re = dynamic_cast<PatternVal*>(v1);
|
auto re = v1->As<PatternVal*>();
|
||||||
const String* s = v2->AsString();
|
const String* s = v2->AsString();
|
||||||
if ( tag == EXPR_EQ )
|
if ( tag == EXPR_EQ )
|
||||||
return val_mgr->Bool(re->MatchExactly(s));
|
return val_mgr->Bool(re->MatchExactly(s));
|
||||||
|
@ -4151,7 +4151,7 @@ ValPtr InExpr::Fold(Val* v1, Val* v2) const
|
||||||
{
|
{
|
||||||
if ( v1->GetType()->Tag() == TYPE_PATTERN )
|
if ( v1->GetType()->Tag() == TYPE_PATTERN )
|
||||||
{
|
{
|
||||||
auto re = dynamic_cast<PatternVal*>(v1);
|
auto re = v1->As<PatternVal*>();
|
||||||
const String* s = v2->AsString();
|
const String* s = v2->AsString();
|
||||||
return val_mgr->Bool(re->MatchAnywhere(s) != 0);
|
return val_mgr->Bool(re->MatchAnywhere(s) != 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ FuncPtr id::find_func(std::string_view name)
|
||||||
reporter->InternalError("Expected variable '%s' to be a function",
|
reporter->InternalError("Expected variable '%s' to be a function",
|
||||||
std::string(name).data());
|
std::string(name).data());
|
||||||
|
|
||||||
return dynamic_cast<FuncVal*>(v.get())->AsFuncPtr();
|
return v.get()->As<FuncVal*>()->AsFuncPtr();
|
||||||
}
|
}
|
||||||
|
|
||||||
void id::detail::init_types()
|
void id::detail::init_types()
|
||||||
|
@ -162,7 +162,7 @@ void ID::SetVal(ValPtr v)
|
||||||
type->AsFuncType()->Flavor() == FUNC_FLAVOR_EVENT )
|
type->AsFuncType()->Flavor() == FUNC_FLAVOR_EVENT )
|
||||||
{
|
{
|
||||||
EventHandler* handler = event_registry->Lookup(name);
|
EventHandler* handler = event_registry->Lookup(name);
|
||||||
auto func = dynamic_cast<FuncVal*>(val.get())->AsFuncPtr();
|
auto func = val.get()->As<FuncVal*>()->AsFuncPtr();
|
||||||
if ( ! handler )
|
if ( ! handler )
|
||||||
{
|
{
|
||||||
handler = new EventHandler(name);
|
handler = new EventHandler(name);
|
||||||
|
|
|
@ -318,7 +318,7 @@ Connection* NetSessions::FindConnection(Val* v)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
RecordType* vr = vt->AsRecordType();
|
RecordType* vr = vt->AsRecordType();
|
||||||
auto vl = dynamic_cast<RecordVal*>(v);
|
auto vl = v->As<RecordVal*>();
|
||||||
|
|
||||||
int orig_h, orig_p; // indices into record's value list
|
int orig_h, orig_p; // indices into record's value list
|
||||||
int resp_h, resp_p;
|
int resp_h, resp_p;
|
||||||
|
|
14
src/Val.h
14
src/Val.h
|
@ -12,6 +12,7 @@
|
||||||
#include "zeek/Type.h"
|
#include "zeek/Type.h"
|
||||||
#include "zeek/Timer.h"
|
#include "zeek/Timer.h"
|
||||||
#include "zeek/Notifier.h"
|
#include "zeek/Notifier.h"
|
||||||
|
#include "zeek/Reporter.h"
|
||||||
#include "zeek/net_util.h"
|
#include "zeek/net_util.h"
|
||||||
|
|
||||||
// We have four different port name spaces: TCP, UDP, ICMP, and UNKNOWN.
|
// 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);
|
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:
|
protected:
|
||||||
|
|
||||||
// Friends with access to Clone().
|
// Friends with access to Clone().
|
||||||
|
|
|
@ -209,7 +209,7 @@ function Option::set_change_handler%(ID: string, on_change: any, priority: int &
|
||||||
return zeek::val_mgr->False();
|
return zeek::val_mgr->False();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto func = dynamic_cast<FuncVal*>(on_change)->AsFuncPtr();
|
auto func = on_change->As<FuncVal*>()->AsFuncPtr();
|
||||||
i->AddOptionHandler(func, -priority);
|
i->AddOptionHandler(func, -priority);
|
||||||
return zeek::val_mgr->True();
|
return zeek::val_mgr->True();
|
||||||
%}
|
%}
|
||||||
|
|
|
@ -707,7 +707,7 @@ function str_smith_waterman%(s1: string, s2: string, params: sw_params%) : sw_su
|
||||||
## .. zeek:see:: split_string split_string1 split_string_all split_string_n
|
## .. zeek:see:: split_string split_string1 split_string_all split_string_n
|
||||||
function str_split%(s: string, idx: index_vec%): string_vec &deprecated="Remove in v4.1. Use str_split_indices."
|
function str_split%(s: string, idx: index_vec%): string_vec &deprecated="Remove in v4.1. Use str_split_indices."
|
||||||
%{
|
%{
|
||||||
auto idx_v = dynamic_cast<VectorVal*>(idx);
|
auto idx_v = idx->As<VectorVal*>();
|
||||||
auto n = idx_v->Size();
|
auto n = idx_v->Size();
|
||||||
zeek::String::IdxVec indices(n);
|
zeek::String::IdxVec indices(n);
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
@ -746,7 +746,7 @@ function str_split%(s: string, idx: index_vec%): string_vec &deprecated="Remove
|
||||||
## .. zeek:see:: split_string split_string1 split_string_all split_string_n
|
## .. zeek:see:: split_string split_string1 split_string_all split_string_n
|
||||||
function str_split_indices%(s: string, idx: index_vec%): string_vec
|
function str_split_indices%(s: string, idx: index_vec%): string_vec
|
||||||
%{
|
%{
|
||||||
auto idx_v = dynamic_cast<VectorVal*>(idx);
|
auto idx_v = idx->As<VectorVal*>();
|
||||||
auto n = idx_v->Size();
|
auto n = idx_v->Size();
|
||||||
zeek::String::IdxVec indices(n);
|
zeek::String::IdxVec indices(n);
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
12
src/zeek.bif
12
src/zeek.bif
|
@ -1432,7 +1432,7 @@ function sort%(v: any, ...%) : any
|
||||||
if ( ! comp && ! IsIntegral(elt_type->Tag()) )
|
if ( ! comp && ! IsIntegral(elt_type->Tag()) )
|
||||||
zeek::emit_builtin_error("comparison function required for sort() with non-integral types");
|
zeek::emit_builtin_error("comparison function required for sort() with non-integral types");
|
||||||
|
|
||||||
auto vv = dynamic_cast<VectorVal*>(v);
|
auto vv = v->As<zeek::VectorVal*>();
|
||||||
|
|
||||||
if ( comp )
|
if ( comp )
|
||||||
{
|
{
|
||||||
|
@ -1502,7 +1502,7 @@ function order%(v: any, ...%) : index_vec
|
||||||
if ( ! comp && ! IsIntegral(elt_type->Tag()) )
|
if ( ! comp && ! IsIntegral(elt_type->Tag()) )
|
||||||
zeek::emit_builtin_error("comparison function required for order() with non-integral types");
|
zeek::emit_builtin_error("comparison function required for order() with non-integral types");
|
||||||
|
|
||||||
auto vv = dynamic_cast<VectorVal*>(v);
|
auto vv = v->As<zeek::VectorVal*>();
|
||||||
auto n = vv->Size();
|
auto n = vv->Size();
|
||||||
|
|
||||||
// Set up initial mapping of indices directly to corresponding
|
// Set up initial mapping of indices directly to corresponding
|
||||||
|
@ -2410,7 +2410,7 @@ function addr_to_counts%(a: addr%): index_vec
|
||||||
## .. zeek:see:: addr_to_counts
|
## .. zeek:see:: addr_to_counts
|
||||||
function counts_to_addr%(v: index_vec%): addr
|
function counts_to_addr%(v: index_vec%): addr
|
||||||
%{
|
%{
|
||||||
auto vv = dynamic_cast<VectorVal*>(v);
|
auto vv = v->As<zeek::VectorVal*>();
|
||||||
|
|
||||||
if ( vv->Size() == 1 )
|
if ( vv->Size() == 1 )
|
||||||
{
|
{
|
||||||
|
@ -3528,8 +3528,8 @@ function lookup_connection%(cid: conn_id%): connection
|
||||||
%%{
|
%%{
|
||||||
const char* conn_id_string(zeek::Val* c)
|
const char* conn_id_string(zeek::Val* c)
|
||||||
{
|
{
|
||||||
auto id = dynamic_cast<const zeek::RecordVal*>(c)->GetField(0);
|
auto id = c->As<zeek::RecordVal*>()->GetField(0);
|
||||||
auto id_r = dynamic_cast<const zeek::RecordVal*>(id.get());
|
auto id_r = id->As<zeek::RecordVal*>();
|
||||||
|
|
||||||
const zeek::IPAddr& orig_h = id_r->GetAddrField(0);
|
const zeek::IPAddr& orig_h = id_r->GetAddrField(0);
|
||||||
uint32_t orig_p = id_r->GetPortValField(1)->Port();
|
uint32_t orig_p = id_r->GetPortValField(1)->Port();
|
||||||
|
@ -3654,7 +3654,7 @@ function dump_packet%(pkt: pcap_packet, file_name: string%) : bool
|
||||||
uint32_t caplen, len, link_type;
|
uint32_t caplen, len, link_type;
|
||||||
u_char *data;
|
u_char *data;
|
||||||
|
|
||||||
auto pkt_r = dynamic_cast<RecordVal*>(pkt);
|
auto pkt_r = pkt->As<zeek::RecordVal*>();
|
||||||
|
|
||||||
ts.tv_sec = pkt_r->GetCountField(0);
|
ts.tv_sec = pkt_r->GetCountField(0);
|
||||||
ts.tv_usec = pkt_r->GetCountField(1);
|
ts.tv_usec = pkt_r->GetCountField(1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue