mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 09:08:20 +00:00
Add RecordVal::AssignField() and use it in supervisor code
This is a convenience method to assign a known record field value by field name. May also be useful to reduce warnings from static analysis (e.g. Coverity) about not checking for negative return values before assigning since that now flows through a [[noreturn]] error path.
This commit is contained in:
parent
7bf885b0b8
commit
2855df63ce
3 changed files with 33 additions and 22 deletions
13
src/Val.h
13
src/Val.h
|
@ -1152,6 +1152,19 @@ public:
|
|||
void Assign(int field, String* new_val)
|
||||
{ Assign(field, new StringVal(new_val)); }
|
||||
|
||||
/**
|
||||
* Assign a value of type @c T to a record field of the given name.
|
||||
* A fatal error occurs if the no such field name exists.
|
||||
*/
|
||||
template <class T>
|
||||
void AssignField(const char* field_name, T&& val)
|
||||
{
|
||||
int idx = GetType()->AsRecordType()->FieldOffset(field_name);
|
||||
if ( idx < 0 )
|
||||
reporter->InternalError("missing record field: %s", field_name);
|
||||
Assign(idx, std::forward<T>(val));
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a value to the record's fields. The caller is responsible
|
||||
* for ensuring that fields are appended in the correct order and
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue