AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ArrayView.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <span>
   15#include <AUI/Util/ARaiiHelper.h>
   16#include "Globals.h"
   17#include "Converter.h"
   18
   19namespace aui::jni {
   20    namespace detail {
   21        struct CastHelper {
   22            jobject value;
   23
   24            template<typename T>
   25            operator T() const noexcept {
   26                return static_cast<T>(value);
   27            }
   28        };
   29    }
   30
   35    template<convertible T>
   36    class ArrayView: public GlobalRef {
   37    public:
   38        using GlobalRef::GlobalRef;
   39
   40        static ArrayView allocate(std::size_t size) {
   41            return (aui::jni::env()->*TypedMethods<T>::NewArray)(size);
   42        }
   43
   44        std::size_t size() const noexcept {
   45            return aui::jni::env()->GetArrayLength((jarray)asObject());
   46        }
   47
   48        template<aui::invocable<std::span<const T>> Callback>
   49        void view(Callback&& callback) const {
   50            //TypedMethods<T>::
   51            jboolean isCopy;
   52            auto ptr = (aui::jni::env()->*TypedMethods<T>::GetArrayElements)(detail::CastHelper{asObject()}, &isCopy);
   53            ARaiiHelper releaser = [&] {
   54                (aui::jni::env()->*TypedMethods<T>::ReleaseArrayElements)(detail::CastHelper{asObject()}, ptr, JNI_ABORT);
   55            };
   56
   57            callback(std::span<const T>(ptr, size()));
   58        }
   59
   60        template<aui::invocable<std::span<T>> Callback>
   61        void modify(Callback&& callback) const {
   62            //TypedMethods<T>::
   63            jboolean isCopy;
   64            auto ptr = (aui::jni::env()->*TypedMethods<T>::GetArrayElements)(detail::CastHelper{asObject()}, &isCopy);
   65            ARaiiHelper releaser = [&] {
   66                (aui::jni::env()->*TypedMethods<T>::ReleaseArrayElements)(detail::CastHelper{asObject()}, ptr, 0);
   67            };
   68
   69            callback(std::span<T>(ptr, size()));
   70        }
   71
   72        void set(std::span<const T> data, std::size_t at = 0) {
   73            (aui::jni::env()->*TypedMethods<T>::SetArrayRegion)(detail::CastHelper{asObject()}, at, data.size(), data.data());
   74        }
   75    };
   76
   77    template<convertible T>
   78    struct Converter<ArrayView<T>>: public Converter<std::string> {
   79        static constexpr auto signature = "["_asl + ::aui::jni::signature_v<T>;
   80
   81        static ArrayView<T> fromJni(jobject val) {
   82            return val;
   83        }
   84
   85        static jobject toJni(const ArrayView<T>& value) {
   86            return value.asObject();
   87        }
   88    };
   89}
Definition ARaiiHelper.h:17
Java array view.
Definition ArrayView.h:36
JNIEnv * env()
Definition Globals.h:38
Definition Converter.h:21
Definition TypedMethods.h:18
Definition SharedPtrTypes.h:115