Skip to content

Commit

Permalink
Python support: merge duplicated code (#2225).
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 26, 2019
1 parent 303cc0b commit 0c1ec6d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 56 deletions.
40 changes: 40 additions & 0 deletions src/misc/closesimulation.cpp.inl
@@ -0,0 +1,40 @@
static PyObject * closeSimulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(pSelf)

// Close a simulation

if (PyTuple_Size(pArgs) > 0) {
#include "pythonbegin.h"
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(pArgs, 0)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
#include "pythonend.h"

if (wrappedSimulation != nullptr) {
#ifdef GUI_SUPPORT
// Close the simulation by closing its file, raising an exception if
// we were unable to do so

auto simulation = static_cast<SimulationSupport::Simulation *>(wrappedSimulation->_objPointerCopy);

if (!Core::centralWidget()->closeFile(simulation->fileName())) {
PyErr_SetString(PyExc_IOError, qPrintable(QObject::tr("unable to close the simulation")));

return nullptr;
}
#else
// Close the simulation by asking our file and simulation managers
// to umanage it

auto simulation = static_cast<SimulationSupport::Simulation *>(wrappedSimulation->_objPointerCopy);
QString fileName = simulation->fileName();

Core::FileManager::instance()->unmanage(fileName);
SimulationManager::instance()->unmanage(fileName);
#endif
}
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
Expand Up @@ -115,35 +115,9 @@ static PyObject * initializeSimulation(const QString &pFileName)

//==============================================================================

static PyObject * closeSimulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(pSelf)

// Close a simulation

if (PyTuple_Size(pArgs) > 0) {
#include "pythonbegin.h"
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(pArgs, 0)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
#include "pythonend.h"

if (wrappedSimulation != nullptr) {
// Close the simulation by closing its file, raising an exception if
// we were unable to do so

auto simulation = static_cast<SimulationSupport::Simulation *>(wrappedSimulation->_objPointerCopy);

if (!Core::centralWidget()->closeFile(simulation->fileName())) {
PyErr_SetString(PyExc_IOError, qPrintable(QObject::tr("unable to close the simulation")));

return nullptr;
}
}
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
#define GUI_SUPPORT
#include "closesimulation.cpp.inl"
#undef GUI_SUPPORT

//==============================================================================

Expand Down
Expand Up @@ -198,33 +198,7 @@ static PyObject * initializeSimulation(const QString &pFileName)

//==============================================================================

static PyObject * closeSimulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(pSelf)

// Close a simulation

if (PyTuple_Size(pArgs) > 0) {
#include "pythonbegin.h"
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(pArgs, 0)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
#include "pythonend.h"

if (wrappedSimulation != nullptr) {
// Close the simulation by asking our file and simulation managers
// to umanage it

auto simulation = static_cast<SimulationSupport::Simulation *>(wrappedSimulation->_objPointerCopy);
QString fileName = simulation->fileName();

Core::FileManager::instance()->unmanage(fileName);
SimulationManager::instance()->unmanage(fileName);
}
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
#include "closesimulation.cpp.inl"

//==============================================================================

Expand Down

0 comments on commit 0c1ec6d

Please sign in to comment.