Ejemplo n.º 1
0
ChannelArrayLocalPtr ChannelArrayLocal::create(
    ChannelLocalPtr const &channelLocal,
    ChannelArrayRequester::shared_pointer const & channelArrayRequester,
    PVStructurePtr const & pvRequest,
    PVRecordPtr const &pvRecord)
{
    PVFieldPtrArray const & pvFields = pvRequest->getPVFields();
    if(pvFields.size()!=1) {
        Status status(
            Status::STATUSTYPE_ERROR,"invalid pvRequest");
        ChannelArrayLocalPtr channelArray;
        ArrayConstPtr array;
        channelArrayRequester->channelArrayConnect(status,channelArray,array);
        return channelArray;
    }
    PVFieldPtr pvField = pvFields[0];
    string fieldName("");
    while(true) {
        string name = pvField->getFieldName();
        if(fieldName.size()>0) fieldName += '.';
        fieldName += name;
        PVStructurePtr pvs = static_pointer_cast<PVStructure>(pvField);
        PVFieldPtrArray const & pvfs = pvs->getPVFields();
        if(pvfs.size()!=1) break;
        pvField = pvfs[0];
    }
    size_t indfield = fieldName.find_first_of("field.");
    if(indfield==0) {
         fieldName = fieldName.substr(6);
    }
    pvField = pvRecord->getPVRecordStructure()->getPVStructure()->getSubField(fieldName);
    if(!pvField) {
        Status status(
            Status::STATUSTYPE_ERROR,fieldName +" not found");
        ChannelArrayLocalPtr channelArray;
        ArrayConstPtr array;
        channelArrayRequester->channelArrayConnect(
            status,channelArray,array);
        return channelArray;
    }
    if(pvField->getField()->getType()!=scalarArray
    && pvField->getField()->getType()!=structureArray
    && pvField->getField()->getType()!=unionArray)
    {
        Status status(
            Status::STATUSTYPE_ERROR,fieldName +" not array");
        ChannelArrayLocalPtr channelArray;
        ArrayConstPtr array;
        channelArrayRequester->channelArrayConnect(
           status,channelArray,array);
        return channelArray;
    }
    PVArrayPtr pvArray = static_pointer_cast<PVArray>(pvField);
    PVArrayPtr pvCopy;
    if(pvField->getField()->getType()==scalarArray) {
        PVScalarArrayPtr xxx = static_pointer_cast<PVScalarArray>(pvField);
        pvCopy = getPVDataCreate()->createPVScalarArray(
            xxx->getScalarArray()->getElementType());
    } else if(pvField->getField()->getType()==structureArray) {
        PVStructureArrayPtr xxx = static_pointer_cast<PVStructureArray>(pvField);
        pvCopy = getPVDataCreate()->createPVStructureArray(
            xxx->getStructureArray()->getStructure());
    } else {
        PVUnionArrayPtr xxx = static_pointer_cast<PVUnionArray>(pvField);
        pvCopy = getPVDataCreate()->createPVUnionArray(
            xxx->getUnionArray()->getUnion());
    }
    ChannelArrayLocalPtr array(new ChannelArrayLocal(
        channelLocal,
        channelArrayRequester,
        pvArray,
        pvCopy,
        pvRecord));
    if(pvRecord->getTraceLevel()>0)
    {
        cout << "ChannelArrayLocal::create";
        cout << " recordName " << pvRecord->getRecordName() << endl;
    }
    channelArrayRequester->channelArrayConnect(
        Status::Ok, array, pvCopy->getArray());
    return array;
}