Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate complex typed static variables #37

Open
wants to merge 1 commit into
base: patched-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/PythonQtClassInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
#include <QMetaObject>
#include <QMetaEnum>

QHash<QByteArray, int> PythonQtMethodInfo::_parameterTypeDict;

PythonQtClassInfo::PythonQtClassInfo() {
_meta = NULL;
_constructors = NULL;
Expand Down
23 changes: 18 additions & 5 deletions src/PythonQtConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ PythonQtValueStorage<qint64, 128> PythonQtConv::global_valueStorage;
PythonQtValueStorage<void*, 128> PythonQtConv::global_ptrStorage;
PythonQtValueStorageWithCleanup<QVariant, 128> PythonQtConv::global_variantStorage;

QHash<int, PythonQtConvertMetaTypeToPythonCB*> PythonQtConv::_metaTypeToPythonConverters;
QHash<int, PythonQtConvertPythonToMetaTypeCB*> PythonQtConv::_pythonToMetaTypeConverters;
QHash<int, PythonQtConvertMetaTypeToPythonCB*>* PythonQtConv::GetMetaTypeToPythonConverters() {
static QHash<int, PythonQtConvertMetaTypeToPythonCB*>* _metaTypeToPythonConverters = nullptr;
if (_metaTypeToPythonConverters == nullptr) {
_metaTypeToPythonConverters = new QHash<int, PythonQtConvertMetaTypeToPythonCB*>();
}
return _metaTypeToPythonConverters;
}

QHash<int, PythonQtConvertPythonToMetaTypeCB*>* PythonQtConv::GetPythonToMetaTypeConverters() {
static QHash<int, PythonQtConvertPythonToMetaTypeCB*>* _pythonToMetaTypeConverters = nullptr;
if (_pythonToMetaTypeConverters == nullptr) {
_pythonToMetaTypeConverters = new QHash<int, PythonQtConvertPythonToMetaTypeCB*>();
}
return _pythonToMetaTypeConverters;
}

PyObject* PythonQtConv::GetPyBool(bool val)
{
Expand Down Expand Up @@ -103,7 +116,7 @@ PyObject* PythonQtConv::ConvertQtValueToPython(const PythonQtMethodInfo::Paramet

if (info.typeId >= QMetaType::User) {
// if a converter is registered, we use is:
PythonQtConvertMetaTypeToPythonCB* converter = _metaTypeToPythonConverters.value(info.typeId);
PythonQtConvertMetaTypeToPythonCB* converter = GetMetaTypeToPythonConverters()->value(info.typeId);
if (converter) {
return (*converter)(info.pointerCount==0?data:*((void**)data), info.typeId);
}
Expand Down Expand Up @@ -668,7 +681,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
// We only do this for registered type > QMetaType::User for performance reasons.
if (info.typeId >= QMetaType::User) {
// Maybe we have a special converter that is registered for that type:
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(info.typeId);
PythonQtConvertPythonToMetaTypeCB* converter = GetPythonToMetaTypeConverters()->value(info.typeId);
if (converter) {
if (!alreadyAllocatedCPPObject) {
// create a new empty variant of concrete type:
Expand Down Expand Up @@ -1174,7 +1187,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
} else if (type >= QVariant::UserType) {
// not an instance wrapper, but there might be other converters
// Maybe we have a special converter that is registered for that type:
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
PythonQtConvertPythonToMetaTypeCB* converter = GetPythonToMetaTypeConverters()->value(type);
if (converter) {
// allocate a default object of the needed type:
v = QVariant(type, (const void*)NULL);
Expand Down
8 changes: 4 additions & 4 deletions src/PythonQtConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ class PYTHONQT_EXPORT PythonQtConv {
static QString CPPObjectToString(int type, const void* data);

//! register a converter callback from python to cpp for given metatype
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { _pythonToMetaTypeConverters.insert(metaTypeId, cb); }
static void registerPythonToMetaTypeConverter(int metaTypeId, PythonQtConvertPythonToMetaTypeCB* cb) { GetPythonToMetaTypeConverters()->insert(metaTypeId, cb); }

//! register a converter callback from cpp to python for given metatype
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB* cb) { _metaTypeToPythonConverters.insert(metaTypeId, cb); }
static void registerMetaTypeToPythonConverter(int metaTypeId, PythonQtConvertMetaTypeToPythonCB* cb) { GetMetaTypeToPythonConverters()->insert(metaTypeId, cb); }

//! converts the Qt parameter given in \c data, interpreting it as a \c type registered qvariant/meta type, into a Python object,
static PyObject* convertQtValueToPythonInternal(int type, const void* data);
Expand All @@ -187,8 +187,8 @@ class PYTHONQT_EXPORT PythonQtConv {
static PythonQtValueStorageWithCleanup<QVariant, 128> global_variantStorage;

protected:
static QHash<int, PythonQtConvertMetaTypeToPythonCB*> _metaTypeToPythonConverters;
static QHash<int, PythonQtConvertPythonToMetaTypeCB*> _pythonToMetaTypeConverters;
static QHash<int, PythonQtConvertMetaTypeToPythonCB*>* GetMetaTypeToPythonConverters();
static QHash<int, PythonQtConvertPythonToMetaTypeCB*>* GetPythonToMetaTypeConverters();

//! handle automatic conversion of some special types (QColor, QBrush, ...)
static void* handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject);
Expand Down
Loading