StorageAPI improvements

This commit is contained in:
Maschell 2023-12-14 17:20:38 +01:00
parent 104fdc3b7b
commit 363048d98f
12 changed files with 909 additions and 925 deletions

View File

@ -1,6 +1,5 @@
#pragma once
#include "WUPSConfigItem.h"
#include <wups/config.h>
#ifdef __cplusplus

View File

@ -111,7 +111,7 @@ typedef struct wups_loader_hook_t {
void init_storage(wups_loader_init_storage_args_t); \
WUPS_HOOK_EX(WUPS_LOADER_HOOK_INIT_STORAGE, init_storage); \
void init_storage(wups_loader_init_storage_args_t args) { \
WUPS_InitStorage(args); \
WUPSStorageAPI_InitInternal(args); \
}

View File

@ -6,19 +6,20 @@ extern "C" {
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
typedef enum {
WUPS_STORAGE_ERROR_SUCCESS = 0,
WUPS_STORAGE_ERROR_INVALID_ARGS = 1,
WUPS_STORAGE_ERROR_INVALID_POINTER = 2,
WUPS_STORAGE_ERROR_INVALID_VERSION = 3,
WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE = 4,
WUPS_STORAGE_ERROR_NOT_FOUND = 5,
WUPS_STORAGE_ERROR_MALLOC_FAILED = 6,
WUPS_STORAGE_ERROR_ALREADY_OPENED = 7,
WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL = 8,
WUPS_STORAGE_ERROR_ALREADY_EXISTS = 9,
WUPS_STORAGE_ERROR_UNKNOWN_ERROR = 10
WUPS_STORAGE_ERROR_SUCCESS = 0,
WUPS_STORAGE_ERROR_INVALID_ARGS = -0x01,
WUPS_STORAGE_ERROR_MALLOC_FAILED = -0x02,
WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE = -0x03,
WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL = -0x04,
WUPS_STORAGE_ERROR_ALREADY_EXISTS = -0x05,
WUPS_STORAGE_ERROR_IO_ERROR = -0x06,
WUPS_STORAGE_ERROR_NOT_FOUND = -0x10,
WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED = -0xF0,
WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION = -0xF1,
WUPS_STORAGE_ERROR_UNKNOWN_ERROR = -0x100,
} WUPSStorageError;
typedef enum {
@ -38,72 +39,386 @@ typedef uint32_t WUPSStorageItemType;
typedef void *wups_storage_root_item;
typedef void *wups_storage_item;
typedef WUPSStorageError (*OpenStorageFunction)(const char *plugin_id, wups_storage_root_item *root);
typedef WUPSStorageError (*CloseStorageFunction)(const char *plugin_id);
typedef WUPSStorageError (*DeleteItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key);
typedef WUPSStorageError (*CreateSubItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, wups_storage_item *outItem);
typedef WUPSStorageError (*GetSubItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, wups_storage_item *outItem);
typedef WUPSStorageError (*StoreItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, void *data, uint32_t length);
typedef WUPSStorageError (*GetItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, void *data, uint32_t maxSize, uint32_t *outSize);
typedef WUPSStorageError (*GetItemSizeFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, uint32_t *outSize);
typedef WUPSStorageError (*WUPSStorage_SaveFunction)(wups_storage_root_item root, bool force);
typedef WUPSStorageError (*WUPSStorage_ForceReloadFunction)(wups_storage_root_item root);
typedef WUPSStorageError (*WUPSStorage_WipeStorageFunction)(wups_storage_root_item root);
typedef WUPSStorageError (*WUPSStorage_DeleteItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key);
typedef WUPSStorageError (*WUPSStorage_CreateSubItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, wups_storage_item *outItem);
typedef WUPSStorageError (*WUPSStorage_GetSubItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, wups_storage_item *outItem);
typedef WUPSStorageError (*WUPSStorage_StoreItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, void *data, uint32_t length);
typedef WUPSStorageError (*WUPSStorage_GetItemFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, void *data, uint32_t maxSize, uint32_t *outSize);
typedef WUPSStorageError (*WUPSStorage_GetItemSizeFunction)(wups_storage_root_item root, wups_storage_item parent, const char *key, uint32_t *outSize);
typedef uint32_t WUPS_STORAGE_API_VERSION;
#define WUPS_STORAGE_CUR_API_VERSION 0x02
typedef struct wups_loader_init_storage_args_t_ {
WUPS_STORAGE_API_VERSION version;
OpenStorageFunction open_storage_ptr;
CloseStorageFunction close_storage_ptr;
DeleteItemFunction delete_item_function_ptr;
CreateSubItemFunction create_sub_item_function_ptr;
GetSubItemFunction get_sub_item_function_ptr;
StoreItemFunction store_item_function_ptr;
GetItemFunction get_item_function_ptr;
GetItemSizeFunction get_item_size_function_ptr;
const char *plugin_id;
wups_storage_root_item root_item;
WUPSStorage_SaveFunction save_function_ptr;
WUPSStorage_ForceReloadFunction force_reload_function_ptr;
WUPSStorage_WipeStorageFunction wipe_storage_function_ptr;
WUPSStorage_DeleteItemFunction delete_item_function_ptr;
WUPSStorage_CreateSubItemFunction create_sub_item_function_ptr;
WUPSStorage_GetSubItemFunction get_sub_item_function_ptr;
WUPSStorage_StoreItemFunction store_item_function_ptr;
WUPSStorage_GetItemFunction get_item_function_ptr;
WUPSStorage_GetItemSizeFunction get_item_size_function_ptr;
} wups_loader_init_storage_args_t;
/* called by backend */
WUPSStorageError WUPS_InitStorage(wups_loader_init_storage_args_t args);
WUPSStorageError WUPSStorageAPI_InitInternal(wups_loader_init_storage_args_t args);
const char *WUPS_GetStorageStatusStr(WUPSStorageError status);
const char *WUPSStorageAPI_GetStatusStr(WUPSStorageError status);
/* opens storage for reading and writing */
WUPSStorageError WUPS_OpenStorage(void);
WUPSStorageError WUPSStorageAPI_SaveStorage(bool forceSave);
WUPSStorageError WUPSStorageAPI_ForceReloadStorage();
WUPSStorageError WUPSStorageAPI_WipeStorage();
/* closes storage and saves changes */
WUPSStorageError WUPS_CloseStorage(void);
WUPSStorageError WUPSStorageAPI_DeleteItem(wups_storage_item parent, const char *key);
/* deletes key from storage */
WUPSStorageError WUPS_DeleteItem(wups_storage_item parent, const char *key);
WUPSStorageError WUPSStorageAPI_CreateSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem);
WUPSStorageError WUPSStorageAPI_GetSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem);
WUPSStorageError WUPS_CreateSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem);
WUPSStorageError WUPS_GetSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem);
WUPSStorageError WUPSStorageAPI_StoreItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t size);
WUPSStorageError WUPS_StoreString(wups_storage_item parent, const char *key, const char *string);
WUPSStorageError WUPS_StoreBool(wups_storage_item parent, const char *key, bool value);
WUPSStorageError WUPS_StoreInt(wups_storage_item parent, const char *key, int32_t value);
WUPSStorageError WUPS_StoreS32(wups_storage_item parent, const char *key, int32_t value);
WUPSStorageError WUPS_StoreS64(wups_storage_item parent, const char *key, int64_t value);
WUPSStorageError WUPS_StoreU32(wups_storage_item parent, const char *key, uint32_t value);
WUPSStorageError WUPS_StoreU64(wups_storage_item parent, const char *key, uint64_t value);
WUPSStorageError WUPS_StoreFloat(wups_storage_item parent, const char *key, float value);
WUPSStorageError WUPS_StoreDouble(wups_storage_item parent, const char *key, double value);
WUPSStorageError WUPS_StoreBinary(wups_storage_item parent, const char *key, const void *data, uint32_t size);
inline WUPSStorageError WUPSStorageAPI_StoreString(wups_storage_item parent, const char *key, const char *value) {
if (value == NULL) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_STRING, (void *) value, strlen(value));
}
WUPSStorageError WUPS_GetString(wups_storage_item parent, const char *key, char *outString, uint32_t maxSize, uint32_t *outSize);
WUPSStorageError WUPS_GetBool(wups_storage_item parent, const char *key, bool *outValue);
WUPSStorageError WUPS_GetInt(wups_storage_item parent, const char *key, int32_t *outValue);
WUPSStorageError WUPS_GetS32(wups_storage_item parent, const char *key, int32_t *outValue);
WUPSStorageError WUPS_GetS64(wups_storage_item parent, const char *key, int64_t *outValue);
WUPSStorageError WUPS_GetU32(wups_storage_item parent, const char *key, uint32_t *outValue);
WUPSStorageError WUPS_GetU64(wups_storage_item parent, const char *key, uint64_t *outValue);
WUPSStorageError WUPS_GetFloat(wups_storage_item parent, const char *key, float *outValue);
WUPSStorageError WUPS_GetDouble(wups_storage_item parent, const char *key, double *outValue);
WUPSStorageError WUPS_GetBinary(wups_storage_item parent, const char *key, void *outData, uint32_t maxSize, uint32_t *outSize);
inline WUPSStorageError WUPSStorageAPI_StoreBool(wups_storage_item parent, const char *key, bool value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_BOOL, (void *) &value, sizeof(bool));
}
WUPSStorageError WUPS_GetItemSize(wups_storage_item parent, const char *key, uint32_t *outSize);
inline WUPSStorageError WUPSStorageAPI_StoreInt(wups_storage_item parent, const char *key, int32_t value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_S32, (void *) &value, sizeof(int32_t));
}
inline WUPSStorageError WUPSStorageAPI_StoreS32(wups_storage_item parent, const char *key, int32_t value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_S32, (void *) &value, sizeof(int32_t));
}
inline WUPSStorageError WUPSStorageAPI_StoreS64(wups_storage_item parent, const char *key, int64_t value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_S64, (void *) &value, sizeof(int64_t));
}
inline WUPSStorageError WUPSStorageAPI_StoreU32(wups_storage_item parent, const char *key, uint32_t value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_U32, (void *) &value, sizeof(uint32_t));
}
inline WUPSStorageError WUPSStorageAPI_StoreU64(wups_storage_item parent, const char *key, uint64_t value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_U64, (void *) &value, sizeof(uint64_t));
}
inline WUPSStorageError WUPSStorageAPI_StoreFloat(wups_storage_item parent, const char *key, float value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_FLOAT, (void *) &value, sizeof(float));
}
inline WUPSStorageError WUPSStorageAPI_StoreDouble(wups_storage_item parent, const char *key, double value) {
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_DOUBLE, (void *) &value, sizeof(double));
}
inline WUPSStorageError WUPSStorageAPI_StoreBinary(wups_storage_item parent, const char *key, const void *data, uint32_t size) {
if (data == NULL) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPSStorageAPI_StoreItem(parent, key, WUPS_STORAGE_ITEM_BINARY, (void *) data, size);
}
WUPSStorageError WUPSStorageAPI_GetItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t maxSize, uint32_t *outSize);
inline WUPSStorageError WUPSStorageAPI_GetString(wups_storage_item parent, const char *key, char *outString, uint32_t maxSize, uint32_t *outSize) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_STRING, outString, maxSize, outSize);
}
inline WUPSStorageError WUPSStorageAPI_GetBool(wups_storage_item parent, const char *key, bool *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_BOOL, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetInt(wups_storage_item parent, const char *key, int32_t *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_S32, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetS32(wups_storage_item parent, const char *key, int32_t *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_S32, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetS64(wups_storage_item parent, const char *key, int64_t *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_S64, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetU32(wups_storage_item parent, const char *key, uint32_t *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_U32, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetU64(wups_storage_item parent, const char *key, uint64_t *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_U64, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetFloat(wups_storage_item parent, const char *key, float *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_FLOAT, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetDouble(wups_storage_item parent, const char *key, double *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_DOUBLE, outValue, sizeof(*outValue), NULL);
}
inline WUPSStorageError WUPSStorageAPI_GetBinary(wups_storage_item parent, const char *key, void *outData, uint32_t maxSize, uint32_t *outValue) {
return WUPSStorageAPI_GetItem(parent, key, WUPS_STORAGE_ITEM_BINARY, outData, maxSize, outValue);
}
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, uint32_t *outSize);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
#include <cstring>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
class WUPSStorageSubItem;
namespace WUPSStorageAPI {
WUPSStorageError DeleteItem(std::string_view key) noexcept;
WUPSStorageError SaveStorage(bool forceSave = false);
WUPSStorageError ForceReloadStorage();
WUPSStorageError WipeStorage();
std::string_view GetStatusStr(const WUPSStorageError &err) noexcept;
WUPSStorageSubItem GetRootItem() noexcept;
std::optional<WUPSStorageSubItem> CreateSubItem(std::string_view key, WUPSStorageError &err) noexcept;
std::optional<WUPSStorageSubItem> GetSubItem(std::string_view key, WUPSStorageError &err) noexcept;
std::optional<WUPSStorageSubItem> GetOrCreateSubItem(std::string_view key, WUPSStorageError &err) noexcept;
WUPSStorageError GetItemSize(std::string_view key, uint32_t &outSize) noexcept;
WUPSStorageSubItem CreateSubItem(std::string_view key);
WUPSStorageSubItem GetSubItem(std::string_view key);
WUPSStorageSubItem GetOrCreateSubItem(std::string_view key);
template<typename T>
inline WUPSStorageError GetEx(wups_storage_item parent, const char *key, T &outValue) noexcept;
template<>
inline WUPSStorageError GetEx<int32_t>(wups_storage_item parent, const char *key, int32_t &outValue) noexcept {
return WUPSStorageAPI_GetS32(parent, key, &outValue);
}
template<>
inline WUPSStorageError GetEx<int64_t>(wups_storage_item parent, const char *key, int64_t &outValue) noexcept {
return WUPSStorageAPI_GetS64(parent, key, &outValue);
}
template<>
inline WUPSStorageError GetEx<uint32_t>(wups_storage_item parent, const char *key, uint32_t &outValue) noexcept {
return WUPSStorageAPI_GetU32(parent, key, &outValue);
}
template<>
inline WUPSStorageError GetEx<uint64_t>(wups_storage_item parent, const char *key, uint64_t &outValue) noexcept {
return WUPSStorageAPI_GetU64(parent, key, &outValue);
}
template<>
inline WUPSStorageError GetEx<bool>(wups_storage_item parent, const char *key, bool &outValue) noexcept {
return WUPSStorageAPI_GetBool(parent, key, &outValue);
}
template<>
inline WUPSStorageError GetEx<float>(wups_storage_item parent, const char *key, float &outValue) noexcept {
return WUPSStorageAPI_GetFloat(parent, key, &outValue);
}
template<>
inline WUPSStorageError GetEx<double>(wups_storage_item parent, const char *key, double &outValue) noexcept {
return WUPSStorageAPI_GetDouble(parent, key, &outValue);
}
template<typename T>
inline WUPSStorageError GetEx(wups_storage_item parent, const char *key, T &outValue) noexcept {
static_assert(sizeof(T) == sizeof(uint32_t) && std::is_enum<T>::value, "T must be an enum of size sizeof(uint32_t)");
return WUPSStorageAPI_GetU32(parent, key, (uint32_t *) &outValue);
}
template<>
inline WUPSStorageError GetEx<std::vector<uint8_t>>(wups_storage_item parent, const char *key, std::vector<uint8_t> &outValue) noexcept {
uint32_t outSize = 0;
if (outValue.empty()) {
uint32_t resizeToSize = 0;
auto r = WUPSStorageAPI_GetItemSize(parent, key, &resizeToSize);
if (r == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(resizeToSize);
} else {
return r;
}
}
auto res = WUPSStorageAPI_GetBinary(parent, key, outValue.data(), outValue.size(), &outSize);
if (res == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(outSize);
} else {
outValue.resize(0);
}
return res;
}
template<>
inline WUPSStorageError GetEx<std::string>(wups_storage_item parent, const char *key, std::string &outValue) noexcept {
uint32_t outSize = 0;
if (outValue.empty()) {
uint32_t resizeToSize = 0;
auto r = WUPSStorageAPI_GetItemSize(parent, key, &resizeToSize);
if (r == WUPS_STORAGE_ERROR_SUCCESS) {
outValue.resize(resizeToSize);
} else {
return r;
}
}
auto res = WUPSStorageAPI_GetString(parent, key, outValue.data(), outValue.size(), &outSize);
if (res == WUPS_STORAGE_ERROR_SUCCESS) {
// outSize does count the null terminator as well, std::string's size() doesn't include a null terminator.
outValue.resize(strlen(outValue.c_str()));
} else {
outValue.resize(0);
}
return res;
}
template<typename T>
inline WUPSStorageError Get(const char *key, T &outValue) noexcept {
return GetEx(nullptr, key, outValue);
}
template<typename T>
inline WUPSStorageError StoreEx(wups_storage_item parent, const char *key, const T &value) noexcept;
template<>
inline WUPSStorageError StoreEx<int32_t>(wups_storage_item parent, const char *key, const int32_t &value) noexcept {
return WUPSStorageAPI_StoreS32(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<int64_t>(wups_storage_item parent, const char *key, const int64_t &value) noexcept {
return WUPSStorageAPI_StoreS64(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<uint32_t>(wups_storage_item parent, const char *key, const uint32_t &value) noexcept {
return WUPSStorageAPI_StoreU32(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<uint64_t>(wups_storage_item parent, const char *key, const uint64_t &value) noexcept {
return WUPSStorageAPI_StoreU64(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<bool>(wups_storage_item parent, const char *key, const bool &value) noexcept {
return WUPSStorageAPI_StoreBool(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<float>(wups_storage_item parent, const char *key, const float &value) noexcept {
return WUPSStorageAPI_StoreFloat(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<double>(wups_storage_item parent, const char *key, const double &value) noexcept {
return WUPSStorageAPI_StoreDouble(parent, key, value);
}
template<>
inline WUPSStorageError StoreEx<std::vector<uint8_t>>(wups_storage_item parent, const char *key, const std::vector<uint8_t> &value) noexcept {
return WUPSStorageAPI_StoreBinary(parent, key, value.data(), value.size());
}
template<>
inline WUPSStorageError StoreEx<std::string>(wups_storage_item parent, const char *key, const std::string &value) noexcept {
return WUPSStorageAPI_StoreString(parent, key, value.c_str());
}
template<typename T>
inline WUPSStorageError StoreEx(wups_storage_item parent, const char *key, const T &value) noexcept {
static_assert(sizeof(T) == sizeof(uint32_t) && std::is_enum<T>::value, "T must be an enum of size sizeof(uint32_t)");
return WUPSStorageAPI_StoreU32(parent, key, (uint32_t) value);
}
template<typename T>
inline WUPSStorageError Store(const char *key, const T &value) noexcept {
return StoreEx(nullptr, key, value);
}
template<typename T>
inline WUPSStorageError GetOrStoreDefaultEx(wups_storage_item parent, const char *key, T &outValue, const T &defaultValue) noexcept {
WUPSStorageError err = GetEx(parent, key, outValue);
if (err == WUPS_STORAGE_ERROR_NOT_FOUND) {
err = StoreEx(parent, key, defaultValue);
}
return err;
}
template<typename T>
inline WUPSStorageError GetOrStoreDefault(const char *key, T &outValue, const T &defaultValue) noexcept {
return GetOrStoreDefaultEx<T>(nullptr, key, outValue, defaultValue);
}
} // namespace WUPSStorageAPI
class WUPSStorageSubItem {
public:
explicit WUPSStorageSubItem(wups_storage_item handle) : mHandle(handle) {
}
virtual ~WUPSStorageSubItem() = default;
bool operator==(const WUPSStorageSubItem &rhs) const;
bool operator!=(const WUPSStorageSubItem &rhs) const;
template<typename T>
inline WUPSStorageError Store(const char *key, const T &value) noexcept {
return WUPSStorageAPI::StoreEx(mHandle, key, value);
}
template<typename T>
inline WUPSStorageError Get(const char *key, T &value) const noexcept {
return WUPSStorageAPI::GetEx(mHandle, key, value);
}
template<typename T>
inline WUPSStorageError GetOrStoreDefault(std::string_view key, T &outValue, const T &defaultValue) const noexcept {
return WUPSStorageAPI::GetOrStoreDefaultEx(mHandle, key.data(), outValue, defaultValue);
}
WUPSStorageError DeleteItem(std::string_view view) noexcept;
WUPSStorageError GetItemSize(std::string_view view, uint32_t &outSize) noexcept;
std::optional<WUPSStorageSubItem> CreateSubItem(std::string_view key, WUPSStorageError &err) noexcept;
std::optional<WUPSStorageSubItem> GetSubItem(std::string_view key, WUPSStorageError &err) const noexcept;
std::optional<WUPSStorageSubItem> GetOrCreateSubItem(std::string_view key, WUPSStorageError &err) noexcept;
[[nodiscard]] WUPSStorageSubItem CreateSubItem(std::string_view key);
[[nodiscard]] WUPSStorageSubItem GetSubItem(std::string_view key) const;
[[nodiscard]] WUPSStorageSubItem GetOrCreateSubItem(std::string_view key);
private:
wups_storage_item mHandle = {};
};
#endif

View File

@ -1,4 +1,5 @@
#include "wups/config/WUPSConfigItemBoolean.h"
#include "wups/config_api.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>

View File

@ -1,4 +1,5 @@
#include "wups/config/WUPSConfigItemIntegerRange.h"
#include "wups/config_api.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>

View File

@ -1,4 +1,5 @@
#include "wups/config/WUPSConfigItemMultipleValues.h"
#include "wups/config_api.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>

View File

@ -1,6 +1,8 @@
#include "wups/config/WUPSConfigItemStub.h"
#include "wups/config_api.h"
#include <cstdlib>
#include <cstring>
#include <wups/config.h>
static int32_t WUPSConfigItemStub_getEmptyTextValue(void *context, char *out_buf, int32_t out_size) {
memset(out_buf, 0, out_size);

View File

@ -2,27 +2,29 @@
#include <wups/storage.h>
struct wups_internal_functions_t {
OpenStorageFunction openfunction_ptr = nullptr;
CloseStorageFunction closefunction_ptr = nullptr;
DeleteItemFunction delete_item_function_ptr = nullptr;
CreateSubItemFunction create_sub_item_function_ptr = nullptr;
GetSubItemFunction get_sub_item_function_ptr = nullptr;
StoreItemFunction store_item_function_ptr = nullptr;
GetItemFunction get_item_function_ptr = nullptr;
GetItemSizeFunction get_item_size_function_ptr = nullptr;
char plugin_id[256]{};
wups_storage_root_item __storageroot_item = nullptr;
WUPSStorage_SaveFunction save_function_ptr = nullptr;
WUPSStorage_ForceReloadFunction force_reload_function_ptr = nullptr;
WUPSStorage_WipeStorageFunction wipe_storage_function_ptr = nullptr;
WUPSStorage_DeleteItemFunction delete_item_function_ptr = nullptr;
WUPSStorage_CreateSubItemFunction create_sub_item_function_ptr = nullptr;
WUPSStorage_GetSubItemFunction get_sub_item_function_ptr = nullptr;
WUPSStorage_StoreItemFunction store_item_function_ptr = nullptr;
WUPSStorage_GetItemFunction get_item_function_ptr = nullptr;
WUPSStorage_GetItemSizeFunction get_item_size_function_ptr = nullptr;
wups_storage_root_item __storageroot_item = nullptr;
};
static wups_internal_functions_t __internal_functions __attribute__((section(".data"))) = {};
WUPSStorageError WUPS_InitStorage(wups_loader_init_storage_args_t args) {
WUPSStorageError WUPSStorageAPI_InitInternal(wups_loader_init_storage_args_t args) {
if (args.version > WUPS_STORAGE_CUR_API_VERSION) {
__internal_functions = {};
return WUPS_STORAGE_ERROR_INVALID_VERSION;
return WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION;
}
__internal_functions.openfunction_ptr = args.open_storage_ptr;
__internal_functions.closefunction_ptr = args.close_storage_ptr;
__internal_functions.__storageroot_item = args.root_item;
__internal_functions.save_function_ptr = args.save_function_ptr;
__internal_functions.force_reload_function_ptr = args.force_reload_function_ptr;
__internal_functions.wipe_storage_function_ptr = args.wipe_storage_function_ptr;
__internal_functions.delete_item_function_ptr = args.delete_item_function_ptr;
__internal_functions.create_sub_item_function_ptr = args.create_sub_item_function_ptr;
__internal_functions.get_sub_item_function_ptr = args.get_sub_item_function_ptr;
@ -30,220 +32,127 @@ WUPSStorageError WUPS_InitStorage(wups_loader_init_storage_args_t args) {
__internal_functions.get_item_function_ptr = args.get_item_function_ptr;
__internal_functions.get_item_size_function_ptr = args.get_item_size_function_ptr;
strncpy(__internal_functions.plugin_id, args.plugin_id, sizeof(__internal_functions.plugin_id) - 1);
return WUPS_STORAGE_ERROR_SUCCESS;
}
const char *WUPS_GetStorageStatusStr(WUPSStorageError status) {
const char *WUPSStorageAPI_GetStatusStr(WUPSStorageError status) {
switch (status) {
case WUPS_STORAGE_ERROR_SUCCESS:
return "WUPS_STORAGE_ERROR_SUCCESS";
case WUPS_STORAGE_ERROR_INVALID_ARGS:
return "WUPS_STORAGE_ERROR_INVALID_ARGS";
case WUPS_STORAGE_ERROR_INVALID_POINTER:
return "WUPS_STORAGE_ERROR_INVALID_POINTER";
case WUPS_STORAGE_ERROR_INVALID_VERSION:
return "WUPS_STORAGE_ERROR_INVALID_VERSION";
case WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE:
return "WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE";
case WUPS_STORAGE_ERROR_NOT_FOUND:
return "WUPS_STORAGE_ERROR_NOT_FOUND";
case WUPS_STORAGE_ERROR_MALLOC_FAILED:
return "WUPS_STORAGE_ERROR_MALLOC_FAILED";
case WUPS_STORAGE_ERROR_ALREADY_OPENED:
return "WUPS_STORAGE_ERROR_ALREADY_OPENED";
case WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE:
return "WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE";
case WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL:
return "WUPS_STORAGE_ERROR_BUFFER_TOO_SMALL";
case WUPS_STORAGE_ERROR_ALREADY_EXISTS:
return "WUPS_STORAGE_ERROR_ALREADY_EXISTS";
case WUPS_STORAGE_ERROR_IO_ERROR:
return "WUPS_STORAGE_ERROR_IO_ERROR";
case WUPS_STORAGE_ERROR_NOT_FOUND:
return "WUPS_STORAGE_ERROR_NOT_FOUND";
case WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED:
return "WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED";
case WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION:
return "WUPS_STORAGE_ERROR_INTERNAL_INVALID_VERSION";
case WUPS_STORAGE_ERROR_UNKNOWN_ERROR:
return "WUPS_STORAGE_ERROR_UNKNOWN_ERROR";
}
return "WUPS_STORAGE_ERROR_UNKNOWN";
}
WUPSStorageError WUPS_OpenStorage(void) {
if (__internal_functions.openfunction_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
WUPSStorageError WUPSStorageAPI_SaveStorage(bool force) {
if (__internal_functions.save_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
auto res = __internal_functions.openfunction_ptr(__internal_functions.plugin_id, &__internal_functions.__storageroot_item);
if (res != WUPS_STORAGE_ERROR_SUCCESS) {
__internal_functions.__storageroot_item = nullptr;
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return res;
return __internal_functions.save_function_ptr(__internal_functions.__storageroot_item, force);
}
WUPSStorageError WUPS_CloseStorage(void) {
if (__internal_functions.closefunction_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
WUPSStorageError WUPSStorageAPI_ForceReloadStorage() {
if (__internal_functions.force_reload_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
auto res = __internal_functions.closefunction_ptr(__internal_functions.plugin_id);
if (res == WUPS_STORAGE_ERROR_SUCCESS) {
__internal_functions.__storageroot_item = nullptr;
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return res;
return __internal_functions.force_reload_function_ptr(__internal_functions.__storageroot_item);
}
WUPSStorageError WUPS_DeleteItem(wups_storage_item parent, const char *key) {
WUPSStorageError WUPSStorageAPI_WipeStorage() {
if (__internal_functions.wipe_storage_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.wipe_storage_function_ptr(__internal_functions.__storageroot_item);
}
WUPSStorageError WUPSStorageAPI_DeleteItem(wups_storage_item parent, const char *key) {
if (__internal_functions.delete_item_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.delete_item_function_ptr(__internal_functions.__storageroot_item, parent, key);
}
WUPSStorageError WUPS_CreateSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem) {
WUPSStorageError WUPSStorageAPI_CreateSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem) {
if (__internal_functions.create_sub_item_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.create_sub_item_function_ptr(__internal_functions.__storageroot_item, parent, key, outItem);
}
WUPSStorageError WUPS_GetSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem) {
WUPSStorageError WUPSStorageAPI_GetSubItem(wups_storage_item parent, const char *key, wups_storage_item *outItem) {
if (__internal_functions.get_sub_item_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.get_sub_item_function_ptr(__internal_functions.__storageroot_item, parent, key, outItem);
}
static inline WUPSStorageError WUPS_StoreItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t size) {
WUPSStorageError WUPSStorageAPI_StoreItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t size) {
if (__internal_functions.store_item_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.store_item_function_ptr(__internal_functions.__storageroot_item, parent, key, type, data, size);
}
WUPSStorageError WUPS_StoreString(wups_storage_item parent, const char *key, const char *value) {
if (value == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_STRING, (void *) value, strlen(value));
}
WUPSStorageError WUPS_StoreBool(wups_storage_item parent, const char *key, bool value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_BOOL, (void *) &value, sizeof(bool));
}
WUPSStorageError WUPS_StoreInt(wups_storage_item parent, const char *key, int32_t value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_S32, (void *) &value, sizeof(int32_t));
}
WUPSStorageError WUPS_StoreS32(wups_storage_item parent, const char *key, int32_t value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_S32, (void *) &value, sizeof(int32_t));
}
WUPSStorageError WUPS_StoreS64(wups_storage_item parent, const char *key, int64_t value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_S64, (void *) &value, sizeof(int64_t));
}
WUPSStorageError WUPS_StoreU32(wups_storage_item parent, const char *key, uint32_t value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_U32, (void *) &value, sizeof(uint32_t));
}
WUPSStorageError WUPS_StoreU64(wups_storage_item parent, const char *key, uint64_t value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_U64, (void *) &value, sizeof(uint64_t));
}
WUPSStorageError WUPS_StoreFloat(wups_storage_item parent, const char *key, float value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_FLOAT, (void *) &value, sizeof(float));
}
WUPSStorageError WUPS_StoreDouble(wups_storage_item parent, const char *key, double value) {
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_DOUBLE, (void *) &value, sizeof(double));
}
WUPSStorageError WUPS_StoreBinary(wups_storage_item parent, const char *key, const void *data, uint32_t size) {
WUPSStorageError WUPSStorageAPI_GetItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t maxSize, uint32_t *outSize) {
if (data == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_StoreItem(parent, key, WUPS_STORAGE_ITEM_BINARY, (void *) data, size);
}
static inline WUPSStorageError WUPS_GetItem(wups_storage_item parent, const char *key, WUPSStorageItemType type, void *data, uint32_t maxSize, uint32_t *outSize) {
if (__internal_functions.get_item_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
if (__internal_functions.__storageroot_item == nullptr) {
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.get_item_function_ptr(__internal_functions.__storageroot_item, parent, key, type, data, maxSize, outSize);
}
WUPSStorageError WUPS_GetString(wups_storage_item parent, const char *key, char *outString, uint32_t maxSize, uint32_t *outSize) {
if (outString == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_STRING, outString, maxSize, outSize);
}
WUPSStorageError WUPS_GetBool(wups_storage_item parent, const char *key, bool *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_BOOL, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetInt(wups_storage_item parent, const char *key, int32_t *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_S32, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetS32(wups_storage_item parent, const char *key, int32_t *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_S32, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetS64(wups_storage_item parent, const char *key, int64_t *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_S64, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetU32(wups_storage_item parent, const char *key, uint32_t *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_U32, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetU64(wups_storage_item parent, const char *key, uint64_t *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_U64, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetFloat(wups_storage_item parent, const char *key, float *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_FLOAT, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetDouble(wups_storage_item parent, const char *key, double *outValue) {
if (outValue == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_DOUBLE, outValue, sizeof(*outValue), nullptr);
}
WUPSStorageError WUPS_GetBinary(wups_storage_item parent, const char *key, void *outData, uint32_t maxSize, uint32_t *outValue) {
if (outData == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
return WUPS_GetItem(parent, key, WUPS_STORAGE_ITEM_BINARY, outData, maxSize, outValue);
}
WUPSStorageError WUPS_GetItemSize(wups_storage_item parent, const char *key, uint32_t *outSize) {
WUPSStorageError WUPSStorageAPI_GetItemSize(wups_storage_item parent, const char *key, uint32_t *outSize) {
if (outSize == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_ARGS;
}
if (__internal_functions.get_item_size_function_ptr == nullptr) {
return WUPS_STORAGE_ERROR_INVALID_POINTER;
return WUPS_STORAGE_ERROR_INTERNAL_NOT_INITIALIZED;
}
return __internal_functions.get_item_size_function_ptr(__internal_functions.__storageroot_item, parent, key, outSize);
}

View File

@ -0,0 +1,136 @@
#include <optional>
#include <stdexcept>
#include <string>
#include <wups/storage.h>
WUPSStorageError WUPSStorageAPI::DeleteItem(std::string_view key) noexcept {
WUPSStorageSubItem item(nullptr);
return item.DeleteItem(key);
}
WUPSStorageError WUPSStorageAPI::SaveStorage(bool forceSave) {
return WUPSStorageAPI_SaveStorage(forceSave);
}
WUPSStorageError WUPSStorageAPI::ForceReloadStorage() {
return WUPSStorageAPI_ForceReloadStorage();
}
WUPSStorageError WUPSStorageAPI::WipeStorage() {
return WUPSStorageAPI_WipeStorage();
}
std::optional<WUPSStorageSubItem> WUPSStorageAPI::CreateSubItem(std::string_view key, WUPSStorageError &err) noexcept {
WUPSStorageSubItem item(nullptr);
return item.CreateSubItem(key, err);
}
std::optional<WUPSStorageSubItem> WUPSStorageAPI::GetSubItem(std::string_view key, WUPSStorageError &err) noexcept {
WUPSStorageSubItem item(nullptr);
return item.GetSubItem(key, err);
}
std::optional<WUPSStorageSubItem> WUPSStorageAPI::GetOrCreateSubItem(std::string_view key, WUPSStorageError &err) noexcept {
WUPSStorageSubItem item(nullptr);
return item.GetOrCreateSubItem(key, err);
}
WUPSStorageSubItem WUPSStorageAPI::CreateSubItem(std::string_view key) {
WUPSStorageSubItem item(nullptr);
return item.CreateSubItem(key);
}
WUPSStorageSubItem WUPSStorageAPI::GetSubItem(std::string_view key) {
WUPSStorageSubItem item(nullptr);
return item.GetSubItem(key);
}
WUPSStorageSubItem WUPSStorageAPI::GetOrCreateSubItem(std::string_view key) {
WUPSStorageSubItem item(nullptr);
return item.GetOrCreateSubItem(key);
}
std::string_view WUPSStorageAPI::GetStatusStr(const WUPSStorageError &err) noexcept {
return WUPSStorageAPI_GetStatusStr(err);
}
WUPSStorageSubItem WUPSStorageAPI::GetRootItem() noexcept {
return WUPSStorageSubItem(nullptr);
}
WUPSStorageError WUPSStorageAPI::GetItemSize(std::string_view key, uint32_t &outSize) noexcept {
WUPSStorageSubItem item(nullptr);
return item.GetItemSize(key, outSize);
}
WUPSStorageError WUPSStorageSubItem::DeleteItem(std::string_view key) noexcept {
return WUPSStorageAPI_DeleteItem(mHandle, key.data());
}
WUPSStorageError WUPSStorageSubItem::GetItemSize(std::string_view key, uint32_t &outSize) noexcept {
return WUPSStorageAPI_GetItemSize(mHandle, key.data(), &outSize);
}
std::optional<WUPSStorageSubItem> WUPSStorageSubItem::CreateSubItem(std::string_view key, WUPSStorageError &err) noexcept {
wups_storage_item outItem = {};
err = WUPSStorageAPI_CreateSubItem(mHandle, key.data(), &outItem);
if (err != WUPS_STORAGE_ERROR_SUCCESS) {
return std::nullopt;
}
return WUPSStorageSubItem(outItem);
}
std::optional<WUPSStorageSubItem> WUPSStorageSubItem::GetSubItem(std::string_view key, WUPSStorageError &err) const noexcept {
wups_storage_item outItem = {};
err = WUPSStorageAPI_GetSubItem(mHandle, key.data(), &outItem);
if (err != WUPS_STORAGE_ERROR_SUCCESS) {
return std::nullopt;
}
return WUPSStorageSubItem(outItem);
}
std::optional<WUPSStorageSubItem> WUPSStorageSubItem::GetOrCreateSubItem(std::string_view key, WUPSStorageError &err) noexcept {
wups_storage_item outItem = {};
err = WUPSStorageAPI_GetSubItem(mHandle, key.data(), &outItem);
if (err == WUPS_STORAGE_ERROR_NOT_FOUND) {
return CreateSubItem(key);
}
return WUPSStorageSubItem(outItem);
}
WUPSStorageSubItem WUPSStorageSubItem::CreateSubItem(std::string_view key) {
WUPSStorageError err;
auto res = CreateSubItem(key, err);
if (!res) {
throw std::runtime_error(std::string("WUPSStorageSubItem::CreateSubItem(\"").append(key).append("\")").append(WUPSStorageAPI_GetStatusStr(err)));
}
return *res;
}
WUPSStorageSubItem WUPSStorageSubItem::GetSubItem(std::string_view key) const {
WUPSStorageError err;
auto res = GetSubItem(key, err);
if (!res) {
throw std::runtime_error(std::string("WUPSStorageSubItem::GetSubItem(\"").append(key).append("\")").append(WUPSStorageAPI_GetStatusStr(err)));
}
return *res;
}
WUPSStorageSubItem WUPSStorageSubItem::GetOrCreateSubItem(std::string_view key) {
WUPSStorageError err;
auto res = GetOrCreateSubItem(key, err);
if (!res) {
throw std::runtime_error(std::string("WUPSStorageSubItem::GetOrCreateSubItem(\"").append(key).append("\")").append(WUPSStorageAPI_GetStatusStr(err)));
}
return *res;
}
bool WUPSStorageSubItem::operator==(const WUPSStorageSubItem &rhs) const {
return mHandle == rhs.mHandle;
}
bool WUPSStorageSubItem::operator!=(const WUPSStorageSubItem &rhs) const {
return !(rhs == *this);
}

View File

@ -38,16 +38,10 @@ void logFSOpenChanged(ConfigItemBoolean *item, bool newValue) {
DEBUG_FUNCTION_LINE_INFO("New value in logFSOpenChanged: %d", newValue);
logFSOpen = newValue;
// If the value has changed, we store it in the storage.
WUPS_StoreInt(NULL, LOG_FS_OPEN_CONFIG_ID, logFSOpen);
WUPSStorageAPI_StoreBool(NULL, LOG_FS_OPEN_CONFIG_ID, logFSOpen);
}
WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle root) {
// We open the storage, so we can persist the configuration the user did.
if (WUPS_OpenStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to open storage");
return WUPSCONFIG_API_CALLBACK_RESULT_ERROR;
}
{
// Let's create a new category called "Settings"
WUPSConfigCategoryHandle settingsCategory;
@ -131,10 +125,7 @@ WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle ro
}
void ConfigMenuClosedCallback() {
// Save all changes
if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
}
WUPSStorageAPI_SaveStorage(false);
}
/**
@ -150,26 +141,21 @@ INITIALIZE_PLUGIN() {
DEBUG_FUNCTION_LINE_ERR("Failed to init config api");
}
// Open storage to read values
WUPSStorageError storageRes = WUPS_OpenStorage();
if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to open storage %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes);
} else {
// Try to get value from storage
if ((storageRes = WUPS_GetBool(NULL, LOG_FS_OPEN_CONFIG_ID, &logFSOpen)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
// Add the value to the storage if it's missing.
if (WUPS_StoreBool(NULL, LOG_FS_OPEN_CONFIG_ID, logFSOpen) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to store bool");
}
} else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to get bool %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes);
}
WUPSStorageError storageRes;
// Try to get value from storage
if ((storageRes = WUPSStorageAPI_GetBool(NULL, LOG_FS_OPEN_CONFIG_ID, &logFSOpen)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
// Close storage
if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to close storage");
// Add the value to the storage if it's missing.
if (WUPSStorageAPI_StoreBool(NULL, LOG_FS_OPEN_CONFIG_ID, logFSOpen) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to store bool");
}
} else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to get bool %s (%d)", WUPSConfigAPI_GetStatusStr(storageRes), storageRes);
} else {
DEBUG_FUNCTION_LINE_ERR("Successfully read the value from storage: %d %s (%d)", logFSOpen, WUPSConfigAPI_GetStatusStr(storageRes), storageRes);
}
WUPSStorageAPI_SaveStorage(false);
deinitLogging();
}

View File

@ -55,7 +55,7 @@ void boolItemChanged(ConfigItemBoolean *item, bool newValue) {
if (std::string_view(LOG_FS_OPEN_CONFIG_ID) == item->identifier) {
sLogFSOpen = newValue;
// If the value has changed, we store it in the storage.
WUPS_StoreInt(nullptr, item->identifier, newValue);
WUPSStorageAPI::Store(item->identifier, newValue);
} else if (std::string_view(OTHER_EXAMPLE_BOOL_CONFIG_ID) == item->identifier) {
DEBUG_FUNCTION_LINE_ERR("Other bool value has changed to %d", newValue);
} else if (std::string_view(OTHER_EXAMPLE2_BOOL_CONFIG_ID) == item->identifier) {
@ -69,7 +69,7 @@ void integerRangeItemChanged(ConfigItemIntegerRange *item, int newValue) {
if (std::string_view(LOG_FS_OPEN_CONFIG_ID) == item->identifier) {
sIntegerRangeValue = newValue;
// If the value has changed, we store it in the storage.
WUPS_StoreInt(nullptr, item->identifier, newValue);
WUPSStorageAPI::Store(item->identifier, newValue);
}
}
@ -79,16 +79,11 @@ void multipleValueItemChanged(ConfigItemIntegerRange *item, uint32_t newValue) {
if (std::string_view(MULTIPLE_VALUES_EXAMPLE_CONFIG_ID) == item->identifier) {
sExampleOptionValue = (ExampleOptions) newValue;
// If the value has changed, we store it in the storage.
WUPS_StoreInt(nullptr, item->identifier, sExampleOptionValue);
WUPSStorageAPI::Store(item->identifier, sExampleOptionValue);
}
}
WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) {
// We open the storage, so we can persist the configuration the user did.
if (WUPS_OpenStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to open storage");
return WUPSCONFIG_API_CALLBACK_RESULT_ERROR;
}
// To use the C++ API, we create new WUPSConfigCategory from the root handle!
WUPSConfigCategory root = WUPSConfigCategory(rootHandle);
@ -189,10 +184,7 @@ WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle ro
}
void ConfigMenuClosedCallback() {
// Save all changes
if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to close storage");
}
WUPSStorageAPI::SaveStorage();
}
/**
@ -203,31 +195,19 @@ INITIALIZE_PLUGIN() {
initLogging();
DEBUG_FUNCTION_LINE("INITIALIZE_PLUGIN of example_plugin!");
WUPSConfigAPIOptionsV1 configOptions = {.name = "example_plugin"};
WUPSConfigAPIOptionsV1 configOptions = {.name = "example_plugin_cpp"};
if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("Failed to init config api");
}
// Open storage to read values
WUPSStorageError storageRes = WUPS_OpenStorage();
if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to open storage %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes);
} else {
// Try to get value from storage
if ((storageRes = WUPS_GetBool(nullptr, LOG_FS_OPEN_CONFIG_ID, &sLogFSOpen)) == WUPS_STORAGE_ERROR_NOT_FOUND) {
// Add the value to the storage if it's missing.
if (WUPS_StoreBool(nullptr, LOG_FS_OPEN_CONFIG_ID, sLogFSOpen) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to store bool");
}
} else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to get bool %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes);
}
// Close storage
if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE("Failed to close storage");
}
WUPSStorageError storageRes;
if ((storageRes = WUPSStorageAPI::GetOrStoreDefault(LOG_FS_OPEN_CONFIG_ID, sLogFSOpen, LOF_FS_OPEN_DEFAULT_VALUE)) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("GetOrStoreDefault failed: %s (%d)", WUPSStorageAPI_GetStatusStr(storageRes), storageRes);
}
if ((storageRes = WUPSStorageAPI::SaveStorage()) != WUPS_STORAGE_ERROR_SUCCESS) {
DEBUG_FUNCTION_LINE_ERR("GetOrStoreDefault failed: %s (%d)", WUPSStorageAPI_GetStatusStr(storageRes), storageRes);
}
deinitLogging();
}

File diff suppressed because it is too large Load Diff