ZValElement: Implement move assignment operator

This commit is contained in:
Arne Welzel 2025-10-10 18:27:10 +02:00
parent d06e329de2
commit 907c1a3ee9
2 changed files with 37 additions and 0 deletions

View file

@ -4238,6 +4238,19 @@ TEST_CASE("assignment") {
CHECK(v2->RefCnt() == 3);
}
TEST_CASE("move assignment") {
auto t = zeek::id::find_type<zeek::RecordType>("conn_id_ctx");
auto v1 = zeek::make_intrusive<zeek::RecordVal>(t);
auto v2 = zeek::make_intrusive<zeek::RecordVal>(t);
zeek::ZValElement element1 = zeek::ZValElement(v1, t);
CHECK(v1->RefCnt() == 2); // v1 and element1
element1 = zeek::ZValElement(v2, t); // Should use move assignment.
CHECK(v1->RefCnt() == 1); // Only v1 holds the ref.
CHECK(v2->RefCnt() == 2); // v1 and element1 adopted ref from temporary.
}
TEST_CASE("copy constructor") {
auto t = zeek::id::find_type<zeek::RecordType>("conn_id_ctx");
auto v = zeek::make_intrusive<zeek::RecordVal>(t);

View file

@ -1198,6 +1198,30 @@ public:
return *this;
}
/**
* Move assignment operator.
*
* Adopts the reference if \a o holds a managed ZVal.
*/
ZValElement& operator=(ZValElement&& o) noexcept {
if ( this == &o )
return *this;
if ( is_set && is_managed )
Unref(zval.ManagedVal());
is_set = o.is_set;
is_managed = o.is_managed;
tag = o.tag;
zval = o.zval; // Adopts the reference.
// Keep is_managed and tag members valid.
o.is_set = false;
o.zval = ZVal();
return *this;
}
/**
* Assign a ZVal to ZValElement.
*