Skip to content

Commit

Permalink
feat(api): [NFD] Update generator: use entrypoint name for static inv…
Browse files Browse the repository at this point in the history
…okers
  • Loading branch information
squid233 committed Dec 7, 2024
1 parent 2a0fd74 commit eef1fa4
Show file tree
Hide file tree
Showing 10 changed files with 652 additions and 604 deletions.
9 changes: 9 additions & 0 deletions buildSrc/src/main/kotlin/generators.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.gradle.api.Project
import org.gradle.kotlin.dsl.register

fun Project.registerGenerateTask(main: String, targetProject: String) {
tasks.register<GenerateTask>("generate") {
mainClass.set(main)
workingDir = project(targetProject).projectDir.resolve("src/main/java/")
}
}
5 changes: 1 addition & 4 deletions generators/nfd/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ dependencies {
implementation(project(":generators"))
}

tasks.register<GenerateTask>("generate") {
mainClass = "overrungl.nfd.NFDGeneratorKt"
workingDir = project(":nfd").projectDir.resolve("src/main/java/")
}
registerGenerateTask("overrungl.nfd.NFDGeneratorKt", ":nfd")
248 changes: 126 additions & 122 deletions generators/nfd/src/main/kotlin/overrungl/nfd/NFDGenerator.kt

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions generators/stb/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("generator.conventions")
}

dependencies {
implementation(project(":generators"))
}

registerGenerateTask("overrungl.stb.STBGeneratorKt", ":stb")
20 changes: 20 additions & 0 deletions generators/stb/src/main/kotlin/overrungl/stb/STBGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.stb

fun main() {
}
266 changes: 133 additions & 133 deletions modules/overrungl.nfd/src/main/java/overrungl/nfd/CNFD.java

Large diffs are not rendered by default.

599 changes: 302 additions & 297 deletions modules/overrungl.nfd/src/main/java/overrungl/nfd/NFD.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import java.util.NoSuchElementException;

import static java.lang.foreign.ValueLayout.ADDRESS;
import static overrungl.nfd.NFD.*;

/**
* A wrapper of NFD path set enumerator.
* <p>
* Users are responsible to close the instance after the iteration finished.
*
* @author squid233
* @see NFD#pathSetGetEnum(MemorySegment, MemorySegment)
* @see NFD#NFD_PathSet_GetEnum(MemorySegment, MemorySegment) NFD_PathSet_GetEnum
* @since 0.1.0
*/
public final class NFDEnumerator implements Iterable<String>, AutoCloseable {
Expand Down Expand Up @@ -80,35 +81,36 @@ public String next() {
}
try (MemoryStack stack = MemoryStack.pushLocal()) {
MemorySegment outPath = stack.allocate(ADDRESS);
int result = NFD.pathSetEnumNext(segment, outPath);
if (result == NFD.ERROR) throw errorIterating();
int result = NFD_PathSet_EnumNext(segment, outPath);
if (result == NFD_ERROR) throw errorIterating();
nextPath = Unmarshal.unmarshalStringPointer(outPath, NFDInternal.nfdCharset);
if (nextPath != null) {
NFD.pathSetFreePath(outPath.get(ADDRESS, 0));
NFD_PathSet_FreePath(outPath.get(ADDRESS, 0));
}
}
return curr;
}
}

/**
* Creates an enumerator from the given path set that is created with {@link NFD#openDialogMultiple}.
* Creates an enumerator from the given path set created with
* {@link NFD#NFD_OpenDialogMultiple(MemorySegment, NFDNFilterItem, String) NFD_OpenDialogMultiple}.
*
* @param allocator the allocator of the enumerator.
* @param pathSet the path set.
* @return the result and the enumerator.
*/
public static Tuple2.OfObjInt<NFDEnumerator> fromPathSet(SegmentAllocator allocator, MemorySegment pathSet) {
MemorySegment struct = allocator.allocate(MemoryLayout.structLayout(ADDRESS));
int result = NFD.pathSetGetEnum(pathSet, struct);
return new Tuple2.OfObjInt<>(result == NFD.OKAY ?
int result = NFD_PathSet_GetEnum(pathSet, struct);
return new Tuple2.OfObjInt<>(result == NFD_OKAY ?
new NFDEnumerator(struct) :
null,
result);
}

private static IllegalStateException errorIterating() {
return new IllegalStateException("Error iterating: " + NFD.getError());
return new IllegalStateException("Error iterating: " + NFD_GetError());
}

@NotNull
Expand All @@ -117,16 +119,16 @@ public Iterator<String> iterator() {
// TODO: 2023/7/6 Value object
try (MemoryStack stack = MemoryStack.pushLocal()) {
MemorySegment outPath = stack.allocate(ADDRESS);
int result = NFD.pathSetEnumNext(segment, outPath);
int result = NFD_PathSet_EnumNext(segment, outPath);
switch (result) {
case NFD.OKAY -> {
case NFD_OKAY -> {
String path = Unmarshal.unmarshalStringPointer(outPath, NFDInternal.nfdCharset);
if (path != null) {
NFD.pathSetFreePath(outPath.get(ADDRESS, 0));
NFD_PathSet_FreePath(outPath.get(ADDRESS, 0));
}
return new EnumIterator(path);
}
case NFD.CANCEL -> {
case NFD_CANCEL -> {
return EMPTY_ITERATOR;
}
default -> throw errorIterating();
Expand All @@ -136,6 +138,6 @@ public Iterator<String> iterator() {

@Override
public void close() {
NFD.pathSetFreeEnum(segment);
NFD_PathSet_FreeEnum(segment);
}
}
71 changes: 36 additions & 35 deletions modules/samples/src/test/java/overrungl/demo/nfd/NFDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import io.github.overrun.memstack.MemoryStack;
import overrungl.nfd.NFDEnumerator;
import overrungl.nfd.NFDNFilterItem;
import overrungl.nfd.NFD;

import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.util.Map;

import static overrungl.nfd.NFD.*;

/**
* @author squid233
* @since 0.1.0
Expand All @@ -35,7 +36,7 @@ private static void openDialog() {
// initialize NFD
// either call NFD::init at the start of your program and NFD::quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD.init();
NFD_Init();

try (MemoryStack stack = MemoryStack.pushLocal()) {
String[] outPath = new String[1];
Expand All @@ -46,25 +47,25 @@ private static void openDialog() {
Map.entry("Image file", "png,jpg"));

// show the dialog
final int result = NFD.openDialog(outPath, filterItem, null);
final int result = NFD_OpenDialog(outPath, filterItem, null);

switch (result) {
case NFD.ERROR -> System.err.println("Error: " + NFD.getError());
case NFD.OKAY -> System.out.println("Success! " + outPath[0]);
case NFD.CANCEL -> System.out.println("User pressed cancel.");
case NFD_ERROR -> System.err.println("Error: " + NFD_GetError());
case NFD_OKAY -> System.out.println("Success! " + outPath[0]);
case NFD_CANCEL -> System.out.println("User pressed cancel.");
}
}

// Quit NFD
NFD.quit();
NFD_Quit();
}

private static void openDialogMultiple() {
System.out.println("===== openDialogMultiple =====");
// initialize NFD
// either call NFD::init at the start of your program and NFD::quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD.init();
NFD_Init();

try (MemoryStack stack = MemoryStack.pushLocal()) {
MemorySegment pOutPaths = stack.allocate(ValueLayout.ADDRESS);
Expand All @@ -76,38 +77,38 @@ private static void openDialogMultiple() {
Map.entry("Image file", "png,jpg"));

// show the dialog
final int result = NFD.openDialogMultiple(pOutPaths, filterItem, null);
final int result = NFD_OpenDialogMultiple(pOutPaths, filterItem, null);
MemorySegment outPaths = pOutPaths.get(ValueLayout.ADDRESS, 0);

switch (result) {
case NFD.ERROR -> System.err.println("Error: " + NFD.getError());
case NFD.OKAY -> {
case NFD_ERROR -> System.err.println("Error: " + NFD_GetError());
case NFD_OKAY -> {
System.out.println("Success!");

long[] pNumPaths = new long[1];
NFD.pathSetGetCount(outPaths, pNumPaths);
NFD_PathSet_GetCount(outPaths, pNumPaths);
for (long i = 0, numPaths = pNumPaths[0]; i < numPaths; i++) {
NFD.pathSetGetPath(outPaths, i, outPath);
NFD_PathSet_GetPath(outPaths, i, outPath);
System.out.println("Path " + i + ": " + outPath[0]);
}

// remember to free the path-set memory (since NFDResult::OKAY is returned)
NFD.pathSetFree(outPaths);
NFD_PathSet_Free(outPaths);
}
case NFD.CANCEL -> System.out.println("User pressed cancel.");
case NFD_CANCEL -> System.out.println("User pressed cancel.");
}
}

// Quit NFD
NFD.quit();
NFD_Quit();
}

private static void openDialogMultipleEnum() {
System.out.println("===== openDialogMultipleEnum =====");
// initialize NFD
// either call NFD::init at the start of your program and NFD::quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD.init();
NFD_Init();

try (MemoryStack stack = MemoryStack.pushLocal()) {
MemorySegment pOutPaths = stack.allocate(ValueLayout.ADDRESS);
Expand All @@ -118,12 +119,12 @@ private static void openDialogMultipleEnum() {
Map.entry("Image file", "png,jpg"));

// show the dialog
final int result = NFD.openDialogMultiple(pOutPaths, filterItem, null);
final int result = NFD_OpenDialogMultiple(pOutPaths, filterItem, null);
MemorySegment outPaths = pOutPaths.get(ValueLayout.ADDRESS, 0);

switch (result) {
case NFD.ERROR -> System.err.println("Error: " + NFD.getError());
case NFD.OKAY -> {
case NFD_ERROR -> System.err.println("Error: " + NFD_GetError());
case NFD_OKAY -> {
System.out.println("Success!");

try (NFDEnumerator enumerator = NFDEnumerator.fromPathSet(stack, outPaths).x()) {
Expand All @@ -135,43 +136,43 @@ private static void openDialogMultipleEnum() {
}

// remember to free the path-set memory (since NFDResult::OKAY is returned)
NFD.pathSetFree(outPaths);
NFD_PathSet_Free(outPaths);
}
case NFD.CANCEL -> System.out.println("User pressed cancel.");
case NFD_CANCEL -> System.out.println("User pressed cancel.");
}
}

// Quit NFD
NFD.quit();
NFD_Quit();
}

private static void pickFolder() {
System.out.println("===== pickFolder =====");
// initialize NFD
// either call NFD::init at the start of your program and NFD::quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD.init();
NFD_Init();

String[] outPath = new String[1];

// show the dialog
final int result = NFD.pickFolder(outPath, null);
final int result = NFD_PickFolder(outPath, null);
switch (result) {
case NFD.ERROR -> System.err.println("Error: " + NFD.getError());
case NFD.OKAY -> System.out.println("Success! " + outPath[0]);
case NFD.CANCEL -> System.out.println("User pressed cancel.");
case NFD_ERROR -> System.err.println("Error: " + NFD_GetError());
case NFD_OKAY -> System.out.println("Success! " + outPath[0]);
case NFD_CANCEL -> System.out.println("User pressed cancel.");
}

// Quit NFD
NFD.quit();
NFD_Quit();
}

private static void saveDialog() {
System.out.println("===== saveDialog =====");
// initialize NFD
// either call NFD::init at the start of your program and NFD::quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD.init();
NFD_Init();

try (MemoryStack stack = MemoryStack.pushLocal()) {
String[] savePath = new String[1];
Expand All @@ -182,16 +183,16 @@ private static void saveDialog() {
Map.entry("Image file", "png,jpg"));

// show the dialog
final int result = NFD.saveDialog(savePath, filterItem, null, "Untitled.java");
final int result = NFD_SaveDialog(savePath, filterItem, null, "Untitled.java");
switch (result) {
case NFD.ERROR -> System.err.println("Error: " + NFD.getError());
case NFD.OKAY -> System.out.println("Success! " + savePath[0]);
case NFD.CANCEL -> System.out.println("User pressed cancel.");
case NFD_ERROR -> System.err.println("Error: " + NFD_GetError());
case NFD_OKAY -> System.out.println("Success! " + savePath[0]);
case NFD_CANCEL -> System.out.println("User pressed cancel.");
}
}

// Quit NFD
NFD.quit();
NFD_Quit();
}

public static void main(String[] args) {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ file("modules").listFiles()?.forEach {
include(
"generators:nfd",
"generators:opengl",
"generators:stb",
"generators:vulkan"
)

0 comments on commit eef1fa4

Please sign in to comment.