void NTNDArrayConverter::fromValue (NDArray *src) { typedef typename arrayType::value_type arrayValType; NDArrayInfo_t arrayInfo; size_t count, nBytes; string unionField(string(ScalarTypeFunc::name(arrayType::typeCode)) + string("Value")); src->getInfo(&arrayInfo); count = arrayInfo.nElements; nBytes = arrayInfo.totalBytes; m_array->getCompressedDataSize()->put(static_cast<int64>(nBytes)); m_array->getUncompressedDataSize()->put(static_cast<int64>(nBytes)); src->reserve(); shared_vector<arrayValType> temp((srcDataType*)src->pData, freeNDArray<srcDataType>(src), 0, count); PVUnionPtr dest = m_array->getValue(); dest->select<arrayType>(unionField)->replace(freeze(temp)); dest->postPut(); }
PVUnionPtr PVDataCreate::createPVUnion(PVUnionPtr const & unionToClone) { PVUnionPtr punion(new PVUnion(unionToClone->getUnion())); // set cloned value punion->set(unionToClone->getSelectedIndex(), createPVField(unionToClone->get())); return punion; }
static void setValue(PVUnionPtr const &pvUnion, double value) { UnionConstPtr u = pvUnion->getUnion(); FieldConstPtr field = u->getField(0); Type type = field->getType(); if(type==scalar) { ScalarConstPtr scalar = static_pointer_cast<const Scalar>(field); ScalarType scalarType = scalar->getScalarType(); if(scalarType==pvDouble) { PVDoublePtr pvValue = static_pointer_cast<PVDouble>( pvDataCreate->createPVScalar(pvDouble)); pvValue->put(value); pvUnion->set(0,pvValue); return; } if(scalarType==pvString) { PVStringPtr pvValue = static_pointer_cast<PVString>( pvDataCreate->createPVScalar(pvString)); stringstream ss; ss << "value" << value; pvValue->put(ss.str()); pvUnion->set(0,pvValue); return; } throw std::runtime_error("only pvDouble and pvString are supported"); } if(type==scalarArray) { ScalarArrayConstPtr scalarArray = static_pointer_cast<const ScalarArray>(field); ScalarType scalarType = scalarArray->getElementType(); if(scalarType==pvDouble) { size_t num = 5; PVDoubleArrayPtr pvValue = static_pointer_cast<PVDoubleArray>( pvDataCreate->createPVScalarArray(pvDouble)); shared_vector<double> data(num); for(size_t i=0; i<num; ++i) data[i] = value +i; pvValue->replace(freeze(data)); pvUnion->set(0,pvValue); return; } if(scalarType==pvString) { size_t num = 5; PVStringArrayPtr pvValue = static_pointer_cast<PVStringArray>( pvDataCreate->createPVScalarArray(pvString)); shared_vector<string> data(num); for(size_t i=0; i<num; ++i) { stringstream ss; ss << "value" << value << i; data[i] = ss.str(); } pvValue->replace(freeze(data)); pvUnion->set(0,pvValue); return; } throw std::runtime_error("only pvDouble and pvString are supported"); } throw std::runtime_error("only scalar and scalarArray fields are supported"); }
void NTNDArrayRecord::setValue(PVByteArray::const_svector const & bytes) { // Get the union value field PVUnionPtr value = ndarray->getValue(); // Select the byteValue field stored in "value" PVByteArrayPtr byteValue = value->select<PVByteArray>("byteValue"); // replace the shared vector with "bytes" byteValue->replace(bytes); // call postPut so that the union sees the change in the stored field value->postPut(); }
size_t fromString(PVUnionPtr const & pvUnion, StringArray const & from, size_t fromStartIndex = 0) { if (pvUnion->getUnion()->isVariant()) throw std::runtime_error("cannot handle variant unions"); size_t fromValueCount = from.size(); if (fromStartIndex >= fromValueCount) throw std::runtime_error("not enough of values"); string selector = from[fromStartIndex++]; PVFieldPtr pv = pvUnion->select(selector); if (!pv) throw std::runtime_error("invalid union selector value '" + selector + "'"); size_t processed = fromString(pv, from, fromStartIndex); return processed + 1; }
static void testCopy() { TEST_COPY(PVBoolean, 1); TEST_COPY(PVByte, 12); TEST_COPY(PVShort, 128); TEST_COPY(PVInt, 128000); TEST_COPY(PVLong, 128000000); TEST_COPY(PVUByte, 12); TEST_COPY(PVUShort, 128); TEST_COPY(PVUInt, 128000); TEST_COPY(PVULong, 128000000); TEST_COPY(PVFloat, 12.8); TEST_COPY(PVDouble, 8.12); TEST_COPY(PVString, "jikes"); int32 testValue = 128; // PVStructure test PVStructurePtr pvStructure = standardPVField->scalar(pvInt, alarmTimeStampValueAlarm); pvStructure->getSubField<PVInt>("value")->put(testValue); PVTimeStamp timeStamp; timeStamp.attach(pvStructure->getSubField<PVStructure>("timeStamp")); TimeStamp current; current.getCurrent(); timeStamp.set(current); PVStructurePtr pvStructureCopy = standardPVField->scalar(pvInt, alarmTimeStampValueAlarm); pvStructureCopy->copy(*pvStructure); testOk(*pvStructure == *pvStructureCopy, "PVStructure copy"); PVStructurePtr pvStructureCopy2 = standardPVField->scalar(pvInt, alarmTimeStampValueAlarm); pvStructureCopy2->copyUnchecked(*pvStructure); testOk(*pvStructure == *pvStructureCopy2, "PVStructure copyUnchecked"); BitSet mask(pvStructure->getNumberFields()); PVStructurePtr pvStructureCopy3 = standardPVField->scalar(pvInt, alarmTimeStampValueAlarm); PVStructurePtr pvStructureCopy4 = standardPVField->scalar(pvInt, alarmTimeStampValueAlarm); pvStructureCopy3->copyUnchecked(*pvStructure, mask); testOk(*pvStructureCopy3 == *pvStructureCopy4, "PVStructure copyUnchecked w/ cleared mask"); mask.set(pvStructure->getSubField<PVInt>("value")->getFieldOffset()); pvStructureCopy3->copyUnchecked(*pvStructure, mask); pvStructureCopy4->getSubField<PVInt>("value")->put(testValue); testOk(*pvStructureCopy3 == *pvStructureCopy4, "PVStructure copyUnchecked w/ value mask only"); mask.set(pvStructure->getSubField<PVStructure>("timeStamp")->getFieldOffset()); PVStructurePtr pvStructureCopy5 = standardPVField->scalar(pvInt, alarmTimeStampValueAlarm); pvStructureCopy5->copyUnchecked(*pvStructure, mask); testOk(*pvStructure == *pvStructureCopy5, "PVStructure copyUnchecked w/ value+timeStamp mask"); UnionConstPtr _union = fieldCreate->createFieldBuilder()-> add("doubleValue", pvDouble)-> add("intValue", pvInt)-> add("timeStamp",standardField->timeStamp())-> createUnion(); PVUnionPtr pvUnion = pvDataCreate->createPVUnion(_union); PVUnionPtr pvUnion2 = pvDataCreate->createPVUnion(_union); pvUnion2->copy(*pvUnion); testOk(*pvUnion == *pvUnion2, "null PVUnion copy"); pvUnion->select<PVInt>("intValue")->put(123); pvUnion2->copy(*pvUnion); testOk(*pvUnion == *pvUnion2, "PVUnion scalar copy, to null PVUnion"); pvUnion->select("doubleValue"); pvUnion2->copy(*pvUnion); testOk(*pvUnion == *pvUnion2, "PVUnion scalar copy, to different type PVUnion"); pvUnion->select<PVStructure>("timeStamp")->copy( *pvStructure->getSubField<PVStructure>("timeStamp") ); pvUnion2->copy(*pvUnion); testOk(*pvUnion == *pvUnion2, "PVUnion PVStructure copy, to different type PVUnion"); }