Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,50 @@ class LogTable @JvmOverloads constructor(
fun get(key: String, default: Array<String>) =
get(key, default.asLogValue()).value as Array<String>

/** Gets an enum item from the table at the specified [key],
/**
* Gets an enum item from the table at the specified [key],
* If the data does not exist or is of the wrong type,
* the [default] is returned.
*/
inline fun <reified E : Enum<E>> get(key: String, default: E) =
enumValueOf<E>(get(key, default.name))
get(key, default, E::class.java)

/**
* Gets an enum array item from the table at the specified [key],
* If the data does not exist or is of the wrong type,
* the [default] is returned.
*/
inline fun <reified E : Enum<E>> get(key: String, default: Array<E>) =
get(key, default.map { it.name }.toTypedArray()).map { enumValueOf<E>(it) }.toTypedArray()
get(key, default, E::class.java)

/**
* Gets an enum item from the table at the specified [key],
* If the data does not exist or is of the wrong type,
* the [default] is returned.
*
* This method requires the enum's class, this is due to Java's type
* erasure, and Kotlin users should leave out the class parameter.
*/
fun <E: Enum<E>> get(key: String, default: E, clazz: Class<E>): E =
java.lang.Enum.valueOf<E>(clazz, get(key, default.name))

/**
* Gets an enum array item from the table at the specified [key],
* If the data does not exist or is of the wrong type,
* the [default] is returned.
*
* This method requires the enum's class, this is due to Java's type
* erasure, and Kotlin users should leave out the class parameter.
*/
@Suppress("UNCHECKED_CAST")
fun <E: Enum<E>> get(key: String, default: Array<E>, clazz: Class<E>): Array<E> {
val names = get(key, default.map { it.name }.toTypedArray())
return (java.lang.reflect.Array.newInstance(clazz, names.size) as Array<E>).apply {
names.forEachIndexed { index, name ->
set(index, java.lang.Enum.valueOf<E>(clazz, name))
}
}
}

/**
* Gets a color object from the table at the specified [key],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ abstract class AutoLoggableInputs : LoggableInputs {
) {
operator fun setValue(thisRef: Any, property: KProperty<*>, newValue: T) { value = newValue }
operator fun getValue(thisRef: Any, property: KProperty<*>) = value

operator fun provideDelegate(thisRef: Any, property: KProperty<*>): Field<T> {
toLogs.add { it.toLog(key, value) }
fromLogs.add { it.fromLog(key, value) }
Expand Down
29 changes: 29 additions & 0 deletions docs/docs/functionality/data-types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: "Logged Data"
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

:::info
Log data is stored in the same way as AdvantageKit, with string keys and
Expand All @@ -23,6 +25,33 @@ Chrono supports these simple types and their arrays:
Chrono also supports logging and replaying generic enum values and enum arrays.
They are represented by string values from the enum's ``name()`` method.

:::note
Enum logging requires Java users to pass the class of your enum to retrieve it from the log table.
This is unnecessary for Kotlin users. Check the example below.

<Tabs groupId="lang" queryString="lang">
<TabItem value="kt" label="Kotlin" default>

```kotlin
override fun fromLog(table: LogTable) {
state = table.get("State", state)
stateArray = table.get("StateArray", stateArray)
}
```
</TabItem>
<TabItem value="java" label="Java" default>

```java
@Override
public void fromLog(LogTable table) {
state = table.get("State", state, State.class);
stateArray = table.get("StateArray", stateArray, State.class);
}
```
</TabItem>
</Tabs>
:::

### Normalized Colors
Chrono also supports logging and replaying ``NormalizedRGBA`` objects from color sensors.
These are represented by float arrays, storing the colors components in order:
Expand Down