mirror of
https://github.com/zeek/zeek.git
synced 2025-10-16 05:28:20 +00:00
Adding a new RecordVal method LookupWithDefault().
The method honors a potential &default attribute, and refs the Val it returns.
This commit is contained in:
parent
ba2c6f6139
commit
d673c8c64c
2 changed files with 17 additions and 3 deletions
13
src/Val.cc
13
src/Val.cc
|
@ -2893,6 +2893,19 @@ Val* RecordVal::Lookup(int field) const
|
||||||
return (*AsRecord())[field];
|
return (*AsRecord())[field];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Val* RecordVal::LookupWithDefault(int field) const
|
||||||
|
{
|
||||||
|
Val* val = (*AsRecord())[field];
|
||||||
|
|
||||||
|
if ( val )
|
||||||
|
return val->Ref();
|
||||||
|
|
||||||
|
// Check for &default.
|
||||||
|
const Attr* def_attr = record_type->FieldDecl(field)->attrs->FindAttr(ATTR_DEFAULT);
|
||||||
|
|
||||||
|
return def_attr ? def_attr->AttrExpr()->Eval(0) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
RecordVal* RecordVal::CoerceTo(const RecordType* t, Val* aggr) const
|
RecordVal* RecordVal::CoerceTo(const RecordType* t, Val* aggr) const
|
||||||
{
|
{
|
||||||
if ( ! record_promotion_compatible(t->AsRecordType(), Type()->AsRecordType()) )
|
if ( ! record_promotion_compatible(t->AsRecordType(), Type()->AsRecordType()) )
|
||||||
|
|
|
@ -909,7 +909,8 @@ public:
|
||||||
{ return new Val(record_type->NumFields(), TYPE_COUNT); }
|
{ return new Val(record_type->NumFields(), TYPE_COUNT); }
|
||||||
|
|
||||||
void Assign(int field, Val* new_val, Opcode op = OP_ASSIGN);
|
void Assign(int field, Val* new_val, Opcode op = OP_ASSIGN);
|
||||||
Val* Lookup(int field) const;
|
Val* Lookup(int field) const; // does not Ref() value.
|
||||||
|
Val* LookupWithDefault(int field) const; // does Ref() value.
|
||||||
|
|
||||||
void Describe(ODesc* d) const;
|
void Describe(ODesc* d) const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue