/*FieldCreateFactory.cpp*/ #include #include #include #include #include #include "pvIntrospect.h" #include "factory.h" namespace epics { namespace pvData { static DebugLevel debugLevel = lowDebug; static void newLine(StringBuilder buffer, int indentLevel) { *buffer += "\n"; for(int i=0; igetFieldName(); // look for duplicates for(int j=i+1; jgetFieldName(); int result = name.compare(otherName); if(result==0) { String message("duplicate fieldName "); message += name; throw std::invalid_argument(message); } } // inc reference counter fields[i]->incReferenceCount(); } } BaseStructure::~BaseStructure() { if(debugLevel==highDebug) printf("~BaseStructure %s\n",BaseField::getFieldName().c_str()); for(int i=0; idecReferenceCount(); } delete[] fields; } FieldConstPtr BaseStructure::getField(String fieldName) const { for(int i=0; igetFieldName()); if(result==0) return pfield; } return 0; } int BaseStructure::getFieldIndex(String fieldName) const { for(int i=0; igetFieldName()); if(result==0) return i; } return -1; } void BaseStructure::toString(StringBuilder buffer,int indentLevel) const{ *buffer += "structure"; BaseField::toString(buffer,indentLevel); newLine(buffer,indentLevel+1); for(int i=0; itoString(buffer,indentLevel+1); if(iincReferenceCount(); } BaseStructureArray::~BaseStructureArray() { if(debugLevel==highDebug) printf("~BaseStructureArray\n"); pstructure->decReferenceCount(); } void BaseStructureArray::toString(StringBuilder buffer,int indentLevel) const { *buffer += " structureArray "; BaseField::toString(buffer,indentLevel); newLine(buffer,indentLevel + 1); pstructure->toString(buffer,indentLevel + 1); } FieldCreate::FieldCreate(){}; ScalarConstPtr FieldCreate::createScalar(String fieldName, ScalarType scalarType) const { BaseScalar *baseScalar = new BaseScalar(fieldName,scalarType); return baseScalar; } ScalarArrayConstPtr FieldCreate::createScalarArray( String fieldName,ScalarType elementType) const { BaseScalarArray *baseScalarArray = new BaseScalarArray(fieldName,elementType); return baseScalarArray; } StructureConstPtr FieldCreate::createStructure ( String fieldName,int numberFields, FieldConstPtr fields[]) const { BaseStructure *baseStructure = new BaseStructure( fieldName,numberFields,fields); return baseStructure; } StructureArrayConstPtr FieldCreate::createStructureArray( String fieldName,StructureConstPtr structure) const { BaseStructureArray *baseStructureArray = new BaseStructureArray(fieldName,structure); return baseStructureArray; } FieldConstPtr FieldCreate::create(String fieldName, FieldConstPtr pfield) const { Type type = pfield->getType(); switch(type) { case scalar: { ScalarConstPtr pscalar = dynamic_cast(pfield); return createScalar(fieldName,pscalar->getScalarType()); } case scalarArray: { ScalarArrayConstPtr pscalarArray = dynamic_cast(pfield); return createScalarArray(fieldName,pscalarArray->getElementType()); } case structure: { StructureConstPtr pstructure = dynamic_cast(pfield); return createStructure(fieldName,pstructure->getNumberFields(),pstructure->getFields()); } case structureArray: { StructureArrayConstPtr pstructureArray = dynamic_cast(pfield); return createStructureArray(fieldName,pstructureArray->getStructure()); } } String message("field "); message += fieldName; throw std::logic_error(message); } static FieldCreate* instance = 0; class FieldCreateExt : public FieldCreate { public: FieldCreateExt(): FieldCreate(){}; }; FieldCreate * getFieldCreate() { static Mutex mutex = Mutex(); Lock xx(&mutex); if(instance==0) instance = new FieldCreateExt(); return instance; } }}