ADTF  3.18.2
value_access.h
Go to the documentation of this file.
1 
12 #ifndef DDL_PREVIEW_VALUE_ACCESS_PRIVATE_CLASS_HEADER
13 #define DDL_PREVIEW_VALUE_ACCESS_PRIVATE_CLASS_HEADER
14 
15 #include <a_util/result.h>
17 
18 #include <vector>
19 
20 namespace ddl {
21 namespace codec {
22 
23 #if defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
24 #pragma GCC diagnostic ignored \
25  "-Wattributes" // standard type attributes are ignored when used in templates
26 #endif // defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
27 
28 namespace detail {
29 
30 template <typename ValueType>
32  : public std::is_convertible<a_util::variant::Variant,
33  a_util::underlying_type_or_type_t<ValueType>> {
34 };
35 
36 template <typename ValueType>
38  : public std::is_convertible<a_util::underlying_type_or_type_t<ValueType>,
39  a_util::variant::Variant> {
40 };
41 
42 } // namespace detail
43 
44 #if defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
45 #pragma GCC diagnostic warning \
46  "-Wattributes" // standard type attributes are ignored when used in templates
47 #endif // defined(__GNUC__) && (__GNUC__ == 5) && defined(__QNX__)
48 
56 template <typename DecoderType, typename ValueType>
57 struct ValueGetter {
65  static void getValue(const DecoderType& decoder,
66  const CodecIndex& codec_index,
67  ValueType& value)
68  {
70  "Unsupported type for decode. "
71  "By default bool, all int types, float, double, std::string is supported. "
72  "To support your own data type, create a specialization of this template");
73  a_util::variant::Variant variant_value;
74  decoder.getElementVariantValue(codec_index, variant_value);
75  value = static_cast<ValueType>(
76  static_cast<a_util::underlying_type_or_type_t<ValueType>>(variant_value));
77  }
78 };
79 
84 template <typename DecoderType>
85 struct ValueGetter<DecoderType, std::string> {
93  static void getValue(const DecoderType& decoder,
94  const CodecIndex& codec_index,
95  std::string& value)
96  {
97  value = decoder.getElementStringValue(codec_index);
98  }
99 };
100 
108 template <typename CodecType, typename ValueType>
109 struct ValueSetter {
117  static void setValue(CodecType& codec, const CodecIndex& codec_index, const ValueType& value)
118  {
120  "Unsupported type for decode. "
121  "By default bool, all int types, float, double, std::string is supported. "
122  "To support your own data type, create a specialization of this template");
123  codec.setElementVariantValue(
124  codec_index,
126  static_cast<a_util::underlying_type_or_type_t<ValueType>>(value)));
127  }
128 };
129 
134 template <typename CodecType>
135 struct ValueSetter<CodecType, std::string> {
143  static void setValue(CodecType& codec, const CodecIndex& codec_index, const std::string& value)
144  {
145  codec.setElementStringValue(codec_index, value);
146  }
147 };
148 
157 template <typename DecoderType,
158  typename ValueType,
159  bool is_supported_leaf_type = detail::SupportedLeafValueTypeExplicit<ValueType>::value>
168  static ValueType getValue(const DecoderType& decoder, const CodecIndex& codec_index)
169  {
170  ValueType value;
171  ValueGetter<DecoderType, ValueType>::getValue(decoder, codec_index, value);
172  return value;
173  }
174 };
175 
184 template <typename DecoderType, typename ValueType>
185 struct ValueGetterSelect<DecoderType, ValueType, true> {
193  static ValueType getValue(const DecoderType& decoder, const CodecIndex& codec_index)
194  {
195  LeafLayout leaf_layout;
196  if (LeafCodecIndex::convertToLeafLayout<false>(
197  codec_index, leaf_layout, decoder.getRepresentation())) {
199  decoder.getData(), decoder.getDataSize(), leaf_layout);
200  }
201  else {
202  ValueType value;
203  ValueGetter<DecoderType, ValueType>::getValue(decoder, codec_index, value);
204  return value;
205  }
206  }
207 };
208 
216 template <typename CodecType,
217  typename ValueType,
218  bool is_supported_leaf_type = detail::SupportedLeafValueTypeExplicit<ValueType>::value>
227  static void setValue(CodecType& codec, const CodecIndex& codec_index, const ValueType& value)
228  {
229  ValueSetter<CodecType, ValueType>::setValue(codec, codec_index, value);
230  }
231 };
232 
240 template <typename CodecType, typename ValueType>
241 struct ValueSetterSelect<CodecType, ValueType, true> {
249  static void setValue(CodecType& codec, const CodecIndex& codec_index, const ValueType& value)
250  {
251  LeafLayout leaf_layout;
252  if (LeafCodecIndex::convertToLeafLayout<false>(
253  codec_index, leaf_layout, codec.getRepresentation())) {
255  codec.getData(), codec.getDataSize(), leaf_layout, value);
256  }
257  else {
258  ValueSetter<CodecType, ValueType>::setValue(codec, codec_index, value);
259  }
260  }
261 };
262 
263 } // namespace codec
264 } // namespace ddl
265 
266 #endif // DDL_PREVIEW_VALUE_ACCESS_PRIVATE_CLASS_HEADER
Fast Access Index Type for the coders.
Definition: codec_index.h:132
typename underlying_type_or_type< T, is_enum >::type underlying_type_or_type_t
Retrieves the underlying type if T is an enum, otherwise the type itself.
Definition: type_traits.h:89
The leaf value access decoding and encoding is a header only optimization for leaf values of standard...
small leaf layout information to access codec data very fast.
static ValueType getValue(const void *data, const size_t data_size, const LeafLayout &leaf_layout)
Returns the current value of the given element as a ValueType if supported.
static void setValue(void *data, const size_t data_size, const LeafLayout &leaf_layout, const ValueType &value)
Sets the current value if the given element as a ValueType if supported.
static void getValue(const DecoderType &decoder, const CodecIndex &codec_index, std::string &value)
Returns the current value of the given element as a std::string.
Definition: value_access.h:93
Value getter to retrieve the current value of the given element as a ValueType if supported.
Definition: value_access.h:57
static void getValue(const DecoderType &decoder, const CodecIndex &codec_index, ValueType &value)
Returns the current value of the given element as a ValueType if supported.
Definition: value_access.h:65
static ValueType getValue(const DecoderType &decoder, const CodecIndex &codec_index)
Returns the current value of the given element as a ValueType if supported.
Definition: value_access.h:193
Value getter to retrieve the current value of the given element as a ValueType if supported.
Definition: value_access.h:160
static ValueType getValue(const DecoderType &decoder, const CodecIndex &codec_index)
Returns the current value of the given element as a ValueType if supported.
Definition: value_access.h:168
static void setValue(CodecType &codec, const CodecIndex &codec_index, const std::string &value)
Sets the value of the given element from std::string.
Definition: value_access.h:143
Value setter to set the current value of the given element from a type ValueType if supported.
Definition: value_access.h:109
static void setValue(CodecType &codec, const CodecIndex &codec_index, const ValueType &value)
Sets the current value if the given element as a ValueType if supported.
Definition: value_access.h:117
static void setValue(CodecType &codec, const CodecIndex &codec_index, const ValueType &value)
Sets the current value of the given element as a ValueType if supported.
Definition: value_access.h:249
Value setter to set the value of the given element as a ValueType if supported.
Definition: value_access.h:219
static void setValue(CodecType &codec, const CodecIndex &codec_index, const ValueType &value)
Sets the current value of the given element as a ValueType if supported.
Definition: value_access.h:227
Common include for component a_util::result.