From 8fd9bf10e5333d38ed662d9b0f7a0cb70c662374 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 25 Jul 2013 17:30:18 -0400 Subject: [PATCH] update pvD array implementation --- pvDataApp/factory/Convert.cpp | 8 ++-- pvDataApp/factory/PVDataCreateFactory.cpp | 46 +++++++++++++---------- pvDataApp/factory/PVStructureArray.cpp | 33 +++++++++------- pvDataApp/factory/StandardPVField.cpp | 4 +- pvDataApp/property/pvEnumerated.cpp | 2 +- 5 files changed, 53 insertions(+), 40 deletions(-) diff --git a/pvDataApp/factory/Convert.cpp b/pvDataApp/factory/Convert.cpp index 7e60f10..fbacdb6 100644 --- a/pvDataApp/factory/Convert.cpp +++ b/pvDataApp/factory/Convert.cpp @@ -125,7 +125,8 @@ size_t Convert::fromStringArray(PVScalarArrayPtr const &pv, from.begin()+fromOffset+length, data.begin()); - pv->putFrom(data); + PVStringArray::const_svector temp(freeze(data)); + pv->putFrom(temp); return length; } else { @@ -399,10 +400,7 @@ void Convert::copyStructureArray( } else if(to->isImmutable()) { throw std::invalid_argument("Convert.copyStructureArray destination is immutable"); } - PVStructureArray::svector data; - from->swap(data); - to->replace(data); - from->swap(data); + to->replace(from->view()); } void Convert::newLine(StringBuilder buffer, int indentLevel) diff --git a/pvDataApp/factory/PVDataCreateFactory.cpp b/pvDataApp/factory/PVDataCreateFactory.cpp index a9efae1..adf813f 100644 --- a/pvDataApp/factory/PVDataCreateFactory.cpp +++ b/pvDataApp/factory/PVDataCreateFactory.cpp @@ -202,11 +202,15 @@ public: DefaultPVArray(ScalarArrayConstPtr const & scalarArray); virtual ~DefaultPVArray(); + virtual size_t getLength() const {return value.size();} + virtual size_t getCapacity() const {return value.capacity();} + virtual void setCapacity(size_t capacity); virtual void setLength(size_t length); - virtual const svector& viewUnsafe() const; - virtual void swap(svector &other); + virtual const_svector view() const {return value;} + virtual void swap(const_svector &other); + virtual void replace(const const_svector& next); // from Serializable virtual void serialize(ByteBuffer *pbuffer,SerializableControl *pflusher) const; @@ -214,7 +218,7 @@ public: virtual void serialize(ByteBuffer *pbuffer, SerializableControl *pflusher, size_t offset, size_t count) const; private: - svector value; + const_svector value; }; template @@ -227,7 +231,6 @@ DefaultPVArray::DefaultPVArray(ScalarArrayConstPtr const & scalarArray) template DefaultPVArray::~DefaultPVArray() { } - template void DefaultPVArray::setCapacity(size_t capacity) { @@ -249,15 +252,15 @@ void DefaultPVArray::setLength(size_t length) value.resize(length); } - template -const typename DefaultPVArray::svector& DefaultPVArray::viewUnsafe() const +void DefaultPVArray::replace(const const_svector& next) { - return value; + value = next; + this->postPut(); } template -void DefaultPVArray::swap(svector &other) +void DefaultPVArray::swap(const_svector &other) { if(this->isImmutable()) THROW_EXCEPTION2(std::logic_error,"Immutable"); @@ -277,9 +280,10 @@ void DefaultPVArray::deserialize(ByteBuffer *pbuffer, DeserializableControl *pcontrol) { size_t size = SerializeHelper::readSize(pbuffer, pcontrol); - value.resize(size); // TODO: avoid copy of stuff we will then overwrite + svector nextvalue(thaw(value)); + nextvalue.resize(size); // TODO: avoid copy of stuff we will then overwrite - T* cur = value.data(); + T* cur = nextvalue.data(); // try to avoid deserializing from the buffer // this is only possible if we do not need to do endian-swapping @@ -318,6 +322,7 @@ void DefaultPVArray::deserialize(ByteBuffer *pbuffer, cur += n2read; remaining -= n2read; } + value = freeze(nextvalue); // inform about the change? PVField::postPut(); } @@ -327,13 +332,13 @@ void DefaultPVArray::serialize(ByteBuffer *pbuffer, SerializableControl *pflusher, size_t offset, size_t count) const { //TODO: avoid incrementing the ref counter... - svector temp(value); + const_svector temp(value); temp.slice(offset, count); count = temp.size(); SerializeHelper::writeSize(temp.size(), pbuffer, pflusher); - T* cur = temp.data(); + const T* cur = temp.data(); // try to avoid copying into the buffer // this is only possible if we do not need to do endian-swapping @@ -369,18 +374,21 @@ void DefaultPVArray::deserialize(ByteBuffer *pbuffer, DeserializableControl *pcontrol) { size_t size = SerializeHelper::readSize(pbuffer, pcontrol); + svector nextvalue(thaw(value)); + // Decide if we must re-allocate - if(size > value.size() || !value.unique()) - value.resize(size); - else if(size < value.size()) - value.slice(0, size); + if(size > nextvalue.size() || !nextvalue.unique()) + nextvalue.resize(size); + else if(size < nextvalue.size()) + nextvalue.slice(0, size); - String * pvalue = value.data(); + String * pvalue = nextvalue.data(); for(size_t i = 0; i void DefaultPVArray::serialize(ByteBuffer *pbuffer, SerializableControl *pflusher, size_t offset, size_t count) const { - svector temp(value); + const_svector temp(value); temp.slice(offset, count); SerializeHelper::writeSize(temp.size(), pbuffer, pflusher); - String * pvalue = temp.data(); + const String * pvalue = temp.data(); for(size_t i = 0; igetStructure(); @@ -34,7 +33,8 @@ size_t PVStructureArray::append(size_t number) size_t newLength = data.size(); - swap(data); + const_svector cdata(freeze(data)); + swap(cdata); return newLength; } @@ -55,7 +55,8 @@ bool PVStructureArray::remove(size_t offset,size_t number) } vec.resize(length - number); - swap(vec); + const_svector cdata(freeze(vec)); + swap(cdata); return true; } @@ -89,15 +90,20 @@ void PVStructureArray::compress() { } vec.resize(newLength); - swap(vec); + const_svector cdata(freeze(vec)); + swap(cdata); } void PVStructureArray::setCapacity(size_t capacity) { if(this->isCapacityMutable()) { - svector value; + const_svector value; swap(value); - value.reserve(capacity); + if(value.capacity()isImmutable()) THROW_EXCEPTION2(std::logic_error,"Immutable"); - svector value; + const_svector value; swap(value); if(length == value.size()) { // nothing } else if(length < value.size()) { value.slice(0, length); } else { - value.resize(length); + svector mvalue(thaw(value)); + mvalue.resize(length); + value = freeze(mvalue); } swap(value); } -void PVStructureArray::swap(svector &other) +void PVStructureArray::swap(const_svector &other) { if(this->isImmutable()) THROW_EXCEPTION2(std::logic_error,"Immutable"); @@ -133,8 +141,7 @@ void PVStructureArray::serialize(ByteBuffer *pbuffer, void PVStructureArray::deserialize(ByteBuffer *pbuffer, DeserializableControl *pcontrol) { - svector data; - swap(data); + svector data(reuse()); size_t size = SerializeHelper::readSize(pbuffer, pcontrol); data.resize(size); @@ -154,7 +161,7 @@ void PVStructureArray::deserialize(ByteBuffer *pbuffer, data[i]->deserialize(pbuffer, pcontrol); } } - replace(data); // calls postPut() + replace(freeze(data)); // calls postPut() } void PVStructureArray::serialize(ByteBuffer *pbuffer, diff --git a/pvDataApp/factory/StandardPVField.cpp b/pvDataApp/factory/StandardPVField.cpp index f74f60a..80058f9 100644 --- a/pvDataApp/factory/StandardPVField.cpp +++ b/pvDataApp/factory/StandardPVField.cpp @@ -61,7 +61,7 @@ PVStructurePtr StandardPVField::enumerated(StringArray const &choices) "choices",pvString); PVStringArray::svector cdata(choices.size()); std::copy(choices.begin(), choices.end(), cdata.begin()); - static_cast(*pvScalarArray).replace(cdata); + static_cast(*pvScalarArray).replace(freeze(cdata)); return pvStructure; } @@ -74,7 +74,7 @@ PVStructurePtr StandardPVField::enumerated( "value.choices",pvString); PVStringArray::svector cdata(choices.size()); std::copy(choices.begin(), choices.end(), cdata.begin()); - static_cast(*pvScalarArray).replace(cdata); + static_cast(*pvScalarArray).replace(freeze(cdata)); return pvStructure; } diff --git a/pvDataApp/property/pvEnumerated.cpp b/pvDataApp/property/pvEnumerated.cpp index ce76355..77f24d9 100644 --- a/pvDataApp/property/pvEnumerated.cpp +++ b/pvDataApp/property/pvEnumerated.cpp @@ -106,7 +106,7 @@ bool PVEnumerated:: setChoices(const StringArray & choices) if(pvChoices->isImmutable()) return false; PVStringArray::svector data(choices.size()); std::copy(choices.begin(), choices.end(), data.begin()); - pvChoices->replace(data); + pvChoices->replace(freeze(data)); return true; }