threading/Value: Support move and copy constructors

This commit is contained in:
Arne Welzel 2024-08-20 14:06:28 +02:00
parent 79ebce6e3c
commit e79ce27c9f
2 changed files with 64 additions and 4 deletions

View file

@ -82,6 +82,57 @@ std::string Field::TypeName() const {
return n; 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() { Value::~Value() {
if ( ! present ) if ( ! present )
return; return;

View file

@ -104,9 +104,9 @@ private:
* those Vals supported). * those Vals supported).
*/ */
struct Value { struct Value {
TypeTag type; //! The type of the value. TypeTag type; //! The type of the value.
TypeTag subtype; //! Inner type for sets and vectors. TypeTag subtype; //! Inner type for sets and vectors.
bool present; //! False for optional record fields that are not set. bool present = false; //! False for optional record fields that are not set.
struct set_t { struct set_t {
zeek_int_t size; zeek_int_t size;
@ -185,6 +185,16 @@ struct Value {
Value(TypeTag arg_type, TypeTag arg_subtype, bool arg_present = true) Value(TypeTag arg_type, TypeTag arg_subtype, bool arg_present = true)
: type(arg_type), subtype(arg_subtype), present(arg_present) {} : type(arg_type), subtype(arg_subtype), present(arg_present) {}
/**
* Copy constructor.
*/
Value(const Value& other);
/**
* Move constructor.
*/
Value(Value&& other) noexcept;
/** /**
* Destructor. * Destructor.
*/ */
@ -241,7 +251,6 @@ struct Value {
private: private:
friend class IPAddr; friend class IPAddr;
Value(const Value& other) = delete;
// For values read by the input framework, this can represent the line number // For values read by the input framework, this can represent the line number
// containing this value. Used by the Ascii reader primarily. // containing this value. Used by the Ascii reader primarily.