PVData C++  8.0.2
convert.h
1 /* convert.h */
2 /*
3  * Copyright information and license terms for this software can be
4  * found in the file LICENSE that is included with the distribution
5  */
6 /**
7  * @author mrk
8  */
9 #ifndef CONVERT_H
10 #define CONVERT_H
11 
12 #include <string>
13 #include <stdexcept>
14 #include <vector>
15 
16 #include <pv/pvIntrospect.h>
17 #include <pv/pvData.h>
18 
19 #include <shareLib.h>
20 
21 namespace epics { namespace pvData {
22 
23 class Convert;
24 typedef std::tr1::shared_ptr<Convert> ConvertPtr;
25 
26 /**
27  * @brief Conversion and Copy facility for pvData.
28  *
29  * Convert between numeric types, convert any field to a string,
30  * or convert from a string to a scalar field.
31  * <p>Numeric conversions are between scalar numeric types or between arrays of
32  * numeric types. It is not possible to convert between a scalar
33  * and an array.
34  * Numeric conversions are between types:
35  * pvByte, pvShort, pvInt, pvLong,
36  * pvUByte, pvUShort, pvUInt, pvULong,
37  * pvFloat, or pvDouble.</p>
38  *
39  * <p>getString converts any supported type to a std::string.</p>
40  *
41  * <p>fromString converts a std::string to a scalar.
42  * fromStringArray converts an array of std::strings
43  * to a pvArray, which must have a scaler element type.
44  * A scalar field is a numeric field or pvBoolean or pvString.</p>
45  * <p>All from methods put data into a PVField, e.g. from means where the PVField gets it's data.</p>
46  */
47 class epicsShareClass Convert {
48 public:
49  static ConvertPtr getConvert();
50 
51  /**
52  * Copy from a PVField to another PVField.
53  * This calls one on copyScalar, copyArray, copyStructure.
54  * The two arguments must be compatible.
55  * @param from The source.
56  * @param to The destination
57  * @throws std::invalid_argument if the arguments are not compatible.
58  * @deprecated use "to->copy[Unchecked](*from)" instead
59  */
60  void copy(PVFieldPtr const & from, PVFieldPtr const & to) {
61  to->copy(*from);
62  }
63 
64  /**
65  * Convert a PVField to a string.
66  * If a PVField is a structure or array be prepared for a very long string.
67  * @param buf string that will hold pvField converted to a string,
68  * @param pvField The PVField to convert to a string.
69  */
70  inline void getString(std::string * buf,PVFieldPtr const & pvField)
71  {getString(buf, pvField.get(), 0);}
72  /**
73  * Convert a PVField to a string.
74  * If a PVField is a structure or array be prepared for a very long string.
75  * @param buf string that will hold pvField converted to a string,
76  * @param pvField The PVField to convert to a string.
77  * @param indentLevel indentation level
78  */
79  void getString(std::string * buf,PVField const * pvField,int indentLevel);
80  /**
81  * Convert from an array of std::string to a PVStructure
82  * @param pv The PV.
83  * @param from The array of std::string value to convert and put into a PV.
84  * @param fromStartIndex The first element if the array of strings.
85  * @return The total number of fields that have been changed.
86  * @throws std::logic_error if the array of std::string does not have a valid values.
87  */
89  PVStructurePtr const &pv,
90  StringArray const & from,
92  /**
93  * Convert from a std::string to a PVScalar
94  * @param pv The PV.
95  * @param from The std::string value to convert and put into a PV.
96  * @throws std::logic_error if the std::string does not have a valid value.
97  */
98  void fromString(PVScalarPtr const & pv, std::string const & from)
99  {
100  pv->putFrom<std::string>(from);
101  }
102 
103  /**
104  * Convert from a std::string to a PVScalarArray.
105  * The std::string must be a comma separated set of values optionally enclosed in []
106  * @param pv The PV.
107  * @param from The std::string value to convert and put into a PV.
108  * @return The number of elements converted.
109  * @throws std::invalid_argument if the element Type is not a scalar.
110  * @throws std::logic_error if the std::string does not have a valid array values.
111  */
113  /**
114  * Convert a PVScalarArray from a std::string array.
115  * The array element type must be a scalar.
116  * @param pv The PV.
117  * @param offset Starting element in a PV.
118  * @param length The number of elements to transfer.
119  * @param from The array of values to put into the PV.
120  * @param fromOffset Starting element in the source array.
121  * @return The number of elements converted.
122  * @throws std::invalid_argument if the element Type is not a scalar.
123  * @throws std::logic_error if the std::string does not have a valid value.
124  */
126  PVScalarArrayPtr const & pv,
128  StringArray const & from,
130  /**
131  * Convert a PVScalarArray to a std::string array.
132  * @param pv The PV.
133  * @param offset Starting element in the PV array.
134  * @param length Number of elements to convert to the string array.
135  * @param to std::string array to receive the converted PV data.
136  * @param toOffset Starting element in the string array.
137  * @return Number of elements converted.
138  */
140  std::size_t offset,
141  std::size_t length,
142  StringArray & to,
143  std::size_t toOffset);
144  /**
145  * Convert a PV to a byte.
146  * @param pv a PV
147  * @return converted value
148  */
149  inline int8 toByte(PVScalarPtr const & pv) { return pv->getAs<int8>();}
150  /**
151  * Convert a PV to a short.
152  * @param pv a PV
153  * @return converted value
154  * @throws std::invalid_argument if the Type is not a numeric scalar
155  */
156  inline int16 toShort(PVScalarPtr const & pv) { return pv->getAs<int16>();}
157  /**
158  * Convert a PV to a int.
159  * @param pv a PV
160  * @return converted value
161  * @throws std::invalid_argument if the Type is not a numeric scalar
162  */
163  inline int32 toInt(PVScalarPtr const & pv) { return pv->getAs<int32>();}
164  /**
165  * Convert a PV to an long
166  * @param pv a PV
167  * @return converted value
168  * @throws std::invalid_argument if the Type is not a numeric scalar
169  */
170  inline int64 toLong(PVScalarPtr const & pv) { return pv->getAs<int32>();}
171  /**
172  * Convert a PV to a ubyte.
173  * @param pv a PV
174  * @return converted value
175  */
176  inline uint8 toUByte(PVScalarPtr const & pv) { return pv->getAs<uint8>();}
177  /**
178  * Convert a PV to a ushort.
179  * @param pv a PV
180  * @return converted value
181  * @throws std::invalid_argument if the Type is not a numeric scalar
182  */
183  inline uint16 toUShort(PVScalarPtr const & pv) { return pv->getAs<uint16>();}
184  /**
185  * Convert a PV to a uint.
186  * @param pv a PV
187  * @return converted value
188  * @throws std::invalid_argument if the Type is not a numeric scalar
189  */
190  inline uint32 toUInt(PVScalarPtr const & pv) { return pv->getAs<uint32>();}
191  /**
192  * Convert a PV to an ulong
193  * @param pv a PV
194  * @return converted value
195  * @throws std::invalid_argument if the Type is not a numeric scalar
196  */
197  inline uint64 toULong(PVScalarPtr const & pv) { return pv->getAs<uint64>();}
198  /**
199  * Convert a PV to a float
200  * @param pv a PV
201  * @return converted value
202  * @throws std::invalid_argument if the Type is not a numeric scalar
203  */
204  inline float toFloat(PVScalarPtr const & pv) { return pv->getAs<float>();}
205  /**
206  * Convert a PV to a double
207  * @param pv a PV
208  * @return converted value
209  * @throws std::invalid_argument if the Type is not a numeric scalar
210  */
211  inline double toDouble(PVScalarPtr const & pv) { return pv->getAs<double>();}
212  /**
213  * Convert a PV to a std::string
214  * @param pv a PV
215  * @return converted value
216  */
217  inline std::string toString(PVScalarPtr const & pv) { return pv->getAs<std::string>();}
218  /**
219  * Convert a PV from a byte
220  * @param pv a PV
221  * @param from value to put into PV
222  * @throws std::invalid_argument if the Type is not a numeric scalar
223  */
224  inline void fromByte(PVScalarPtr const & pv,int8 from) { pv->putFrom<int8>(from); }
225  /**
226  * Convert a PV from a short
227  * @param pv a PV
228  * @param from value to put into PV
229  * @throws std::invalid_argument if the Type is not a numeric scalar
230  */
231  inline void fromShort(PVScalarPtr const & pv,int16 from) { pv->putFrom<int16>(from); }
232  /**
233  * Convert a PV from an int
234  * @param pv a PV
235  * @param from value to put into PV
236  * @throws std::invalid_argument if the Type is not a numeric scalar
237  */
238  inline void fromInt(PVScalarPtr const & pv, int32 from) { pv->putFrom<int32>(from); }
239  /**
240  * Convert a PV from a long
241  * @param pv a PV
242  * @param from value to put into PV
243  * @throws std::invalid_argument if the Type is not a numeric scalar
244  */
245  inline void fromLong(PVScalarPtr const & pv, int64 from) { pv->putFrom<int64>(from); }
246  /**
247  * Convert a PV from a ubyte
248  * @param pv a PV
249  * @param from value to put into PV
250  * @throws std::invalid_argument if the Type is not a numeric scalar
251  */
252  inline void fromUByte(PVScalarPtr const & pv,uint8 from) { pv->putFrom<uint8>(from); }
253  /**
254  * Convert a PV from a ushort
255  * @param pv a PV
256  * @param from value to put into PV
257  * @throws std::invalid_argument if the Type is not a numeric scalar
258  */
259  inline void fromUShort(PVScalarPtr const & pv,uint16 from) { pv->putFrom<uint16>(from); }
260  /**
261  * Convert a PV from an uint
262  * @param pv a PV
263  * @param from value to put into PV
264  * @throws std::invalid_argument if the Type is not a numeric scalar
265  */
266  inline void fromUInt(PVScalarPtr const & pv, uint32 from) { pv->putFrom<uint32>(from); }
267  /**
268  * Convert a PV from a ulong
269  * @param pv a PV
270  * @param from value to put into PV
271  * @throws std::invalid_argument if the Type is not a numeric scalar
272  */
273  inline void fromULong(PVScalarPtr const & pv, uint64 from) { pv->putFrom<uint64>(from); }
274  /**
275  * Convert a PV from a float
276  * @param pv a PV
277  * @param from value to put into PV
278  * @throws std::invalid_argument if the Type is not a numeric scalar
279  */
280  inline void fromFloat(PVScalarPtr const & pv, float from) { pv->putFrom<float>(from); }
281  /**
282  * Convert a PV from a double
283  * @param pv a PV
284  * @param from value to put into PV
285  * @throws std::invalid_argument if the Type is not a numeric scalar
286  */
287  inline void fromDouble(PVScalarPtr const & pv, double from) { pv->putFrom<double>(from); }
288 
289 };
290 
291 static inline ConvertPtr getConvert() { return Convert::getConvert(); }
292 
293 }}
294 #endif /* CONVERT_H */
Conversion and Copy facility for pvData.
Definition: convert.h:47
epicsShareFunc bool yajl_parse_helper(std::istream &src, yajl_handle handle)