factory: templates for BasePV*Array implementations
This commit is contained in:
@@ -18,15 +18,6 @@
|
||||
#include "PVArray.cpp"
|
||||
#include "PVScalarArray.cpp"
|
||||
#include "PVStructure.cpp"
|
||||
#include "DefaultPVString.cpp"
|
||||
#include "DefaultPVBooleanArray.cpp"
|
||||
#include "DefaultPVByteArray.cpp"
|
||||
#include "DefaultPVShortArray.cpp"
|
||||
#include "DefaultPVIntArray.cpp"
|
||||
#include "DefaultPVLongArray.cpp"
|
||||
#include "DefaultPVFloatArray.cpp"
|
||||
#include "DefaultPVDoubleArray.cpp"
|
||||
#include "DefaultPVStringArray.cpp"
|
||||
#include "DefaultPVStructureArray.cpp"
|
||||
|
||||
namespace epics { namespace pvData {
|
||||
@@ -128,6 +119,241 @@ typedef BasePVScalar<float> BasePVFloat;
|
||||
typedef BasePVScalar<double> BasePVDouble;
|
||||
typedef BasePVScalar<String> BasePVString;
|
||||
|
||||
|
||||
/** Default storage for arrays
|
||||
*/
|
||||
template<typename T>
|
||||
class DefaultPVArray : public PVValueArray<T> {
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
|
||||
DefaultPVArray(PVStructure *parent,ScalarArrayConstPtr scalarArray);
|
||||
virtual ~DefaultPVArray();
|
||||
virtual void setCapacity(int capacity);
|
||||
virtual int get(int offset, int length, PVArrayData<T> *data) ;
|
||||
virtual int put(int offset,int length, pointer from,
|
||||
int fromOffset);
|
||||
virtual void shareData(pointer value,int capacity,int length);
|
||||
// from Serializable
|
||||
virtual void serialize(ByteBuffer *pbuffer,SerializableControl *pflusher) ;
|
||||
virtual void deserialize(ByteBuffer *pbuffer,DeserializableControl *pflusher);
|
||||
virtual void serialize(ByteBuffer *pbuffer,
|
||||
SerializableControl *pflusher, int offset, int count) ;
|
||||
virtual bool operator==(PVField& pv) ;
|
||||
virtual bool operator!=(PVField& pv) ;
|
||||
private:
|
||||
pointer value;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
DefaultPVArray<T>::DefaultPVArray(PVStructure *parent,
|
||||
ScalarArrayConstPtr scalarArray)
|
||||
: PVValueArray<T>(parent,scalarArray),value(new T[0])
|
||||
{ }
|
||||
|
||||
template<typename T>
|
||||
DefaultPVArray<T>::~DefaultPVArray()
|
||||
{
|
||||
delete[] value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void DefaultPVArray<T>::setCapacity(int capacity)
|
||||
{
|
||||
if(PVArray::getCapacity()==capacity) return;
|
||||
if(!PVArray::isCapacityMutable()) {
|
||||
std::string message("not capacityMutable");
|
||||
PVField::message(message, errorMessage);
|
||||
return;
|
||||
}
|
||||
int length = PVArray::getLength();
|
||||
if(length>capacity) length = capacity;
|
||||
T *newValue = new T[capacity];
|
||||
for(int i=0; i<length; i++) newValue[i] = value[i];
|
||||
delete[]value;
|
||||
value = newValue;
|
||||
PVArray::setCapacityLength(capacity,length);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int DefaultPVArray<T>::get(int offset, int len, PVArrayData<T> *data)
|
||||
{
|
||||
int n = len;
|
||||
int length = PVArray::getLength();
|
||||
if(offset+len > length) {
|
||||
n = length-offset;
|
||||
if(n<0) n = 0;
|
||||
}
|
||||
data->data = value;
|
||||
data->offset = offset;
|
||||
return n;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int DefaultPVArray<T>::put(int offset,int len,
|
||||
pointer from,int fromOffset)
|
||||
{
|
||||
if(PVField::isImmutable()) {
|
||||
PVField::message("field is immutable",errorMessage);
|
||||
return 0;
|
||||
}
|
||||
if(from==value) return len;
|
||||
if(len<1) return 0;
|
||||
int length = PVArray::getLength();
|
||||
int capacity = PVArray::getCapacity();
|
||||
if(offset+len > length) {
|
||||
int newlength = offset + len;
|
||||
if(newlength>capacity) {
|
||||
setCapacity(newlength);
|
||||
newlength = PVArray::getCapacity();
|
||||
len = newlength - offset;
|
||||
if(len<=0) return 0;
|
||||
}
|
||||
length = newlength;
|
||||
}
|
||||
for(int i=0;i<len;i++) {
|
||||
value[i+offset] = from[i+fromOffset];
|
||||
}
|
||||
PVArray::setLength(length);
|
||||
this->postPut();
|
||||
return len;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void DefaultPVArray<T>::shareData(pointer shareValue,int capacity,int length)
|
||||
{
|
||||
delete[] value;
|
||||
value = shareValue;
|
||||
PVArray::setCapacityLength(capacity,length);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void DefaultPVArray<T>::serialize(ByteBuffer *pbuffer,
|
||||
SerializableControl *pflusher) {
|
||||
serialize(pbuffer, pflusher, 0, PVArray::getLength());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void DefaultPVArray<T>::deserialize(ByteBuffer *pbuffer,
|
||||
DeserializableControl *pcontrol) {
|
||||
int size = SerializeHelper::readSize(pbuffer, pcontrol);
|
||||
if(size>=0) {
|
||||
// prepare array, if necessary
|
||||
if(size>PVArray::getCapacity()) PVArray::setCapacity(size);
|
||||
// retrieve value from the buffer
|
||||
int i = 0;
|
||||
while(true) {
|
||||
int maxIndex = std::min(size-i, pbuffer->getRemaining())+i;
|
||||
for(; i<maxIndex; i++)
|
||||
value[i] = pbuffer->getBoolean();
|
||||
if(i<size)
|
||||
pcontrol->ensureData(1); // // TODO is there a better way to ensureData?
|
||||
else
|
||||
break;
|
||||
}
|
||||
// set new length
|
||||
PVArray::setLength(size);
|
||||
PVField::postPut();
|
||||
}
|
||||
// TODO null arrays (size == -1) not supported
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void DefaultPVArray<T>::serialize(ByteBuffer *pbuffer,
|
||||
SerializableControl *pflusher, int offset, int count) {
|
||||
// cache
|
||||
int length = PVArray::getLength();
|
||||
|
||||
// check bounds
|
||||
if(offset<0)
|
||||
offset = 0;
|
||||
else if(offset>length) offset = length;
|
||||
if(count<0) count = length;
|
||||
|
||||
int maxCount = length-offset;
|
||||
if(count>maxCount) count = maxCount;
|
||||
|
||||
// write
|
||||
SerializeHelper::writeSize(count, pbuffer, pflusher);
|
||||
int end = offset+count;
|
||||
int i = offset;
|
||||
while(true) {
|
||||
int maxIndex = std::min<int>(end-i, pbuffer->getRemaining())+i;
|
||||
for(; i<maxIndex; i++)
|
||||
pbuffer->putBoolean(value[i]);
|
||||
if(i<end)
|
||||
pflusher->flushSerializeBuffer();
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool DefaultPVArray<T>::operator==(PVField& pv)
|
||||
{
|
||||
return getConvert()->equals(this, &pv);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool DefaultPVArray<T>::operator!=(PVField& pv)
|
||||
{
|
||||
return !(getConvert()->equals(this, &pv));
|
||||
}
|
||||
|
||||
// specializations for String
|
||||
|
||||
template<>
|
||||
void DefaultPVArray<String>::deserialize(ByteBuffer *pbuffer,
|
||||
DeserializableControl *pcontrol) {
|
||||
int size = SerializeHelper::readSize(pbuffer, pcontrol);
|
||||
if(size>=0) {
|
||||
// prepare array, if necessary
|
||||
if(size>getCapacity()) setCapacity(size);
|
||||
// retrieve value from the buffer
|
||||
for(int i = 0; i<size; i++)
|
||||
value[i] = SerializeHelper::deserializeString(pbuffer,
|
||||
pcontrol);
|
||||
// set new length
|
||||
setLength(size);
|
||||
postPut();
|
||||
}
|
||||
// TODO null arrays (size == -1) not supported
|
||||
}
|
||||
|
||||
template<>
|
||||
void DefaultPVArray<String>::serialize(ByteBuffer *pbuffer,
|
||||
SerializableControl *pflusher, int offset, int count) {
|
||||
int length = getLength();
|
||||
|
||||
// check bounds
|
||||
if(offset<0)
|
||||
offset = 0;
|
||||
else if(offset>length) offset = length;
|
||||
if(count<0) count = length;
|
||||
|
||||
int maxCount = length-offset;
|
||||
if(count>maxCount) count = maxCount;
|
||||
|
||||
// write
|
||||
SerializeHelper::writeSize(count, pbuffer, pflusher);
|
||||
int end = offset+count;
|
||||
for(int i = offset; i<end; i++)
|
||||
SerializeHelper::serializeString(value[i], pbuffer, pflusher);
|
||||
}
|
||||
|
||||
typedef DefaultPVArray<bool> DefaultPVBooleanArray;
|
||||
typedef DefaultPVArray<int8> BasePVByteArray;
|
||||
typedef DefaultPVArray<int16> BasePVShortArray;
|
||||
typedef DefaultPVArray<int32> BasePVIntArray;
|
||||
typedef DefaultPVArray<int64> BasePVLongArray;
|
||||
typedef DefaultPVArray<float> BasePVFloatArray;
|
||||
typedef DefaultPVArray<double> BasePVDoubleArray;
|
||||
typedef DefaultPVArray<String> BasePVStringArray;
|
||||
|
||||
// Factory
|
||||
|
||||
PVDataCreate::PVDataCreate(){ }
|
||||
|
||||
PVField *PVDataCreate::createPVField(PVStructure *parent,
|
||||
|
||||
Reference in New Issue
Block a user