Some review cleanup in Val.cc

This commit is contained in:
Tim Wojtulewicz 2020-12-03 09:56:40 -07:00
parent e652aff277
commit 42f2691251
2 changed files with 27 additions and 17 deletions

View file

@ -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("]");
}

View file

@ -403,7 +403,7 @@ public:
: BoolVal(bro_int_t(b))
{}
bool Get() const { return int_val; }
bool Get() const { return static_cast<bool>(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);