ADTF
adtf_a_util_variant.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <a_util/variant.h>
10 #include <adtf_utils.h>
11 
12 namespace adtf_util
13 {
14 #define ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(_type_, _getter_functionname_) \
15 namespace detail \
16 { \
17  inline _type_ GetArrayValueByType(const a_util::variant::Variant& oValue, size_t nIdx, _type_ ) \
18  { \
19  return (&(oValue))->_getter_functionname_(nIdx); \
20  } \
21 }
22 
23 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(int8_t, getInt8)
24 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(uint8_t, getUInt8)
25 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(int16_t, getInt16)
26 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(uint16_t, getUInt16)
27 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(int32_t, getInt32)
28 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(uint32_t, getUInt32)
29 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(int64_t, getInt64)
30 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(uint64_t, getUInt64)
31 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(bool, getBool)
32 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(float, getFloat)
33 ADTF_DEFINE_GET_AUTIL_VARIANT_VALUE_BY_TYPE(double, getDouble)
34 
35 template<typename T, typename U = T>
36 cVariant ConvertToADTFVariantByType(const a_util::variant::Variant& oValue)
37 {
38  cVariant oResultVariant;
39  if (!oValue.isArray())
40  {
41  oResultVariant.Set(static_cast<T>(oValue));
42  }
43  else
44  {
45  std::vector<U> oValueArray;
46  for (size_t nIdx = 0; nIdx < oValue.getArraySize(); ++nIdx)
47  {
48  oValueArray.push_back(detail::GetArrayValueByType(oValue, nIdx, U{}));
49  }
50  oResultVariant.Set(static_cast<U*>(oValueArray.data()), oValueArray.size());
51  }
52  return oResultVariant;
53 }
54 
55 inline cVariant ConvertToADTFVariant(const a_util::variant::Variant& oValue)
56 {
57  cVariant oResultVariant;
58  switch (oValue.getType())
59  {
61  oResultVariant = ConvertToADTFVariantByType<bool, uint8_t>(oValue);
62  break;
64  oResultVariant = ConvertToADTFVariantByType<double>(oValue);
65  break;
67  oResultVariant = ConvertToADTFVariantByType<float>(oValue);
68  break;
70  oResultVariant = ConvertToADTFVariantByType<int8_t>(oValue);
71  break;
73  oResultVariant = ConvertToADTFVariantByType<uint8_t>(oValue);
74  break;
76  oResultVariant = ConvertToADTFVariantByType<int16_t>(oValue);
77  break;
79  oResultVariant = ConvertToADTFVariantByType<uint16_t>(oValue);
80  break;
82  oResultVariant = ConvertToADTFVariantByType<int32_t>(oValue);
83  break;
85  oResultVariant = ConvertToADTFVariantByType<uint32_t>(oValue);
86  break;
88  oResultVariant = ConvertToADTFVariantByType<int64_t>(oValue);
89  break;
91  oResultVariant = ConvertToADTFVariantByType<uint64_t>(oValue);
92  break;
94  oResultVariant.Set(oValue.asString().c_str());
95  break;
97  break;
98  default:
99  //do nothing here
100  //null is also empty, VTArray was handled before
101  break;
102  }
103  return oResultVariant;
104 }
105 
106 inline a_util::variant::Variant ConvertToAUtilVariant(const cVariant& oValue)
107 {
108  a_util::variant::Variant oResultVariant;
109  bool bIsArray = oValue.IsArray();
110  switch (oValue.GetType())
111  {
112  case adtf_util::VT_Bool:
113  if (!bIsArray)
114  {
115  oResultVariant = a_util::variant::Variant(oValue.GetBool());
116  }
117  else
118  {
119  oResultVariant.reset(oValue.GetBoolArray(), oValue.GetArraySize());
120  }
121  break;
122  case adtf_util::VT_Float64:
123  if (!bIsArray)
124  {
125  oResultVariant = a_util::variant::Variant(oValue.GetFloat64());
126  }
127  else
128  {
129  oResultVariant.reset(oValue.GetFloat64Array(), oValue.GetArraySize());
130  }
131  break;
132  case adtf_util::VT_Float32:
133  if (!bIsArray)
134  {
135  oResultVariant = a_util::variant::Variant(oValue.GetFloat32());
136  }
137  else
138  {
139  oResultVariant.reset(oValue.GetFloat32Array(), oValue.GetArraySize());
140  }
141  break;
142  case adtf_util::VT_Int8:
143  if (!bIsArray)
144  {
145  oResultVariant = a_util::variant::Variant(oValue.GetInt8());
146  }
147  else
148  {
149  oResultVariant.reset(oValue.GetInt8Array(), oValue.GetArraySize());
150  }
151  break;
152  case adtf_util::VT_UInt8:
153  if (!bIsArray)
154  {
155  oResultVariant = a_util::variant::Variant(oValue.GetUInt8());
156  }
157  else
158  {
159  oResultVariant.reset(oValue.GetUInt8Array(), oValue.GetArraySize());
160  }
161  break;
162  case adtf_util::VT_Int16:
163  if (!bIsArray)
164  {
165  oResultVariant = a_util::variant::Variant(oValue.GetInt16());
166  }
167  else
168  {
169  oResultVariant.reset(oValue.GetInt16Array(), oValue.GetArraySize());
170  }
171  break;
172  case adtf_util::VT_UInt16:
173  if (!bIsArray)
174  {
175  oResultVariant = a_util::variant::Variant(oValue.GetUInt16());
176  }
177  else
178  {
179  oResultVariant.reset(oValue.GetUInt16Array(), oValue.GetArraySize());
180  }
181  break;
182  case adtf_util::VT_Int32:
183  if (!bIsArray)
184  {
185  oResultVariant = a_util::variant::Variant(oValue.GetInt32());
186  }
187  else
188  {
189  oResultVariant.reset(oValue.GetInt32Array(), oValue.GetArraySize());
190  }
191  break;
192  case adtf_util::VT_UInt32:
193  if (!bIsArray)
194  {
195  oResultVariant = a_util::variant::Variant(oValue.GetUInt32());
196  }
197  else
198  {
199  oResultVariant.reset(oValue.GetUInt32Array(), oValue.GetArraySize());
200  }
201  break;
202  case adtf_util::VT_Int64:
203  if (!bIsArray)
204  {
205  oResultVariant = a_util::variant::Variant(oValue.GetInt64());
206  }
207  else
208  {
209  oResultVariant.reset(oValue.GetInt64Array(), oValue.GetArraySize());
210  }
211  break;
212  case adtf_util::VT_UInt64:
213  if (!bIsArray)
214  {
215  oResultVariant = a_util::variant::Variant(oValue.GetUInt64());
216  }
217  else
218  {
219  oResultVariant.reset(oValue.GetUInt64Array(), oValue.GetArraySize());
220  }
221  break;
222  case adtf_util::VT_String:
223  oResultVariant = a_util::variant::Variant(oValue.GetStringPtr());
224  break;
225  case adtf_util::VT_Empty:
226  break;
227  default:
228  break;
229  }
230  return oResultVariant;
231 }
232 
233 inline bool GetVariantInfoIsString(const cVariant& oValue)
234 {
235  return oValue.GetType() == adtf_util::VT_String;
236 }
237 
238 inline bool GetVariantInfoIsUnsignedInt(const cVariant& oValue)
239 {
240  switch (oValue.GetType())
241  {
242  case adtf_util::VT_Bool:
243  case adtf_util::VT_UInt8:
244  case adtf_util::VT_UInt16:
245  case adtf_util::VT_UInt32:
246  case adtf_util::VT_UInt64:
247  case adtf_util::VT_Empty:
248  return true;
249  default:
250  break;
251  }
252  return false;
253 }
254 
255 } // namespace adtf_util
Copyright © Audi Electronics Venture GmbH.
std::string asString() const
Convert the value of the variant.
VariantType getType() const
Returns the current underlying data type of the instance.
std::size_t getArraySize() const
Get the current array size of the instance.
void reset()
Resets the instance to VT_Empty.
bool isArray() const
Returns whether the instance stores an array of any kind.
ADTF adtf_util Namespace - Within adtf this is used as adtf::util or adtf_util and also defined as A_...
Common include for component a_util::variant.