From e251a62c023906c3876043ea0edabb948e284c3a Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 23 Mar 2024 19:15:29 +0100 Subject: [PATCH] Refactor PluginUtils to not return shared_ptr anymore --- .clang-format | 2 +- include/wups_backend/PluginContainer.h | 18 +- include/wups_backend/PluginData.h | 24 ++- include/wups_backend/PluginMetaInformation.h | 122 +++++--------- include/wups_backend/PluginUtils.h | 22 ++- source/PluginContainer.cpp | 9 +- source/PluginData.cpp | 22 ++- source/PluginMetaInformation.cpp | 28 ++-- source/PluginUtils.cpp | 163 ++++++++----------- 9 files changed, 187 insertions(+), 223 deletions(-) diff --git a/.clang-format b/.clang-format index 56cc685..34c35a0 100644 --- a/.clang-format +++ b/.clang-format @@ -42,7 +42,7 @@ IndentPPDirectives: None IndentWidth: 4 KeepEmptyLinesAtTheStartOfBlocks: true MaxEmptyLinesToKeep: 2 -NamespaceIndentation: All +NamespaceIndentation: None ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: true PointerAlignment: Right diff --git a/include/wups_backend/PluginContainer.h b/include/wups_backend/PluginContainer.h index 3c440a1..62b2beb 100644 --- a/include/wups_backend/PluginContainer.h +++ b/include/wups_backend/PluginContainer.h @@ -25,17 +25,17 @@ #include "PluginMetaInformation.h" namespace WUPSBackend { - class PluginContainer { +class PluginContainer { - public: - PluginContainer(std::shared_ptr data, std::shared_ptr metaInfo); +public: + PluginContainer(PluginData data, PluginMetaInformation metaInfo); - [[nodiscard]] const std::shared_ptr &getMetaInformation() const; + [[nodiscard]] const PluginMetaInformation &getMetaInformation() const; - [[nodiscard]] const std::shared_ptr &getPluginData() const; + [[nodiscard]] const PluginData &getPluginData() const; - private: - const std::shared_ptr pluginData; - const std::shared_ptr metaInformation; - }; +private: + PluginData pluginData; + PluginMetaInformation metaInformation; +}; } // namespace WUPSBackend \ No newline at end of file diff --git a/include/wups_backend/PluginData.h b/include/wups_backend/PluginData.h index 7941bd7..0e59690 100644 --- a/include/wups_backend/PluginData.h +++ b/include/wups_backend/PluginData.h @@ -20,16 +20,22 @@ #include namespace WUPSBackend { - class PluginData { +class PluginData { - public: - explicit PluginData(uint32_t handle); - ~PluginData(); +public: + explicit PluginData(uint32_t handle); - [[nodiscard]] uint32_t getHandle() const { - return handle; - } + PluginData(const PluginData &) = delete; - uint32_t handle; - }; + PluginData(PluginData &&src) noexcept; + + ~PluginData(); + + PluginData &operator=(PluginData &&src) noexcept; + + [[nodiscard]] uint32_t getHandle() const; + +private: + uint32_t mHandle; +}; } // namespace WUPSBackend \ No newline at end of file diff --git a/include/wups_backend/PluginMetaInformation.h b/include/wups_backend/PluginMetaInformation.h index 37fec76..95cbb5e 100644 --- a/include/wups_backend/PluginMetaInformation.h +++ b/include/wups_backend/PluginMetaInformation.h @@ -22,91 +22,59 @@ #include namespace WUPSBackend { - class PluginMetaInformation { - public: - [[nodiscard]] const std::string &getName() const { - return name; - } +class PluginMetaInformation { +public: + [[nodiscard]] const std::string &getName() const { + return name; + } - [[nodiscard]] const std::string &getAuthor() const { - return this->author; - } + [[nodiscard]] const std::string &getAuthor() const { + return this->author; + } - [[nodiscard]] const std::string &getVersion() const { - return this->version; - } + [[nodiscard]] const std::string &getVersion() const { + return this->version; + } - [[nodiscard]] const std::string &getLicense() const { - return this->license; - } + [[nodiscard]] const std::string &getLicense() const { + return this->license; + } - [[nodiscard]] const std::string &getBuildTimestamp() const { - return this->buildtimestamp; - } + [[nodiscard]] const std::string &getBuildTimestamp() const { + return this->buildtimestamp; + } - [[nodiscard]] const std::string &getDescription() const { - return this->description; - } + [[nodiscard]] const std::string &getDescription() const { + return this->description; + } - [[nodiscard]] const std::string &getStorageId() const { - return this->storageId; - } + [[nodiscard]] const std::string &getStorageId() const { + return this->storageId; + } - [[nodiscard]] size_t getSize() const { - return this->size; - } + [[nodiscard]] size_t getSize() const { + return this->size; + } - PluginMetaInformation(std::string name, - std::string author, - std::string version, - std::string license, - std::string buildtimestamp, - std::string description, - std::string storageId, - size_t size); + PluginMetaInformation(std::string_view name, + std::string_view author, + std::string_view version, + std::string_view license, + std::string_view buildtimestamp, + std::string_view description, + std::string_view storageId, + size_t size); - private: - PluginMetaInformation() = default; +private: + PluginMetaInformation() = default; - void setName(std::string name_) { - this->name = std::move(name_); - } - - void setAuthor(std::string author_) { - this->author = std::move(author_); - } - - void setVersion(std::string version_) { - this->version = std::move(version_); - } - - void setLicense(std::string license_) { - this->license = std::move(license_); - } - - void setBuildTimestamp(std::string buildtimestamp_) { - this->buildtimestamp = std::move(buildtimestamp_); - } - - void setDescription(std::string description_) { - this->description = std::move(description_); - } - - void setStorageId(std::string storageId_) { - this->storageId = std::move(storageId_); - } - - void setSize(size_t size_) { - this->size = size_; - } - - std::string name; - std::string author; - std::string version; - std::string license; - std::string buildtimestamp; - std::string description; - std::string storageId; - size_t size{}; - }; + std::string name; + std::string author; + std::string version; + std::string license; + std::string buildtimestamp; + std::string description; + std::string storageId; + size_t size{}; +}; } // namespace WUPSBackend \ No newline at end of file diff --git a/include/wups_backend/PluginUtils.h b/include/wups_backend/PluginUtils.h index b48f47d..364c734 100644 --- a/include/wups_backend/PluginUtils.h +++ b/include/wups_backend/PluginUtils.h @@ -21,18 +21,22 @@ #include namespace WUPSBackend { - class PluginUtils { - public: - static std::optional> getMetaInformationForBuffer(char *buffer, size_t size); - static std::optional> getMetaInformationForPath(const std::string &path); +const char *GetStatusStr(PluginBackendApiErrorType err); - static std::vector> getLoadedPlugins(uint32_t maxSize); +namespace PluginUtils { - static std::optional> getPluginForPath(const std::string &path); +std::optional getMetaInformationForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err); - static std::optional> getPluginForBuffer(char *buffer, size_t size); +std::optional getMetaInformationForPath(const std::string &path, PluginBackendApiErrorType &err); - static int32_t LoadAndLinkOnRestart(const std::vector> &plugins); - }; +std::vector getLoadedPlugins(PluginBackendApiErrorType &err); + +std::optional getPluginForPath(const std::string &path, PluginBackendApiErrorType &err); + +std::optional getPluginForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err); + +PluginBackendApiErrorType LoadAndLinkOnRestart(const std::vector &plugins); + +} // namespace PluginUtils } // namespace WUPSBackend \ No newline at end of file diff --git a/source/PluginContainer.cpp b/source/PluginContainer.cpp index 6fc99a3..6e26225 100644 --- a/source/PluginContainer.cpp +++ b/source/PluginContainer.cpp @@ -3,15 +3,14 @@ using namespace WUPSBackend; - -PluginContainer::PluginContainer(std::shared_ptr data, std::shared_ptr metaInfo) : pluginData(std::move(data)), - metaInformation(std::move(metaInfo)) { +PluginContainer::PluginContainer(PluginData data, PluginMetaInformation metaInfo) : pluginData(std::move(data)), + metaInformation(std::move(metaInfo)) { } -[[nodiscard]] const std::shared_ptr &PluginContainer::getMetaInformation() const { +[[nodiscard]] const PluginMetaInformation &PluginContainer::getMetaInformation() const { return this->metaInformation; } -[[nodiscard]] const std::shared_ptr &PluginContainer::getPluginData() const { +[[nodiscard]] const PluginData &PluginContainer::getPluginData() const { return pluginData; } diff --git a/source/PluginData.cpp b/source/PluginData.cpp index fc965cc..ed1470a 100644 --- a/source/PluginData.cpp +++ b/source/PluginData.cpp @@ -23,13 +23,29 @@ using namespace WUPSBackend; PluginData::PluginData(uint32_t handle) { - this->handle = handle; + this->mHandle = handle; +} + +PluginData::PluginData(PluginData &&src) noexcept : mHandle(src.mHandle) { + src.mHandle = {}; } PluginData::~PluginData() { - if (handle != 0) { - if (WUPSBackend_DeletePluginData(&handle, 1) != PLUGIN_BACKEND_API_ERROR_NONE) { + if (mHandle != 0) { + if (WUPSBackend_DeletePluginData(&mHandle, 1) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("Failed to delete plugin data"); } } } + +PluginData &PluginData::operator=(PluginData &&src) noexcept { + if (this != &src) { + this->mHandle = src.mHandle; + src.mHandle = {}; + } + return *this; +} + +uint32_t PluginData::getHandle() const { + return mHandle; +} diff --git a/source/PluginMetaInformation.cpp b/source/PluginMetaInformation.cpp index 97fb90a..492f9db 100644 --- a/source/PluginMetaInformation.cpp +++ b/source/PluginMetaInformation.cpp @@ -22,20 +22,20 @@ using namespace WUPSBackend; -PluginMetaInformation::PluginMetaInformation(std::string name, - std::string author, - std::string version, - std::string license, - std::string buildtimestamp, - std::string description, - std::string storageId, +PluginMetaInformation::PluginMetaInformation(std::string_view name, + std::string_view author, + std::string_view version, + std::string_view license, + std::string_view buildtimestamp, + std::string_view description, + std::string_view storageId, size_t size) { - this->name = std::move(name); - this->author = std::move(author); + this->name = name; + this->author = author; this->size = size; - this->buildtimestamp = std::move(buildtimestamp); - this->description = std::move(description); - this->license = std::move(license); - this->version = std::move(version); - this->storageId = std::move(storageId); + this->buildtimestamp = buildtimestamp; + this->description = description; + this->license = license; + this->version = version; + this->storageId = storageId; } diff --git a/source/PluginUtils.cpp b/source/PluginUtils.cpp index 39f5841..43aa571 100644 --- a/source/PluginUtils.cpp +++ b/source/PluginUtils.cpp @@ -22,110 +22,92 @@ #include #include -using namespace WUPSBackend; +namespace WUPSBackend { -std::optional> getMetaInformation(const wups_backend_plugin_information &info) { +const char *GetStatusStr(PluginBackendApiErrorType status) { + return WUPSBackend_GetStatusStr(status); +} + +namespace PluginUtils { +static std::optional getMetaInformation(const wups_backend_plugin_information &info, PluginBackendApiErrorType &err) { if (info.plugin_information_version != WUPS_BACKEND_PLUGIN_INFORMATION_VERSION) { + err = PLUGIN_BACKEND_API_ERROR_UNSUPPORTED_VERSION; DEBUG_FUNCTION_LINE_ERR("Version mismatch"); return {}; } - auto res = make_unique_nothrow(info.name, - info.author, - info.version, - info.license, - info.buildTimestamp, - info.description, - info.storageId, - info.size); - if (!res) { - DEBUG_FUNCTION_LINE_ERR("Not enough memory"); - return {}; - } - return res; + return PluginMetaInformation(info.name, + info.author, + info.version, + info.license, + info.buildTimestamp, + info.description, + info.storageId, + info.size); } -std::optional> PluginUtils::getMetaInformationForBuffer(char *buffer, size_t size) { - wups_backend_plugin_information info; - if (WUPSBackend_GetPluginMetaInformationByBuffer(&info, buffer, size) != PLUGIN_BACKEND_API_ERROR_NONE) { +std::optional getMetaInformationForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err) { + wups_backend_plugin_information info = {}; + if ((err = WUPSBackend_GetPluginMetaInformationByBuffer(&info, buffer, size)) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("Failed to load meta infos for buffer %08X with size %08X", buffer, size); return {}; } - - return getMetaInformation(info); + return getMetaInformation(info, err); } -std::optional> PluginUtils::getMetaInformationForPath(const std::string &path) { +std::optional getMetaInformationForPath(const std::string &path, PluginBackendApiErrorType &err) { wups_backend_plugin_information info = {}; - if (WUPSBackend_GetPluginMetaInformationByPath(&info, path.c_str()) != PLUGIN_BACKEND_API_ERROR_NONE) { + if ((err = WUPSBackend_GetPluginMetaInformationByPath(&info, path.c_str())) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("Failed to load meta infos for %s", path.c_str()); return {}; } - - return getMetaInformation(info); + return getMetaInformation(info, err); } -std::optional> PluginUtils::getPluginForPath(const std::string &path) { - auto metaInfoOpt = PluginUtils::getMetaInformationForPath(path); +std::optional getPluginForPath(const std::string &path, PluginBackendApiErrorType &err) { + auto metaInfoOpt = getMetaInformationForPath(path, err); if (!metaInfoOpt) { DEBUG_FUNCTION_LINE_ERR("Failed to get MetaInformation for path %s", path.c_str()); return {}; } wups_backend_plugin_data_handle dataHandle; - if (WUPSBackend_LoadPluginAsDataByPath(&dataHandle, path.c_str()) != PLUGIN_BACKEND_API_ERROR_NONE) { + if ((err = WUPSBackend_LoadPluginAsDataByPath(&dataHandle, path.c_str())) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("WUPSLoadPluginAsDataByPath failed for path %s", path.c_str()); return {}; } - auto pluginData = make_shared_nothrow(dataHandle); - if (!pluginData) { - DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginData"); - return {}; - } - - auto pluginContainer = make_unique_nothrow(std::move(pluginData), std::move(metaInfoOpt.value())); - if (!pluginContainer) { - DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginContainer"); - return {}; - } - - return pluginContainer; + return PluginContainer(PluginData(dataHandle), std::move(metaInfoOpt.value())); } -std::optional> PluginUtils::getPluginForBuffer(char *buffer, size_t size) { - auto metaInfoOpt = PluginUtils::getMetaInformationForBuffer(buffer, size); +std::optional getPluginForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err) { + auto metaInfoOpt = getMetaInformationForBuffer(buffer, size, err); if (!metaInfoOpt) { DEBUG_FUNCTION_LINE_ERR("Failed to get MetaInformation for buffer %08X (%d bytes)", buffer, size); return {}; } wups_backend_plugin_data_handle dataHandle; - if (WUPSBackend_LoadPluginAsDataByBuffer(&dataHandle, buffer, size) != PLUGIN_BACKEND_API_ERROR_NONE) { + if ((err = WUPSBackend_LoadPluginAsDataByBuffer(&dataHandle, buffer, size)) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("WUPSLoadPluginAsDataByBuffer failed for buffer %08X (%d bytes)", buffer, size); return {}; } - auto pluginData = make_shared_nothrow(dataHandle); - if (!pluginData) { - DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginData"); - return {}; - } - - auto pluginContainer = make_unique_nothrow(std::move(pluginData), std::move(metaInfoOpt.value())); - if (!pluginContainer) { - DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginContainer"); - return {}; - } - - return pluginContainer; + return PluginContainer(PluginData(dataHandle), std::move(metaInfoOpt.value())); } +std::vector getLoadedPlugins(PluginBackendApiErrorType &err) { + std::vector result; -std::vector> PluginUtils::getLoadedPlugins(uint32_t maxSize) { - std::vector> result; + PluginBackendApiErrorType err2; + uint32_t maxSize; + if ((err2 = WUPSBackend_GetNumberOfLoadedPlugins(&maxSize)) != PLUGIN_BACKEND_API_ERROR_NONE) { + DEBUG_FUNCTION_LINE_WARN("Failed to get number of loaded plugins: %s", WUPSBackend_GetStatusStr(err2)); + maxSize = 120; + } auto handles = make_unique_nothrow(maxSize); if (!handles) { + err = PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC; DEBUG_FUNCTION_LINE_ERR("Not enough memory"); return result; } @@ -136,87 +118,76 @@ std::vector> PluginUtils::getLoadedPlugins(uint } uint32_t plugin_information_version = 0; - - if (WUPSBackend_GetLoadedPlugins(handles.get(), maxSize, &realSize, &plugin_information_version) != PLUGIN_BACKEND_API_ERROR_NONE) { + if ((err = WUPSBackend_GetLoadedPlugins(handles.get(), maxSize, &realSize, &plugin_information_version)) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("WUPSGetLoadedPlugins: Failed"); return result; } if (realSize == 0 || plugin_information_version != WUPS_BACKEND_PLUGIN_INFORMATION_VERSION) { + err = PLUGIN_BACKEND_API_ERROR_UNSUPPORTED_VERSION; DEBUG_FUNCTION_LINE_ERR("realSize is 0 or version mismatch"); return result; } auto dataHandles = make_unique_nothrow(realSize); if (!dataHandles) { + err = PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC; DEBUG_FUNCTION_LINE_ERR("Not enough memory"); return result; } - if (WUPSBackend_GetPluginDataForContainerHandles(handles.get(), dataHandles.get(), realSize) != PLUGIN_BACKEND_API_ERROR_NONE) { + if ((err = WUPSBackend_GetPluginDataForContainerHandles(handles.get(), dataHandles.get(), realSize)) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("Failed to get plugin data"); return result; } auto information = make_unique_nothrow(realSize); if (!information) { + err = PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC; DEBUG_FUNCTION_LINE_ERR("Not enough memory"); return result; } - if (WUPSBackend_GetMetaInformation(handles.get(), information.get(), realSize) != PLUGIN_BACKEND_API_ERROR_NONE) { + if ((err = WUPSBackend_GetMetaInformation(handles.get(), information.get(), realSize)) != PLUGIN_BACKEND_API_ERROR_NONE) { DEBUG_FUNCTION_LINE_ERR("Failed to get meta information for handles"); return result; } for (uint32_t i = 0; i < realSize; i++) { if (information[i].plugin_information_version != WUPS_BACKEND_PLUGIN_INFORMATION_VERSION) { + err = PLUGIN_BACKEND_API_ERROR_UNSUPPORTED_VERSION; DEBUG_FUNCTION_LINE_ERR("Skip, wrong struct version."); - continue; + return {}; } - auto metaInfo = make_shared_nothrow(information[i].name, - information[i].author, - information[i].version, - information[i].license, - information[i].buildTimestamp, - information[i].description, - information[i].storageId, - information[i].size); - - if (!metaInfo) { - DEBUG_FUNCTION_LINE_ERR("Skip, failed to allocate MetaInformation"); - continue; - } - - auto pluginData = std::make_shared((uint32_t) dataHandles[i]); - if (!pluginData) { - DEBUG_FUNCTION_LINE_ERR("Skip, failed to allocate PluginData"); - continue; - } - - auto pluginContainer = make_unique_nothrow(std::move(pluginData), std::move(metaInfo)); - if (!pluginContainer) { - DEBUG_FUNCTION_LINE_ERR("Skip, failed to allocate PluginContainer"); - continue; - } - - result.push_back(std::move(pluginContainer)); + result.emplace_back(PluginData(dataHandles[i]), PluginMetaInformation(information[i].name, + information[i].author, + information[i].version, + information[i].license, + information[i].buildTimestamp, + information[i].description, + information[i].storageId, + information[i].size)); } return result; } -int32_t PluginUtils::LoadAndLinkOnRestart(const std::vector> &plugins) { +PluginBackendApiErrorType LoadAndLinkOnRestart(const std::vector &plugins) { uint32_t dataSize = plugins.size(); - wups_backend_plugin_data_handle handles[dataSize]; + auto handles = make_unique_nothrow(dataSize); + if (!handles) { + return PLUGIN_BACKEND_API_ERROR_FAILED_ALLOC; + } int i = 0; - for (auto &plugin : plugins) { - const wups_backend_plugin_data_handle &handle = plugin->getPluginData()->getHandle(); + for (const auto &plugin : plugins) { + const auto &handle = plugin.getPluginData().getHandle(); if (handle == 0) { - dataSize--; + return PLUGIN_BACKEND_API_INVALID_HANDLE; } else { handles[i] = handle; i++; } } - return WUPSBackend_LoadAndLinkByDataHandle(handles, dataSize); + return WUPSBackend_LoadAndLinkByDataHandle(handles.get(), dataSize); } +} // namespace PluginUtils +} // namespace WUPSBackend \ No newline at end of file