[hal] Add SimInt and SimLong wrappers for int/long SimValue (#3066)

This commit is contained in:
Peter Johnson
2021-01-09 23:26:19 -08:00
committed by GitHub
parent e620bd4d3f
commit fb99910c23
5 changed files with 274 additions and 0 deletions

View File

@@ -153,6 +153,30 @@ Java_edu_wpi_first_hal_SimDeviceJNI_getSimValue
return hal::CreateHALValue(env, HAL_GetSimValue(handle));
}
/*
* Class: edu_wpi_first_hal_SimDeviceJNI
* Method: getSimValueInt
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_SimDeviceJNI_getSimValueInt
(JNIEnv*, jclass, jint handle)
{
return HAL_GetSimValueInt(handle);
}
/*
* Class: edu_wpi_first_hal_SimDeviceJNI
* Method: getSimValueLong
* Signature: (I)J
*/
JNIEXPORT jlong JNICALL
Java_edu_wpi_first_hal_SimDeviceJNI_getSimValueLong
(JNIEnv*, jclass, jint handle)
{
return HAL_GetSimValueLong(handle);
}
/*
* Class: edu_wpi_first_hal_SimDeviceJNI
* Method: getSimValueDouble

View File

@@ -196,6 +196,30 @@ inline HAL_Value HAL_GetSimValue(HAL_SimValueHandle handle) {
} // extern "C++"
#endif
/**
* Gets a simulated value (int).
*
* @param handle simulated value handle
* @return The current value
*/
inline int32_t HAL_GetSimValueInt(HAL_SimValueHandle handle) {
struct HAL_Value v;
HAL_GetSimValue(handle, &v);
return v.type == HAL_INT ? v.data.v_int : 0;
}
/**
* Gets a simulated value (long).
*
* @param handle simulated value handle
* @return The current value
*/
inline int64_t HAL_GetSimValueLong(HAL_SimValueHandle handle) {
struct HAL_Value v;
HAL_GetSimValue(handle, &v);
return v.type == HAL_LONG ? v.data.v_long : 0;
}
/**
* Gets a simulated value (double).
*
@@ -248,6 +272,28 @@ inline void HAL_SetSimValue(HAL_SimValueHandle handle, const HAL_Value& value) {
} // extern "C++"
#endif
/**
* Sets a simulated value (int).
*
* @param handle simulated value handle
* @param value the value to set
*/
inline void HAL_SetSimValueInt(HAL_SimValueHandle handle, int value) {
struct HAL_Value v = HAL_MakeInt(value);
HAL_SetSimValue(handle, &v);
}
/**
* Sets a simulated value (long).
*
* @param handle simulated value handle
* @param value the value to set
*/
inline void HAL_SetSimValueLong(HAL_SimValueHandle handle, int64_t value) {
struct HAL_Value v = HAL_MakeLong(value);
HAL_SetSimValue(handle, &v);
}
/**
* Sets a simulated value (double).
*
@@ -342,6 +388,74 @@ class SimValue {
HAL_SimValueHandle m_handle = HAL_kInvalidHandle;
};
/**
* C++ wrapper around a HAL simulator int value handle.
*/
class SimInt : public SimValue {
public:
/**
* Default constructor that results in an "empty" object that is false in
* a boolean context.
*/
SimInt() = default;
/**
* Wraps a simulated value handle as returned by HAL_CreateSimValueInt().
*
* @param handle simulated value handle
*/
/*implicit*/ SimInt(HAL_SimValueHandle val) // NOLINT
: SimValue(val) {}
/**
* Gets the simulated value.
*
* @return The current value
*/
int32_t Get() const { return HAL_GetSimValueInt(m_handle); }
/**
* Sets the simulated value.
*
* @param value the value to set
*/
void Set(int32_t value) { HAL_SetSimValueInt(m_handle, value); }
};
/**
* C++ wrapper around a HAL simulator long value handle.
*/
class SimLong : public SimValue {
public:
/**
* Default constructor that results in an "empty" object that is false in
* a boolean context.
*/
SimLong() = default;
/**
* Wraps a simulated value handle as returned by HAL_CreateSimValueLong().
*
* @param handle simulated value handle
*/
/*implicit*/ SimLong(HAL_SimValueHandle val) // NOLINT
: SimValue(val) {}
/**
* Gets the simulated value.
*
* @return The current value
*/
int64_t Get() const { return HAL_GetSimValueLong(m_handle); }
/**
* Sets the simulated value.
*
* @param value the value to set
*/
void Set(int64_t value) { HAL_SetSimValueLong(m_handle, value); }
};
/**
* C++ wrapper around a HAL simulator double value handle.
*/