-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VehicleController: make it also implement RefTarget
- Loading branch information
1 parent
32092af
commit baf9b9a
Showing
4 changed files
with
216 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [email protected] | ||
*/ | ||
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); | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/com/github/stephengold/joltjni/VehicleControllerRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<VehicleController>}) | ||
* | ||
* @author Stephen Gold [email protected] | ||
*/ | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<VehicleController *> (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<VehicleController *> (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<VehicleController *> (controllerVa); | ||
Ref<VehicleController> * const pResult | ||
= new Ref<VehicleController>(pController); | ||
TRACE_NEW("Ref<VehicleController>", pResult) | ||
return reinterpret_cast<jlong> (pResult); | ||
} |