diff --git a/src/main/java/com/github/stephengold/joltjni/VehicleController.java b/src/main/java/com/github/stephengold/joltjni/VehicleController.java index 16cdde0..e57ae5d 100644 --- a/src/main/java/com/github/stephengold/joltjni/VehicleController.java +++ b/src/main/java/com/github/stephengold/joltjni/VehicleController.java @@ -21,12 +21,15 @@ of this software and associated documentation files (the "Software"), to deal */ package com.github.stephengold.joltjni; +import com.github.stephengold.joltjni.template.Ref; +import com.github.stephengold.joltjni.template.RefTarget; + /** * Control the acceleration and deceleration of a vehicle. * * @author Stephen Gold sgold@sonic.net */ -public class VehicleController extends NonCopyable { +public class VehicleController extends NonCopyable implements RefTarget { // ************************************************************************* // fields @@ -69,4 +72,51 @@ public class VehicleController extends NonCopyable { public VehicleConstraint getConstraint() { return constraint; } + // ************************************************************************* + // RefTarget methods + + /** + * Count the active references to the native {@code VehicleController}. The + * controller is unaffected. + * + * @return the count (≥0) + */ + @Override + public int getRefCount() { + long controllerVa = va(); + int result = getRefCount(controllerVa); + + return result; + } + + /** + * Mark the native {@code VehicleController} as embedded. + */ + @Override + public void setEmbedded() { + long controllerVa = va(); + setEmbedded(controllerVa); + } + + /** + * Create a counted reference to the native {@code VehicleController}. + * + * @return a new JVM object with a new native object assigned + */ + @Override + public Ref toRef() { + long controllerVa = va(); + long copyVa = toRef(controllerVa); + Ref result = new VehicleControllerRef(copyVa, true); + + return result; + } + // ************************************************************************* + // native methods + + native private static int getRefCount(long controllerVa); + + native private static void setEmbedded(long controllerVa); + + native static long toRef(long controllerVa); } diff --git a/src/main/java/com/github/stephengold/joltjni/VehicleControllerRef.java b/src/main/java/com/github/stephengold/joltjni/VehicleControllerRef.java new file mode 100644 index 0000000..0f99e0d --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/VehicleControllerRef.java @@ -0,0 +1,86 @@ +/* +Copyright (c) 2024 Stephen Gold + +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. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +import com.github.stephengold.joltjni.template.Ref; + +/** + * A counted reference to a {@code VehicleController}. (native type: + * {@code Ref}) + * + * @author Stephen Gold sgold@sonic.net + */ +final public class VehicleControllerRef extends Ref { + // ************************************************************************* + // constructors + + /** + * Instantiate a reference with the specified native object assigned. + * + * @param refVa the virtual address of the native object to assign (not + * zero) + * @param owner true → make the current object the owner, false → + * the current object isn't the owner + */ + VehicleControllerRef(long refVa, boolean owner) { + Runnable freeingAction = owner ? () -> free(refVa) : null; + setVirtualAddress(refVa, freeingAction); + } + // ************************************************************************* + // Ref methods + + /** + * Temporarily access the referenced {@code VehicleController}. + * + * @return a new JVM object that refers to the pre-existing native object + */ + @Override + public VehicleController getPtr() { + long refVa = va(); + long controllerVa = getPtr(refVa); + VehicleController result = new VehicleController(null, controllerVa); + + return result; + } + + /** + * Create a counted reference to the native {@code VehicleController}. + * + * @return a new JVM object with a new native object assigned + */ + @Override + public VehicleControllerRef toRef() { + long refVa = va(); + long copyVa = copy(refVa); + VehicleControllerRef result = new VehicleControllerRef(copyVa, true); + + return result; + } + // ************************************************************************* + // native private methods + + native private static long copy(long refVa); + + native private static void free(long refVa); + + native private static long getPtr(long refVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/WheeledVehicleController.java b/src/main/java/com/github/stephengold/joltjni/WheeledVehicleController.java index 5c4f018..f375bc6 100644 --- a/src/main/java/com/github/stephengold/joltjni/WheeledVehicleController.java +++ b/src/main/java/com/github/stephengold/joltjni/WheeledVehicleController.java @@ -63,30 +63,7 @@ public void setDriverInput( setDriverInput(controllerVa, forward, right, brake, handBrake); } // ************************************************************************* - // RefTarget methods - - /** - * Count the active references to the native - * {@code WheeledVehicleController}. The controller is unaffected. - * - * @return the count (≥0) - */ - @Override - public int getRefCount() { - long controllerVa = va(); - int result = getRefCount(controllerVa); - - return result; - } - - /** - * Mark the native {@code WheeledVehicleController} as embedded. - */ - @Override - public void setEmbedded() { - long controllerVa = va(); - setEmbedded(controllerVa); - } + // VehicleController methods /** * Create a counted reference to the native @@ -97,7 +74,7 @@ public void setEmbedded() { @Override public WheeledVehicleControllerRef toRef() { long controllerVa = va(); - long copyVa = toRef(controllerVa); + long copyVa = VehicleController.toRef(controllerVa); WheeledVehicleControllerRef result = new WheeledVehicleControllerRef(copyVa, true); @@ -106,12 +83,6 @@ public WheeledVehicleControllerRef toRef() { // ************************************************************************* // native private methods - native private static int getRefCount(long shapeVa); - native private static void setDriverInput(long controllerVa, float forward, float right, float brake, float handBrake); - - native private static void setEmbedded(long shapeVa); - - native private static long toRef(long shapeVa); } diff --git a/src/main/native/glue/v/VehicleController.cpp b/src/main/native/glue/v/VehicleController.cpp new file mode 100644 index 0000000..8f75b24 --- /dev/null +++ b/src/main/native/glue/v/VehicleController.cpp @@ -0,0 +1,77 @@ +/* +Copyright (c) 2024 Stephen Gold + +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. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ + +/* + * Author: Stephen Gold + */ +#include "Jolt/Jolt.h" +#include "Jolt/Physics/Vehicle/VehicleController.h" +#include "auto/com_github_stephengold_joltjni_VehicleController.h" +#include "glue/glue.h" + +using namespace JPH; + +IMPLEMENT_REF(VehicleController, + Java_com_github_stephengold_joltjni_VehicleController_copy, + Java_com_github_stephengold_joltjni_VehicleController_createEmpty, + Java_com_github_stephengold_joltjni_VehicleController_free, + Java_com_github_stephengold_joltjni_VehicleController_getPtr) + +/* + * Class: com_github_stephengold_joltjni_VehicleController + * Method: getRefCount + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_VehicleController_getRefCount + (JNIEnv *, jclass, jlong controllerVa) { + const VehicleController * const pController + = reinterpret_cast (controllerVa); + const uint32 result = pController->GetRefCount(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_VehicleController + * Method: setEmbedded + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_VehicleController_setEmbedded + (JNIEnv *, jclass, jlong controllerVa) { + VehicleController * const pController + = reinterpret_cast (controllerVa); + pController->SetEmbedded(); +} + +/* + * Class: com_github_stephengold_joltjni_VehicleController + * Method: toRef + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_VehicleController_toRef + (JNIEnv *, jclass, jlong controllerVa) { + VehicleController * const pController + = reinterpret_cast (controllerVa); + Ref * const pResult + = new Ref(pController); + TRACE_NEW("Ref", pResult) + return reinterpret_cast (pResult); +} \ No newline at end of file