variant.h
Go to the documentation of this file.
1 
11 #ifndef __OPC_UA_VARIANT_H__
12 #define __OPC_UA_VARIANT_H__
13 
15 #include <opc/ua/protocol/types.h>
17 
18 #include <boost/any.hpp>
19 #include <string>
20 
21 #include <stdexcept>
22 
23 
24 namespace OpcUa
25 {
26 
27 enum class VariantType : uint8_t
28 {
29  NUL = 0,
30  BOOLEAN = 1,
31  SBYTE = 2,
32  BYTE = 3,
33  INT16 = 4,
34  UINT16 = 5,
35  INT32 = 6,
36  UINT32 = 7,
37  INT64 = 8,
38  UINT64 = 9,
39  FLOAT = 10,
40  DOUBLE = 11,
41  STRING = 12,
42  DATE_TIME = 13,
43  GUId = 14,
44  BYTE_STRING = 15,
45  XML_ELEMENT = 16,
46  NODE_Id = 17,
47  EXPANDED_NODE_Id = 18,
48  STATUS_CODE = 19,
49  QUALIFIED_NAME = 20,
50  LOCALIZED_TEXT = 21,
51  EXTENSION_OBJECT = 22,
52  DATA_VALUE = 23,
53  VARIANT = 24,
54  DIAGNOSTIC_INFO = 25,
55 };
56 
57 const uint8_t VALUE_TYPE_MASK = 0x3F;
58 const uint8_t HAS_DIMENSIONS_MASK = 0x40;
59 const uint8_t HAS_ARRAY_MASK = 0x80;
60 
61 
62 template<typename T>
64 {
65 private:
66  typedef char yes;
67  typedef struct { char array[2]; } no;
68 
69  template<typename C> static yes test(typename C::const_iterator *);
70  template<typename C> static no test(...);
71 public:
72  static const bool value = sizeof(test<T>(0)) == sizeof(yes);
73  typedef T type;
74 };
75 
76 template <typename T>
78 {
79  template<typename C> static char (&f(typename std::enable_if <
80  std::is_same<decltype(static_cast<typename C::const_iterator(C::*)() const>(&C::begin)),
81  typename C::const_iterator(C::*)() const>::value, void >::type *))[1];
82 
83  template<typename C> static char (&f(...))[2];
84 
85  template<typename C> static char (&g(typename std::enable_if <
86  std::is_same<decltype(static_cast<typename C::const_iterator(C::*)() const>(&C::end)),
87  typename C::const_iterator(C::*)() const>::value, void >::type *))[1];
88 
89  template<typename C> static char (&g(...))[2];
90 
91  static bool const beg_value = sizeof(f<T>(0)) == 1;
92  static bool const end_value = sizeof(g<T>(0)) == 1;
93 };
94 
95 template<typename T>
96 struct is_container_not_string : std::integral_constant < bool, has_const_iterator<T>::value && has_begin_end<T>::beg_value && has_begin_end<T>::end_value >
97 { };
98 
99 template<>
100 struct is_container_not_string<std::string> : std::integral_constant<bool, false> {};
101 
102 
103 class Variant;
104 
105 
106 // Such monster due to msvs.
108 {
109 public:
110  typedef void result_type;
111 
112 public:
113  virtual void Visit(bool val) = 0;
114  virtual void Visit(const std::vector<bool> & val) = 0;
115  virtual void Visit(int8_t val) = 0;
116  virtual void Visit(const std::vector<int8_t> & val) = 0;
117  virtual void Visit(uint8_t val) = 0;
118  virtual void Visit(const std::vector<uint8_t> & val) = 0;
119  virtual void Visit(int16_t val) = 0;
120  virtual void Visit(const std::vector<int16_t> & val) = 0;
121  virtual void Visit(uint16_t val) = 0;
122  virtual void Visit(const std::vector<uint16_t> & val) = 0;
123  virtual void Visit(int32_t val) = 0;
124  virtual void Visit(const std::vector<int32_t> & val) = 0;
125  virtual void Visit(uint32_t val) = 0;
126  virtual void Visit(const std::vector<uint32_t> & val) = 0;
127  virtual void Visit(int64_t val) = 0;
128  virtual void Visit(const std::vector<int64_t> & val) = 0;
129  virtual void Visit(uint64_t val) = 0;
130  virtual void Visit(const std::vector<uint64_t> & val) = 0;
131  virtual void Visit(float val) = 0;
132  virtual void Visit(const std::vector<float> & val) = 0;
133  virtual void Visit(double val) = 0;
134  virtual void Visit(const std::vector<double> & val) = 0;
135  virtual void Visit(const std::string & val) = 0;
136  virtual void Visit(const std::vector<std::string> & val) = 0;
137  virtual void Visit(const DateTime & val) = 0;
138  virtual void Visit(const std::vector<DateTime> & val) = 0;
139  virtual void Visit(const Guid & val) = 0;
140  virtual void Visit(const std::vector<Guid> & val) = 0;
141  virtual void Visit(const ByteString & val) = 0;
142  virtual void Visit(const std::vector<ByteString> & val) = 0;
143  virtual void Visit(const NodeId & val) = 0;
144  virtual void Visit(const std::vector<NodeId> & val) = 0;
145  virtual void Visit(const StatusCode & val) = 0;
146  virtual void Visit(const std::vector<StatusCode> & val) = 0;
147  virtual void Visit(const LocalizedText & val) = 0;
148  virtual void Visit(const std::vector<LocalizedText> & val) = 0;
149  virtual void Visit(const QualifiedName & val) = 0;
150  virtual void Visit(const std::vector<QualifiedName> & val) = 0;
151  virtual void Visit(const Variant & val) = 0;
152  virtual void Visit(const std::vector<Variant> & val) = 0;
153  virtual void Visit(const DiagnosticInfo & val) = 0;
154  virtual void Visit(const std::vector<DiagnosticInfo> & val) = 0;
155 };
156 
157 
158 class Variant
159 {
160  boost::any Value;
161 
162 public:
163  std::vector<uint32_t> Dimensions;
164 
165  Variant() {}
166  Variant(const Variant & var)
167  : Value(var.Value)
168  , Dimensions(var.Dimensions)
169  {
170  }
171 
172  template <typename T>
173  Variant(const T & value) : Value(value) {}
174  Variant(const char * value) : Variant(std::string(value)) {}
179  explicit Variant(VariantType);
180 
181  Variant & operator= (const Variant & variant)
182  {
183  this->Value = variant.Value;
184  this->Dimensions = variant.Dimensions;
185  return *this;
186  }
187 
188  template <typename T>
189  Variant & operator=(const T & value)
190  {
191  Value = value;
192  return *this;
193  }
194 
195  Variant & operator=(const char * value)
196  {
197  Value = std::string(value);
198  return *this;
199  }
200 
202  {
203  Value = NodeId(value);
204  return *this;
205  }
206 
208  {
209  Value = NodeId(value);
210  return *this;
211  }
212 
214  {
215  Value = NodeId(value);
216  return *this;
217  }
218 
220  {
221  Value = NodeId(value);
222  return *this;
223  }
224 
225 
226  bool operator== (const Variant & var) const;
227 
228  template <typename T>
229  bool operator==(const T & value) const
230  {
231  return boost::any_cast<T>(Value) == value;
232  }
233 
234  bool operator==(const char * value) const
235  {
236  return *this == std::string(value);
237  }
238 
239  bool operator==(MessageId id) const
240  {
241  return *this == NodeId(id);
242  }
243 
244  bool operator==(ReferenceId id) const
245  {
246  return *this == NodeId(id);
247  }
248 
249  bool operator==(ObjectId id) const
250  {
251  return *this == NodeId(id);
252  }
253 
255  {
256  return *this == NodeId(id);
257  }
258 
259 
260  template <typename T>
261  bool operator!= (const T & t) const
262  {
263  return !(*this == t);
264  }
265 
266  bool IsArray() const;
267  bool IsScalar() const;
268  bool IsNul() const;
269 
270  template <typename T>
271  T As() const
272  {
273  return boost::any_cast<T>(Value);
274  }
275 
276  template <typename T>
277  explicit operator T() const
278  {
279  return As<T>();
280  }
281 
282  VariantType Type() const;
283  void Visit(VariantVisitor & visitor) const;
284  std::string ToString() const;
285 };
286 
288 VariantType DataTypeToVariantType(const NodeId & dataType);
289 
290 } // namespace OpcUa
291 
292 #endif // __OPC_UA_VARIANT_H__
f
Variant(const T &value)
Definition: variant.h:173
Definition: test.py:1
Variant(ObjectId id)
Definition: variant.h:177
const uint8_t HAS_DIMENSIONS_MASK
Definition: variant.h:58
boost::any Value
Definition: variant.h:160
const uint8_t HAS_ARRAY_MASK
Definition: variant.h:59
bool operator==(const OpcUa::BrowseDescription &lhs, const OpcUa::BrowseDescription &rhs)
Variant & operator=(ReferenceId value)
Definition: variant.h:207
Variant & operator=(ExpandedObjectId value)
Definition: variant.h:219
Variant & operator=(ObjectId value)
Definition: variant.h:213
bool operator==(ExpandedObjectId id) const
Definition: variant.h:254
ObjectId VariantTypeToDataType(VariantType vt)
VariantType
Definition: variant.h:27
bool operator==(const T &value) const
Definition: variant.h:229
bool operator==(ObjectId id) const
Definition: variant.h:249
ObjectId
Definition: object_ids.h:12
Variant(ReferenceId id)
Definition: variant.h:176
bool operator==(ReferenceId id) const
Definition: variant.h:244
OPC UA Address space part. GNU LGPL.
bool operator==(MessageId id) const
Definition: variant.h:239
std::vector< uint32_t > Dimensions
Definition: variant.h:163
bool operator!=(const std::tm &tm1, const std::tm &tm2)
Definition: os.h:129
const uint8_t VALUE_TYPE_MASK
Definition: variant.h:57
VariantType DataTypeToVariantType(const NodeId &dataType)
std::string ToString(const AttributeId &value)
Variant(MessageId id)
Definition: variant.h:175
Variant & operator=(const T &value)
Definition: variant.h:189
Variant(const char *value)
Definition: variant.h:174
T As() const
Definition: variant.h:271
bool operator==(const char *value) const
Definition: variant.h:234
Variant & operator=(MessageId value)
Definition: variant.h:201
Variant(const Variant &var)
Definition: variant.h:166
Variant(ExpandedObjectId id)
Definition: variant.h:178
Variant & operator=(const char *value)
Definition: variant.h:195


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08