ADTF
hashedvaluemap.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include "hashedvaluemap_intf.h"
9 #include "rawmemory_intf.h"
10 #include <adtf_utils.h>
11 
12 namespace adtf
13 {
14 namespace base
15 {
16 namespace ant
17 {
18 
19 
25 template <typename STORAGE = adtf::util::cMemoryBlock, typename INTERFACE = IHashValueMap>
26 class hash_value_map : public INTERFACE
27 {
28  private:
29  STORAGE m_oStorage;
30 
31  protected:
34 
35  public:
36  #pragma pack(push)
37  #pragma pack(1)
38  struct tHashValueStorageType //16 Byte per key value pair
39  {
40  static uint8_t GetVersion()
41  {
42  return 1;
43  }
45  nByteSize(0),
46  ui8StorageVersion(GetVersion())
47  {
48  }
49  uint8_t ui8StorageVersion; //we using 8 bit for versioning
50  uint8_t nByteSize; // we using 8 bit to restrict the value size
52  uint8_t nReserved[1];
53  IHashValueMap::tHashKey oKey; // for accessing its important to have aligned value
54  uint8_t eStorage[8]; // for accessing its important to have aligned value
55  };
56  #pragma pack(pop)
58 
59  private:
61  {
62  tHashValueStorageType* m_pCurrent;
63  public:
64  explicit cHashedValueSetter(tHashValueStorageType* pCurrent) : m_pCurrent(pCurrent)
65  {
66  }
67  tResult Set(const void* pData, size_t szSize) override
68  {
69  if (szSize > 8)
70  {
71  RETURN_ERROR(ERR_MEMORY);
72  }
73  else
74  {
75  m_pCurrent->nByteSize = static_cast<uint8_t>(szSize);
76  adtf::util::cMemoryBlock::MemCopy(&m_pCurrent->eStorage[0], pData, szSize);
78  }
79  }
80 
81  const void* Get() const override
82  {
83  return &m_pCurrent->eStorage[0];
84  }
85  size_t GetSize() const override
86  {
87  return static_cast<size_t>(m_pCurrent->nByteSize);
88  }
89  };
90 
92  {
93  tHashValueStorageType* m_pCurrent;
94  public:
95  explicit cHashedValueGetter(tHashValueStorageType* pCurrent) : m_pCurrent(pCurrent)
96  {
97  }
98 
99  tResult Set(const IHashValueMapValue& oValue) override
100  {
101  RETURN_ERROR(ERR_NOT_SUPPORTED);
102  }
103  tResult FromRaw(const IRawMemory& oRawValue)
104  {
105  RETURN_ERROR(ERR_NOT_SUPPORTED);
106  }
107 
108  tResult ToRaw(IRawMemory&& oRawValue) const
109  {
110  return oRawValue.Set(&m_pCurrent->eStorage[0], static_cast<size_t>(m_pCurrent->nByteSize));
111  }
112 
113  tType GetType() const override
114  {
115  return (m_pCurrent->eType);
116  }
117  };
118 
119  public:
121  hash_value_map() : m_oStorage()
122  {
123  }
126  hash_value_map(size_t szPreAllocatedKeyValueSize) : m_oStorage()
127  {
128  m_szPreAllocatedKeyValueSize = szPreAllocatedKeyValueSize;
129  Reset();
130  }
131 
133  virtual ~hash_value_map()
134  {
135  }
136 
139  hash_value_map(const hash_value_map& oToCopy) : hash_value_map(oToCopy.GetSize())
140  {
142  oToCopy.ToRaw(*this);
143  }
144 
149  {
151  oToCopy.ToRaw(*this);
152  return *this;
153  }
154 
156  hash_value_map(hash_value_map&& oToCopy) = delete;
159 
160  protected:
161  hash_value_map(size_t szPreallocateMemorySize, bool bZeroMemory) : m_oStorage()
162  {
163  m_oStorage.Alloc(szPreallocateMemorySize);
164  if (szPreallocateMemorySize > 0 && bZeroMemory)
165  {
166  adtf::util::cMemoryBlock::MemZero(m_oStorage.GetPtr(), szPreallocateMemorySize);
167  }
168  }
169  public:
170 
171  bool Exists(const IHashValueMap::tHashKey& oKey) const override
172  {
173  value_type* pValue = GetValueT(oKey, nullptr);
174  return (pValue != nullptr);
175  }
176 
177  tResult Get(IHashValueMap& oHashMap) const override
178  {
180  {
181  adtf_memory_buffer<const void> oMem(m_oStorage.GetPtr(), m_oStorage.GetSize());
182  return oHashMap.FromRaw(oMem);
183  }
184  else
185  {
186  value_type* pKeyValue = reinterpret_cast<value_type*>(m_oStorage.GetPtr());
187  size_t szCurrentSize = GetCapacity();
188  for (size_t szIdx = 0; szIdx < szCurrentSize; ++szIdx)
189  {
190  value_type* pCurrentVal = &pKeyValue[szIdx];
191  if (pCurrentVal->eType != IHashValueMapValue::HVT_Invalid)
192  {
193  cHashedValueGetter oValueGetter(pCurrentVal);
194  RETURN_IF_FAILED(oHashMap.SetValue(pCurrentVal->oKey, oValueGetter));
195  }
196  else
197  {
198  break;
199  }
200  }
202  }
203  }
204 
205  tResult GetValue(const IHashValueMap::tHashKey& oHashKey, IHashValueMapValue& oValue) const override
206  {
207  value_type* pValue = GetValueT(oHashKey, nullptr);
208  if (pValue)
209  {
210  cHashedValueGetter oValueGetter(pValue);
211  return oValue.Set(oValueGetter);
212  }
213  else
214  {
215  RETURN_ERROR(ERR_NOT_FOUND);
216  }
217  }
218 
219  void Reset() override
220  {
221  size_t szNewSize = m_szPreAllocatedKeyValueSize * sizeof(value_type);
222  m_oStorage.Alloc(szNewSize);
223  if (szNewSize > 0)
224  {
225  adtf::util::cMemoryBlock::MemZero(m_oStorage.GetPtr(), szNewSize);
226  }
227  }
228 
229 
230  tResult SetValue(const IHashValueMap::tHashKey& oHashKey, const IHashValueMapValue& oValue) override
231  {
232  value_type* pFirstInvalidValue = nullptr;
233  value_type* pKeyValue = GetValueT(oHashKey, &pFirstInvalidValue);
234  if (pKeyValue != nullptr)
235  {
236  //use the found to set value
237  }
238  else
239  {
240  //use the first invalid
241  if (pFirstInvalidValue)
242  {
243  pKeyValue = pFirstInvalidValue;
244  }
245  //else append
246  else
247  {
248  size_t szNewLastPos = GetCapacity();
249  RETURN_IF_FAILED(m_oStorage.Resize((szNewLastPos + 1) * sizeof(value_type)));
250  value_type* pNewFirstValue = reinterpret_cast<value_type*>(m_oStorage.GetPtr());
251  pKeyValue = &pNewFirstValue[szNewLastPos];
252  }
253 
254  }
255  pKeyValue->oKey = oHashKey;
256  pKeyValue->eType = oValue.GetType();
257  pKeyValue->ui8StorageVersion = value_type::GetVersion();
258  return oValue.ToRaw(cHashedValueSetter(pKeyValue));
259  }
260 
263  size_t GetCapacity() const
264  {
265  return m_oStorage.GetSize() / sizeof(value_type);
266  }
267 
268  bool IsEmpty() const override
269  {
270  value_type* pNewFirstValue = reinterpret_cast<value_type*>(m_oStorage.GetPtr());
271  return !((GetCapacity() > 0) && (pNewFirstValue->eType != IHashValueMapValue::HVT_Invalid));
272  }
273 
274  tResult FromRaw(const IRawMemory& oRawValue) override
275  {
276  return m_oStorage.Set(oRawValue.Get(), oRawValue.GetSize());
277  }
278 
279  tResult ToRaw(IRawMemory&& oRawValue) const override
280  {
281  return oRawValue.Set(m_oStorage.GetPtr(), m_oStorage.GetSize());
282  }
284  {
285  return value_type::GetVersion();
286  }
287  void* GetPtr() const
288  {
289  return m_oStorage.GetPtr();
290  }
291 
292  private:
293  value_type* GetValueT(const IHashValueMap::tHashKey& oKey, value_type** ppFirstInvalid) const
294  {
295  if (ppFirstInvalid)
296  {
297  *ppFirstInvalid = nullptr;
298  }
299  value_type* pKeyValue = reinterpret_cast<value_type*>(m_oStorage.GetPtr());
300  size_t szCurrentSize = GetCapacity();
301  for (size_t szIdx = 0; szIdx < szCurrentSize; ++szIdx)
302  {
303  if (pKeyValue[szIdx].eType != IHashValueMapValue::HVT_Invalid)
304  {
305  if (pKeyValue[szIdx].oKey == oKey)
306  {
307  return &pKeyValue[szIdx];
308  }
309  }
310  else
311  {
312  if (ppFirstInvalid && *ppFirstInvalid == nullptr)
313  {
314  *ppFirstInvalid = &pKeyValue[szIdx];
315  }
316  }
317  }
318  return nullptr;
319  }
320 };
321 
330 adtf::util::cVariant get_hash_value(const IHashValueMap& oMap,
331  const IHashValueMap::tHashKey& oHash,
332  const adtf::util::cVariant oDefault);
343  const IHashValueMap::tHashKey& oHash,
344  const adtf::util::cVariant oValue);
345 
354 
355 } //namespace ant
356 
357 using ant::hash_value_map;
359 using ant::get_hash_value;
360 using ant::set_hash_value;
361 
362 
363 } //namespace base
364 } // namespace adtf
Copyright © Audi Electronics Venture GmbH.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#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 hashed vlaue table store interface to set and retrieve values via a hash key.
virtual uint8_t GetRawMemoryLayoutVersion() const =0
Return the Version of the memory layout used to store the values.
adtf::util::cStringUtil::tHashKey32 tHashKey
Type for the HashKey of the Value.
virtual tResult SetValue(const tHashKey &oHashKey, const IHashValueMapValue &oValue)=0
Sets or resets the value for the hash key oHashKey as copy of oValue.
Value Interface for the IHashedValueMap.
virtual tType GetType() const =0
Retrieves the type of the Value.
tType
Enumeration of variant types for the HashValueMap Values.
virtual tResult Set(const IHashValueMapValue &oValue)=0
Sets the the Value.
The IRawMemory interface provides methods for getting and setting memory values through abstract inte...
virtual size_t GetSize() const =0
Returns the size in bytes of the memory.
virtual const void * Get() const =0
Returns the raw memory pointer.
virtual tResult ToRaw(IRawMemory &&oRawValue) const =0
Implement to create a fast value copy in memory.
virtual tResult FromRaw(const IRawMemory &oRawValue)=0
Implement to create a fast value copy in memory.
tResult Set(const IHashValueMapValue &oValue) override
Sets the the Value.
tResult FromRaw(const IRawMemory &oRawValue)
Implement to create a fast value copy in memory.
tResult ToRaw(IRawMemory &&oRawValue) const
Implement to create a fast value copy in memory.
tType GetType() const override
Retrieves the type of the Value.
tResult Set(const void *pData, 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.
implementation of the IHashValueMap.
tResult FromRaw(const IRawMemory &oRawValue) override
Implement to create a fast value copy in memory.
hash_value_map(size_t szPreAllocatedKeyValueSize)
CTOR.
hash_value_map & operator=(hash_value_map &&oToCopy)=delete
no move operator
tResult GetValue(const IHashValueMap::tHashKey &oHashKey, IHashValueMapValue &oValue) const override
Retrieves the value for the hash key oHashKey to oValue if exists.
tResult ToRaw(IRawMemory &&oRawValue) const override
Implement to create a fast value copy in memory.
hash_value_map & operator=(const hash_value_map &oToCopy)
Copy operator.
bool IsEmpty() const override
Tests if value map is empty.
uint8_t GetRawMemoryLayoutVersion() const
Return the Version of the memory layout used to store the values.
hash_value_map(hash_value_map &&oToCopy)=delete
no move DTOR
tResult SetValue(const IHashValueMap::tHashKey &oHashKey, const IHashValueMapValue &oValue) override
Sets or resets the value for the hash key oHashKey as copy of oValue.
size_t GetCapacity() const
Retrieve the current count of value can be stored.
tResult Get(IHashValueMap &oHashMap) const override
Copy operation to the oHashMap.
bool Exists(const IHashValueMap::tHashKey &oKey) const override
Tests is a value for the oKey exists.
size_t m_szPreAllocatedKeyValueSize
Value count for preallocation of the memory.
void Reset() override
Clears the value map.
hash_value_map(const hash_value_map &oToCopy)
Copy CTOR.
Copyright © Audi Electronics Venture GmbH.
IHashValueMap::tHashKey create_hash_value_key(const char *strToHash)
Generation of unambigious hash key for a string.
tResult set_hash_value(IHashValueMap &oMap, const IHashValueMap::tHashKey &oHash, const adtf::util::cVariant oValue)
Sets a variant value for the hash key oHash into the hashed value map oMap.
adtf::util::cVariant get_hash_value(const IHashValueMap &oMap, const IHashValueMap::tHashKey &oHash, const adtf::util::cVariant oDefault)
Retrieves a variant value for the hash key oHash out of the hashed value map oMap.
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.