Skip to content

Commit

Permalink
VehicleController: make it also implement RefTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Oct 16, 2024
1 parent 32092af commit baf9b9a
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
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 &rarr; make the current object the owner, false &rarr;
* 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 (&ge;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
Expand All @@ -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);

Expand All @@ -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);
}
77 changes: 77 additions & 0 deletions src/main/native/glue/v/VehicleController.cpp
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);
}

0 comments on commit baf9b9a

Please sign in to comment.