IDS peak ICV v1.0.0
Loading...
Searching...
No Matches
peak_icv_archive.ipp
Go to the documentation of this file.
1
11#pragma once
12
15#include <peak_icv_c/serialization/peak_icv_archive.h>
16
17#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
22namespace peak
23{
24namespace icv
25{
26namespace detail
27{
28
29inline Archive::ValueType MapApiValueType(peak_icv_archive_value_type_t type)
30{
31 switch (type)
32 {
33 case PEAK_ICV_ARCHIVE_VALUE_TYPE_BOOL:
35 case PEAK_ICV_ARCHIVE_VALUE_TYPE_BOOL_ARRAY:
37 case PEAK_ICV_ARCHIVE_VALUE_TYPE_INT:
39 case PEAK_ICV_ARCHIVE_VALUE_TYPE_INT_ARRAY:
41 case PEAK_ICV_ARCHIVE_VALUE_TYPE_DOUBLE:
43 case PEAK_ICV_ARCHIVE_VALUE_TYPE_DOUBLE_ARRAY:
45 case PEAK_ICV_ARCHIVE_VALUE_TYPE_STRING:
47 case PEAK_ICV_ARCHIVE_VALUE_TYPE_STRING_ARRAY:
49 case PEAK_ICV_ARCHIVE_VALUE_TYPE_ARCHIVE:
51 case PEAK_ICV_ARCHIVE_VALUE_TYPE_ARCHIVE_ARRAY:
53 default:
54 throw NotSupportedException("The given archive type is not supported.");
55 }
56}
57} /* namespace detail */
58
60{
62 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Create(&m_handle);
63 });
64}
65
66inline Archive::Archive(peak_icv_archive_handle handle)
67 : m_handle(handle)
68{}
69
71{
72 if (m_handle != nullptr)
73 {
74 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Destroy(m_handle);
75 }
76}
77
78inline Archive::Archive(Archive&& other) noexcept
79{
80 m_handle = std::exchange(other.m_handle, nullptr);
81}
82
83inline Archive& Archive::operator=(Archive&& other) noexcept
84{
85 if (m_handle != nullptr)
86 {
87 PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Destroy(m_handle);
88 }
89 m_handle = std::exchange(other.m_handle, nullptr);
90 return *this;
91}
92
93inline Archive::ValueType Archive::GetValueType(const std::string& key) const
94{
95 peak_icv_archive_value_type_t recordType{};
96 detail::ExecuteAndMapReturnCodes([this, &key, &recordType] {
97 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetValueType(m_handle, key.c_str(), &recordType);
98 });
99
100 return detail::MapApiValueType(recordType);
101}
102
103inline std::vector<std::string> Archive::GetKeys() const
104{
105 size_t count{};
106
107 detail::ExecuteAndMapReturnCodes([this, &count] {
108 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetKeys_GetCount(m_handle, &count);
109 });
110
111 std::vector<std::string> keys;
112 keys.reserve(count);
113
114 detail::ExecuteAndMapReturnCodes([this, &keys, &count] {
115 for (size_t i = 0; i < count; i++)
116 {
117 size_t size{};
118 auto status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetKeysElement_GetSizeInBytes(m_handle, i, &size);
119 if (status != PEAK_ICV_STATUS_SUCCESS)
120 {
121 return status;
122 }
123
124 std::vector<char> buffer(size);
125 status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetKeysElement(m_handle, i, buffer.data(), size);
126 if (status != PEAK_ICV_STATUS_SUCCESS)
127 {
128 return status;
129 }
130
131 keys.emplace_back(buffer.data(), buffer.size() - 1);
132 }
133
134 return PEAK_ICV_STATUS_SUCCESS;
135 });
136
137 return keys;
138}
139
140inline size_t Archive::GetArrayCount(const std::string& key) const
141{
142 size_t arrayCount{};
143 detail::ExecuteAndMapReturnCodes([this, &key, &arrayCount] {
144 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &arrayCount);
145 });
146 return arrayCount;
147}
148
149inline std::shared_ptr<peak::common::serialization::IArchive> Archive::CreateArchive() const
150{
151 peak_icv_archive_handle handle{};
153 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Create(&handle);
154 });
155 return std::make_shared<Archive>(peak::common::detail::BackendAccessor<Archive>::CreateInstance(handle));
156}
157
158inline bool Archive::HasKey(const std::string& key) const
159{
160 bool hasKey{};
161 detail::ExecuteAndMapReturnCodes([this, &key, &hasKey] {
162 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_HasKey(m_handle, key.c_str(), &hasKey);
163 });
164
165 return hasKey != 0;
166}
167
168inline void Archive::SetBool(const std::string& key, bool value)
169{
170 detail::ExecuteAndMapReturnCodes([this, &key, &value] {
171 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetBool(m_handle, key.c_str(), value);
172 });
173}
174
175inline void Archive::SetInt(const std::string& key, int64_t value)
176{
177 detail::ExecuteAndMapReturnCodes([this, &key, &value] {
178 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetInt(m_handle, key.c_str(), value);
179 });
180}
181
182inline void Archive::SetDouble(const std::string& key, double value)
183{
184 detail::ExecuteAndMapReturnCodes([this, &key, &value] {
185 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetDouble(m_handle, key.c_str(), value);
186 });
187}
188
189inline void Archive::SetString(const std::string& key, const std::string& value)
190{
191 detail::ExecuteAndMapReturnCodes([this, &key, &value] {
192 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetString(m_handle, key.c_str(), value.c_str());
193 });
194}
195
196inline void Archive::SetArchive(const std::string& key, const std::shared_ptr<IArchive>& value)
197{
198 auto archive = std::dynamic_pointer_cast<Archive>(value);
199 if (archive == nullptr)
200 {
201 throw NullPointerException("Given archive or archive type is invalid.");
202 }
203 detail::ExecuteAndMapReturnCodes([this, &key, &archive] {
204 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetArchive(m_handle, key.c_str(), archive->m_handle);
205 });
206}
207
208inline void Archive::SetBoolArray(const std::string& key, const std::vector<bool>& value)
209{
210 detail::ExecuteAndMapReturnCodes([this, &value, &key] {
211 const std::size_t size = value.size();
212 std::vector<uint8_t> list;
213 list.resize(size);
214
215 std::transform(value.begin(), value.end(), list.data(), [](const auto& item) {
216 return item;
217 });
218
219 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetBoolArray(m_handle, key.c_str(), reinterpret_cast<bool*>(list.data()), size);
220 });
221}
222
223inline void Archive::SetIntArray(const std::string& key, const std::vector<int64_t>& value)
224{
225 detail::ExecuteAndMapReturnCodes([this, &key, &value] {
226 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetIntArray(m_handle, key.c_str(), value.data(), value.size());
227 });
228}
229
230inline void Archive::SetDoubleArray(const std::string& key, const std::vector<double>& value)
231{
232 detail::ExecuteAndMapReturnCodes([this, &key, &value] {
233 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetDoubleArray(m_handle, key.c_str(), value.data(), value.size());
234 });
235}
236
237inline void Archive::SetStringArray(const std::string& key, const std::vector<std::string>& value)
238{
239 detail::ExecuteAndMapReturnCodes([this, &value, &key] {
240 std::vector<const char*> stringList;
241 stringList.reserve(value.size());
242
243 std::transform(value.cbegin(), value.cend(), std::back_inserter(stringList), [](const auto& item) {
244 return item.c_str();
245 });
246 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetStringArray(m_handle, key.c_str(), stringList.data(), stringList.size());
247 });
248}
249
250inline void Archive::SetArchiveArray(const std::string& key, const std::vector<std::shared_ptr<IArchive>>& value)
251{
252 detail::ExecuteAndMapReturnCodes([this, &value, &key] {
253 std::vector<peak_icv_archive_handle> handles;
254 handles.reserve(value.size());
255 std::transform(std::cbegin(value), std::cend(value), std::back_inserter(handles), [](const auto& item) {
256 const auto archive = std::dynamic_pointer_cast<Archive>(item);
257 if (archive == nullptr)
258 {
259 throw NullPointerException("One or multiple of the given archives or archive types are invalid.");
260 }
261 return archive->m_handle;
262 });
263
264 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetArchiveArray(m_handle, key.c_str(), handles.data(), handles.size());
265 });
266}
267
268inline bool Archive::GetBool(const std::string& key) const
269{
270 bool value{};
271
272 detail::ExecuteAndMapReturnCodes([this, &key, &value]() {
273 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetBool(m_handle, key.c_str(), &value);
274 });
275
276 return value;
277}
278
279inline int64_t Archive::GetInt(const std::string& key) const
280{
281 int64_t value{};
282
283 detail::ExecuteAndMapReturnCodes([this, &key, &value]() {
284 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetInt(m_handle, key.c_str(), &value);
285 });
286
287 return value;
288}
289
290inline double Archive::GetDouble(const std::string& key) const
291{
292 double value{};
293
294 detail::ExecuteAndMapReturnCodes([this, &key, &value]() {
295 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetDouble(m_handle, key.c_str(), &value);
296 });
297
298 return value;
299}
300
301inline std::string Archive::GetString(const std::string& key) const
302{
303 std::string value;
304
305 detail::ExecuteAndMapReturnCodes([this, &key, &value]() {
306 size_t size{};
307 auto status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetString_GetSizeInBytes(m_handle, key.c_str(), &size);
308 if (status != PEAK_ICV_STATUS_SUCCESS)
309 {
310 return status;
311 }
312
313 std::vector<char> buffer;
314 buffer.resize(size);
315 status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetString(m_handle, key.c_str(), buffer.data(), size);
316 if (status != PEAK_ICV_STATUS_SUCCESS)
317 {
318 return status;
319 }
320
321 buffer.resize(size - 1);
322 value.assign(buffer.data(), buffer.size());
323
324 return PEAK_ICV_STATUS_SUCCESS;
325 });
326
327 return value;
328}
329
330inline std::shared_ptr<peak::common::serialization::IArchive> Archive::GetArchive(const std::string& key) const
331{
332 peak_icv_archive_handle handle{};
333
334 detail::ExecuteAndMapReturnCodes([this, &key, &handle]() {
335 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArchive(m_handle, key.c_str(), &handle);
336 });
337
338 return std::make_shared<Archive>(peak::common::detail::BackendAccessor<Archive>::CreateInstance(handle));
339}
340
341inline std::vector<bool> Archive::GetBoolArray(const std::string& key) const
342{
343 std::vector<bool> values;
344
345 size_t count{};
347 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
348 });
349
350 if (count == 0)
351 {
352 return {};
353 }
354
355 std::vector<uint8_t> buffer(count);
357 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetBoolArray(m_handle, key.c_str(), reinterpret_cast<bool*>(buffer.data()), count);
358 });
359
360 values.resize(count);
361 std::transform(buffer.begin(), buffer.end(), values.begin(), [](uint8_t b) {
362 return static_cast<bool>(b);
363 });
364
365 return values;
366}
367
368inline std::vector<int64_t> Archive::GetIntArray(const std::string& key) const
369{
370 size_t count{};
371 detail::ExecuteAndMapReturnCodes([this, &key, &count]() {
372 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
373 });
374 if (count == 0)
375 {
376 return {};
377 }
378
379 std::vector<int64_t> list(count);
380
381 detail::ExecuteAndMapReturnCodes([this, &key, &list, &count]() {
382 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetIntArray(m_handle, key.c_str(), list.data(), count);
383 });
384
385 return list;
386}
387
388inline std::vector<double> Archive::GetDoubleArray(const std::string& key) const
389{
390 size_t count{};
391
392 detail::ExecuteAndMapReturnCodes([this, &key, &count]() {
393 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
394 });
395
396 if (count == 0)
397 {
398 return {};
399 }
400
401 std::vector<double> list(count);
402
403 detail::ExecuteAndMapReturnCodes([this, &key, &list, &count]() {
404 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetDoubleArray(m_handle, key.c_str(), list.data(), count);
405 });
406
407 return list;
408}
409
410inline std::vector<std::string> Archive::GetStringArray(const std::string& key) const
411{
412 size_t count{};
413
414 detail::ExecuteAndMapReturnCodes([this, &key, &count]() {
415 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
416 });
417
418 if (count == 0)
419 {
420 return {};
421 }
422
423 std::vector<std::string> list;
424 list.reserve(count);
425
426 detail::ExecuteAndMapReturnCodes([this, &key, &list, &count]() {
427 for (size_t i = 0; i < count; i++)
428 {
429 size_t size{};
430 auto status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetStringArrayElement_GetSizeInBytes(m_handle, key.c_str(), i, &size);
431 if (status != PEAK_ICV_STATUS_SUCCESS)
432 {
433 return status;
434 }
435
436 std::vector<char> buffer(size);
437
438 status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetStringArrayElement(m_handle, key.c_str(), i, buffer.data(), size);
439 if (status != PEAK_ICV_STATUS_SUCCESS)
440 {
441 return status;
442 }
443
444 buffer.resize(size - 1);
445 list.emplace_back(buffer.data(), buffer.size());
446 }
447
448 return PEAK_ICV_STATUS_SUCCESS;
449 });
450
451 return list;
452}
453
454inline std::vector<std::shared_ptr<peak::common::serialization::IArchive>> Archive::GetArchiveArray(const std::string& key) const
455{
456 std::vector<std::shared_ptr<IArchive>> archiveList;
457
458 size_t count{};
459 detail::ExecuteAndMapReturnCodes([this, &key, &count, &archiveList]() {
460 auto status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
461 if (status != PEAK_ICV_STATUS_SUCCESS || count == 0)
462 {
463 return status;
464 }
465
466 std::vector<peak_icv_archive_handle> archiveHandleList{ count };
467 archiveList.reserve(count);
468 status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArchiveArray(m_handle, key.c_str(), archiveHandleList.data(), count);
469 std::transform(archiveHandleList.begin(), archiveHandleList.end(), std::back_inserter(archiveList), [](const auto& element) {
470 return std::make_shared<Archive>(peak::common::detail::BackendAccessor<Archive>::CreateInstance(element));
471 });
472
473 return status;
474 });
475
476 return archiveList;
477}
478} /* namespace icv */
479} /* namespace peak */
ValueType
Enumerates supported value types that can be stored or queried in the archive.
@ Double
Double-precision floating point.
A simple key-value store that implements the IArchive interface.
std::vector< int64_t > GetIntArray(const std::string &key) const override
Gets an array of integer values.
std::vector< std::shared_ptr< IArchive > > GetArchiveArray(const std::string &key) const override
Gets an array of nested archive values.
void SetBoolArray(const std::string &key, const std::vector< bool > &value) override
Sets an array of boolean values.
double GetDouble(const std::string &key) const override
Gets a double precision floating point value.
void SetDouble(const std::string &key, double value) override
Sets a double precision floating point value.
std::string GetString(const std::string &key) const override
Gets a string value.
void SetInt(const std::string &key, int64_t value) override
Sets an integer value.
std::shared_ptr< IArchive > GetArchive(const std::string &key) const override
Gets a nested archive value.
std::vector< std::string > GetStringArray(const std::string &key) const override
Gets an array of string values.
std::vector< double > GetDoubleArray(const std::string &key) const override
Gets an array of double precision floating point values.
void SetString(const std::string &key, const std::string &value) override
Sets a string value.
void SetIntArray(const std::string &key, const std::vector< int64_t > &value) override
Sets an array of integer values.
Archive & operator=(const Archive &)=delete
Deleted copy assignment operator.
void SetArchive(const std::string &key, const std::shared_ptr< IArchive > &value) override
Sets a nested archive value.
int64_t GetInt(const std::string &key) const override
Gets an integer value.
bool GetBool(const std::string &key) const override
Gets a boolean value.
size_t GetArrayCount(const std::string &key) const override
Gets the count of elements in an array value.
ValueType GetValueType(const std::string &key) const override
Gets the type of value stored under a key.
void SetStringArray(const std::string &key, const std::vector< std::string > &value) override
Sets an array of string values.
~Archive() override
Virtual destructor.
std::shared_ptr< IArchive > CreateArchive() const override
Creates a new Archive instance.
std::vector< std::string > GetKeys() const override
Gets all keys stored in the archive.
std::vector< bool > GetBoolArray(const std::string &key) const override
Gets an array of boolean values.
Archive()
Default constructor.
bool HasKey(const std::string &key) const override
Checks if a key exists in the archive.
void SetBool(const std::string &key, bool value) override
Sets a boolean value.
void SetDoubleArray(const std::string &key, const std::vector< double > &value) override
Sets an array of double precision floating point values.
void SetArchiveArray(const std::string &key, const std::vector< std::shared_ptr< IArchive > > &value) override
Sets an array of nested archive values.
Thrown when a requested operation is not implemented or supported.
Thrown when attempting to access an object or value that has not been initialized.
Archive::ValueType MapApiValueType(peak_icv_archive_value_type_t type)
void ExecuteAndMapReturnCodes(const CallableType &callableObject)