pvaClientCPP 4.8.1
pvaClientPutData.cpp
Go to the documentation of this file.
1/* pvaClientPutData.cpp */
12#include <typeinfo>
13#include <sstream>
14
15#include <pv/createRequest.h>
16#include <pv/convert.h>
17
18#define epicsExportSharedSymbols
19
20#include <pv/pvaClient.h>
21
22using std::tr1::static_pointer_cast;
23using namespace epics::pvData;
24using namespace epics::pvAccess;
25using namespace std;
26
27namespace epics { namespace pvaClient {
28static ConvertPtr convert = getConvert();
29static string notCompatibleScalar("value is not a compatible scalar");
30static string notDoubleArray("value is not a doubleArray");
31static string notStringArray("value is not a stringArray");
32
33class PvaClientPostHandlerPvt: public PostHandler
34{
35 PvaClientPutData * putData;
36 size_t fieldNumber;
37public:
38 PvaClientPostHandlerPvt(PvaClientPutData *putData,size_t fieldNumber)
39 : putData(putData),fieldNumber(fieldNumber){}
40 void postPut() { putData->postPut(fieldNumber);}
41};
42
43
44PvaClientPutDataPtr PvaClientPutData::create(StructureConstPtr const & structure)
45{
46 if(PvaClient::getDebug()) cout << "PvaClientPutData::create\n";
47 PvaClientPutDataPtr epv(new PvaClientPutData(structure));
48 return epv;
49}
50
51PvaClientPutData::PvaClientPutData(StructureConstPtr const & structure)
52: PvaClientData(structure)
53{
54 if(PvaClient::getDebug()) cout << "PvaClientPutData::PvaClientPutData\n";
55 PVStructurePtr pvStructure(getPVDataCreate()->createPVStructure(structure));
56 BitSetPtr bitSet(BitSetPtr(new BitSet(pvStructure->getNumberFields())));
57 setData(pvStructure,bitSet);
58 size_t nfields = pvStructure->getNumberFields();
59 postHandler.resize(nfields);
60 PVFieldPtr pvField;
61 for(size_t i =0; i<nfields; ++i)
62 {
63 postHandler[i] = PostHandlerPtr(new PvaClientPostHandlerPvt(this, i));
64 if(i==0) {
65 pvField = pvStructure;
66 } else {
67 pvField = pvStructure->getSubField(i);
68 }
69 pvField->setPostHandler(postHandler[i]);
70 }
71}
72
73
75{
76 if(PvaClient::getDebug()) cout << "PvaClientPutData::putDouble\n";
77 PVFieldPtr pvField = getSinglePVField();
78 Type type = pvField->getField()->getType();
79 if(type!=scalar) {
80 throw std::logic_error("PvaClientData::putDouble() did not find a scalar field");
81 }
82 PVScalarPtr pvScalar = static_pointer_cast<PVScalar>(pvField);
83 ScalarType scalarType = pvScalar->getScalar()->getScalarType();
84 if(scalarType==pvDouble) {
85 PVDoublePtr pvDouble = static_pointer_cast<PVDouble>(pvScalar);
86 pvDouble->put(value);
87 return;
88 }
89 if(!ScalarTypeFunc::isNumeric(scalarType)) {
90 throw std::logic_error(
91 "PvaClientData::putDouble() did not find a numeric scalar field");
92 }
93 convert->fromDouble(pvScalar,value);
94}
95
96void PvaClientPutData::putString(std::string const & value)
97{
98 if(PvaClient::getDebug()) cout << "PvaClientPutData::putString\n";
99 PVFieldPtr pvField = getSinglePVField();
100 Type type = pvField->getField()->getType();
101 if(type!=scalar) {
102 throw std::logic_error("PvaClientData::putString() did not find a scalar field");
103 }
104 PVScalarPtr pvScalar = static_pointer_cast<PVScalar>(pvField);
105 convert->fromString(pvScalar,value);
106}
107
108void PvaClientPutData::putDoubleArray(shared_vector<const double> const & value)
109{
110 if(PvaClient::getDebug()) cout << "PvaClientPutData::putDoubleArray\n";
111 PVFieldPtr pvField = getSinglePVField();
112 Type type = pvField->getField()->getType();
113 if(type!=scalarArray) {
114 throw std::logic_error("PvaClientData::putDoubleArray() did not find a scalarArray field");
115 }
116 PVScalarArrayPtr pvScalarArray = static_pointer_cast<PVScalarArray>(pvField);
117 ScalarType scalarType = pvScalarArray->getScalarArray()->getElementType();
118 if(!ScalarTypeFunc::isNumeric(scalarType)) {
119 throw std::logic_error(
120 "PvaClientData::putDoubleArray() did not find a numeric scalarArray field");
121 }
122 pvScalarArray->putFrom<const double>(value);
123}
124
125void PvaClientPutData::putStringArray(shared_vector<const std::string> const & value)
126{
127 if(PvaClient::getDebug()) cout << "PvaClientPutData::putStringArray\n";
128 PVFieldPtr pvField = getSinglePVField();
129 Type type = pvField->getField()->getType();
130 if(type!=scalarArray) {
131 throw std::logic_error("PvaClientData::putStringArray() did not find a scalarArray field");
132 }
133 PVScalarArrayPtr pvScalarArray = static_pointer_cast<PVScalarArray>(pvField);
134 pvScalarArray->putFrom<const string>(value);
135 return;
136}
137
138void PvaClientPutData::putStringArray(std::vector<string> const & value)
139{
140 size_t length = value.size();
141 shared_vector<string> val(length);
142 for(size_t i=0; i < length; ++i) val[i] = value[i];
143 putStringArray(freeze(val));
144 return;
145}
146
147void PvaClientPutData::postPut(size_t fieldNumber)
148{
149 if(PvaClient::getDebug()) cout << "PvaClientPutData::postPut\n";
150 getChangedBitSet()->set(fieldNumber);
151}
152
153
154}}
A base class for PvaClientGetData, PvaClientPutData, and PvaClientMonitorData.
Definition pvaClient.h:542
epics::pvData::BitSetPtr getChangedBitSet()
Get the changed BitSet for the pvStructure.
void setData(epics::pvData::PVStructurePtr const &pvStructureFrom, epics::pvData::BitSetPtr const &bitSetFrom)
New data is present.
epics::pvData::PVFieldPtr getSinglePVField()
A class that holds data given to by PvaClientPut or PvaClientPutGet.
Definition pvaClient.h:737
void putDoubleArray(epics::pvData::shared_vector< const double > const &value)
Copy the array to the value field.
static PvaClientPutDataPtr create(epics::pvData::StructureConstPtr const &structure)
void putStringArray(epics::pvData::shared_vector< const std::string > const &value)
Copy array to the value field.
void putDouble(double value)
Put the value as a double.
void putString(std::string const &value)
Put the value as a string.
static bool getDebug()
Is debug set?
Definition pvaClient.cpp:97
std::tr1::shared_ptr< PvaClientPutData > PvaClientPutDataPtr
Definition pvaClient.h:53