ADTF  3.18.2
rawmemory_intf.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "rawmemory_base.h"
10 
11 #include <vector>
12 #include <string>
13 
14 namespace adtf
15 {
16 namespace base
17 {
18 namespace ant
19 {
22 template<>
23 struct adtf_memory_forward<adtf_util::cMemoryBlock>
24 {
25 public:
27  static tResult Assign(adtf_util::cMemoryBlock* pAssignValue,
28  const size_t /* szStaticSize */,
29  const void* pValueToSet,
30  const size_t szSizeToSet)
31  {
32  return pAssignValue->Set(pValueToSet, szSizeToSet);
33  }
35  static const void* GetPtr(const adtf_util::cMemoryBlock* pAssignValue)
36  {
37  return pAssignValue->GetPtr();
38  }
40  static size_t GetSize(const adtf_util::cMemoryBlock* pAssignValue, const size_t /* szStaticSize */)
41  {
42  return static_cast<tSize>(pAssignValue->GetSize());
43  }
44 };
45 
48 template<>
49 struct adtf_memory_forward<adtf_util::cMemoryPtr>
50 {
51 public:
52  static tResult Assign(adtf_util::cMemoryPtr* pAssignValue,
53  const size_t /* szStaticSize */,
54  const void* pValueToSet,
55  const size_t szSizeToSet)
56  {
57  return pAssignValue->Assign(pValueToSet, szSizeToSet);
58  }
59  static const void* GetPtr(const adtf_util::cMemoryPtr* pAssignValue)
60  {
61  return pAssignValue->GetPtr();
62  }
63  static size_t GetSize(const adtf_util::cMemoryPtr* pAssignValue, const size_t /* szStaticSize */)
64  {
65  return static_cast<tSize>(pAssignValue->GetSize());
66  }
67 };
68 
72 template<>
73 struct adtf_memory_forward<const void>
74 {
75 public:
76  static tResult Assign(const void* /* pAssignValue */,
77  const size_t /* szStaticSize */,
78  const void* /* pValueToSet */,
79  const size_t /* szSizeToSet */)
80  {
81  RETURN_ERROR(ERR_ACCESS_DENIED);
82  }
83  static const void* GetPtr(const void* pAssignValue)
84  {
85  return pAssignValue;
86  }
87  static size_t GetSize(const void* /* pAssignValue */, const size_t szStaticSize)
88  {
89  return szStaticSize;
90  }
91 };
92 
95 template<>
96 struct adtf_memory_forward<void>
97 {
98 private:
99  size_t m_szCurrentSize = g_npos;
100 
101 public:
102  tResult Assign(void* pAssignValue,
103  const size_t szStaticSize,
104  const void* pValueToSet,
105  const size_t szSizeToSet)
106  {
107  if (szSizeToSet <= szStaticSize)
108  {
109  adtf_util::cMemoryBlock::MemCopy(pAssignValue, pValueToSet, szSizeToSet);
110  m_szCurrentSize = szSizeToSet;
112  }
113  else
114  {
115  RETURN_ERROR(ERR_MEMORY);
116  }
117  }
118  static const void* GetPtr(const void* pAssignValue)
119  {
120  return pAssignValue;
121  }
122  size_t GetSize(const void* /* pAssignValue */, const size_t szStaticSize) const
123  {
124  if (m_szCurrentSize != g_npos)
125  {
126  return m_szCurrentSize;
127  }
128  return szStaticSize;
129  }
130 };
131 
135 template<>
136 struct adtf_memory_forward<const std::string>
137 {
138 public:
139  static tResult Assign(const std::string* /* pAssignValue */,
140  const size_t /* szStaticSize */,
141  const void* /* pValueToSet */,
142  const size_t /* szSizeToSet */)
143  {
144  RETURN_ERROR(ERR_ACCESS_DENIED);
145  }
146  static const void* GetPtr(const std::string* pAssignValue)
147  {
148  return static_cast<const void*>(pAssignValue->data());
149  }
150  static size_t GetSize(const std::string* pAssignValue, const size_t /* szStaticSize */)
151  {
152  //there is no nulltermination within the std::string, we only write the size
153  return pAssignValue->size();
154  }
155 };
156 
160 template<>
161 struct adtf_memory_forward<const std::u16string>
162 {
163 public:
164  static tResult Assign(const std::u16string* /* pAssignValue */,
165  const size_t /* szStaticSize */,
166  const void* /* pValueToSet */,
167  const size_t /* szSizeToSet */)
168  {
169  RETURN_ERROR(ERR_ACCESS_DENIED);
170  }
171  static const void* GetPtr(const std::u16string* pAssignValue)
172  {
173  return static_cast<const void*>(pAssignValue->data());
174  }
175  static size_t GetSize(const std::u16string* pAssignValue, const size_t /* szStaticSize */)
176  {
177  return pAssignValue->size() * sizeof(typename std::u16string::value_type);
178  }
179 };
180 
183 template<>
184 struct adtf_memory_forward<std::string>
185 {
186 public:
187  static tResult Assign(std::string* pAssignValue,
188  const size_t /* szStaticSize */,
189  const void* pValueToSet,
190  const size_t szSizeToSet)
191  {
192  if (szSizeToSet > 0)
193  {
194  const auto pValuePtr = static_cast<const typename std::string::value_type*>(pValueToSet);
195  pAssignValue->reserve(szSizeToSet);
196  pAssignValue->assign(pValuePtr, szSizeToSet);
197  }
199  }
200  static const void* GetPtr(const std::string* pAssignValue)
201  {
202  return static_cast<const void*>(pAssignValue->data());
203  }
204  static size_t GetSize(const std::string* pAssignValue, const size_t /* szStaticSize */)
205  {
206  //there is no nulltermination within the std::string, we only write the size
207  return pAssignValue->size();
208  }
209 };
210 
213 template<>
214 struct adtf_memory_forward<std::u16string>
215 {
216 public:
217  static tResult Assign(std::u16string* pAssignValue,
218  const size_t /* szStaticSize */,
219  const void* pValueToSet,
220  const size_t szSizeToSet)
221  {
222  if (szSizeToSet > 0)
223  {
224  const auto pValuePtr = static_cast<const typename std::u16string::value_type*>(pValueToSet);
225  pAssignValue->reserve(szSizeToSet / sizeof(typename std::u16string::value_type));
226  pAssignValue->assign(pValuePtr, szSizeToSet / sizeof(typename std::u16string::value_type));
227  }
229  }
230  static const void* GetPtr(const std::u16string* pAssignValue)
231  {
232  return static_cast<const void*>(pAssignValue->data());
233  }
234  static size_t GetSize(const std::u16string* pAssignValue, const size_t /* szStaticSize */)
235  {
236  // there is no nulltermination within the std::u16string, we only write the size in bytes
237  return pAssignValue->size() * sizeof(typename std::u16string::value_type);
238  }
239 };
240 
243 template<typename... Args>
244 struct adtf_memory_forward<std::vector<Args...>>
245 {
246 public:
247  static tResult Assign(std::vector<Args...>* pAssignValue,
248  const size_t /* szStaticSize */,
249  const void* pValueToSet,
250  const size_t szSizeToSet)
251  {
252  static_assert(std::is_trivially_copyable<typename std::vector<Args...>::value_type>::value,
253  "only trivial types are allowed for adtf_memory_forward with vector");
254  pAssignValue->resize(szSizeToSet / sizeof(typename std::vector<Args...>::value_type));
255  if (pAssignValue->size() * sizeof(typename std::vector<Args...>::value_type) == szSizeToSet)
256  {
257  adtf_util::cMemoryBlock::MemCopy(pAssignValue->data(), pValueToSet, szSizeToSet);
258  }
259  else
260  {
261  RETURN_ERROR(ERR_MEMORY);
262  }
264  }
265  static const void* GetPtr(const std::vector<Args...>* pAssignValue)
266  {
267  return pAssignValue->data();
268  }
269  static size_t GetSize(const std::vector<Args...>* pAssignValue, const size_t /* szStaticSize */)
270  {
271  return pAssignValue->size() * sizeof(typename std::vector<Args...>::value_type);
272  }
273 };
274 
278 template<typename... Args>
279 struct adtf_memory_forward<const std::vector<Args...>>
280 {
281 public:
282  static tResult Assign(const std::vector<Args...>* /* pAssignValue */,
283  const size_t /* szStaticSize */,
284  const void* /* pValueToSet */,
285  const size_t /* szSizeToSet */)
286  {
287  RETURN_ERROR(ERR_ACCESS_DENIED);
288  }
289  static const void* GetPtr(const std::vector<Args...>* pAssignValue)
290  {
291  static_assert(std::is_trivially_copyable<typename std::vector<Args...>::value_type>::value,
292  "only trivial types are allowed for adtf_memory_forward with vector");
293  return static_cast<const void*>(pAssignValue->data());
294  }
295  static size_t GetSize(const std::vector<Args...>* pAssignValue, const size_t /* szStaticSize */)
296  {
297  return pAssignValue->size() * sizeof(typename std::vector<Args...>::value_type);
298  }
299 };
300 
301 } // namespace ant
302 
303 namespace spider
304 {
305 
307 {
308 public:
309  cRawMemoryRedirect(std::function<tResult(const IRawMemory&)> fnForward):
310  m_fnForward(std::move(fnForward))
311  {
312  }
313 
314  tResult Set(const void* pValue, size_t szSize) override
315  {
316  return m_fnForward(adtf_memory_buffer<const void>(pValue, szSize));
317  }
318 
319  size_t GetSize() const override
320  {
321  return 0;
322  }
323 
324  const void* Get() const override
325  {
326  return nullptr;
327  }
328 
329 private:
330  std::function<tResult(const IRawMemory&)> m_fnForward;
331 };
332 
333 }
334 
335 using spider::cRawMemoryRedirect;
336 
337 } // namespace base
338 } // namespace adtf
339 
346 #define adtf_memory_intf(__utils_memblock__) adtf::base::adtf_memory<adtf_util::cMemoryBlock>(& (__utils_memblock__))
size_t tSize
type definition for a array size values, map size values etc.
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
The IRawMemory interface provides methods for getting and setting memory values through abstract inte...
tResult Set(const void *pValue, size_t szSize) override
Sets the Raw pointer memory.
size_t GetSize() const override
Returns the size in bytes of the memory.
const void * Get() const override
Returns the raw memory pointer.
ADTF adtf_util Namespace - Within adtf this is used as adtf::util or adtf_util and also defined as A_...
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
static tResult Assign(adtf_util::cMemoryBlock *pAssignValue, const size_t, const void *pValueToSet, const size_t szSizeToSet)
Sets (copy) the memory value pValueToSet of size in bytes szSizeToSet to the of container class T in ...
static const void * GetPtr(const adtf_util::cMemoryBlock *pAssignValue)
Gets the memory pointer to the of container class T in parameter pAssignValue.
static size_t GetSize(const adtf_util::cMemoryBlock *pAssignValue, const size_t)
Gets size in bytes of the memory pointer of container class T in parameter pAssignValue.
Concept template class for non trivial penguin::adtf_memory types of type T to specialize the usage o...
static tResult Assign(T *pAssignValue, const size_t szStaticSize, const void *pValueToSet, const size_t szSizeToSet)
Sets (copy) the memory value pValueToSet of size in bytes szSizeToSet to the of container class T in ...
static size_t GetSize(const T *pAssignValue, const size_t szStaticSize)
Gets size in bytes of the memory pointer of container class T in parameter pAssignValue.
static const void * GetPtr(const T *pAssignValue)
Gets the memory pointer to the of container class T in parameter pAssignValue.