Skip to content

Commit

Permalink
StorageAPI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Dec 17, 2023
1 parent 104fdc3 commit 3236f31
Show file tree
Hide file tree
Showing 12 changed files with 912 additions and 928 deletions.
1 change: 0 additions & 1 deletion include/wups/config/WUPSConfigItemBoolean.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "WUPSConfigItem.h"
#include <wups/config.h>

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion include/wups/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
}


Expand Down
449 changes: 382 additions & 67 deletions include/wups/storage.h

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions libraries/libwups/WUPSConfigItemBoolean.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wups/config/WUPSConfigItemBoolean.h"
#include "wups/config_api.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down
1 change: 1 addition & 0 deletions libraries/libwups/WUPSConfigItemIntegerRange.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wups/config/WUPSConfigItemIntegerRange.h"
#include "wups/config_api.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down
1 change: 1 addition & 0 deletions libraries/libwups/WUPSConfigItemMultipleValues.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wups/config/WUPSConfigItemMultipleValues.h"
#include "wups/config_api.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
Expand Down
2 changes: 2 additions & 0 deletions libraries/libwups/WUPSConfigItemStub.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
243 changes: 76 additions & 167 deletions libraries/libwups/storage.cpp

Large diffs are not rendered by default.

136 changes: 136 additions & 0 deletions libraries/libwups/storageCPP.cpp
Original file line number Diff line number Diff line change
@@ -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{""};
}
return *res;
}

WUPSStorageSubItem WUPSStorageSubItem::GetSubItem(std::string_view key) const {
WUPSStorageError err;
auto res = GetSubItem(key, err);
if (!res) {
throw std::runtime_error{""};
}
return *res;
}

WUPSStorageSubItem WUPSStorageSubItem::GetOrCreateSubItem(std::string_view key) {
WUPSStorageError err;
auto res = GetOrCreateSubItem(key, err);
if (!res) {
throw std::runtime_error{""};
}
return *res;
}

bool WUPSStorageSubItem::operator==(const WUPSStorageSubItem &rhs) const {
return mHandle == rhs.mHandle;
}

bool WUPSStorageSubItem::operator!=(const WUPSStorageSubItem &rhs) const {
return !(rhs == *this);
}
42 changes: 14 additions & 28 deletions plugins/example_plugin/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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();
}

Expand Down
44 changes: 12 additions & 32 deletions plugins/example_plugin_cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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);

Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

Expand Down
Loading

0 comments on commit 3236f31

Please sign in to comment.