update pvD array implementation

This commit is contained in:
Michael Davidsaver
2013-07-25 17:30:18 -04:00
parent 9ac030169a
commit 8fd9bf10e5
5 changed files with 53 additions and 40 deletions
+3 -5
View File
@@ -125,7 +125,8 @@ size_t Convert::fromStringArray(PVScalarArrayPtr const &pv,
from.begin()+fromOffset+length,
data.begin());
pv->putFrom<pvString>(data);
PVStringArray::const_svector temp(freeze(data));
pv->putFrom<pvString>(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)
+27 -19
View File
@@ -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<typename T>
@@ -227,7 +231,6 @@ DefaultPVArray<T>::DefaultPVArray(ScalarArrayConstPtr const & scalarArray)
template<typename T>
DefaultPVArray<T>::~DefaultPVArray()
{ }
template<typename T>
void DefaultPVArray<T>::setCapacity(size_t capacity)
{
@@ -249,15 +252,15 @@ void DefaultPVArray<T>::setLength(size_t length)
value.resize(length);
}
template<typename T>
const typename DefaultPVArray<T>::svector& DefaultPVArray<T>::viewUnsafe() const
void DefaultPVArray<T>::replace(const const_svector& next)
{
return value;
value = next;
this->postPut();
}
template<typename T>
void DefaultPVArray<T>::swap(svector &other)
void DefaultPVArray<T>::swap(const_svector &other)
{
if(this->isImmutable())
THROW_EXCEPTION2(std::logic_error,"Immutable");
@@ -277,9 +280,10 @@ void DefaultPVArray<T>::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<T>::deserialize(ByteBuffer *pbuffer,
cur += n2read;
remaining -= n2read;
}
value = freeze(nextvalue);
// inform about the change?
PVField::postPut();
}
@@ -327,13 +332,13 @@ void DefaultPVArray<T>::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<String>::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<size; i++) {
pvalue[i] = SerializeHelper::deserializeString(pbuffer,
pcontrol);
}
value = freeze(nextvalue);
// inform about the change?
postPut();
}
@@ -389,12 +397,12 @@ template<>
void DefaultPVArray<String>::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; i<temp.size(); i++) {
SerializeHelper::serializeString(pvalue[i], pbuffer, pflusher);
}
+20 -13
View File
@@ -23,8 +23,7 @@ namespace epics { namespace pvData {
size_t PVStructureArray::append(size_t number)
{
svector data;
swap(data);
svector data(reuse());
data.resize(data.size()+number);
StructureConstPtr structure = structureArray->getStructure();
@@ -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()<capacity) {
svector mvalue(thaw(value));
mvalue.reserve(capacity);
value = freeze(mvalue);
}
swap(value);
}
}
@@ -106,19 +112,21 @@ void PVStructureArray::setLength(size_t length)
{
if(this->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,
+2 -2
View File
@@ -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<PVStringArray&>(*pvScalarArray).replace(cdata);
static_cast<PVStringArray&>(*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<PVStringArray&>(*pvScalarArray).replace(cdata);
static_cast<PVStringArray&>(*pvScalarArray).replace(freeze(cdata));
return pvStructure;
}
+1 -1
View File
@@ -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;
}