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

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

The 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.Arrays;
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 QScopedArrayPointer * to be used inside a try-with-resource block.

*

Example:

* * try(var pointer = QScopedArrayPointer.disposing(new QDialog(), new QColor())){
*     QDialog dialog = pointer.get(0);
*     dialog.exec();
* }
* // dialog is disposed *
* */ public final class QScopedArrayPointer 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) { for(O o : data) cleanup.accept(o); } }finally{ cleanup = null; } } data = null; } } private final Data data = new Data<>(); private QScopedArrayPointer(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==null ? null : Arrays.copyOf(data.data, data.data.length); } @QtUninvokable public O[] get() { return data(); } @QtUninvokable public O data(int i) { return data.data==null ? null : data.data[i]; } @QtUninvokable public O get(int i) { return data(i); } @QtUninvokable public O[] take() { if(data.data==null) return null; O[] oldData = data.data; data.data = null; return oldData; } @QtUninvokable public O take(int i) { if(data.data==null) return null; O oldData = data.data[i]; data.data[i] = null; return oldData; } @QtUninvokable @SafeVarargs public final void reset(O... other) { if(data.cleanup!=null) { O[] oldData = data.data; data.data = other; if(oldData!=null) { for(O o : oldData) data.cleanup.accept(o); } } } @QtUninvokable public boolean isNull(){ return data.data==null; } @QtUninvokable public void swap(QScopedArrayPointer 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(); } @SafeVarargs @QtUninvokable public static QScopedArrayPointer disposing(O... data){ return new QScopedArrayPointer<>(data, QScopedArrayPointer::dispose); } @SafeVarargs @QtUninvokable public static QScopedArrayPointer disposingLater(O... data){ return new QScopedArrayPointer<>(data, QScopedArrayPointer::disposeLater); } @SafeVarargs @QtUninvokable public static QScopedArrayPointer cleanup(Consumer cleanup, O... data){ return new QScopedArrayPointer<>(data, cleanup); } /** * Performs an action on resources which will be disposes subsequently. * * @param data multiple resources * @param action the action */ @SafeVarargs @QtUninvokable public static void performAndDispose(Consumer action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.disposing(data)){ action.accept(ptr.get()); } } /** * Performs an action on multiple {@link QObject} resources which will be called {@link QObject#disposeLater()} subsequently. * * @param data multiple resources * @param action the action */ @SafeVarargs @QtUninvokable public static void performAndDisposeLater(Consumer action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.disposingLater(data)){ action.accept(ptr.get()); } } /** * Performs an action on resources which will be cleaned up subsequently. * * @param data multiple resources * @param action the action */ @SafeVarargs @QtUninvokable public static void performAndCleanup(Consumer cleanup, Consumer action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.cleanup(cleanup, data)){ action.accept(ptr.get()); } } /** * Performs an action on resources which will be disposes subsequently. * * @param data multiple resources * @param action the action * @return result */ @SafeVarargs @QtUninvokable public static R performAndDispose(Function action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.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 multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static R performAndDisposeLater(Function action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.disposingLater(data)){ return action.apply(ptr.get()); } } /** * Performs an action on resources which will be cleaned up subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static R performAndCleanup(Consumer cleanup, Function action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.cleanup(cleanup, data)){ return action.apply(ptr.get()); } } /** * Performs an action on resources which will be disposes subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static int performAndDispose(ToIntFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.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 multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static int performAndDisposeLater(ToIntFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.disposingLater(data)){ return action.applyAsInt(ptr.get()); } } /** * Performs an action on resources which will be cleaned up subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static int performAndCleanup(Consumer cleanup, ToIntFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.cleanup(cleanup, data)){ return action.applyAsInt(ptr.get()); } } /** * Performs an action on resources which will be disposes subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static double performAndDispose(ToDoubleFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.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 multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static double performAndDisposeLater(ToDoubleFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.disposingLater(data)){ return action.applyAsDouble(ptr.get()); } } /** * Performs an action on resources which will be cleaned up subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static double performAndCleanup(Consumer cleanup, ToDoubleFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.cleanup(cleanup, data)){ return action.applyAsDouble(ptr.get()); } } /** * Performs an action on resources which will be disposes subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static long performAndDispose(ToLongFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.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 multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static long performAndDisposeLater(ToLongFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.disposingLater(data)){ return action.applyAsLong(ptr.get()); } } /** * Performs an action on resources which will be cleaned up subsequently. * * @param data multiple resources * @param action the action * @return result */ @QtUninvokable @SafeVarargs public static long performAndCleanup(Consumer cleanup, ToLongFunction action, O... data){ try(QScopedArrayPointer ptr = QScopedArrayPointer.cleanup(cleanup, data)){ return action.applyAsLong(ptr.get()); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy