15#include <peak_icv_c/serialization/peak_icv_archive.h>
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:
62 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Create(&m_handle);
72 if (m_handle !=
nullptr)
74 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Destroy(m_handle);
80 m_handle = std::exchange(other.m_handle,
nullptr);
85 if (m_handle !=
nullptr)
87 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Destroy(m_handle);
89 m_handle = std::exchange(other.m_handle,
nullptr);
95 peak_icv_archive_value_type_t recordType{};
97 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetValueType(m_handle, key.c_str(), &recordType);
108 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetKeys_GetCount(m_handle, &count);
111 std::vector<std::string> keys;
115 for (
size_t i = 0; i < count; i++)
118 auto status = PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetKeysElement_GetSizeInBytes(m_handle, i, &size);
119 if (status != PEAK_ICV_STATUS_SUCCESS)
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)
131 keys.emplace_back(buffer.data(), buffer.size() - 1);
134 return PEAK_ICV_STATUS_SUCCESS;
144 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &arrayCount);
151 peak_icv_archive_handle handle{};
153 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_Create(&handle);
155 return std::make_shared<Archive>(peak::common::detail::BackendAccessor<Archive>::CreateInstance(handle));
162 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_HasKey(m_handle, key.c_str(), &hasKey);
171 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetBool(m_handle, key.c_str(), value);
178 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetInt(m_handle, key.c_str(), value);
185 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetDouble(m_handle, key.c_str(), value);
192 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetString(m_handle, key.c_str(), value.c_str());
198 auto archive = std::dynamic_pointer_cast<Archive>(value);
199 if (archive ==
nullptr)
204 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetArchive(m_handle, key.c_str(), archive->m_handle);
211 const std::size_t size = value.size();
212 std::vector<uint8_t> list;
215 std::transform(value.begin(), value.end(), list.data(), [](
const auto& item) {
219 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetBoolArray(m_handle, key.c_str(),
reinterpret_cast<bool*
>(list.data()), size);
226 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetIntArray(m_handle, key.c_str(), value.data(), value.size());
233 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetDoubleArray(m_handle, key.c_str(), value.data(), value.size());
240 std::vector<const char*> stringList;
241 stringList.reserve(value.size());
243 std::transform(value.cbegin(), value.cend(), std::back_inserter(stringList), [](
const auto& item) {
246 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetStringArray(m_handle, key.c_str(), stringList.data(), stringList.size());
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)
259 throw NullPointerException(
"One or multiple of the given archives or archive types are invalid.");
261 return archive->m_handle;
264 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_SetArchiveArray(m_handle, key.c_str(), handles.data(), handles.size());
273 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetBool(m_handle, key.c_str(), &value);
284 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetInt(m_handle, key.c_str(), &value);
295 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetDouble(m_handle, key.c_str(), &value);
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)
313 std::vector<char> buffer;
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)
321 buffer.resize(size - 1);
322 value.assign(buffer.data(), buffer.size());
324 return PEAK_ICV_STATUS_SUCCESS;
330inline std::shared_ptr<peak::common::serialization::IArchive>
Archive::GetArchive(
const std::string& key)
const
332 peak_icv_archive_handle handle{};
335 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArchive(m_handle, key.c_str(), &handle);
338 return std::make_shared<Archive>(peak::common::detail::BackendAccessor<Archive>::CreateInstance(handle));
343 std::vector<bool> values;
347 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
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);
360 values.resize(count);
361 std::transform(buffer.begin(), buffer.end(), values.begin(), [](uint8_t b) {
362 return static_cast<bool>(b);
372 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
379 std::vector<int64_t> list(count);
382 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetIntArray(m_handle, key.c_str(), list.data(), count);
393 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
401 std::vector<double> list(count);
404 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetDoubleArray(m_handle, key.c_str(), list.data(), count);
415 return PEAK_ICV_C_ABI_PREFIX peak_icv_Archive_GetArray_GetCount(m_handle, key.c_str(), &count);
423 std::vector<std::string> list;
427 for (
size_t i = 0; i < count; i++)
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)
436 std::vector<char> buffer(size);
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)
444 buffer.resize(size - 1);
445 list.emplace_back(buffer.data(), buffer.size());
448 return PEAK_ICV_STATUS_SUCCESS;
456 std::vector<std::shared_ptr<IArchive>> 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)
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));
ValueType
Enumerates supported value types that can be stored or queried in the archive.
@ Int
64-bit signed integer
@ String
UTF-8 encoded string.
@ DoubleArray
Array of doubles.
@ StringArray
Array of strings.
@ IntArray
Array of integers.
@ Bool
Single boolean value.
@ BoolArray
Array of booleans.
@ Double
Double-precision floating point.
@ Archive
Nested archive object.
@ ArchiveArray
Array of nested archives.
A key-value store that implements the IArchive interface.
std::vector< int64_t > GetIntArray(const std::string &key) const override
Returns the array of integer values.
std::vector< std::shared_ptr< IArchive > > GetArchiveArray(const std::string &key) const override
Returns the array of nested archives.
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
Returns the 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
Returns the 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
Returns the nested archive.
std::vector< std::string > GetStringArray(const std::string &key) const override
Returns the array of string values.
std::vector< double > GetDoubleArray(const std::string &key) const override
Returns the 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
Archive instances cannot be copy-assigned.
void SetArchive(const std::string &key, const std::shared_ptr< IArchive > &value) override
Sets a nested archive.
int64_t GetInt(const std::string &key) const override
Returns the integer value.
bool GetBool(const std::string &key) const override
Returns the boolean value.
size_t GetArrayCount(const std::string &key) const override
Retruns the number of elements in an array value.
ValueType GetValueType(const std::string &key) const override
Returns 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
Retunrs all keys stored in the archive.
std::vector< bool > GetBoolArray(const std::string &key) const override
Returns the array of boolean values.
Archive()
Constructs an empty Archive instance.
bool HasKey(const std::string &key) const override
Checks whether 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 archives.
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)