From b5e46c5b0d37771d49c3c348c06641663579e69e Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:49:48 +0800 Subject: [PATCH] feat(generator): [NFD] Update generator --- .../main/java/overrungl/nfd/NFDGenerator.kt | 190 +++++++++++++++- .../main/kotlin/overrungl/gen/Constants.kt | 4 +- .../src/main/java/overrungl/nfd/CNFD.java | 18 +- .../src/main/java/overrungl/nfd/NFD.java | 26 --- .../main/java/overrungl/nfd/NFDstatic.java | 215 +++++++++++++++++- .../main/java/overrungl/demo/nfd/NFDTest.java | 3 +- 6 files changed, 403 insertions(+), 53 deletions(-) diff --git a/generators/nfd/src/main/java/overrungl/nfd/NFDGenerator.kt b/generators/nfd/src/main/java/overrungl/nfd/NFDGenerator.kt index d67989cf..39877245 100644 --- a/generators/nfd/src/main/java/overrungl/nfd/NFDGenerator.kt +++ b/generators/nfd/src/main/java/overrungl/nfd/NFDGenerator.kt @@ -72,6 +72,7 @@ val U8char = CharVariant( fun main() { val NFDInternal = ClassName.get("overrungl.nfd", "NFDInternal") + //region Structs fun NFDFilterItem(variant: CharVariant, utf: String, charset: CodeBlock) { struct( "overrungl.nfd", @@ -195,6 +196,7 @@ fun main() { NFDPickFolderArgs(U8char) NFDPickFolderArgs(Nchar) StructRegistration.generate("overrungl.nfd", "NFDStructTypes") + //endregion downcall("overrungl.nfd", "CNFD", javadoc = { doFirst { add("Base functions of {@link \$T}.", ClassName.get("overrungl.nfd", "NFD")) } @@ -217,6 +219,7 @@ fun main() { ) } + //region methods fun freePath(variant: CharVariant) { "freePath${variant.uppercaseName}"( void, @@ -257,10 +260,10 @@ fun main() { variant.nfdchar_t_ptr_ptr("outPath"), variant.const_nfdfilteritem_t_ptr("filterList"), nfdfiltersize_t("filterCount") { - javadoc = CodeBlock.of("If zero, filterList is ignored (you can use null).") + javadoc = CodeBlock.of("If zero, filterList is ignored (you can use null).\n") }, variant.const_nfdchar_t_ptr("defaultPath") { - javadoc = CodeBlock.of("If null, the operating system will decide.") + javadoc = CodeBlock.of("If null, the operating system will decide.\n") }, entrypoint = "NFD_OpenDialog${variant.uppercaseName}", javadoc = { @@ -317,10 +320,10 @@ fun main() { const_nfdpathset_t_ptr_ptr("outPaths"), variant.const_nfdfilteritem_t_ptr("filterList"), nfdfiltersize_t("filterCount") { - javadoc = CodeBlock.of("If zero, filterList is ignored (you can use null).") + javadoc = CodeBlock.of("If zero, filterList is ignored (you can use null).\n") }, variant.const_nfdchar_t_ptr("defaultPath") { - javadoc = CodeBlock.of("If null, the operating system will decide.") + javadoc = CodeBlock.of("If null, the operating system will decide.\n") }, entrypoint = "NFD_OpenDialogMultiple${variant.uppercaseName}", javadoc = { @@ -385,10 +388,10 @@ fun main() { variant.nfdchar_t_ptr_ptr("outPath"), variant.const_nfdfilteritem_t_ptr("filterList"), nfdfiltersize_t("filterCount") { - javadoc = CodeBlock.of("If zero, filterList is ignored (you can use null).") + javadoc = CodeBlock.of("If zero, filterList is ignored (you can use null).\n") }, variant.const_nfdchar_t_ptr("defaultPath") { - javadoc = CodeBlock.of("If null, the operating system will decide.") + javadoc = CodeBlock.of("If null, the operating system will decide.\n") }, variant.const_nfdchar_t_ptr("defaultName"), entrypoint = "NFD_SaveDialog${variant.uppercaseName}", @@ -446,7 +449,7 @@ fun main() { nfdresult_t, variant.nfdchar_t_ptr_ptr("outPath"), variant.const_nfdchar_t_ptr("defaultPath") { - javadoc = CodeBlock.of("If null, the operating system will decide.") + javadoc = CodeBlock.of("If null, the operating system will decide.\n") }, entrypoint = "NFD_PickFolder${variant.uppercaseName}", javadoc = { @@ -502,7 +505,7 @@ fun main() { nfdresult_t, const_nfdpathset_t_ptr_ptr("outPaths"), variant.const_nfdchar_t_ptr("defaultPath") { - javadoc = CodeBlock.of("If null, the operating system will decide.") + javadoc = CodeBlock.of("If null, the operating system will decide.\n") }, entrypoint = "NFD_PickFolderMultiple${variant.uppercaseName}", javadoc = { @@ -682,6 +685,7 @@ fun main() { entrypoint = "NFD_PathSet_Free", javadoc = { add("Free the pathSet") } ) + //endregion }.also { // TODO val path = Path("overrungl", "nfd", "NFDstatic.java") @@ -714,11 +718,179 @@ fun main() { .build() ) } + + val nfdCharset = ", NFDInternal.nfdCharset" + val StringArray = ArrayTypeName.of(String::class.java) + + fun openDialog(variant: CharVariant, charset: String) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("openDialog${variant.uppercaseName}") + .addJavadoc("Overloads {@link #openDialog${variant.uppercaseName}(MemorySegment, MemorySegment, int, MemorySegment)}") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(StringArray, "outPath") + .addParameter(ClassName.get("overrungl.nfd", "NFD${variant.uppercaseName}FilterItem"), "filterList") + .addParameter(String::class.java, "defaultPath") + .beginControlFlow("try (MemoryStack stack = MemoryStack.pushLocal())") + .addStatement("var seg = Marshal.marshal(stack, outPath$1L)", charset) + .addStatement( + "int result = openDialog${variant.uppercaseName}(seg, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath$1L))", + charset + ) + .beginControlFlow("if (result == OKAY)") + .addStatement("copyOutPath${variant.uppercaseName}(seg, outPath)") + .endControlFlow() + .addStatement("return result") + .endControlFlow() + .build() + ) + } + openDialog(Nchar, nfdCharset) + openDialog(U8char, "") + + fun openDialogWith(variant: CharVariant, charset: String) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("openDialog${variant.uppercaseName}With") + .addJavadoc("Overloads {@link #openDialog${variant.uppercaseName}With(MemorySegment, MemorySegment)}") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(StringArray, "outPath") + .addParameter(ClassName.get("overrungl.nfd", "NFDOpenDialog${variant.uppercaseName}Args"), "args") + .beginControlFlow("try (MemoryStack stack = MemoryStack.pushLocal())") + .addStatement("var seg = Marshal.marshal(stack, outPath$1L)", charset) + .addStatement("int result = openDialog${variant.uppercaseName}With(seg, Marshal.marshal(args))") + .beginControlFlow("if (result == OKAY)") + .addStatement("copyOutPath${variant.uppercaseName}(seg, outPath)") + .endControlFlow() + .addStatement("return result") + .endControlFlow() + .build() + ) + } + openDialogWith(Nchar, nfdCharset) + openDialogWith(U8char, "") + + fun openDialogMultiple(variant: CharVariant, charset: String) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("openDialogMultiple${variant.uppercaseName}") + .addJavadoc("Overloads {@link #openDialogMultiple${variant.uppercaseName}(MemorySegment, MemorySegment, int, MemorySegment)") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(MemorySegment_, "outPaths") + .addParameter(ClassName.get("overrungl.nfd", "NFD${variant.uppercaseName}FilterItem"), "filterList") + .addParameter(String::class.java, "defaultPath") + .beginControlFlow("try (MemoryStack stack = MemoryStack.pushLocal())") + .addStatement( + "return openDialogMultiple${variant.uppercaseName}(outPaths, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath$1L))", + charset + ) + .endControlFlow() + .build() + ) + } + openDialogMultiple(Nchar, nfdCharset) + openDialogMultiple(U8char, "") + + fun openDialogMultipleWith(variant: CharVariant) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("openDialogMultiple${variant.uppercaseName}With") + .addJavadoc("Overloads {@link #openDialogMultiple${variant.uppercaseName}With(MemorySegment, MemorySegment)") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(MemorySegment_, "outPaths") + .addParameter(ClassName.get("overrungl.nfd", "NFDOpenDialog${variant.uppercaseName}Args"), "args") + .addStatement("return openDialogMultiple${variant.uppercaseName}With(outPaths, Marshal.marshal(args))") + .build() + ) + } + openDialogMultipleWith(Nchar) + openDialogMultipleWith(U8char) + + + fun saveDialog(variant: CharVariant, charset: String) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("saveDialog${variant.uppercaseName}") + .addJavadoc("Overloads {@link #saveDialog${variant.uppercaseName}(MemorySegment, MemorySegment, int, MemorySegment, MemorySegment)}") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(StringArray, "outPath") + .addParameter(ClassName.get("overrungl.nfd", "NFD${variant.uppercaseName}FilterItem"), "filterList") + .addParameter(String::class.java, "defaultPath") + .addParameter(String::class.java, "defaultName") + .beginControlFlow("try (MemoryStack stack = MemoryStack.pushLocal())") + .addStatement("var seg = Marshal.marshal(stack, outPath$1L)", charset) + .addStatement( + "int result = saveDialog${variant.uppercaseName}(seg, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath$1L), Marshal.marshal(stack, defaultName$1L))", + charset + ) + .beginControlFlow("if (result == OKAY)") + .addStatement("copyOutPath${variant.uppercaseName}(seg, outPath)") + .endControlFlow() + .addStatement("return result") + .endControlFlow() + .build() + ) + } + saveDialog(Nchar, nfdCharset) + saveDialog(U8char, "") + + fun saveDialogWith(variant: CharVariant, charset: String) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("saveDialog${variant.uppercaseName}With") + .addJavadoc("Overloads {@link #saveDialog${variant.uppercaseName}With(MemorySegment, MemorySegment)}") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(StringArray, "outPath") + .addParameter(ClassName.get("overrungl.nfd", "NFDSaveDialog${variant.uppercaseName}Args"), "args") + .beginControlFlow("try (MemoryStack stack = MemoryStack.pushLocal())") + .addStatement("var seg = Marshal.marshal(stack, outPath$1L)", charset) + .addStatement("int result = saveDialog${variant.uppercaseName}With(seg, Marshal.marshal(args))") + .beginControlFlow("if (result == OKAY)") + .addStatement("copyOutPath${variant.uppercaseName}(seg, outPath)") + .endControlFlow() + .addStatement("return result") + .endControlFlow() + .build() + ) + } + saveDialogWith(Nchar, nfdCharset) + saveDialogWith(U8char, "") + + + fun pickFolder(variant: CharVariant, charset: String) { + codeBuilder.add( + "$1L", + MethodSpec.methodBuilder("pickFolder${variant.uppercaseName}") + .addJavadoc("Overloads {@link #pickFolder${variant.uppercaseName}(MemorySegment, MemorySegment)") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(TypeName.INT) + .addParameter(StringArray, "outPath") + .addParameter(String::class.java, "defaultPath") + .beginControlFlow("try (MemoryStack stack = MemoryStack.pushLocal())") + .addStatement("var seg = Marshal.marshal(stack, outPath$1L)", charset) + .addStatement("int result = pickFolder${variant.uppercaseName}(seg, Marshal.marshal(stack, defaultPath$1L))", charset) + .beginControlFlow("if (result == OKAY)") + .addStatement("copyOutPath${variant.uppercaseName}(seg, outPath)") + .endControlFlow() + .addStatement("return result") + .endControlFlow() + .build() + ) + } + pickFolder(Nchar, nfdCharset) + pickFolder(U8char, "") + Files.writeString( path, "${split[0]}$GENERATOR_BEGIN\n${ codeBuilder.build().toString().prependIndent(" ") - }\n $GENERATOR_END${split[2]}" + }$GENERATOR_END${split[2]}" ) } } diff --git a/generators/src/main/kotlin/overrungl/gen/Constants.kt b/generators/src/main/kotlin/overrungl/gen/Constants.kt index e049971d..fd62d3d2 100644 --- a/generators/src/main/kotlin/overrungl/gen/Constants.kt +++ b/generators/src/main/kotlin/overrungl/gen/Constants.kt @@ -32,5 +32,5 @@ copies or substantial portions of the Software. This file is auto-generated. DO NOT EDIT!""" -const val GENERATOR_BEGIN = "// ---[BEGIN GENERATOR BEGIN]---" -const val GENERATOR_END = "// ---[END GENERATOR END]---" +const val GENERATOR_BEGIN = "//region ---[BEGIN GENERATOR BEGIN]---" +const val GENERATOR_END = "//endregion ---[END GENERATOR END]---" diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/CNFD.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/CNFD.java index c367c295..9ec9b30f 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/CNFD.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/CNFD.java @@ -103,7 +103,8 @@ public interface CNFD extends DirectAccess { *

* It's the caller's responsibility to free {@code outPath} via {@link #freePathN} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @CType("nfdresult_t") @Entrypoint("NFD_OpenDialogN") @@ -117,7 +118,8 @@ int openDialogN(@CType("nfdnchar_t**") MemorySegment outPath, *

* It's the caller's responsibility to free {@code outPath} via {@link #freePathU8} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @CType("nfdresult_t") @Entrypoint("NFD_OpenDialogU8") @@ -177,7 +179,8 @@ default int openDialogU8With(@CType("nfdu8char_t**") MemorySegment outPath, *

* It is the caller's responsibility to free {@code outPaths} via {@link #pathSetFree} if this function * returns NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @CType("nfdresult_t") @Entrypoint("NFD_OpenDialogMultipleN") @@ -191,7 +194,8 @@ int openDialogMultipleN(@CType("const nfdpathset_t**") MemorySegment outPaths, *

* It is the caller's responsibility to free {@code outPaths} via {@link #pathSetFree} if this function * returns NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @CType("nfdresult_t") @Entrypoint("NFD_OpenDialogMultipleU8") @@ -253,7 +257,8 @@ default int openDialogMultipleU8With(@CType("const nfdpathset_t**") MemorySegmen *

* It is the caller's responsibility to free {@code outPath} via {@link #freePathN} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @CType("nfdresult_t") @Entrypoint("NFD_SaveDialogN") @@ -268,7 +273,8 @@ int saveDialogN(@CType("nfdnchar_t**") MemorySegment outPath, *

* It is the caller's responsibility to free {@code outPath} via {@link #freePathU8} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @CType("nfdresult_t") @Entrypoint("NFD_SaveDialogU8") diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java index 817a1987..47595492 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java @@ -174,32 +174,6 @@ default int openDialogN(String[] outPath, NFDNFilterItem filterList, String defa } } - /** - * Single file open dialog - * - * @param outPath the out path - * @param filterList the filter list - * @param defaultPath If null, the operating system will decide. - * @return the result - * @see #openDialogU8(MemorySegment, MemorySegment, int, MemorySegment) nopenDialogU8 - */ - @Skip - default int openDialogU8(String[] outPath, NFDU8FilterItem filterList, String defaultPath) { - try (MemoryStack stack = MemoryStack.pushLocal()) { - final MemorySegment seg = Marshal.marshal(stack, outPath); - final int result = openDialogU8(seg, - Marshal.marshal(filterList), - filterList != null ? Math.toIntExact(filterList.elementCount()) : 0, - Marshal.marshal(stack, defaultPath, NFDInternal.nfdCharset)); - if (result == OKAY) { - final MemorySegment path = seg.get(Unmarshal.STR_LAYOUT, 0L); - outPath[0] = path.getString(0L, NFDInternal.nfdCharset); - freePathU8(path); - } - return result; - } - } - /** * Multiple file open dialog * diff --git a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDstatic.java b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDstatic.java index 54631fe4..47581ca9 100644 --- a/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDstatic.java +++ b/modules/overrungl.nfd/src/main/java/overrungl/nfd/NFDstatic.java @@ -16,9 +16,15 @@ package overrungl.nfd; +import io.github.overrun.memstack.MemoryStack; import overrun.marshal.Downcall; import overrun.marshal.DowncallOption; +import overrun.marshal.Marshal; +import overrun.marshal.Unmarshal; +import overrun.marshal.struct.Struct; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; import java.lang.invoke.MethodHandles; /** @@ -27,7 +33,8 @@ */ // TODO this -> NFD public final class NFDstatic { - // ---[BEGIN GENERATOR BEGIN]--- + //@formatter:off + //region ---[BEGIN GENERATOR BEGIN]--- /** * Programmatic error */ @@ -103,7 +110,8 @@ public static void quit() { *

* It's the caller's responsibility to free {@code outPath} via {@link #freePathN} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @overrun.marshal.gen.CType("nfdresult_t") @overrun.marshal.gen.Entrypoint("NFD_OpenDialogN") @@ -119,7 +127,8 @@ public static int openDialogN( *

* It's the caller's responsibility to free {@code outPath} via {@link #freePathU8} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @overrun.marshal.gen.CType("nfdresult_t") @overrun.marshal.gen.Entrypoint("NFD_OpenDialogU8") @@ -183,7 +192,8 @@ public static int openDialogU8With( *

* It is the caller's responsibility to free {@code outPaths} via {@link #pathSetFree} if this function * returns NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @overrun.marshal.gen.CType("nfdresult_t") @overrun.marshal.gen.Entrypoint("NFD_OpenDialogMultipleN") @@ -199,7 +209,8 @@ public static int openDialogMultipleN( *

* It is the caller's responsibility to free {@code outPaths} via {@link #pathSetFree} if this function * returns NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @overrun.marshal.gen.CType("nfdresult_t") @overrun.marshal.gen.Entrypoint("NFD_OpenDialogMultipleU8") @@ -267,7 +278,8 @@ public static int openDialogMultipleU8With( *

* It is the caller's responsibility to free {@code outPath} via {@link #freePathN} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @overrun.marshal.gen.CType("nfdresult_t") @overrun.marshal.gen.Entrypoint("NFD_SaveDialogN") @@ -284,7 +296,8 @@ public static int saveDialogN( *

* It is the caller's responsibility to free {@code outPath} via {@link #freePathU8} if this function returns * NFD_OKAY. - * @param filterCount If zero, filterList is ignored (you can use null).@param defaultPath If null, the operating system will decide. + * @param filterCount If zero, filterList is ignored (you can use null). + * @param defaultPath If null, the operating system will decide. */ @overrun.marshal.gen.CType("nfdresult_t") @overrun.marshal.gen.Entrypoint("NFD_SaveDialogU8") @@ -649,8 +662,192 @@ public static void pathSetFree( @overrun.marshal.gen.CType("const nfdpathset_t*") java.lang.foreign.MemorySegment pathSet) { getInstance().pathSetFree(pathSet); } - - // ---[END GENERATOR END]--- + /** + * Overloads {@link #openDialogN(MemorySegment, MemorySegment, int, MemorySegment)} + */ + public static int openDialogN(java.lang.String[] outPath, overrungl.nfd.NFDNFilterItem filterList, + java.lang.String defaultPath) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath, NFDInternal.nfdCharset); + int result = openDialogN(seg, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath, NFDInternal.nfdCharset)); + if (result == OKAY) { + copyOutPathN(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #openDialogU8(MemorySegment, MemorySegment, int, MemorySegment)} + */ + public static int openDialogU8(java.lang.String[] outPath, overrungl.nfd.NFDU8FilterItem filterList, + java.lang.String defaultPath) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath); + int result = openDialogU8(seg, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath)); + if (result == OKAY) { + copyOutPathU8(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #openDialogNWith(MemorySegment, MemorySegment)} + */ + public static int openDialogNWith(java.lang.String[] outPath, + overrungl.nfd.NFDOpenDialogNArgs args) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath, NFDInternal.nfdCharset); + int result = openDialogNWith(seg, Marshal.marshal(args)); + if (result == OKAY) { + copyOutPathN(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #openDialogU8With(MemorySegment, MemorySegment)} + */ + public static int openDialogU8With(java.lang.String[] outPath, + overrungl.nfd.NFDOpenDialogU8Args args) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath); + int result = openDialogU8With(seg, Marshal.marshal(args)); + if (result == OKAY) { + copyOutPathU8(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #openDialogMultipleN(MemorySegment, MemorySegment, int, MemorySegment) + */ + public static int openDialogMultipleN(java.lang.foreign.MemorySegment outPaths, + overrungl.nfd.NFDNFilterItem filterList, java.lang.String defaultPath) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + return openDialogMultipleN(outPaths, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath, NFDInternal.nfdCharset)); + } + } + /** + * Overloads {@link #openDialogMultipleU8(MemorySegment, MemorySegment, int, MemorySegment) + */ + public static int openDialogMultipleU8(java.lang.foreign.MemorySegment outPaths, + overrungl.nfd.NFDU8FilterItem filterList, java.lang.String defaultPath) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + return openDialogMultipleU8(outPaths, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath)); + } + } + /** + * Overloads {@link #openDialogMultipleNWith(MemorySegment, MemorySegment) + */ + public static int openDialogMultipleNWith(java.lang.foreign.MemorySegment outPaths, + overrungl.nfd.NFDOpenDialogNArgs args) { + return openDialogMultipleNWith(outPaths, Marshal.marshal(args)); + } + /** + * Overloads {@link #openDialogMultipleU8With(MemorySegment, MemorySegment) + */ + public static int openDialogMultipleU8With(java.lang.foreign.MemorySegment outPaths, + overrungl.nfd.NFDOpenDialogU8Args args) { + return openDialogMultipleU8With(outPaths, Marshal.marshal(args)); + } + /** + * Overloads {@link #saveDialogN(MemorySegment, MemorySegment, int, MemorySegment, MemorySegment)} + */ + public static int saveDialogN(java.lang.String[] outPath, overrungl.nfd.NFDNFilterItem filterList, + java.lang.String defaultPath, java.lang.String defaultName) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath, NFDInternal.nfdCharset); + int result = saveDialogN(seg, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath, NFDInternal.nfdCharset), Marshal.marshal(stack, defaultName, NFDInternal.nfdCharset)); + if (result == OKAY) { + copyOutPathN(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #saveDialogU8(MemorySegment, MemorySegment, int, MemorySegment, MemorySegment)} + */ + public static int saveDialogU8(java.lang.String[] outPath, overrungl.nfd.NFDU8FilterItem filterList, + java.lang.String defaultPath, java.lang.String defaultName) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath); + int result = saveDialogU8(seg, Marshal.marshal(filterList), filterItemCount(filterList), Marshal.marshal(stack, defaultPath), Marshal.marshal(stack, defaultName)); + if (result == OKAY) { + copyOutPathU8(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #saveDialogNWith(MemorySegment, MemorySegment)} + */ + public static int saveDialogNWith(java.lang.String[] outPath, + overrungl.nfd.NFDSaveDialogNArgs args) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath, NFDInternal.nfdCharset); + int result = saveDialogNWith(seg, Marshal.marshal(args)); + if (result == OKAY) { + copyOutPathN(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #saveDialogU8With(MemorySegment, MemorySegment)} + */ + public static int saveDialogU8With(java.lang.String[] outPath, + overrungl.nfd.NFDSaveDialogU8Args args) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath); + int result = saveDialogU8With(seg, Marshal.marshal(args)); + if (result == OKAY) { + copyOutPathU8(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #pickFolderN(MemorySegment, MemorySegment) + */ + public static int pickFolderN(java.lang.String[] outPath, java.lang.String defaultPath) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath, NFDInternal.nfdCharset); + int result = pickFolderN(seg, Marshal.marshal(stack, defaultPath, NFDInternal.nfdCharset)); + if (result == OKAY) { + copyOutPathN(seg, outPath); + } + return result; + } + } + /** + * Overloads {@link #pickFolderU8(MemorySegment, MemorySegment) + */ + public static int pickFolderU8(java.lang.String[] outPath, java.lang.String defaultPath) { + try (MemoryStack stack = MemoryStack.pushLocal()) { + var seg = Marshal.marshal(stack, outPath); + int result = pickFolderU8(seg, Marshal.marshal(stack, defaultPath)); + if (result == OKAY) { + copyOutPathU8(seg, outPath); + } + return result; + } + } + //endregion ---[END GENERATOR END]--- + //@formatter:on + + private static int filterItemCount(Struct struct) { + return struct != null ? Math.toIntExact(struct.elementCount()) : 0; + } + + private static void copyOutPathN(MemorySegment src, String[] outPath) { + Unmarshal.copy(src, outPath); + freePathN(src.get(ValueLayout.ADDRESS, 0)); + } + + private static void copyOutPathU8(MemorySegment src, String[] outPath) { + Unmarshal.copy(src, outPath); + freePathU8(src.get(ValueLayout.ADDRESS, 0)); + } public static CNFD getInstance() { final class Holder { diff --git a/modules/samples/src/main/java/overrungl/demo/nfd/NFDTest.java b/modules/samples/src/main/java/overrungl/demo/nfd/NFDTest.java index fce37f99..8c56165a 100644 --- a/modules/samples/src/main/java/overrungl/demo/nfd/NFDTest.java +++ b/modules/samples/src/main/java/overrungl/demo/nfd/NFDTest.java @@ -21,6 +21,7 @@ import overrungl.nfd.NFDEnumerator; import overrungl.nfd.NFDNFilterItem; +import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; import java.lang.foreign.ValueLayout; import java.util.Map; @@ -79,7 +80,7 @@ private static void openDialogMultiple() { // show the dialog final int result = nfd.openDialogMultipleN(pOutPaths, filterItem, null); - MemorySegment outPaths = pOutPaths.get(ValueLayout.ADDRESS, 0); + MemorySegment outPaths = pOutPaths.get(ValueLayout.ADDRESS, 0).reinterpret(Arena.global(), nfd::pathSetFreePathN); switch (result) { case NFD.ERROR -> System.err.println("Error: " + nfd.getError());