adtf_file_library  0.13.2
ddl_helpers.h
Go to the documentation of this file.
1 
17 #pragma once
18 
20 #include <adtf_file/stream_type.h>
21 #include <ddl/ddl.h>
22 #include <ddl/codec/codec_factory.h>
23 
27 #define A_UTIL_THROW_IF_FAILED(__exp, __msg)\
28 {\
29  auto __result = (__exp);\
30  if (a_util::result::isFailed(__result))\
31  {\
32  throw std::runtime_error(std::string(__msg) + ": " +\
33  a_util::result::toString(__result));\
34  }\
35 }
36 
38 {
39 
45 inline std::tuple<std::string, std::string, bool> getMediaDescriptionFromStreamType(const std::shared_ptr<const adtf_file::StreamType>& stream_type)
46 {
47  auto property_type = std::dynamic_pointer_cast<const adtf_file::PropertyStreamType>(stream_type);
48  if (!property_type)
49  {
50  throw std::runtime_error("The stream type is not a property stream type");
51  }
52 
53  auto media_description = property_type->getProperty("md_definitions");
54  auto struct_name = property_type->getProperty("md_struct");
55  bool data_is_serialized = false;
56  try
57  {
58  data_is_serialized = property_type->getProperty("md_data_serialized").second == "true";
59  }
60  catch (...)
61  {
62  }
63 
64  return {struct_name.second, media_description.second, data_is_serialized};
65 }
66 
74 inline std::shared_ptr<adtf_file::StreamType> createAdtfDefaultStreamType(const std::string& struct_name,
75  const std::string& struct_definition,
76  bool is_serialized = false)
77 {
78  auto type = std::make_shared<adtf_file::DefaultStreamType>("adtf/default");
79  type->setProperty("md_definitions", "cString", struct_definition);
80  type->setProperty("md_struct", "cString", struct_name);
81  type->setProperty("md_data_serialized", "tBool", is_serialized ? "true" : "false");
82  return type;
83 }
84 
90 inline std::tuple<ddl::codec::CodecFactory, bool> createDDLCodecFactoryFromStreamType(const std::shared_ptr<const adtf_file::StreamType>& stream_type)
91 {
92  const auto media_description = getMediaDescriptionFromStreamType(stream_type);
93 
94  ddl::codec::CodecFactory factory(std::get<0>(media_description), std::get<1>(media_description));
95  if (a_util::result::isFailed(factory.isValid()))
96  {
97  throw std::runtime_error("unable to parse media description: " + a_util::result::toString(factory.isValid()));
98  }
99 
100  return std::tuple<ddl::codec::CodecFactory, bool>(std::move(factory), std::get<2>(media_description));
101 }
102 
106 template<typename ElementsType>
107 using element_callback = std::function<void(std::conditional_t<std::is_const<ElementsType>::value,
108  const typename ElementsType::element_type,
109  typename ElementsType::element_type>&)>;
110 
146 template<typename ElementsType>
147 void for_each_leaf_element(ElementsType& elements, const element_callback<ElementsType>& callback)
148 {
149  for (auto& current_element : elements)
150  {
151  if (current_element.hasChildren())
152  {
153  if (current_element.isArray())
154  {
155  for (size_t array_pos = 0; array_pos < current_element.getArraySize(); ++array_pos)
156  {
157  auto array_element = current_element.getArrayElement(array_pos);
158  auto& children = array_element.getChildElements();
159  for_each_leaf_element(children, callback);
160  }
161  }
162  else
163  {
164  auto& children = current_element.getChildElements();
165  for_each_leaf_element(children, callback);
166  }
167  }
168  else
169  {
170  if (current_element.isArray())
171  {
172  for (size_t array_pos = 0; array_pos < current_element.getArraySize(); ++array_pos)
173  {
174  auto array_element = current_element.getArrayElement(array_pos);
175  callback(array_element);
176  }
177  }
178  else
179  {
180  callback(current_element);
181  }
182  }
183  }
184 }
185 
186 }
namespace for ADTF DAT Processing library.
Definition: ddl_helpers.h:38
void for_each_leaf_element(ElementsType &elements, const element_callback< ElementsType > &callback)
Iterates ALL leaf elements within ALL array elements.
Definition: ddl_helpers.h:147
std::tuple< std::string, std::string, bool > getMediaDescriptionFromStreamType(const std::shared_ptr< const adtf_file::StreamType > &stream_type)
Definition: ddl_helpers.h:45
std::tuple< ddl::codec::CodecFactory, bool > createDDLCodecFactoryFromStreamType(const std::shared_ptr< const adtf_file::StreamType > &stream_type)
Definition: ddl_helpers.h:90
std::function< void(std::conditional_t< std::is_const< ElementsType >::value, const typename ElementsType::element_type, typename ElementsType::element_type > &)> element_callback
Callback type for member elements.
Definition: ddl_helpers.h:109
std::shared_ptr< adtf_file::StreamType > createAdtfDefaultStreamType(const std::string &struct_name, const std::string &struct_definition, bool is_serialized=false)
Definition: ddl_helpers.h:74

Copyright © CARIAD SE.
Generated on Mon Jun 10 2024 by doxygen 1.9.1
GIT Commit Hash: eb3af397a6b49ad6fcad9a60d8277d909b458b48