Field::getID() added

This commit is contained in:
Matej Sekoranja
2012-07-31 10:30:50 +02:00
parent 689d0875b7
commit ac10b73e69
4 changed files with 136 additions and 16 deletions
+2
View File
@@ -70,6 +70,8 @@ bool operator==(const Structure& a, const Structure& b)
{
if(&a==&b)
return true;
if (a.getID()!=b.getID())
return false;
size_t nflds=a.getNumberFields();
if (b.getNumberFields()!=nflds)
return false;
+67 -5
View File
@@ -71,6 +71,25 @@ void Scalar::toString(StringBuilder buffer,int indentLevel) const{
ScalarTypeFunc::toString(buffer,scalarType);
}
static const String idScalarLUT[] = {
"boolean", // pvBoolean
"byte", // pvByte
"short", // pvShort
"int", // pvInt
"long", // pvLong
"ubyte", // pvUByte
"ushort", // pvUShort
"uint", // pvUInt
"ulong", // pvULong
"float", // pvFloat
"double", // pvDouble
"string" // pvString
};
String Scalar::getID() const
{
return idScalarLUT[scalarType];
}
static const int8 typeCodeLUT[] = {
0x00, // pvBoolean
@@ -99,6 +118,7 @@ void Scalar::deserialize(ByteBuffer *buffer, DeserializableControl *control) {
static void serializeStructureField(const Structure* structure, ByteBuffer* buffer, SerializableControl* control)
{
SerializeHelper::serializeString(structure->getID(), buffer, control);
FieldConstPtrArray const & fields = structure->getFields();
StringArray const & fieldNames = structure->getFieldNames();
std::size_t len = fields.size();
@@ -112,6 +132,7 @@ static void serializeStructureField(const Structure* structure, ByteBuffer* buff
static StructureConstPtr deserializeStructureField(const FieldCreate* fieldCreate, ByteBuffer* buffer, DeserializableControl* control)
{
String id = SerializeHelper::deserializeString(buffer, control);
const std::size_t size = SerializeHelper::readSize(buffer, control);
FieldConstPtrArray fields; fields.reserve(size);
StringArray fieldNames; fieldNames.reserve(size);
@@ -121,7 +142,7 @@ static StructureConstPtr deserializeStructureField(const FieldCreate* fieldCreat
fields.push_back(control->cachedDeserialize(buffer));
}
return fieldCreate->createStructure(fieldNames, fields);
return fieldCreate->createStructure(id, fieldNames, fields);
}
ScalarArray::ScalarArray(ScalarType elementType)
@@ -129,6 +150,26 @@ ScalarArray::ScalarArray(ScalarType elementType)
ScalarArray::~ScalarArray() {}
static const String idScalarArrayLUT[] = {
"boolean[]", // pvBoolean
"byte[]", // pvByte
"short[]", // pvShort
"int[]", // pvInt
"long[]", // pvLong
"ubyte[]", // pvUByte
"ushort[]", // pvUShort
"uint[]", // pvUInt
"ulong[]", // pvULong
"float[]", // pvFloat
"double[]", // pvDouble
"string[]" // pvString
};
String ScalarArray::getID() const
{
return idScalarArrayLUT[elementType];
}
void ScalarArray::toString(StringBuilder buffer,int indentLevel) const{
ScalarTypeFunc::toString(buffer,elementType);
*buffer += "[]";
@@ -152,6 +193,12 @@ StructureArray::~StructureArray() {
if(debugLevel==highDebug) printf("~StructureArray\n");
}
String StructureArray::getID() const
{
// NOTE: structure->getID() can return an empty string
return pstructure->getID() + "[]";
}
void StructureArray::toString(StringBuilder buffer,int indentLevel) const {
if(indentLevel==0) {
*buffer += "structure[]";
@@ -172,11 +219,11 @@ void StructureArray::deserialize(ByteBuffer *buffer, DeserializableControl *cont
throw std::runtime_error("not valid operation, use FieldCreate::deserialize instead");
}
Structure::Structure (StringArray const & fieldNames,FieldConstPtrArray const & infields)
Structure::Structure (StringArray const & fieldNames,FieldConstPtrArray const & infields, String inid)
: Field(structure),
fieldNames(fieldNames),
fields(infields)
fields(infields),
id(inid)
{
if(fieldNames.size()!=fields.size()) {
throw std::invalid_argument("fieldNames.size()!=fields.size()");
@@ -203,6 +250,11 @@ Structure::Structure (StringArray const & fieldNames,FieldConstPtrArray const &
Structure::~Structure() { }
String Structure::getID() const
{
return id;
}
FieldConstPtr Structure::getField(String fieldName) const {
size_t numberFields = fields.size();
for(size_t i=0; i<numberFields; i++) {
@@ -225,7 +277,8 @@ size_t Structure::getFieldIndex(String fieldName) const {
void Structure::toString(StringBuilder buffer,int indentLevel) const{
*buffer += "structure";
toStringCommon(buffer,indentLevel+1);
if (!id.empty()) { *buffer += ' '; *buffer += id; };
toStringCommon(buffer,indentLevel+1);
}
void Structure::toStringCommon(StringBuilder buffer,int indentLevel) const{
@@ -291,6 +344,15 @@ StructureConstPtr FieldCreate::createStructure (
return structure;
}
StructureConstPtr FieldCreate::createStructure (
String id,
StringArray const & fieldNames,FieldConstPtrArray const & fields) const
{
StructureConstPtr structure(
new Structure(fieldNames,fields,id), Field::Deleter());
return structure;
}
StructureArrayConstPtr FieldCreate::createStructureArray(
StructureConstPtr const & structure) const
{
+36 -11
View File
@@ -209,6 +209,11 @@ public:
* @return The type.
*/
Type getType() const{return m_fieldType;}
/**
* Get the identification string.
* @return The identification string, can be empty.
*/
virtual String getID() const = 0;
/**
* Convert the scalarType to a string and add it to builder.
* @param builder The string builder.
@@ -268,9 +273,11 @@ public:
* @param indentLevel The number of blanks at the beginning of new lines.
*/
virtual void toString(StringBuilder buf,int indentLevel) const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *flusher) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *flusher);
virtual String getID() const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *control) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *control);
protected:
Scalar(ScalarType scalarType);
@@ -310,8 +317,10 @@ public:
*/
virtual void toString(StringBuilder buf,int indentLevel) const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *flusher) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *flusher);
virtual String getID() const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *control) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *control);
protected:
/**
@@ -345,8 +354,10 @@ public:
*/
virtual void toString(StringBuilder buf,int indentLevel=0) const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *flusher) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *flusher);
virtual String getID() const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *control) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *control);
protected:
/**
@@ -431,15 +442,18 @@ public:
*/
virtual void toString(StringBuilder buf,int indentLevel) const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *flusher) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *flusher);
virtual String getID() const;
virtual void serialize(ByteBuffer *buffer, SerializableControl *control) const;
virtual void deserialize(ByteBuffer *buffer, DeserializableControl *control);
protected:
Structure(StringArray const & fieldNames, FieldConstPtrArray const & fields);
Structure(StringArray const & fieldNames, FieldConstPtrArray const & fields, String id = "");
private:
void toStringCommon(StringBuilder buf,int indentLevel) const;
StringArray fieldNames;
FieldConstPtrArray fields;
String id;
friend class FieldCreate;
};
@@ -474,13 +488,24 @@ public:
StructureArrayConstPtr createStructureArray(StructureConstPtr const & structure) const;
/**
* Create a {@code Structure} field.
* @param fieldNames The array of {@code fieldNames} for the structure
* @param fieldNames The array of {@code fieldNames} for the structure.
* @param fields The array of {@code fields} for the structure.
* @return a {@code Structure} interface for the newly created object.
*/
StructureConstPtr createStructure (
StringArray const & fieldNames,
FieldConstPtrArray const & fields) const;
/**
* Create a {@code Structure} field with identification string.
* @param id The identification string for the structure.
* @param fieldNames The array of {@code fieldNames} for the structure.
* @param fields The array of {@code fields} for the structure.
* @return a {@code Structure} interface for the newly created object.
*/
StructureConstPtr createStructure (
String id,
StringArray const & fieldNames,
FieldConstPtrArray const & fields) const;
/**
* Append a field to a structure.
* @param structure The structure to which the field is appended.
+31
View File
@@ -573,6 +573,37 @@ void testStructure(std::ostream& ofile) {
ofile<<"!!! PASSED\n\n";
}
void testStructureId(std::ostream& ofile) {
ofile<<"Testing structureID...\n";
FieldCreatePtr fieldCreate = getFieldCreate();
StringArray fieldNames;
fieldNames.push_back("longField");
fieldNames.push_back("intField");
FieldConstPtrArray fields;
fields.push_back(fieldCreate->createScalar(pvLong));
fields.push_back(fieldCreate->createScalar(pvInt));
StructureConstPtr structureWithNoId = fieldCreate->createStructure(fieldNames, fields);
StructureConstPtr structure1 = fieldCreate->createStructure("id1", fieldNames, fields);
StructureConstPtr structure2 = fieldCreate->createStructure("id2", fieldNames, fields);
assert(structureWithNoId!=structure1);
assert(structure1!=structure2);
//serializationTest(structure1);
PVStructurePtr pvStructure = getPVDataCreate()->createPVStructure(structure1);
serializationTest(pvStructure);
ofile<<"!!! PASSED\n\n";
}
void serializatioTest(FieldConstPtr const & field)
{
buffer->clear();