io.qt.core.QScopedPointer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qtjambi Show documentation
Show all versions of qtjambi Show documentation
QtJambi base module containing QtCore, QtGui and QtWidgets.
/****************************************************************************
**
** Copyright (C) 2009-2024 Dr. Peter Droste, Omix Visualization GmbH & Co. KG. All rights reserved.
**
** This file is part of Qt Jambi.
**
** $BEGIN_LICENSE$
** GNU Lesser General Public License Usage
** 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.
**
** 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.
** $END_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
package io.qt.core;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.logging.Logger;
import io.qt.InternalAccess.Cleanable;
import io.qt.QtObjectInterface;
import io.qt.QtUninvokable;
/**
* Java wrapper for Qt class QScopedPointer
* to be used inside a try-with-resource block.
* Example:
*
* try(var pointer = QScopedPointer.disposing(new QDialog())){
* QDialog dialog = pointer.get();
* dialog.exec();
* }
* // dialog is disposed
*
* Alternatively, performAndDispose disposes a recource after calling a lambda expression:
*
* QScopedPointer.performAndDispose(QDialog::exec, new QDialog());
*
*
*/
public final class QScopedPointer implements AutoCloseable {
static final Logger logger = Logger.getLogger("io.qt.core");
static class Data{
QScope.AbstractEntry entry;
Cleanable cleanable;
void cleanup() {
if(entry!=null) {
entry.cleanup();
entry = null;
}
}
}
private final Data data = new Data<>();
private QScopedPointer(O data, Consumer cleanup) {
this(new QScope.CleanupEntry<>(data, cleanup));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private QScopedPointer(QtObjectInterface data) {
this((QScope.AbstractEntry)new QScope.DisposingEntry(data));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private QScopedPointer(QObject data) {
this((QScope.AbstractEntry)new QScope.DisposingLaterEntry(data));
}
private QScopedPointer(QScope.AbstractEntry entry) {
this.data.entry = entry;
this.data.cleanable = QtJambi_LibraryUtilities.internal.registerCleaner(this, this.data::cleanup);
}
@QtUninvokable
public O data() {
return data.entry.data;
}
@QtUninvokable
public O get() {
return data.entry.data;
}
@QtUninvokable
public O take() {
O oldData = data.entry.data;
data.entry.data = null;
return oldData;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@QtUninvokable
public void reset(O other) {
if(data.entry!=null) {
QScope.AbstractEntry oldEntry = data.entry;
if(oldEntry instanceof QScope.DisposingEntry) {
this.data.entry = (QScope.AbstractEntry)new QScope.DisposingEntry((QtObjectInterface)other);
}else if(oldEntry instanceof QScope.RunningEntry) {
this.data.entry = (QScope.AbstractEntry)new QScope.RunningEntry((Runnable)other);
}else if(oldEntry instanceof QScope.CleanupEntry) {
this.data.entry = (QScope.AbstractEntry)new QScope.CleanupEntry(other, ((QScope.CleanupEntry)oldEntry).getCleanup());
}else {
throw new IllegalStateException("Unable to reset scoped pointer.");
}
oldEntry.cleanup();
}
}
@QtUninvokable
public boolean isNull(){
return data.entry==null || data.entry.data==null;
}
@QtUninvokable
public void swap(QScopedPointer other){
QScope.AbstractEntry oldEntry = data.entry;
this.data.entry = other.data.entry;
other.data.entry = oldEntry;
}
@Override
@QtUninvokable
public void close(){
if(this.data.cleanable!=null) {
this.data.cleanable.clean();
this.data.cleanable = null;
}
}
@QtUninvokable
private static void dispose(QtObjectInterface data) {
if(QtJambi_LibraryUtilities.internal.nativeId(data)!=0)
data.dispose();
}
@QtUninvokable
private static void disposeLater(QObject data) {
if(QtJambi_LibraryUtilities.internal.nativeId(data)!=0)
data.disposeLater();
}
@QtUninvokable
public static QScopedPointer disposing(O data){
return new QScopedPointer<>(data);
}
@QtUninvokable
public static QScopedPointer disposingLater(O data){
return new QScopedPointer<>(data);
}
@QtUninvokable
@Deprecated
public static QScopedPointer cleanup(O data, Consumer cleanup){
return new QScopedPointer<>(data, cleanup);
}
@QtUninvokable
public static QScopedPointer cleanup(Consumer cleanup, O data){
return new QScopedPointer<>(data, cleanup);
}
/**
* Performs an action on a resource which will be disposes subsequently.
*
* QScopedPointer.performAndDispose(QDialog::exec, new QDialog());
*
*
* @param data the resource
* @param action the action
*/
@QtUninvokable
public static void performAndDispose(Consumer action, O data){
try(QScopedPointer ptr = QScopedPointer.disposing(data)){
action.accept(ptr.get());
}
}
/**
* Performs an action on a {@link QObject} resource which will be called {@link QObject#disposeLater()} subsequently.
*
* QScopedPointer.performAndDispose(QDialog::exec, new QDialog());
*
*
* @param data the resource
* @param action the action
*/
@QtUninvokable
public static void performAndDisposeLater(Consumer action, O data){
try(QScopedPointer ptr = QScopedPointer.disposingLater(data)){
action.accept(ptr.get());
}
}
/**
* Performs an action on a resource which will be cleaned up subsequently.
*
* QScopedPointer.performAndCleanup(object-> {
* ...//action
* },
* object -> { object.cleanup(); }, new CustomObject() );
*
*
* @param data the resource
* @param action the action
*/
@QtUninvokable
public static void performAndCleanup(Consumer action, Consumer cleanup, O data){
try(QScopedPointer ptr = QScopedPointer.cleanup(cleanup, data)){
action.accept(ptr.get());
}
}
/**
* Performs an action on a resource which will be disposes subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static R performAndDispose(Function action, O data){
try(QScopedPointer ptr = QScopedPointer.disposing(data)){
return action.apply(ptr.get());
}
}
/**
* Performs an action on a {@link QObject} resource which will be called {@link QObject#disposeLater()} subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static R performAndDisposeLater(Function action, O data){
try(QScopedPointer ptr = QScopedPointer.disposingLater(data)){
return action.apply(ptr.get());
}
}
/**
* Performs an action on a resource which will be cleaned up subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static R performAndCleanup(Function action, Consumer cleanup, O data){
try(QScopedPointer ptr = QScopedPointer.cleanup(cleanup, data)){
return action.apply(ptr.get());
}
}
/**
* Performs an action on a resource which will be disposes subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static int performAndDispose(ToIntFunction action, O data){
try(QScopedPointer ptr = QScopedPointer.disposing(data)){
return action.applyAsInt(ptr.get());
}
}
/**
* Performs an action on a {@link QObject} resource which will be called {@link QObject#disposeLater()} subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static int performAndDisposeLater(ToIntFunction action, O data){
try(QScopedPointer ptr = QScopedPointer.disposingLater(data)){
return action.applyAsInt(ptr.get());
}
}
/**
* Performs an action on a resource which will be cleaned up subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static int performAndCleanup(ToIntFunction action, Consumer cleanup, O data){
try(QScopedPointer ptr = QScopedPointer.cleanup(cleanup, data)){
return action.applyAsInt(ptr.get());
}
}
/**
* Performs an action on a resource which will be disposes subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static double performAndDispose(ToDoubleFunction action, O data){
try(QScopedPointer ptr = QScopedPointer.disposing(data)){
return action.applyAsDouble(ptr.get());
}
}
/**
* Performs an action on a {@link QObject} resource which will be called {@link QObject#disposeLater()} subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static double performAndDisposeLater(ToDoubleFunction action, O data){
try(QScopedPointer ptr = QScopedPointer.disposingLater(data)){
return action.applyAsDouble(ptr.get());
}
}
/**
* Performs an action on a resource which will be cleaned up subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static double performAndCleanup(ToDoubleFunction action, Consumer cleanup, O data){
try(QScopedPointer ptr = QScopedPointer.cleanup(cleanup, data)){
return action.applyAsDouble(ptr.get());
}
}
/**
* Performs an action on a resource which will be disposes subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static long performAndDispose(ToLongFunction action, O data){
try(QScopedPointer ptr = QScopedPointer.disposing(data)){
return action.applyAsLong(ptr.get());
}
}
/**
* Performs an action on a {@link QObject} resource which will be called {@link QObject#disposeLater()} subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static long performAndDisposeLater(ToLongFunction action, O data){
try(QScopedPointer ptr = QScopedPointer.disposingLater(data)){
return action.applyAsLong(ptr.get());
}
}
/**
* Performs an action on a resource which will be cleaned up subsequently.
*
* @param data the resource
* @param action the action
* @return result
*/
@QtUninvokable
public static long performAndCleanup(ToLongFunction action, Consumer cleanup, O data){
try(QScopedPointer ptr = QScopedPointer.cleanup(cleanup, data)){
return action.applyAsLong(ptr.get());
}
}
}