diff --git a/src/Val.cc b/src/Val.cc index e63ce9cc9f..1c01058a8e 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1145,13 +1145,13 @@ ValPtr FileVal::DoClone(CloneState* state) // I think we can just ref the file here - it is unclear what else // to do. In the case of cached files, I think this is equivalent // to what happened before - serialization + unserialization just - // have you the same pointer that you already had. In the case of + // gave you the same pointer that you already had. In the case of // non-cached files, the behavior now is different; in the past, // serialize + unserialize gave you a new file object because the // old one was not in the list anymore. This object was // automatically opened. This does not happen anymore - instead you // get the non-cached pointer back which is brought back into the - // cache when written too. + // cache when written to. return {NewRef{}, this}; } @@ -3369,10 +3369,10 @@ ValPtr VectorVal::DoClone(CloneState* state) vv->Reserve(vector_val->size()); state->NewClone(this, vv); - for ( unsigned int i = 0; i < vector_val->size(); ++i ) + for ( const auto& e : *vector_val ) { - auto v = (*vector_val)[i]->Clone(state); - vv->Assign(vv->Size(), std::move(v)); + auto vc = e->Clone(state); + vv->Append(std::move(vc)); } return vv; @@ -3382,17 +3382,20 @@ void VectorVal::ValDescribe(ODesc* d) const { d->Add("["); - if ( vector_val->size() > 0 ) - for ( unsigned int i = 0; i < (vector_val->size() - 1); ++i ) + size_t vector_size = vector_val->size(); + + if ( vector_size != 0) + { + for ( unsigned int i = 0; i < (vector_size - 1); ++i ) { - if ( (*vector_val)[i] ) - (*vector_val)[i]->Describe(d); + if ( vector_val->at(i) ) + vector_val->at(i)->Describe(d); d->Add(", "); } + } - if ( vector_val->size() && - (*vector_val)[vector_val->size() - 1] ) - (*vector_val)[vector_val->size() - 1]->Describe(d); + if ( vector_size != 0 && vector_val->back() ) + vector_val->back()->Describe(d); d->Add("]"); } diff --git a/src/Val.h b/src/Val.h index d9d6667fff..60300ca03b 100644 --- a/src/Val.h +++ b/src/Val.h @@ -403,7 +403,7 @@ public: : BoolVal(bro_int_t(b)) {} - bool Get() const { return int_val; } + bool Get() const { return static_cast(int_val); } }; class CountVal : public UnsignedValImplementation { @@ -438,7 +438,7 @@ public: quantity * units) {} - double Get() const { return double_val; } + // Same as for IntVal: no Get() method needed. protected: void ValDescribe(ODesc* d) const override; @@ -449,7 +449,7 @@ public: TimeVal(double t) : DoubleValImplementation(base_type(TYPE_TIME), t) {} - double Get() const { return double_val; } + // Same as for IntVal: no Get() method needed. }; class PortVal final : public UnsignedValImplementation { @@ -1328,8 +1328,6 @@ class EnumVal final : public IntValImplementation { public: ValPtr SizeVal() const override; - int Get() const { return int_val; } - protected: friend class Val; friend class EnumType; @@ -1472,6 +1470,15 @@ public: bool Insert(unsigned int index, Val* element) { return Insert(index, {AdoptRef{}, element}); } + /** + * Inserts an element at the end of the vector. + * @param element The value to insert into the vector. + * @return True if the element was inserted or false if the element was + * the wrong type. + */ + bool Append(ValPtr element) + { return Insert(Size(), element); } + // Removes an element at a specific position. bool Remove(unsigned int index);