ADTF
Loading...
Searching...
No Matches
hashedvaluemap.h
Go to the documentation of this file.
1
7#pragma once
9#include "rawmemory_intf.h"
10#include <adtf_utils.h>
11
12namespace adtf
13{
14namespace base
15{
16namespace ant
17{
18
24template<typename STORAGE = adtf::util::cMemoryBlock, typename INTERFACE = IHashValueMap>
25class hash_value_map : public INTERFACE
26{
27private:
28 STORAGE m_oStorage;
29
30protected:
33
34public:
35#pragma pack(push)
36#pragma pack(1)
40 struct tHashValueStorageType
41 {
42 static uint8_t GetVersion()
43 {
44 return 1;
45 }
46 tHashValueStorageType(): eType(IHashValueMapValue::HVT_Invalid), nByteSize(0), 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)
57 typedef tHashValueStorageType value_type;
58
59private:
60 class cHashedValueSetter : public IRawMemory
61 {
62 tHashValueStorageType* m_pCurrent;
63
64 public:
65 explicit cHashedValueSetter(tHashValueStorageType* pCurrent): m_pCurrent(pCurrent)
66 {
67 }
68 tResult Set(const void* pData, size_t szSize) override
69 {
70 if (szSize > 8)
71 {
72 RETURN_ERROR(ERR_MEMORY);
73 }
74 else
75 {
76 m_pCurrent->nByteSize = static_cast<uint8_t>(szSize);
77 adtf::util::cMemoryBlock::MemCopy(&m_pCurrent->eStorage[0], pData, szSize);
79 }
80 }
81
82 const void* Get() const override
83 {
84 return &m_pCurrent->eStorage[0];
85 }
86 size_t GetSize() const override
87 {
88 return static_cast<size_t>(m_pCurrent->nByteSize);
89 }
90 };
91
92 class cHashedValueGetter : public IHashValueMapValue
93 {
94 tHashValueStorageType* m_pCurrent;
95
96 public:
97 explicit cHashedValueGetter(tHashValueStorageType* pCurrent): m_pCurrent(pCurrent)
98 {
99 }
100
101 tResult Set(const IHashValueMapValue& /* oValue */) override
102 {
103 RETURN_ERROR(ERR_NOT_SUPPORTED);
104 }
105 tResult FromRaw(const IRawMemory& /* oRawValue */)
106 {
107 RETURN_ERROR(ERR_NOT_SUPPORTED);
108 }
109
110 tResult ToRaw(IRawMemory&& oRawValue) const
111 {
112 return oRawValue.Set(&m_pCurrent->eStorage[0], static_cast<size_t>(m_pCurrent->nByteSize));
113 }
114
115 tType GetType() const override
116 {
117 return (m_pCurrent->eType);
118 }
119 };
120
121public:
123 hash_value_map(): m_oStorage()
124 {
125 }
126
128 hash_value_map(size_t szPreAllocatedKeyValueSize): m_oStorage()
129 {
130 m_szPreAllocatedKeyValueSize = szPreAllocatedKeyValueSize;
131 Reset();
132 }
133
136 {
137 }
138
141 hash_value_map(const hash_value_map& oToCopy): hash_value_map(oToCopy.GetSize())
142 {
144 oToCopy.ToRaw(*this);
145 }
146
151 {
153 oToCopy.ToRaw(*this);
154 return *this;
155 }
156
158 hash_value_map(hash_value_map&& oToCopy) = delete;
161
162protected:
163 hash_value_map(size_t szPreallocateMemorySize, bool bZeroMemory): m_oStorage()
164 {
165 m_oStorage.Alloc(szPreallocateMemorySize);
166 if (szPreallocateMemorySize > 0 && bZeroMemory)
167 {
168 adtf::util::cMemoryBlock::MemZero(m_oStorage.GetPtr(), szPreallocateMemorySize);
169 }
170 }
171
172public:
173 bool Exists(const IHashValueMap::tHashKey& oKey) const override
174 {
175 value_type* pValue = GetValueT(oKey, nullptr);
176 return (pValue != nullptr);
177 }
178
179 tResult Get(IHashValueMap& oHashMap) const override
180 {
182 {
183 adtf_memory_buffer<const void> oMem(m_oStorage.GetPtr(), m_oStorage.GetSize());
184 return oHashMap.FromRaw(oMem);
185 }
186 else
187 {
188 value_type* pKeyValue = reinterpret_cast<value_type*>(m_oStorage.GetPtr());
189 size_t szCurrentSize = GetCapacity();
190 for (size_t szIdx = 0; szIdx < szCurrentSize; ++szIdx)
191 {
192 value_type* pCurrentVal = &pKeyValue[szIdx];
193 if (pCurrentVal->eType != IHashValueMapValue::HVT_Invalid)
194 {
195 cHashedValueGetter oValueGetter(pCurrentVal);
196 RETURN_IF_FAILED(oHashMap.SetValue(pCurrentVal->oKey, oValueGetter));
197 }
198 else
199 {
200 break;
201 }
202 }
204 }
205 }
206
207 tResult GetValue(const IHashValueMap::tHashKey& oHashKey, IHashValueMapValue& oValue) const override
208 {
209 value_type* pValue = GetValueT(oHashKey, nullptr);
210 if (pValue)
211 {
212 cHashedValueGetter oValueGetter(pValue);
213 return oValue.Set(oValueGetter);
214 }
215 else
216 {
217 RETURN_ERROR(ERR_NOT_FOUND);
218 }
219 }
220
221 void Reset() override
222 {
223 size_t szNewSize = m_szPreAllocatedKeyValueSize * sizeof(value_type);
224 m_oStorage.Alloc(szNewSize);
225 if (szNewSize > 0)
226 {
227 adtf::util::cMemoryBlock::MemZero(m_oStorage.GetPtr(), szNewSize);
228 }
229 }
230
231 tResult SetValue(const IHashValueMap::tHashKey& oHashKey, const IHashValueMapValue& oValue) override
232 {
233 value_type* pFirstInvalidValue = nullptr;
234 value_type* pKeyValue = GetValueT(oHashKey, &pFirstInvalidValue);
235 if (pKeyValue != nullptr)
236 {
237 // use the found to set value
238 }
239 else
240 {
241 // use the first invalid
242 if (pFirstInvalidValue)
243 {
244 pKeyValue = pFirstInvalidValue;
245 }
246 // else append
247 else
248 {
249 size_t szNewLastPos = GetCapacity();
250 RETURN_IF_FAILED(m_oStorage.Resize((szNewLastPos + 1) * sizeof(value_type)));
251 value_type* pNewFirstValue = reinterpret_cast<value_type*>(m_oStorage.GetPtr());
252 pKeyValue = &pNewFirstValue[szNewLastPos];
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
292private:
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
330adtf::util::cVariant get_hash_value(const IHashValueMap& oMap,
331 const IHashValueMap::tHashKey& oHash,
332 const adtf::util::cVariant oDefault);
342tResult set_hash_value(IHashValueMap& oMap, const IHashValueMap::tHashKey& oHash, const adtf::util::cVariant oValue);
343
352
353} // namespace ant
354
359
360} // namespace base
361} // namespace adtf
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
Definition hashedvaluemap_intf.h:21
tType
Definition hashedvaluemap_intf.h:30
@ HVT_Invalid
Invalid type.
Definition hashedvaluemap_intf.h:32
virtual tType GetType() const =0
virtual tResult Set(const IHashValueMapValue &oValue)=0
Definition hashedvaluemap_intf.h:80
virtual uint8_t GetRawMemoryLayoutVersion() const =0
adtf::util::cStringUtil::tHashKey32 tHashKey
Type for the HashKey of the Value.
Definition hashedvaluemap_intf.h:87
virtual tResult SetValue(const tHashKey &oHashKey, const IHashValueMapValue &oValue)=0
Definition rawmemory_base.h:23
virtual size_t GetSize() const =0
virtual const void * Get() const =0
virtual tResult ToRaw(IRawMemory &&oRawValue) const =0
virtual tResult FromRaw(const IRawMemory &oRawValue)=0
Template class implementation for the IRawMemory interface.
Definition rawmemory_base.h:216
Definition hashedvaluemap.h:26
void Reset() override
Definition hashedvaluemap.h:221
bool Exists(const IHashValueMap::tHashKey &oKey) const override
Definition hashedvaluemap.h:173
tResult ToRaw(IRawMemory &&oRawValue) const override
Definition hashedvaluemap.h:279
tResult SetValue(const IHashValueMap::tHashKey &oHashKey, const IHashValueMapValue &oValue) override
Definition hashedvaluemap.h:231
bool IsEmpty() const override
Definition hashedvaluemap.h:268
hash_value_map(hash_value_map &&oToCopy)=delete
no move DTOR
virtual ~hash_value_map()
DTOR.
Definition hashedvaluemap.h:135
size_t m_szPreAllocatedKeyValueSize
Value count for preallocation of the memory.
Definition hashedvaluemap.h:32
hash_value_map & operator=(const hash_value_map &oToCopy)
Definition hashedvaluemap.h:150
hash_value_map(const hash_value_map &oToCopy)
Definition hashedvaluemap.h:141
tResult Get(IHashValueMap &oHashMap) const override
Definition hashedvaluemap.h:179
hash_value_map & operator=(hash_value_map &&oToCopy)=delete
no move operator
hash_value_map(size_t szPreAllocatedKeyValueSize)
Definition hashedvaluemap.h:128
hash_value_map()
CTOR.
Definition hashedvaluemap.h:123
uint8_t GetRawMemoryLayoutVersion() const
Definition hashedvaluemap.h:283
tResult GetValue(const IHashValueMap::tHashKey &oHashKey, IHashValueMapValue &oValue) const override
Definition hashedvaluemap.h:207
tResult FromRaw(const IRawMemory &oRawValue) override
Definition hashedvaluemap.h:274
size_t GetCapacity() const
Definition hashedvaluemap.h:263
Namespace for all functionality of the ADTF Base SDK provided since v3.0.
Definition adtf_class_version.h:16
IHashValueMap::tHashKey create_hash_value_key(const char *strToHash)
tResult set_hash_value(IHashValueMap &oMap, const IHashValueMap::tHashKey &oHash, const adtf::util::cVariant oValue)
adtf::util::cVariant get_hash_value(const IHashValueMap &oMap, const IHashValueMap::tHashKey &oHash, const adtf::util::cVariant oDefault)
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14