From e79ce27c9f09b0cf8b566d57a004f8e268b97359 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Tue, 20 Aug 2024 14:06:28 +0200 Subject: [PATCH] threading/Value: Support move and copy constructors --- src/threading/SerialTypes.cc | 51 ++++++++++++++++++++++++++++++++++++ src/threading/SerialTypes.h | 17 +++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/threading/SerialTypes.cc b/src/threading/SerialTypes.cc index 44cddf4636..6ae211c3bc 100644 --- a/src/threading/SerialTypes.cc +++ b/src/threading/SerialTypes.cc @@ -82,6 +82,57 @@ std::string Field::TypeName() const { return n; } +Value::Value(const Value& other) { + type = other.type; + subtype = other.subtype; + present = other.present; + + switch ( other.type ) { + case TYPE_ENUM: + case TYPE_STRING: + case TYPE_FILE: + case TYPE_FUNC: { + val.string_val.data = util::copy_string(other.val.string_val.data, other.val.string_val.length); + break; + } + + case TYPE_PATTERN: { + val.pattern_text_val = util::copy_string(val.pattern_text_val); + break; + } + case TYPE_TABLE: { + val.set_val.vals = new Value*[other.val.set_val.size]; + for ( zeek_int_t i = 0; i < other.val.set_val.size; i++ ) + val.set_val.vals[i] = new Value(*other.val.set_val.vals[i]); + break; + } + case TYPE_VECTOR: { + val.vector_val.vals = new Value*[other.val.vector_val.size]; + for ( zeek_int_t i = 0; i < other.val.vector_val.size; i++ ) + val.vector_val.vals[i] = new Value(*other.val.vector_val.vals[i]); + break; + } + default: { + // Deal with simple/atomic types. + val = other.val; + break; + } + }; +} + +Value::Value(Value&& other) noexcept { + present = other.present; + type = other.type; + subtype = other.type; + line_number = other.line_number; + + val = other.val; // take ownership. + + other.val = _val(); + other.line_number = -1; + other.present = false; +} + Value::~Value() { if ( ! present ) return; diff --git a/src/threading/SerialTypes.h b/src/threading/SerialTypes.h index 85e96d8e4a..dabf27210e 100644 --- a/src/threading/SerialTypes.h +++ b/src/threading/SerialTypes.h @@ -104,9 +104,9 @@ private: * those Vals supported). */ struct Value { - TypeTag type; //! The type of the value. - TypeTag subtype; //! Inner type for sets and vectors. - bool present; //! False for optional record fields that are not set. + TypeTag type; //! The type of the value. + TypeTag subtype; //! Inner type for sets and vectors. + bool present = false; //! False for optional record fields that are not set. struct set_t { zeek_int_t size; @@ -185,6 +185,16 @@ struct Value { Value(TypeTag arg_type, TypeTag arg_subtype, bool arg_present = true) : type(arg_type), subtype(arg_subtype), present(arg_present) {} + /** + * Copy constructor. + */ + Value(const Value& other); + + /** + * Move constructor. + */ + Value(Value&& other) noexcept; + /** * Destructor. */ @@ -241,7 +251,6 @@ struct Value { private: friend class IPAddr; - Value(const Value& other) = delete; // For values read by the input framework, this can represent the line number // containing this value. Used by the Ascii reader primarily.