diff options
author | Christian Strømme <christian.stromme@digia.com> | 2013-09-18 01:30:54 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-09-20 00:59:55 +0200 |
commit | 9329f786da8e167130fa36b91ff288bfdb046ce1 (patch) | |
tree | b738eb06051ce51b3d537fba26458271d1f1893d /src/corelib/kernel/qjni_p.h | |
parent | ad4657f639a037b9c8330e3bf7a3ef03bdd299c6 (diff) |
Android: Add private API for jni in QtCore
When interfacing with Java API's on Android we need to write code using
the Java Native Interface (JNI). Writing JNI code requires a lot of
boilerplate code to be written. This patch contains API's to minimize the
amount of work needed to write JNI code.
QJNIEnvironmentPrivate:
On creation QJNIEnvironmentPrivate will attach the current running
thread to the Java VM, and expose the java environment.
QJNIObjectPrivate:
Wrapps around a Java class enabling the user to access the class from C++.
Change-Id: Ib633437ae36ff513d934292e9eeefcdd5b757d29
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
Diffstat (limited to 'src/corelib/kernel/qjni_p.h')
-rw-r--r-- | src/corelib/kernel/qjni_p.h | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/src/corelib/kernel/qjni_p.h b/src/corelib/kernel/qjni_p.h new file mode 100644 index 0000000000..912b5dbee4 --- /dev/null +++ b/src/corelib/kernel/qjni_p.h @@ -0,0 +1,264 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QJNI_P_H +#define QJNI_P_H + +#include <jni.h> +#include <QtCore/qglobal.h> +#include <QtCore/qsharedpointer.h> + +QT_BEGIN_NAMESPACE + +class Q_CORE_EXPORT QJNIEnvironmentPrivate +{ +public: + QJNIEnvironmentPrivate(); + ~QJNIEnvironmentPrivate(); + JNIEnv *operator->(); + operator JNIEnv*() const; + +private: + friend class QJNIEnvironment; + Q_DISABLE_COPY(QJNIEnvironmentPrivate) + JNIEnv *jniEnv; +}; + +class Q_CORE_EXPORT QJNIObjectData +{ +public: + QJNIObjectData(); + ~QJNIObjectData(); + jobject m_jobject; + jclass m_jclass; + bool m_own_jclass; +}; + +class Q_CORE_EXPORT QJNIObjectPrivate +{ +public: + QJNIObjectPrivate(); + explicit QJNIObjectPrivate(const char *className); + QJNIObjectPrivate(const char *className, const char *sig, ...); + explicit QJNIObjectPrivate(jclass clazz); + QJNIObjectPrivate(jclass clazz, const char *sig, ...); + QJNIObjectPrivate(jobject obj); + + template <typename T> + T callMethod(const char *methodName, + const char *sig, + ...) const; + template <typename T> + T callMethod(const char *methodName) const; + template <typename T> + QJNIObjectPrivate callObjectMethod(const char *methodName) const; + QJNIObjectPrivate callObjectMethod(const char *methodName, + const char *sig, + ...) const; + template <typename T> + static T callStaticMethod(const char *className, + const char *methodName, + const char *sig, ...); + template <typename T> + static T callStaticMethod(const char *className, + const char *methodName); + template <typename T> + static T callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, ...); + template <typename T> + static T callStaticMethod(jclass clazz, + const char *methodName); + static QJNIObjectPrivate callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, ...); + + static QJNIObjectPrivate callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, ...); + + template <typename T> + T getField(const char *fieldName) const; + template <typename T> + static T getStaticField(const char *className, const char *fieldName); + template <typename T> + static T getStaticField(jclass clazz, const char *fieldName); + + QJNIObjectPrivate getObjectField(const char *fieldName, const char *sig) const; + static QJNIObjectPrivate getStaticObjectField(const char *className, + const char *fieldName, + const char *sig); + static QJNIObjectPrivate getStaticObjectField(jclass clazz, + const char *fieldName, + const char *sig); + + template <typename T> + void setField(const char *fieldName, T value); + template <typename T> + void setField(const char *fieldName, const char *sig, T value); + template <typename T> + static void setStaticField(const char *className, + const char *fieldName, + T value); + template <typename T> + static void setStaticField(const char *className, + const char *fieldName, + const char *sig, + T value); + template <typename T> + static void setStaticField(jclass clazz, + const char *fieldName, + const char *sig, + T value); + + template <typename T> + static void setStaticField(jclass clazz, + const char *fieldName, + T value); + + static QJNIObjectPrivate fromString(const QString &string); + QString toString() const; + + static bool isClassAvailable(const char *className); + bool isValid() const; + jobject object() const { return d->m_jobject; } + + template <typename T> + inline QJNIObjectPrivate &operator=(T o) + { + jobject jobj = static_cast<jobject>(o); + if (!isSameObject(jobj)) { + d = QSharedPointer<QJNIObjectData>(new QJNIObjectData()); + QJNIEnvironmentPrivate env; + d->m_jobject = env->NewGlobalRef(jobj); + d->m_jclass = static_cast<jclass>(env->NewGlobalRef(env->GetObjectClass(jobj))); + } + + return *this; + } + +private: + friend class QJNIObject; + + template <typename T> + T callMethod(const char *methodName, + const char *sig, + va_list args) const; + QJNIObjectPrivate callObjectMethod(const char *methodName, + const char *sig, + va_list args) const; + template <typename T> + static T callStaticMethod(const char *className, + const char *methodName, + const char *sig, va_list args); + template <typename T> + static T callStaticMethod(jclass clazz, + const char *methodName, + const char *sig, va_list args); + static QJNIObjectPrivate callStaticObjectMethod(const char *className, + const char *methodName, + const char *sig, va_list args); + + static QJNIObjectPrivate callStaticObjectMethod(jclass clazz, + const char *methodName, + const char *sig, va_list args); + + bool isSameObject(jobject obj) const; + bool isSameObject(const QJNIObjectPrivate &other) const; + + friend bool operator==(const QJNIObjectPrivate &, const QJNIObjectPrivate &); + friend bool operator!=(const QJNIObjectPrivate&, const QJNIObjectPrivate&); + template <typename T> friend bool operator!=(const QJNIObjectPrivate&, T); + template <typename T> friend bool operator==(const QJNIObjectPrivate&, T); + template <typename T> friend bool operator!=(T, const QJNIObjectPrivate&); + template <typename T> friend bool operator==(T, const QJNIObjectPrivate&); + + QSharedPointer<QJNIObjectData> d; +}; + +inline bool operator==(const QJNIObjectPrivate&obj1, const QJNIObjectPrivate&obj2) +{ + return obj1.isSameObject(obj2); +} + +inline bool operator!=(const QJNIObjectPrivate&obj1, const QJNIObjectPrivate&obj2) +{ + return !obj1.isSameObject(obj2); +} + +template <typename T> +inline bool operator==(const QJNIObjectPrivate &obj1, T obj2) +{ + return obj1.isSameObject(static_cast<jobject>(obj2)); +} + +template <typename T> +inline bool operator==(T obj1, const QJNIObjectPrivate &obj2) +{ + return obj2.isSameObject(static_cast<jobject>(obj1)); +} + +template <typename T> +inline bool operator!=(const QJNIObjectPrivate &obj1, T obj2) +{ + return !obj1.isSameObject(obj2); +} + +template <typename T> +inline bool operator!=(T obj1, const QJNIObjectPrivate &obj2) +{ + return !obj2.isSameObject(obj1); +} + +QT_END_NAMESPACE + +#endif // QJNI_P_H |