Merge pull request #11 from AnyOldName3/reusable-wrapper-template

Helpful error message and reduce code duplication
This commit is contained in:
Jeremy Rimpo
2018-05-03 20:49:05 -05:00
committed by GitHub
7 changed files with 193 additions and 385 deletions
+28 -137
View File
@@ -10,7 +10,7 @@
#include <isavegameinfowidget.h>
#include "gilock.h"
#include "pycatch.h"
#include "pythonwrapperutilities.h"
/////////////////////////////
/// BSAInvalidation Wrapper
@@ -18,29 +18,17 @@
bool BSAInvalidationWrapper::isInvalidationBSA(const QString &bsaName)
{
GILock lock;
try {
return this->get_override("isInvalidationBSA")(bsaName);
} PYCATCH;
return basicWrapperFunctionImplementation<BSAInvalidationWrapper, bool>(this, "isInvalidationBSA", bsaName);
}
void BSAInvalidationWrapper::deactivate(MOBase::IProfile *profile)
{
GILock lock;
try {
this->get_override("deactivate")(boost::python::ptr(profile));
} PYCATCH;
basicWrapperFunctionImplementation<BSAInvalidationWrapper, void>(this, "deactivate", boost::python::ptr(profile));
}
void BSAInvalidationWrapper::activate(MOBase::IProfile *profile)
{
GILock lock;
try {
this->get_override("activate")(boost::python::ptr(profile));
} PYCATCH;
return basicWrapperFunctionImplementation<BSAInvalidationWrapper, void>(this, "activate", boost::python::ptr(profile));
}
/// end BSAInvalidation Wrapper
/////////////////////////////
@@ -49,38 +37,22 @@ void BSAInvalidationWrapper::activate(MOBase::IProfile *profile)
QStringList DataArchivesWrapper::vanillaArchives() const
{
GILock lock;
try {
return this->get_override("vanillaArchives")();
} PYCATCH;
return basicWrapperFunctionImplementation<DataArchivesWrapper, QStringList>(this, "vanillaArchives");
}
QStringList DataArchivesWrapper::archives(const MOBase::IProfile *profile) const
{
GILock lock;
try {
return this->get_override("archives")(boost::python::ptr(profile));
} PYCATCH;
return basicWrapperFunctionImplementation<DataArchivesWrapper, QStringList>(this, "archives", boost::python::ptr(profile));
}
void DataArchivesWrapper::addArchive(MOBase::IProfile *profile, int index, const QString &archiveName)
{
GILock lock;
try {
this->get_override("addArchive")(boost::python::ptr(profile), index, archiveName);
} PYCATCH;
return basicWrapperFunctionImplementation<DataArchivesWrapper, void>(this, "addArchive", boost::python::ptr(profile), index, archiveName);
}
void DataArchivesWrapper::removeArchive(MOBase::IProfile * profile, const QString & archiveName)
{
GILock lock;
try {
this->get_override("removeArchive")(boost::python::ptr(profile), archiveName);
} PYCATCH;
return basicWrapperFunctionImplementation<DataArchivesWrapper, void>(this, "removeArchive", boost::python::ptr(profile), archiveName);
}
/// end DataArchives Wrapper
/////////////////////////////
@@ -89,20 +61,12 @@ void DataArchivesWrapper::removeArchive(MOBase::IProfile * profile, const QStrin
void GamePluginsWrapper::writePluginLists(const MOBase::IPluginList * pluginList)
{
GILock lock;
try {
this->get_override("writePluginLists")(boost::python::ptr(pluginList));
} PYCATCH;
return basicWrapperFunctionImplementation<GamePluginsWrapper, void>(this, "writePluginLists", boost::python::ptr(pluginList));
}
void GamePluginsWrapper::readPluginLists(MOBase::IPluginList * pluginList)
{
GILock lock;
try {
this->get_override("readPluginLists")(boost::python::ptr(pluginList));
} PYCATCH;
return basicWrapperFunctionImplementation<GamePluginsWrapper, void>(this, "readPluginLists", boost::python::ptr(pluginList));
}
/// end GamePlugins Wrapper
/////////////////////////////
@@ -111,20 +75,12 @@ void GamePluginsWrapper::readPluginLists(MOBase::IPluginList * pluginList)
MappingType LocalSavegamesWrapper::mappings(const QDir & profileSaveDir) const
{
GILock lock;
try {
return this->get_override("mappings")(profileSaveDir);
} PYCATCH;
return basicWrapperFunctionImplementation<LocalSavegamesWrapper, MappingType>(this, "mappings", profileSaveDir);
}
void LocalSavegamesWrapper::prepareProfile(MOBase::IProfile * profile)
{
GILock lock;
try {
this->get_override("prepareProfile")(boost::python::ptr(profile));
} PYCATCH;
return basicWrapperFunctionImplementation<LocalSavegamesWrapper, void>(this, "prepareProfile", boost::python::ptr(profile));
}
/// end LocalSavegames Wrapper
/////////////////////////////
@@ -133,40 +89,23 @@ void LocalSavegamesWrapper::prepareProfile(MOBase::IProfile * profile)
MOBase::ISaveGame const * SaveGameInfoWrapper::getSaveGameInfo(QString const & file) const
{
GILock lock;
try {
return this->get_override("getSaveGameInfo")(file);
} PYCATCH;
return basicWrapperFunctionImplementation<SaveGameInfoWrapper, MOBase::ISaveGame const *>(this, "getSaveGameInfo", file);
}
SaveGameInfoWrapper::MissingAssets SaveGameInfoWrapper::getMissingAssets(QString const & file) const
{
GILock lock;
try {
return this->get_override("getMissingAssets")(file);
} PYCATCH;
return basicWrapperFunctionImplementation<SaveGameInfoWrapper, SaveGameInfoWrapper::MissingAssets>(this, "getMissingAssets", file);
}
MOBase::ISaveGameInfoWidget * SaveGameInfoWrapper::getSaveGameWidget(QWidget * parent) const
{
qCritical("Calling method with unimplemented from_python converter.");
GILock lock;
try {
return this->get_override("getSaveGameWidget")(boost::python::ptr(parent));
} PYCATCH;
return basicWrapperFunctionImplementation<SaveGameInfoWrapper, MOBase::ISaveGameInfoWidget *>(this, "getSaveGameWidget", boost::python::ptr(parent));
}
bool SaveGameInfoWrapper::hasScriptExtenderSave(QString const & file) const
{
GILock lock;
try {
return this->get_override("hasScriptExtenderSave")(file);
} PYCATCH;
return basicWrapperFunctionImplementation<SaveGameInfoWrapper, bool>(this, "hasScriptExtenderSave", file);
}
/// end SaveGameInfo Wrapper
/////////////////////////////
@@ -174,74 +113,42 @@ bool SaveGameInfoWrapper::hasScriptExtenderSave(QString const & file) const
QString ScriptExtenderWrapper::BinaryName() const
{
GILock lock;
try {
return this->get_override("BinaryName")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, QString>(this, "BinaryName");
}
QString ScriptExtenderWrapper::PluginPath() const
{
GILock lock;
try {
return this->get_override("PluginPath")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, QString>(this, "PluginPath");
}
QString ScriptExtenderWrapper::loaderName() const
{
GILock lock;
try {
return this->get_override("loaderName")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, QString>(this, "loaderName");
}
QString ScriptExtenderWrapper::loaderPath() const
{
GILock lock;
try {
return this->get_override("loaderPath")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, QString>(this, "loaderPath");
}
QStringList ScriptExtenderWrapper::saveGameAttachmentExtensions() const
{
GILock lock;
try {
return this->get_override("saveGameAttachmentExtensions")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, QStringList>(this, "saveGameAttachmentExtensions");
}
bool ScriptExtenderWrapper::isInstalled() const
{
GILock lock;
try {
return this->get_override("isInstalled")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, bool>(this, "isInstalled");
}
QString ScriptExtenderWrapper::getExtenderVersion() const
{
GILock lock;
try {
return this->get_override("getExtenderVersion")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, QString>(this, "getExtenderVersion");
}
WORD ScriptExtenderWrapper::getArch() const
{
GILock lock;
try {
return this->get_override("getArch")();
} PYCATCH;
return basicWrapperFunctionImplementation<ScriptExtenderWrapper, WORD>(this, "getArch");
}
/// end ScriptExtender Wrapper
@@ -251,38 +158,22 @@ WORD ScriptExtenderWrapper::getArch() const
QStringList UnmanagedModsWrapper::mods(bool onlyOfficial) const
{
GILock lock;
try {
return this->get_override("mods")(onlyOfficial);
} PYCATCH;
return basicWrapperFunctionImplementation<UnmanagedModsWrapper, QStringList>(this, "mods", onlyOfficial);
}
QString UnmanagedModsWrapper::displayName(const QString & modName) const
{
GILock lock;
try {
return this->get_override("displayName")(modName);
} PYCATCH;
return basicWrapperFunctionImplementation<UnmanagedModsWrapper, QString>(this, "displayName", modName);
}
QFileInfo UnmanagedModsWrapper::referenceFile(const QString & modName) const
{
GILock lock;
try {
return this->get_override("referenceFile")(modName);
} PYCATCH;
return basicWrapperFunctionImplementation<UnmanagedModsWrapper, QFileInfo>(this, "referenceFile", modName);
}
QStringList UnmanagedModsWrapper::secondaryFiles(const QString & modName) const
{
GILock lock;
try {
return this->get_override("secondaryFiles")(modName);
} PYCATCH;
return basicWrapperFunctionImplementation<UnmanagedModsWrapper, QStringList>(this, "secondaryFiles", modName);
}
/// end UnmanagedMods Wrapper
/////////////////////////////
+28
View File
@@ -17,6 +17,10 @@
class BSAInvalidationWrapper : public BSAInvalidation, public boost::python::wrapper<BSAInvalidation>
{
public:
static constexpr const char* className = "BSAInvalidationWrapper";
using boost::python::wrapper<BSAInvalidation>::get_override;
virtual bool isInvalidationBSA(const QString &bsaName) override;
virtual void deactivate(MOBase::IProfile *profile) override;
virtual void activate(MOBase::IProfile *profile) override;
@@ -24,6 +28,10 @@ class BSAInvalidationWrapper : public BSAInvalidation, public boost::python::wra
class DataArchivesWrapper : public DataArchives, public boost::python::wrapper<DataArchives>
{
public:
static constexpr const char* className = "DataArchivesWrapper";
using boost::python::wrapper<DataArchives>::get_override;
virtual QStringList vanillaArchives() const override;
virtual QStringList archives(const MOBase::IProfile *profile) const override;
virtual void addArchive(MOBase::IProfile *profile, int index, const QString &archiveName) override;
@@ -32,18 +40,30 @@ class DataArchivesWrapper : public DataArchives, public boost::python::wrapper<D
class GamePluginsWrapper : public GamePlugins, public boost::python::wrapper<GamePlugins>
{
public:
static constexpr const char* className = "GamePluginsWrapper";
using boost::python::wrapper<GamePlugins>::get_override;
virtual void writePluginLists(const MOBase::IPluginList *pluginList) override;
virtual void readPluginLists(MOBase::IPluginList *pluginList) override;
};
class LocalSavegamesWrapper : public LocalSavegames, public boost::python::wrapper<LocalSavegames>
{
public:
static constexpr const char* className = "LocalSavegamesWrapper";
using boost::python::wrapper<LocalSavegames>::get_override;
virtual MappingType mappings(const QDir &profileSaveDir) const override;
virtual void prepareProfile(MOBase::IProfile *profile) override;
};
class SaveGameInfoWrapper : public SaveGameInfo, public boost::python::wrapper<SaveGameInfo>
{
public:
static constexpr const char* className = "SaveGameInfoWrapper";
using boost::python::wrapper<SaveGameInfo>::get_override;
virtual MOBase::ISaveGame const *getSaveGameInfo(QString const &file) const override;
virtual MissingAssets getMissingAssets(QString const &file) const override;
virtual MOBase::ISaveGameInfoWidget *getSaveGameWidget(QWidget *parent = 0) const override;
@@ -52,6 +72,10 @@ class SaveGameInfoWrapper : public SaveGameInfo, public boost::python::wrapper<S
class ScriptExtenderWrapper : public ScriptExtender, public boost::python::wrapper<ScriptExtender>
{
public:
static constexpr const char* className = "ScriptExtenderWrapper";
using boost::python::wrapper<ScriptExtender>::get_override;
virtual QString BinaryName() const override;
virtual QString PluginPath() const override;
virtual QString loaderName() const override;
@@ -64,6 +88,10 @@ class ScriptExtenderWrapper : public ScriptExtender, public boost::python::wrapp
class UnmanagedModsWrapper : public UnmanagedMods, public boost::python::wrapper<UnmanagedMods>
{
public:
static constexpr const char* className = "UnmanagedModsWrapper";
using boost::python::wrapper<UnmanagedMods>::get_override;
virtual QStringList mods(bool onlyOfficial) const override;
virtual QString displayName(const QString &modName) const override;
virtual QFileInfo referenceFile(const QString &modName) const override;
File diff suppressed because it is too large Load Diff
+24
View File
@@ -31,6 +31,9 @@ class IPluginWrapper : public MOBase::IPlugin, public boost::python::wrapper<MOB
Q_INTERFACES(MOBase::IPlugin)
COMMON_I_PLUGIN_WRAPPER_DECLARATIONS
public:
static constexpr const char* className = "IPluginWrapper";
using boost::python::wrapper<MOBase::IPlugin>::get_override;
};
@@ -42,6 +45,9 @@ class IPluginDiagnoseWrapper : public QObject, public MOBase::IPluginDiagnose, p
Q_INTERFACES(MOBase::IPlugin MOBase::IPluginDiagnose)
public:
static constexpr const char* className = "IPluginDiagnoseWrapper";
using boost::python::wrapper<MOBase::IPluginDiagnose>::get_override;
virtual std::vector<unsigned int> activeProblems() const override;
virtual QString shortDescription(unsigned int key) const override;
virtual QString fullDescription(unsigned int key) const override;
@@ -63,6 +69,9 @@ class IPluginFileMapperWrapper : public QObject, public MOBase::IPluginFileMappe
Q_INTERFACES(MOBase::IPlugin MOBase::IPluginFileMapper)
public:
static constexpr const char* className = "IPluginFileMapperWrapper";
using boost::python::wrapper<MOBase::IPluginFileMapper>::get_override;
virtual MappingType mappings() const override;
COMMON_I_PLUGIN_WRAPPER_DECLARATIONS
@@ -74,6 +83,9 @@ class IPluginGameWrapper : public MOBase::IPluginGame, public boost::python::wra
Q_INTERFACES(MOBase::IPlugin MOBase::IPluginGame)
public:
static constexpr const char* className = "IPluginGameWrapper";
using boost::python::wrapper<MOBase::IPluginGame>::get_override;
virtual QString gameName() const override;
virtual void initializeProfile(const QDir &directory, ProfileSettings settings) const override;
virtual QString savegameExtension() const override;
@@ -123,6 +135,9 @@ class IPluginInstallerCustomWrapper : public MOBase::IPluginInstallerCustom, pub
COMMON_I_PLUGIN_WRAPPER_DECLARATIONS
public:
static constexpr const char* className = "IPluginInstallerCustomWrapper";
using boost::python::wrapper<MOBase::IPluginInstallerCustom>::get_override;
virtual unsigned int priority() const;
virtual bool isManualInstaller() const;
virtual bool isArchiveSupported(const MOBase::DirectoryTree &tree) const;
@@ -142,6 +157,9 @@ class IPluginModPageWrapper : public MOBase::IPluginModPage, public boost::pytho
COMMON_I_PLUGIN_WRAPPER_DECLARATIONS
public:
static constexpr const char* className = "IPluginModPageWrapper";
using boost::python::wrapper<MOBase::IPluginModPage>::get_override;
virtual QString displayName() const override;
virtual QIcon icon() const override;
virtual QUrl pageURL() const override;
@@ -158,6 +176,9 @@ class IPluginPreviewWrapper : public MOBase::IPluginPreview, public boost::pytho
COMMON_I_PLUGIN_WRAPPER_DECLARATIONS
public:
static constexpr const char* className = "IPluginPreviewWrapper";
using boost::python::wrapper<MOBase::IPluginPreview>::get_override;
virtual std::set<QString> supportedExtensions() const override;
virtual QWidget *genFilePreview(const QString &fileName, const QSize &maxSize) const override;
};
@@ -170,6 +191,9 @@ class IPluginToolWrapper: public MOBase::IPluginTool, public boost::python::wrap
COMMON_I_PLUGIN_WRAPPER_DECLARATIONS
public:
static constexpr const char* className = "IPluginToolWrapper";
using boost::python::wrapper<MOBase::IPluginTool>::get_override;
virtual QString displayName() const;
virtual QString tooltip() const;
virtual QIcon icon() const;
-11
View File
@@ -1,11 +0,0 @@
#ifndef PYCATCH_H
#define PYCATCH_H
#include <utility.h>
#include "error.h"
#define PYCATCH catch (const boost::python::error_already_set &) { reportPythonError(); throw MOBase::MyException("unhandled exception"); }\
catch (...) { throw MOBase::MyException("An unknown exception was thrown in python code"); }
#endif // PYCATCH_H
+32
View File
@@ -0,0 +1,32 @@
#ifndef PYTHONWRAPPERUTILITIES_H
#define PYTHONWRAPPERUTILITIES_H
#include <utility.h>
#include "error.h"
class MissingImplementation : public MOBase::MyException {
public:
MissingImplementation(QString className, QString methodName) : MyException("Python class implementing \"" +
className +
"\" has no implementation of method \"" +
methodName + "\"") {}
};
#define PYCATCH catch (const boost::python::error_already_set &) { reportPythonError(); throw MOBase::MyException("unhandled exception"); }\
catch (const MissingImplementation &missingImplementationException) { throw missingImplementationException; }\
catch (...) { throw MOBase::MyException("An unknown exception was thrown in python code"); }
template <typename WrapperType, typename ReturnType, typename... Args>
ReturnType basicWrapperFunctionImplementation(const WrapperType *wrapper, const char *methodName, Args... args)
{
try {
GILock lock;
boost::python::override implementation = wrapper->get_override(methodName);
if (!implementation)
throw MissingImplementation(wrapper->className, methodName);
return implementation(args...).as<ReturnType>();
} PYCATCH;
}
#endif // PYTHONWRAPPERUTILITIES_H
+9 -6
View File
@@ -26,7 +26,7 @@
#include "error.h"
#include "gilock.h"
#include "pycatch.h"
#include "pythonwrapperutilities.h"
extern MOBase::IOrganizer *s_Organizer;
@@ -443,11 +443,14 @@ struct IModListWrapper: MOBase::IModList, boost::python::wrapper<MOBase::IModLis
class ISaveGameWrapper : public MOBase::ISaveGame, public boost::python::wrapper<MOBase::ISaveGame>
{
public:
virtual QString getFilename() const override { try { return this->get_override("getFilename")(); } PYCATCH };
virtual QDateTime getCreationTime() const override { try { return this->get_override("getCreationTime")(); } PYCATCH };
virtual QString getSaveGroupIdentifier() const override { try { return this->get_override("getSaveGroupIdentifier")(); } PYCATCH };
virtual QStringList allFiles() const override { try { return this->get_override("allFiles")(); } PYCATCH };
virtual bool hasScriptExtenderFile() const override { try { return this->get_override("hasScriptExtenderFile")(); } PYCATCH };
static constexpr const char* className = "ISaveGameWrapper";
using boost::python::wrapper<MOBase::ISaveGame>::get_override;
virtual QString getFilename() const override { return basicWrapperFunctionImplementation<ISaveGameWrapper, QString>(this, "getFilename"); };
virtual QDateTime getCreationTime() const override { return basicWrapperFunctionImplementation<ISaveGameWrapper, QDateTime>(this, "getCreationTime"); };
virtual QString getSaveGroupIdentifier() const override { return basicWrapperFunctionImplementation<ISaveGameWrapper, QString>(this, "getSaveGroupIdentifier"); };
virtual QStringList allFiles() const override { return basicWrapperFunctionImplementation<ISaveGameWrapper, QStringList>(this, "allFiles"); };
virtual bool hasScriptExtenderFile() const override { return basicWrapperFunctionImplementation<ISaveGameWrapper, bool>(this, "hasScriptExtenderFile"); };
};