Refactor PluginUtils to not return shared_ptr anymore

This commit is contained in:
Maschell 2024-03-23 19:15:29 +01:00
parent 2f137293b9
commit e251a62c02
9 changed files with 187 additions and 223 deletions

View File

@ -42,7 +42,7 @@ IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right

View File

@ -25,17 +25,17 @@
#include "PluginMetaInformation.h"
namespace WUPSBackend {
class PluginContainer {
class PluginContainer {
public:
PluginContainer(std::shared_ptr<PluginData> data, std::shared_ptr<PluginMetaInformation> metaInfo);
public:
PluginContainer(PluginData data, PluginMetaInformation metaInfo);
[[nodiscard]] const std::shared_ptr<PluginMetaInformation> &getMetaInformation() const;
[[nodiscard]] const PluginMetaInformation &getMetaInformation() const;
[[nodiscard]] const std::shared_ptr<PluginData> &getPluginData() const;
[[nodiscard]] const PluginData &getPluginData() const;
private:
const std::shared_ptr<PluginData> pluginData;
const std::shared_ptr<PluginMetaInformation> metaInformation;
};
private:
PluginData pluginData;
PluginMetaInformation metaInformation;
};
} // namespace WUPSBackend

View File

@ -20,16 +20,22 @@
#include <wups_backend/import_defines.h>
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

View File

@ -22,91 +22,59 @@
#include <vector>
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

View File

@ -21,18 +21,22 @@
#include <optional>
namespace WUPSBackend {
class PluginUtils {
public:
static std::optional<std::unique_ptr<PluginMetaInformation>> getMetaInformationForBuffer(char *buffer, size_t size);
static std::optional<std::unique_ptr<PluginMetaInformation>> getMetaInformationForPath(const std::string &path);
const char *GetStatusStr(PluginBackendApiErrorType err);
static std::vector<std::unique_ptr<PluginContainer>> getLoadedPlugins(uint32_t maxSize);
namespace PluginUtils {
static std::optional<std::unique_ptr<PluginContainer>> getPluginForPath(const std::string &path);
std::optional<PluginMetaInformation> getMetaInformationForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err);
static std::optional<std::unique_ptr<PluginContainer>> getPluginForBuffer(char *buffer, size_t size);
std::optional<PluginMetaInformation> getMetaInformationForPath(const std::string &path, PluginBackendApiErrorType &err);
static int32_t LoadAndLinkOnRestart(const std::vector<std::unique_ptr<PluginContainer>> &plugins);
};
std::vector<PluginContainer> getLoadedPlugins(PluginBackendApiErrorType &err);
std::optional<PluginContainer> getPluginForPath(const std::string &path, PluginBackendApiErrorType &err);
std::optional<PluginContainer> getPluginForBuffer(char *buffer, size_t size, PluginBackendApiErrorType &err);
PluginBackendApiErrorType LoadAndLinkOnRestart(const std::vector<PluginContainer> &plugins);
} // namespace PluginUtils
} // namespace WUPSBackend

View File

@ -3,15 +3,14 @@
using namespace WUPSBackend;
PluginContainer::PluginContainer(std::shared_ptr<PluginData> data, std::shared_ptr<PluginMetaInformation> 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<PluginMetaInformation> &PluginContainer::getMetaInformation() const {
[[nodiscard]] const PluginMetaInformation &PluginContainer::getMetaInformation() const {
return this->metaInformation;
}
[[nodiscard]] const std::shared_ptr<PluginData> &PluginContainer::getPluginData() const {
[[nodiscard]] const PluginData &PluginContainer::getPluginData() const {
return pluginData;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -22,110 +22,92 @@
#include <cstring>
#include <memory>
using namespace WUPSBackend;
namespace WUPSBackend {
std::optional<std::unique_ptr<PluginMetaInformation>> getMetaInformation(const wups_backend_plugin_information &info) {
const char *GetStatusStr(PluginBackendApiErrorType status) {
return WUPSBackend_GetStatusStr(status);
}
namespace PluginUtils {
static std::optional<PluginMetaInformation> 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<PluginMetaInformation>(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<std::unique_ptr<PluginMetaInformation>> 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<PluginMetaInformation> 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<std::unique_ptr<PluginMetaInformation>> PluginUtils::getMetaInformationForPath(const std::string &path) {
std::optional<PluginMetaInformation> 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<std::unique_ptr<PluginContainer>> PluginUtils::getPluginForPath(const std::string &path) {
auto metaInfoOpt = PluginUtils::getMetaInformationForPath(path);
std::optional<PluginContainer> 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<PluginData>(dataHandle);
if (!pluginData) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginData");
return {};
}
auto pluginContainer = make_unique_nothrow<PluginContainer>(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<std::unique_ptr<PluginContainer>> PluginUtils::getPluginForBuffer(char *buffer, size_t size) {
auto metaInfoOpt = PluginUtils::getMetaInformationForBuffer(buffer, size);
std::optional<PluginContainer> 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<PluginData>(dataHandle);
if (!pluginData) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate PluginData");
return {};
}
auto pluginContainer = make_unique_nothrow<PluginContainer>(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<PluginContainer> getLoadedPlugins(PluginBackendApiErrorType &err) {
std::vector<PluginContainer> result;
std::vector<std::unique_ptr<PluginContainer>> PluginUtils::getLoadedPlugins(uint32_t maxSize) {
std::vector<std::unique_ptr<PluginContainer>> 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<wups_backend_plugin_container_handle[]>(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<std::unique_ptr<PluginContainer>> 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<wups_backend_plugin_data_handle[]>(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<wups_backend_plugin_information[]>(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<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);
if (!metaInfo) {
DEBUG_FUNCTION_LINE_ERR("Skip, failed to allocate MetaInformation");
continue;
}
auto pluginData = std::make_shared<PluginData>((uint32_t) dataHandles[i]);
if (!pluginData) {
DEBUG_FUNCTION_LINE_ERR("Skip, failed to allocate PluginData");
continue;
}
auto pluginContainer = make_unique_nothrow<PluginContainer>(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<std::unique_ptr<PluginContainer>> &plugins) {
PluginBackendApiErrorType LoadAndLinkOnRestart(const std::vector<PluginContainer> &plugins) {
uint32_t dataSize = plugins.size();
wups_backend_plugin_data_handle handles[dataSize];
auto handles = make_unique_nothrow<wups_backend_plugin_data_handle[]>(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