32 using namespace OpcUa;
36 bool IsValueArray(
const std::vector<T> & t)
42 void IsValueArray(
const std::vector<T> & t,
bool & isArray)
44 isArray = IsValueArray(t);
48 bool IsNulValue(
const std::vector<T> & t)
54 void IsNulValue(
const std::vector<T> & t,
bool & isNul)
56 isNul = IsNulValue(t);
60 void RawValueSizeArray(
const std::vector<T> & t, std::size_t & size)
67 void RawValueSize(
const std::vector<T> & t, std::size_t & size)
74 else if (!IsNulValue(t))
94 if (IsValueArray(value))
99 else if (!IsNulValue(value))
105 template <
typename T>
111 template <
typename T>
116 value.push_back(tmp);
119 struct RawSizeVisitor
123 template <
typename T>
124 void OnContainer(
const T & val)
129 template <
typename T>
130 void OnScalar(
const T & val)
136 struct VariantSerializer
141 : Serializer(serializer)
145 template <
typename T>
146 void OnContainer(
const T & val)
151 template <
typename T>
152 void OnScalar(
const T & val)
158 struct VariantDeserializer
163 : Deserializer(deserializer)
168 template <
typename T>
169 typename std::enable_if<is_container_not_string<T>::value ==
true, T>::type
get()
176 template <
typename T>
177 typename std::enable_if<is_container_not_string<T>::value ==
false, T>::type
get()
180 *Deserializer >> tmp;
188 return lhs.
As<T>() == rhs.
As<T>();
215 using namespace boost;
216 const std::type_info & t =
Value.type();
218 if (t ==
typeid(
bool))
219 {
return Compare<bool>(*
this, var); }
221 else if (t ==
typeid(std::vector<bool>))
222 {
return Compare<std::vector<bool>>(*
this, var); }
224 else if (t ==
typeid(int8_t))
225 {
return Compare<int8_t>(*
this, var); }
227 else if (t ==
typeid(std::vector<int8_t>))
228 {
return Compare<std::vector<int8_t>>(*
this, var); }
230 else if (t ==
typeid(uint8_t))
231 {
return Compare<uint8_t>(*
this, var); }
233 else if (t ==
typeid(std::vector<uint8_t>))
234 {
return Compare<std::vector<uint8_t>>(*
this, var); }
236 else if (t ==
typeid(int16_t))
237 {
return Compare<int16_t>(*
this, var); }
239 else if (t ==
typeid(std::vector<int16_t>))
240 {
return Compare<std::vector<int16_t>>(*
this, var); }
242 else if (t ==
typeid(uint16_t))
243 {
return Compare<uint16_t>(*
this, var); }
245 else if (t ==
typeid(std::vector<uint16_t>))
246 {
return Compare<std::vector<uint16_t>>(*
this, var); }
248 else if (t ==
typeid(int32_t))
249 {
return Compare<int32_t>(*
this, var); }
251 else if (t ==
typeid(std::vector<int32_t>))
252 {
return Compare<std::vector<int32_t>>(*
this, var); }
254 else if (t ==
typeid(uint32_t))
255 {
return Compare<uint32_t>(*
this, var); }
257 else if (t ==
typeid(std::vector<uint32_t>))
258 {
return Compare<std::vector<uint32_t>>(*
this, var); }
260 else if (t ==
typeid(int64_t))
261 {
return Compare<int64_t>(*
this, var); }
263 else if (t ==
typeid(std::vector<int64_t>))
264 {
return Compare<std::vector<int64_t>>(*
this, var); }
266 else if (t ==
typeid(uint64_t))
267 {
return Compare<uint64_t>(*
this, var); }
269 else if (t ==
typeid(std::vector<uint64_t>))
270 {
return Compare<std::vector<uint64_t>>(*
this, var); }
272 else if (t ==
typeid(
float))
273 {
return Compare<float>(*
this, var); }
275 else if (t ==
typeid(std::vector<float>))
276 {
return Compare<std::vector<float>>(*
this, var); }
278 else if (t ==
typeid(
double))
279 {
return Compare<double>(*
this, var); }
281 else if (t ==
typeid(std::vector<double>))
282 {
return Compare<std::vector<double>>(*
this, var); }
285 {
return Compare<std::string>(*
this, var); }
287 else if (t ==
typeid(std::vector<std::string>))
288 {
return Compare<std::vector<std::string>>(*
this, var); }
291 {
return Compare<DateTime>(*
this, var); }
293 else if (t ==
typeid(std::vector<DateTime>))
294 {
return Compare<std::vector<DateTime>>(*
this, var); }
296 else if (t ==
typeid(
Guid))
297 {
return Compare<Guid>(*
this, var); }
299 else if (t ==
typeid(std::vector<Guid>))
300 {
return Compare<std::vector<Guid>>(*
this, var); }
303 {
return Compare<ByteString>(*
this, var); }
305 else if (t ==
typeid(std::vector<ByteString>))
306 {
return Compare<std::vector<ByteString>>(*
this, var); }
308 else if (t ==
typeid(
NodeId))
309 {
return Compare<NodeId>(*
this, var); }
311 else if (t ==
typeid(std::vector<NodeId>))
312 {
return Compare<std::vector<NodeId>>(*
this, var); }
315 {
return Compare<StatusCode>(*
this, var); }
317 else if (t ==
typeid(std::vector<StatusCode>))
318 {
return Compare<std::vector<StatusCode>>(*
this, var); }
321 {
return Compare<LocalizedText>(*
this, var); }
323 else if (t ==
typeid(std::vector<LocalizedText>))
324 {
return Compare<std::vector<LocalizedText>>(*
this, var); }
327 {
return Compare<QualifiedName>(*
this, var); }
329 else if (t ==
typeid(std::vector<QualifiedName>))
330 {
return Compare<std::vector<QualifiedName>>(*
this, var); }
339 {
return Compare<Variant>(*
this, var); }
341 else if (t ==
typeid(std::vector<Variant>))
342 {
return Compare<std::vector<Variant>>(*
this, var); }
345 {
return Compare<DiagnosticInfo>(*
this, var); }
347 else if (t ==
typeid(std::vector<DiagnosticInfo>))
348 {
return Compare<std::vector<DiagnosticInfo>>(*
this, var); }
360 return Value.empty();
365 const std::type_info & t =
Value.type();
367 (t ==
typeid(std::vector<bool>)) ||
368 (t ==
typeid(std::vector<int8_t>)) ||
369 (t ==
typeid(std::vector<uint8_t>)) ||
370 (t ==
typeid(std::vector<int16_t>)) ||
371 (t ==
typeid(std::vector<uint16_t>)) ||
372 (t ==
typeid(std::vector<int32_t>)) ||
373 (t ==
typeid(std::vector<uint32_t>)) ||
374 (t ==
typeid(std::vector<int64_t>)) ||
375 (t ==
typeid(std::vector<uint64_t>)) ||
376 (t ==
typeid(std::vector<float>)) ||
377 (t ==
typeid(std::vector<double>)) ||
378 (t ==
typeid(std::vector<std::string>)) ||
379 (t ==
typeid(std::vector<DateTime>)) ||
380 (t ==
typeid(std::vector<Guid>)) ||
381 (t ==
typeid(std::vector<ByteString>)) ||
382 (t ==
typeid(std::vector<NodeId>)) ||
383 (t ==
typeid(std::vector<StatusCode>)) ||
384 (t ==
typeid(std::vector<LocalizedText>)) ||
385 (t ==
typeid(std::vector<QualifiedName>)) ||
387 (t ==
typeid(std::vector<Variant>)) ||
388 (t ==
typeid(std::vector<DiagnosticInfo>));
396 const std::type_info & t =
Value.type();
398 if (t ==
typeid(
bool) || t ==
typeid(std::vector<bool>))
401 else if (t ==
typeid(int8_t) || t ==
typeid(std::vector<int8_t>))
404 else if (t ==
typeid(uint8_t) || t ==
typeid(std::vector<uint8_t>))
407 else if (t ==
typeid(int16_t) || t ==
typeid(std::vector<int16_t>))
410 else if (t ==
typeid(uint16_t) || t ==
typeid(std::vector<uint16_t>))
413 else if (t ==
typeid(int32_t) || t ==
typeid(std::vector<int32_t>))
416 else if (t ==
typeid(uint32_t) || t ==
typeid(std::vector<uint32_t>))
419 else if (t ==
typeid(int64_t) || t ==
typeid(std::vector<int64_t>))
422 else if (t ==
typeid(uint64_t) || t ==
typeid(std::vector<uint64_t>))
425 else if (t ==
typeid(
float) || t ==
typeid(std::vector<float>))
428 else if (t ==
typeid(
double) || t ==
typeid(std::vector<double>))
431 else if (t ==
typeid(
std::string) || t ==
typeid(std::vector<std::string>))
434 else if (t ==
typeid(
DateTime) || t ==
typeid(std::vector<DateTime>))
437 else if (t ==
typeid(
Guid) || t ==
typeid(std::vector<Guid>))
440 else if (t ==
typeid(
ByteString) || t ==
typeid(std::vector<ByteString>))
443 else if (t ==
typeid(
NodeId) || t ==
typeid(std::vector<NodeId>))
446 else if (t ==
typeid(
StatusCode) || t ==
typeid(std::vector<StatusCode>))
449 else if (t ==
typeid(
LocalizedText) || t ==
typeid(std::vector<LocalizedText>))
452 else if (t ==
typeid(
QualifiedName) || t ==
typeid(std::vector<QualifiedName>))
459 else if (t ==
typeid(
Variant) || t ==
typeid(std::vector<Variant>))
462 else if (t ==
typeid(
DiagnosticInfo) || t ==
typeid(std::vector<DiagnosticInfo>))
465 throw std::runtime_error(
std::string(
"Unknown variant type '") + t.name() +
"'.");
471 using namespace boost;
472 const std::type_info & t =
Value.type();
474 if (t ==
typeid(
bool))
477 else if (t ==
typeid(std::vector<bool>))
478 { visitor.
Visit(any_cast<std::vector<bool>>(
Value)); }
480 else if (t ==
typeid(int8_t))
483 else if (t ==
typeid(std::vector<int8_t>))
484 { visitor.
Visit(any_cast<std::vector<int8_t>>(
Value)); }
486 else if (t ==
typeid(uint8_t))
489 else if (t ==
typeid(std::vector<uint8_t>))
490 { visitor.
Visit(any_cast<std::vector<uint8_t>>(
Value)); }
492 else if (t ==
typeid(int16_t))
495 else if (t ==
typeid(std::vector<int16_t>))
496 { visitor.
Visit(any_cast<std::vector<int16_t>>(
Value)); }
498 else if (t ==
typeid(uint16_t))
501 else if (t ==
typeid(std::vector<uint16_t>))
502 { visitor.
Visit(any_cast<std::vector<uint16_t>>(
Value)); }
504 else if (t ==
typeid(int32_t))
507 else if (t ==
typeid(std::vector<int32_t>))
508 { visitor.
Visit(any_cast<std::vector<int32_t>>(
Value)); }
510 else if (t ==
typeid(uint32_t))
513 else if (t ==
typeid(std::vector<uint32_t>))
514 { visitor.
Visit(any_cast<std::vector<uint32_t>>(
Value)); }
516 else if (t ==
typeid(int64_t))
519 else if (t ==
typeid(std::vector<int64_t>))
520 { visitor.
Visit(any_cast<std::vector<int64_t>>(
Value)); }
522 else if (t ==
typeid(uint64_t))
525 else if (t ==
typeid(std::vector<uint64_t>))
526 { visitor.
Visit(any_cast<std::vector<uint64_t>>(
Value)); }
528 else if (t ==
typeid(
float))
531 else if (t ==
typeid(std::vector<float>))
532 { visitor.
Visit(any_cast<std::vector<float>>(
Value)); }
534 else if (t ==
typeid(
double))
537 else if (t ==
typeid(std::vector<double>))
538 { visitor.
Visit(any_cast<std::vector<double>>(
Value)); }
541 { visitor.
Visit(any_cast<std::string>(
Value)); }
543 else if (t ==
typeid(std::vector<std::string>))
544 { visitor.
Visit(any_cast<std::vector<std::string>>(
Value)); }
549 else if (t ==
typeid(std::vector<DateTime>))
550 { visitor.
Visit(any_cast<std::vector<DateTime>>(
Value)); }
552 else if (t ==
typeid(
Guid))
555 else if (t ==
typeid(std::vector<Guid>))
556 { visitor.
Visit(any_cast<std::vector<Guid>>(
Value)); }
559 { visitor.
Visit(any_cast<ByteString>(
Value)); }
561 else if (t ==
typeid(std::vector<ByteString>))
562 { visitor.
Visit(any_cast<std::vector<ByteString>>(
Value)); }
564 else if (t ==
typeid(
NodeId))
567 else if (t ==
typeid(std::vector<NodeId>))
568 { visitor.
Visit(any_cast<std::vector<NodeId>>(
Value)); }
571 { visitor.
Visit(any_cast<StatusCode>(
Value)); }
573 else if (t ==
typeid(std::vector<StatusCode>))
574 { visitor.
Visit(any_cast<std::vector<StatusCode>>(
Value)); }
577 { visitor.
Visit(any_cast<LocalizedText>(
Value)); }
579 else if (t ==
typeid(std::vector<LocalizedText>))
580 { visitor.
Visit(any_cast<std::vector<LocalizedText>>(
Value)); }
583 { visitor.
Visit(any_cast<QualifiedName>(
Value)); }
585 else if (t ==
typeid(std::vector<QualifiedName>))
586 { visitor.
Visit(any_cast<std::vector<QualifiedName>>(
Value)); }
597 else if (t ==
typeid(std::vector<Variant>))
598 { visitor.
Visit(any_cast<std::vector<Variant>>(
Value)); }
601 { visitor.
Visit(any_cast<DiagnosticInfo>(
Value)); }
603 else if (t ==
typeid(std::vector<DiagnosticInfo>))
604 { visitor.
Visit(any_cast<std::vector<DiagnosticInfo>>(
Value)); }
607 {
throw std::runtime_error(
std::string(
"Unknown variant type '") + t.name() +
"'."); }
690 throw std::runtime_error(
"Unknown variant type.");
699 std::string msg(
"Cannot convert to variant type: invalid namespace of node ");
700 throw std::runtime_error(msg +
ToString(dataType));
792 std::stringstream str;
797 str << OpcUa::ToString(boost::any_cast<DateTime> (
Value));
801 str << boost::any_cast<std::string> (
Value);
805 str << ((boost::any_cast<bool> (
Value)) ?
"true" :
"false");
809 str << boost::any_cast<unsigned char> (
Value);
813 str << boost::any_cast<char> (
Value);
817 str << boost::any_cast<double> (
Value);
821 str << boost::any_cast<float> (
Value);
825 str << boost::any_cast<int16_t> (
Value);
829 str << boost::any_cast<int32_t> (
Value);
833 str << boost::any_cast<int64_t> (
Value);
837 str << boost::any_cast<uint16_t> (
Value);
841 str << boost::any_cast<uint32_t> (
Value);
845 str << boost::any_cast<uint64_t> (
Value);
849 str <<
"conversion to string is not supported";
858 return "conversion to string is not supported";
866 const uint8_t encodingMask = 0;
867 std::size_t size =
RawSize(encodingMask);
874 RawSizeVisitor rawSizeCalc;
877 size += rawSizeCalc.Result;
879 if (!var.Dimensions.empty())
888 void DataSerializer::Serialize<Variant>(
const Variant & var)
890 uint8_t encodingMask =
static_cast<uint8_t
>(var.Type());
897 if (!var.Dimensions.empty())
902 Serialize(encodingMask);
909 VariantSerializer variantSerializer(
this);
913 if (!var.Dimensions.empty())
920 void DataDeserializer::Deserialize<Variant>(
Variant & var)
923 uint8_t encoding = 0;
924 Deserialize(encoding);
926 VariantDeserializer deserializer(
this);
935 { var = deserializer.get<
bool>(); }
938 { var = deserializer.get<std::vector<bool>>(); }
941 { var = deserializer.get<int8_t>(); }
944 { var = deserializer.get<std::vector<int8_t>>(); }
947 { var = deserializer.get<uint8_t>(); }
950 { var = deserializer.get<std::vector<uint8_t>>(); }
953 { var = deserializer.get<int16_t>(); }
956 { var = deserializer.get<std::vector<int16_t>>(); }
959 { var = deserializer.get<uint16_t>(); }
962 { var = deserializer.get<std::vector<uint16_t>>(); }
965 { var = deserializer.get<int32_t>(); }
968 { var = deserializer.get<std::vector<int32_t>>(); }
971 { var = deserializer.get<uint32_t>(); }
974 { var = deserializer.get<std::vector<uint32_t>>(); }
977 { var = deserializer.get<int64_t>(); }
980 { var = deserializer.get<std::vector<int64_t>>(); }
983 { var = deserializer.get<uint64_t>(); }
986 { var = deserializer.get<std::vector<uint64_t>>(); }
989 { var = deserializer.get<
float>(); }
992 { var = deserializer.get<std::vector<float>>(); }
995 { var = deserializer.get<
double>(); }
998 { var = deserializer.get<std::vector<double>>(); }
1004 { var = deserializer.get<std::vector<std::string>>(); }
1007 { var = deserializer.get<
DateTime>(); }
1010 { var = deserializer.get<std::vector<DateTime>>(); }
1013 { var = deserializer.get<
Guid>(); }
1016 { var = deserializer.get<std::vector<Guid>>(); }
1022 { var = deserializer.get<std::vector<ByteString>>(); }
1025 { var = deserializer.get<
NodeId>(); }
1028 { var = deserializer.get<std::vector<NodeId>>(); }
1034 { var = deserializer.get<std::vector<StatusCode>>(); }
1040 { var = deserializer.get<std::vector<LocalizedText>>(); }
1046 { var = deserializer.get<std::vector<QualifiedName>>(); }
1055 { var = deserializer.get<
Variant>(); }
1058 { var = deserializer.get<std::vector<Variant>>(); }
1064 { var = deserializer.get<std::vector<DiagnosticInfo>>(); }
1070 { var = deserializer.get<std::vector<ExtensionObject>>(); }
1073 {
throw std::logic_error(
"Deserialization of VariantType: " + std::to_string(encodingMask) +
" is not supported yet."); }
1082 void DataSerializer::Serialize<std::vector<Variant>>(
const std::vector<Variant> & targets)
1088 void DataDeserializer::Deserialize<std::vector<Variant>>(std::vector<Variant> & targets)
uint32_t GetNamespaceIndex() const
std::size_t RawSize< Variant >(const Variant &var)
Opc Ua computer interface. GNU LGPL.
void SerializeContainer(Stream &out, const Container &c, uint32_t emptySizeValue=~uint32_t())
const uint8_t HAS_DIMENSIONS_MASK
void DeserializeContainer(Stream &in, Container &c)
const uint8_t HAS_ARRAY_MASK
ObjectId VariantTypeToDataType(VariantType vt)
uint32_t GetIntegerIdentifier() const
void Visit(VariantVisitor &visitor) const
void Serialize(const T &value)
OPC UA Address space part. GNU LGPL.
const char * Binary(const char *input, short n)
bool operator==(const Variant &var) const
VariantType DataTypeToVariantType(const NodeId &dataType)
virtual void Visit(bool val)=0
std::string ToString(const AttributeId &value)
std::size_t RawSizeContainer(const T &container)
std::size_t RawSize(const T &obj)
std::string ToString() const