All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.qt.core.QScopedPointer Maven / Gradle / Ivy

There is a newer version: 6.8.2
Show newest version
/****************************************************************************
**
** Copyright (C) 2009-2022 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.Objects;
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 io.qt.QtUninvokable;
import io.qt.InternalAccess.Cleanable;
import io.qt.QtObjectInterface;

/**
 * 

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 { private static class Data{ private O data; private Consumer cleanup; private Cleanable cleanable; @QtUninvokable void close(){ if(cleanup!=null) { try{ if(data!=null) cleanup.accept(data); }finally{ cleanup = null; } } data = null; } } private final Data data = new Data<>(); private QScopedPointer(O data, Consumer cleanup) { this.data.data = data; this.data.cleanup = Objects.requireNonNull(cleanup); this.data.cleanable = QtJambi_LibraryUtilities.internal.registerCleaner(this, this.data::close); } @QtUninvokable public O data() { return data.data; } @QtUninvokable public O get() { return data.data; } @QtUninvokable public O take() { O oldData = data.data; data.data = null; return oldData; } @QtUninvokable public void reset(O other) { if(data.cleanup!=null) { O oldData = data.data; data.data = other; data.cleanup.accept(oldData); } } @QtUninvokable public boolean isNull(){ return data.data==null; } @QtUninvokable public void swap(QScopedPointer other){ O oldData = data.data; Consumer oldCleanup = data.cleanup; this.data.data = other.data.data; this.data.cleanup = other.data.cleanup; other.data.data = oldData; other.data.cleanup = oldCleanup; } @Override @QtUninvokable public void close(){ data.close(); if(this.data.cleanable!=null) this.data.cleanable.clean(); } @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, QScopedPointer::dispose); } @QtUninvokable public static QScopedPointer disposingLater(O data){ return new QScopedPointer<>(data, QScopedPointer::disposeLater); } @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()); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy