Val: cast_value_to_type() returns IntrusivePtr

This commit is contained in:
Max Kellermann 2020-02-27 10:15:03 +01:00
parent 96951c1300
commit 3331abb4f2
4 changed files with 6 additions and 8 deletions

View file

@ -4797,8 +4797,7 @@ IntrusivePtr<Val> CastExpr::Eval(Frame* f) const
if ( ! v ) if ( ! v )
return 0; return 0;
IntrusivePtr<Val> nv{AdoptRef{}, cast_value_to_type(v.get(), Type())}; auto nv = cast_value_to_type(v.get(), Type());
if ( nv ) if ( nv )
return nv; return nv;

View file

@ -834,7 +834,7 @@ Val* SwitchStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const
if ( matching_id ) if ( matching_id )
{ {
auto cv = cast_value_to_type(v, matching_id->Type()); auto cv = cast_value_to_type(v, matching_id->Type());
f->SetElement(matching_id, cv); f->SetElement(matching_id, cv.release());
} }
flow = FLOW_NEXT; flow = FLOW_NEXT;

View file

@ -3206,7 +3206,7 @@ void delete_vals(val_list* vals)
} }
} }
Val* cast_value_to_type(Val* v, BroType* t) IntrusivePtr<Val> cast_value_to_type(Val* v, BroType* t)
{ {
// Note: when changing this function, adapt all three of // Note: when changing this function, adapt all three of
// cast_value_to_type()/can_cast_value_to_type()/can_cast_value_to_type(). // cast_value_to_type()/can_cast_value_to_type()/can_cast_value_to_type().
@ -3217,7 +3217,7 @@ Val* cast_value_to_type(Val* v, BroType* t)
// Always allow casting to same type. This also covers casting 'any' // Always allow casting to same type. This also covers casting 'any'
// to the actual type. // to the actual type.
if ( same_type(v->Type(), t) ) if ( same_type(v->Type(), t) )
return v->Ref(); return {NewRef{}, v};
if ( same_type(v->Type(), bro_broker::DataVal::ScriptDataType()) ) if ( same_type(v->Type(), bro_broker::DataVal::ScriptDataType()) )
{ {
@ -3226,7 +3226,7 @@ Val* cast_value_to_type(Val* v, BroType* t)
if ( ! dv ) if ( ! dv )
return nullptr; return nullptr;
return static_cast<bro_broker::DataVal *>(dv)->castTo(t).release(); return static_cast<bro_broker::DataVal *>(dv)->castTo(t);
} }
return nullptr; return nullptr;

View file

@ -1037,10 +1037,9 @@ extern void delete_vals(val_list* vals);
inline bool is_vector(Val* v) { return v->Type()->Tag() == TYPE_VECTOR; } inline bool is_vector(Val* v) { return v->Type()->Tag() == TYPE_VECTOR; }
// Returns v casted to type T if the type supports that. Returns null if not. // Returns v casted to type T if the type supports that. Returns null if not.
// The returned value will be ref'ed.
// //
// Note: This implements the script-level cast operator. // Note: This implements the script-level cast operator.
extern Val* cast_value_to_type(Val* v, BroType* t); extern IntrusivePtr<Val> cast_value_to_type(Val* v, BroType* t);
// Returns true if v can be casted to type T. If so, check_and_cast() will // Returns true if v can be casted to type T. If so, check_and_cast() will
// succeed as well. // succeed as well.