support unsigned; move fieldName; pvData use shared_pointer everywhere
This commit is contained in:
@@ -12,10 +12,10 @@
|
||||
#include <pv/pvIntrospect.h>
|
||||
#include <pv/convert.h>
|
||||
#include <pv/factory.h>
|
||||
#include <pv/CDRMonitor.h>
|
||||
#include <pv/serializeHelper.h>
|
||||
|
||||
using std::tr1::static_pointer_cast;
|
||||
using std::size_t;
|
||||
|
||||
namespace epics { namespace pvData {
|
||||
|
||||
@@ -27,54 +27,41 @@ static void newLine(StringBuilder buffer, int indentLevel)
|
||||
for(int i=0; i<indentLevel; i++) *buffer += " ";
|
||||
}
|
||||
|
||||
PVDATA_REFCOUNT_MONITOR_DEFINE(field);
|
||||
|
||||
Field::Field(String fieldName,Type type)
|
||||
:m_fieldName(fieldName)
|
||||
,m_fieldType(type)
|
||||
Field::Field(Type type)
|
||||
: m_fieldType(type)
|
||||
{
|
||||
PVDATA_REFCOUNT_MONITOR_CONSTRUCT(field);
|
||||
}
|
||||
|
||||
Field::~Field() {
|
||||
PVDATA_REFCOUNT_MONITOR_DESTRUCT(field);
|
||||
// note that compiler automatically calls destructor for fieldName
|
||||
if(debugLevel==highDebug) printf("~Field %s\n",m_fieldName.c_str());
|
||||
}
|
||||
|
||||
void Field::renameField(String newName)
|
||||
{
|
||||
m_fieldName = newName;
|
||||
}
|
||||
|
||||
void Field::toString(StringBuilder buffer,int indentLevel) const{
|
||||
*buffer += " ";
|
||||
*buffer += m_fieldName.c_str();
|
||||
}
|
||||
|
||||
Scalar::Scalar(String fieldName,ScalarType scalarType)
|
||||
: Field(fieldName,scalar),scalarType(scalarType){}
|
||||
Scalar::Scalar(ScalarType scalarType)
|
||||
: Field(scalar),scalarType(scalarType){}
|
||||
|
||||
Scalar::~Scalar(){}
|
||||
|
||||
void Scalar::toString(StringBuilder buffer,int indentLevel) const{
|
||||
ScalarTypeFunc::toString(buffer,scalarType);
|
||||
Field::toString(buffer,indentLevel);
|
||||
}
|
||||
|
||||
void Scalar::serialize(ByteBuffer *buffer, SerializableControl *control) const {
|
||||
/*
|
||||
control->ensureBuffer(1);
|
||||
buffer->putByte((int8)(epics::pvData::scalar << 4 | getScalarType()));
|
||||
SerializeHelper::serializeString(getFieldName(), buffer, control);
|
||||
*/
|
||||
}
|
||||
|
||||
void Scalar::deserialize(ByteBuffer *buffer, DeserializableControl *control) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void serializeStructureField(const Structure* structure, ByteBuffer* buffer, SerializableControl* control)
|
||||
{
|
||||
/*
|
||||
SerializeHelper::serializeString(structure->getFieldName(), buffer, control);
|
||||
FieldConstPtrArray fields = structure->getFields();
|
||||
SerializeHelper::writeSize(structure->getNumberFields(), buffer, control);
|
||||
@@ -82,10 +69,13 @@ static void serializeStructureField(const Structure* structure, ByteBuffer* buff
|
||||
{
|
||||
control->cachedSerialize(fields[i], buffer);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
static StructureConstPtr deserializeStructureField(const FieldCreate* fieldCreate, ByteBuffer* buffer, DeserializableControl* control)
|
||||
{
|
||||
throw std::invalid_argument("for Matej to convert");
|
||||
/*
|
||||
const String structureFieldName = SerializeHelper::deserializeString(buffer, control);
|
||||
const int32 size = SerializeHelper::readSize(buffer, control);
|
||||
FieldConstPtrArray fields = NULL;
|
||||
@@ -105,33 +95,32 @@ static StructureConstPtr deserializeStructureField(const FieldCreate* fieldCreat
|
||||
|
||||
StructureConstPtr structure = fieldCreate->createStructure(structureFieldName, size, fields);
|
||||
return structure;
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ScalarArray::ScalarArray(String fieldName,ScalarType elementType)
|
||||
: Field(fieldName,scalarArray),elementType(elementType){}
|
||||
ScalarArray::ScalarArray(ScalarType elementType)
|
||||
: Field(scalarArray),elementType(elementType){}
|
||||
|
||||
ScalarArray::~ScalarArray() {}
|
||||
|
||||
void ScalarArray::toString(StringBuilder buffer,int indentLevel) const{
|
||||
ScalarTypeFunc::toString(buffer,elementType);
|
||||
*buffer += "[]";
|
||||
Field::toString(buffer,indentLevel);
|
||||
}
|
||||
|
||||
void ScalarArray::serialize(ByteBuffer *buffer, SerializableControl *control) const {
|
||||
/*
|
||||
control->ensureBuffer(1);
|
||||
buffer->putByte((int8)(epics::pvData::scalarArray << 4 | getElementType()));
|
||||
SerializeHelper::serializeString(getFieldName(), buffer, control);
|
||||
*/
|
||||
}
|
||||
|
||||
void ScalarArray::deserialize(ByteBuffer *buffer, DeserializableControl *control) {
|
||||
}
|
||||
|
||||
StructureArray::StructureArray(String fieldName,StructureConstPtr structure)
|
||||
: Field(fieldName,structureArray),pstructure(structure)
|
||||
StructureArray::StructureArray(StructureConstPtr structure)
|
||||
: Field(structureArray),pstructure(structure)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -140,187 +129,195 @@ StructureArray::~StructureArray() {
|
||||
}
|
||||
|
||||
void StructureArray::toString(StringBuilder buffer,int indentLevel) const {
|
||||
*buffer += "structure[]";
|
||||
Field::toString(buffer,indentLevel);
|
||||
newLine(buffer,indentLevel + 1);
|
||||
pstructure->toString(buffer,indentLevel + 1);
|
||||
if(indentLevel==0) {
|
||||
*buffer += "structure[]";
|
||||
newLine(buffer,indentLevel + 1);
|
||||
pstructure->toString(buffer,indentLevel + 1);
|
||||
return;
|
||||
}
|
||||
pstructure->toString(buffer,indentLevel);
|
||||
}
|
||||
|
||||
void StructureArray::serialize(ByteBuffer *buffer, SerializableControl *control) const {
|
||||
/*
|
||||
control->ensureBuffer(1);
|
||||
buffer->putByte((int8)(epics::pvData::structureArray << 4));
|
||||
SerializeHelper::serializeString(getFieldName(), buffer, control);
|
||||
// we also need to serialize structure field...
|
||||
serializeStructureField(getStructure().get(), buffer, control);
|
||||
*/
|
||||
}
|
||||
|
||||
void StructureArray::deserialize(ByteBuffer *buffer, DeserializableControl *control) {
|
||||
}
|
||||
|
||||
|
||||
Structure::Structure (String fieldName,
|
||||
int numberFields, FieldConstPtrArray infields)
|
||||
: Field(fieldName,structure),
|
||||
numberFields(numberFields),
|
||||
Structure::Structure (StringArray fieldNames,FieldConstPtrArray infields)
|
||||
: Field(structure),
|
||||
fieldNames(fieldNames),
|
||||
fields(infields)
|
||||
{
|
||||
for(int i=0; i<numberFields; i++) {
|
||||
String name = fields[i]->getFieldName();
|
||||
if(fieldNames.size()!=fields.size()) {
|
||||
throw std::invalid_argument("fieldNames.size()!=fields.size()");
|
||||
}
|
||||
size_t number = fields.size();
|
||||
for(size_t i=0; i<number; i++) {
|
||||
String name = fieldNames[i];
|
||||
// look for duplicates
|
||||
for(int j=i+1; j<numberFields; j++) {
|
||||
String otherName = fields[j]->getFieldName();
|
||||
for(size_t j=i+1; j<number; j++) {
|
||||
String otherName = fieldNames[j];
|
||||
int result = name.compare(otherName);
|
||||
if(result==0) {
|
||||
String message("duplicate fieldName ");
|
||||
message += name;
|
||||
delete[] fields;
|
||||
throw std::invalid_argument(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Structure::~Structure() {
|
||||
if(debugLevel==highDebug)
|
||||
printf("~Structure %s\n",Field::getFieldName().c_str());
|
||||
Structure::~Structure() { }
|
||||
|
||||
delete[] fields;
|
||||
}
|
||||
|
||||
FieldConstPtr Structure::getField(String fieldName) const {
|
||||
for(int i=0; i<numberFields; i++) {
|
||||
size_t numberFields = fields.size();
|
||||
for(size_t i=0; i<numberFields; i++) {
|
||||
FieldConstPtr pfield = fields[i];
|
||||
int result = fieldName.compare(pfield->getFieldName());
|
||||
int result = fieldName.compare(fieldNames[i]);
|
||||
if(result==0) return pfield;
|
||||
}
|
||||
return FieldConstPtr();
|
||||
}
|
||||
|
||||
int Structure::getFieldIndex(String fieldName) const {
|
||||
for(int i=0; i<numberFields; i++) {
|
||||
size_t Structure::getFieldIndex(String fieldName) const {
|
||||
size_t numberFields = fields.size();
|
||||
for(size_t i=0; i<numberFields; i++) {
|
||||
FieldConstPtr pfield = fields[i];
|
||||
int result = fieldName.compare(pfield->getFieldName());
|
||||
int result = fieldName.compare(fieldNames[i]);
|
||||
if(result==0) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void Structure::appendField(FieldConstPtr field)
|
||||
{
|
||||
FieldConstPtr *newFields = new FieldConstPtr[numberFields+1];
|
||||
for(int i=0; i<numberFields; i++) newFields[i] = fields[i];
|
||||
newFields[numberFields] = field;
|
||||
delete[] fields;
|
||||
fields = newFields;
|
||||
numberFields++;
|
||||
}
|
||||
|
||||
void Structure::appendFields(int numberNew,FieldConstPtrArray nfields)
|
||||
{
|
||||
FieldConstPtr *newFields = new FieldConstPtr[numberFields+numberNew];
|
||||
for(int i=0; i<numberFields; i++) newFields[i] = fields[i];
|
||||
for(int i=0; i<numberNew; i++) newFields[numberFields+i] = nfields[i];
|
||||
delete[] fields;
|
||||
fields = newFields;
|
||||
numberFields += numberNew;
|
||||
}
|
||||
|
||||
void Structure::removeField(int index)
|
||||
{
|
||||
if(index<0 || index>=numberFields) {
|
||||
throw std::invalid_argument(
|
||||
String("Structure::removeField index out of bounds"));
|
||||
}
|
||||
FieldConstPtr *newFields = new FieldConstPtr[numberFields-1];
|
||||
|
||||
int ind=0;
|
||||
for(int i=0; i<numberFields; i++) {
|
||||
if(i==index) continue;
|
||||
newFields[ind++] = fields[i];
|
||||
}
|
||||
delete[] fields;
|
||||
fields = newFields;
|
||||
--numberFields;
|
||||
}
|
||||
|
||||
void Structure::toString(StringBuilder buffer,int indentLevel) const{
|
||||
*buffer += "structure";
|
||||
Field::toString(buffer,indentLevel);
|
||||
newLine(buffer,indentLevel+1);
|
||||
for(int i=0; i<numberFields; i++) {
|
||||
toStringCommon(buffer,indentLevel+1);
|
||||
}
|
||||
|
||||
void Structure::toStringCommon(StringBuilder buffer,int indentLevel) const{
|
||||
newLine(buffer,indentLevel);
|
||||
size_t numberFields = fields.size();
|
||||
for(size_t i=0; i<numberFields; i++) {
|
||||
FieldConstPtr pfield = fields[i];
|
||||
pfield->toString(buffer,indentLevel+1);
|
||||
if(i<numberFields-1) newLine(buffer,indentLevel+1);
|
||||
switch(pfield->getType()) {
|
||||
case scalar:
|
||||
case scalarArray:
|
||||
pfield->toString(buffer, indentLevel);
|
||||
*buffer += " ";
|
||||
*buffer += fieldNames[i];
|
||||
break;
|
||||
case structure:
|
||||
{
|
||||
Field const *xxx = pfield.get();
|
||||
Structure const *pstruct = static_cast<Structure const*>(xxx);
|
||||
*buffer += "structure ";
|
||||
*buffer += fieldNames[i];
|
||||
pstruct->toStringCommon(buffer,indentLevel + 1);
|
||||
break;
|
||||
}
|
||||
case structureArray:
|
||||
*buffer += "structure[] " + fieldNames[i];
|
||||
newLine(buffer,indentLevel +1);
|
||||
pfield->toString(buffer,indentLevel +1);
|
||||
break;
|
||||
}
|
||||
if(i<numberFields-1) newLine(buffer,indentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
void Structure::serialize(ByteBuffer *buffer, SerializableControl *control) const {
|
||||
/*
|
||||
control->ensureBuffer(1);
|
||||
buffer->putByte((int8)(epics::pvData::structure << 4));
|
||||
serializeStructureField(this, buffer, control);
|
||||
*/
|
||||
}
|
||||
|
||||
void Structure::deserialize(ByteBuffer *buffer, DeserializableControl *control) {
|
||||
}
|
||||
|
||||
ScalarConstPtr FieldCreate::createScalar(String fieldName,
|
||||
ScalarType scalarType) const
|
||||
ScalarConstPtr FieldCreate::createScalar(ScalarType scalarType) const
|
||||
{
|
||||
ScalarConstPtr scalar(new Scalar(fieldName,scalarType), Field::Deleter());
|
||||
ScalarConstPtr scalar(new Scalar(scalarType), Field::Deleter());
|
||||
return scalar;
|
||||
}
|
||||
|
||||
ScalarArrayConstPtr FieldCreate::createScalarArray(
|
||||
String fieldName,ScalarType elementType) const
|
||||
ScalarArrayConstPtr FieldCreate::createScalarArray(ScalarType elementType) const
|
||||
{
|
||||
ScalarArrayConstPtr scalarArray(new ScalarArray(fieldName,elementType), Field::Deleter());
|
||||
ScalarArrayConstPtr scalarArray(new ScalarArray(elementType), Field::Deleter());
|
||||
return scalarArray;
|
||||
}
|
||||
|
||||
StructureConstPtr FieldCreate::createStructure (
|
||||
String fieldName,int numberFields,
|
||||
FieldConstPtr fields[]) const
|
||||
StringArray const & fieldNames,FieldConstPtrArray const & fields) const
|
||||
{
|
||||
StructureConstPtr structure(new Structure(
|
||||
fieldName,numberFields,fields), Field::Deleter());
|
||||
StructureConstPtr structure(
|
||||
new Structure(fieldNames,fields), Field::Deleter());
|
||||
return structure;
|
||||
}
|
||||
|
||||
StructureArrayConstPtr FieldCreate::createStructureArray(
|
||||
String fieldName,StructureConstPtr structure) const
|
||||
StructureConstPtr structure) const
|
||||
{
|
||||
StructureArrayConstPtr structureArray(new StructureArray(fieldName,structure), Field::Deleter());
|
||||
StructureArrayConstPtr structureArray(
|
||||
new StructureArray(structure), Field::Deleter());
|
||||
return structureArray;
|
||||
}
|
||||
|
||||
FieldConstPtr FieldCreate::create(String fieldName,
|
||||
FieldConstPtr pfield) const
|
||||
StructureConstPtr FieldCreate::appendField(
|
||||
StructureConstPtr structure,String fieldName, FieldConstPtr field) const
|
||||
{
|
||||
FieldConstPtr ret;
|
||||
Type type = pfield->getType();
|
||||
switch(type) {
|
||||
case scalar: {
|
||||
ScalarConstPtr pscalar = static_pointer_cast<const Scalar>(pfield);
|
||||
return createScalar(fieldName,pscalar->getScalarType());
|
||||
StringArray oldNames = structure->getFieldNames();
|
||||
FieldConstPtrArray oldFields = structure->getFields();
|
||||
size_t oldLen = oldNames.size();
|
||||
StringArray newNames(oldLen+1);
|
||||
FieldConstPtrArray newFields(oldLen+1);
|
||||
for(size_t i = 0; i<oldLen; i++) {
|
||||
newNames[i] = oldNames[i];
|
||||
newFields[i] = oldFields[i];
|
||||
}
|
||||
case scalarArray: {
|
||||
ScalarArrayConstPtr pscalarArray = static_pointer_cast<const ScalarArray>(pfield);
|
||||
return createScalarArray(fieldName,pscalarArray->getElementType());
|
||||
newNames[oldLen] = fieldName;
|
||||
newFields[oldLen] = field;
|
||||
return createStructure(newNames,newFields);
|
||||
}
|
||||
|
||||
StructureConstPtr FieldCreate::appendFields(
|
||||
StructureConstPtr structure,
|
||||
StringArray const & fieldNames,
|
||||
FieldConstPtrArray const & fields) const
|
||||
{
|
||||
StringArray oldNames = structure->getFieldNames();
|
||||
FieldConstPtrArray oldFields = structure->getFields();
|
||||
size_t oldLen = oldNames.size();
|
||||
size_t extra = fieldNames.size();
|
||||
StringArray newNames(oldLen+extra);
|
||||
FieldConstPtrArray newFields(oldLen+extra);
|
||||
for(size_t i = 0; i<oldLen; i++) {
|
||||
newNames[i] = oldNames[i];
|
||||
newFields[i] = oldFields[i];
|
||||
}
|
||||
case structure: {
|
||||
StructureConstPtr pstructure = static_pointer_cast<const Structure>(pfield);
|
||||
return createStructure(fieldName,pstructure->getNumberFields(),pstructure->getFields());
|
||||
for(size_t i = 0; i<extra; i++) {
|
||||
newNames[oldLen +i] = fieldNames[i];
|
||||
newFields[oldLen +i] = fields[i];
|
||||
}
|
||||
case structureArray: {
|
||||
StructureArrayConstPtr pstructureArray = static_pointer_cast<const StructureArray>(pfield);
|
||||
return createStructureArray(fieldName,pstructureArray->getStructure());
|
||||
}
|
||||
}
|
||||
String message("field ");
|
||||
message += fieldName;
|
||||
THROW_EXCEPTION2(std::logic_error, message);
|
||||
return createStructure(newNames,newFields);
|
||||
}
|
||||
|
||||
FieldConstPtr FieldCreate::deserialize(ByteBuffer* buffer, DeserializableControl* control) const
|
||||
{
|
||||
throw std::invalid_argument("for Matej to convert");
|
||||
/*
|
||||
|
||||
control->ensureData(1);
|
||||
const int8 typeCode = buffer->getByte();
|
||||
|
||||
@@ -356,20 +353,23 @@ FieldConstPtr FieldCreate::deserialize(ByteBuffer* buffer, DeserializableControl
|
||||
return FieldConstPtr();
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
static FieldCreate* fieldCreate = 0;
|
||||
|
||||
FieldCreate::FieldCreate()
|
||||
FieldCreatePtr FieldCreate::getFieldCreate()
|
||||
{
|
||||
}
|
||||
|
||||
FieldCreate * getFieldCreate() {
|
||||
static FieldCreatePtr fieldCreate;
|
||||
static Mutex mutex;
|
||||
Lock xx(mutex);
|
||||
|
||||
if(fieldCreate==0) fieldCreate = new FieldCreate();
|
||||
if(fieldCreate.get()==0) fieldCreate = FieldCreatePtr(new FieldCreate());
|
||||
return fieldCreate;
|
||||
}
|
||||
|
||||
FieldCreate::FieldCreate(){}
|
||||
|
||||
FieldCreatePtr getFieldCreate() {
|
||||
return FieldCreate::getFieldCreate();
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user