Skip to content

Commit

Permalink
Added inline to create and invoke methods, made ValueProperty Java fr…
Browse files Browse the repository at this point in the history
…iendly, updated readme
  • Loading branch information
retrodaredevil committed Dec 1, 2019
1 parent 62db5b1 commit f61c0d1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# abstract-dashboard
An abstraction of NetworkTables, Shuffleboard, SmartDashboard, and LiveWindow

### Why use this?
This library has no global state, meaning that you are free to use NetworkTables how you want to.

Although this library was heavily influenced by NetworkTables, Shuffleboard, etc, this library gives
your program a layer of abstraction that allows you to change how different things work later.

### Why was this created
While working on [robo-sim](https://github.com/frc1444/robo-sim), I needed to be able to completely
clear the shuffleboard state and restart NetworkTables. Because the Shuffleboard API in WPILib is
lots and lots of static variables, I was unable to clear that global state.

In an attempt to be able to restart a robot simulation without restarting the JVM,
I over-engineered this beast while (in my opinion) improving on some tightly coupled aspects
of the NetworkTable/Sendable/Shuffleboard library in WPILib. Some people won't like the change that this
library brings, and that's OK. I needed this library to be very loosely coupled to work in my simulation
and it won't fit everyone's needs.

### How to use this?
See [robot2019-sim](https://github.com/frc1444/robot2019-sim) for an example.

### Who should use this?
Until this has been used for a full year, it is not recommended for public use. However, if you are
an advanced team and are OK with using libraries outside of WPILib

### TODO
* Nothing right now!
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ interface ChooserListener {
val IGNORE = ChooserListener {}

@JvmSynthetic
operator fun invoke(lambda: (String) -> Unit) = object : ChooserListener {
override fun onKeyChange(key: String) {
lambda(key)
}
inline operator fun invoke(crossinline lambda: (String) -> Unit) = object : ChooserListener {
override fun onKeyChange(key: String) = lambda(key)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface EnabledProvider {

companion object {
@JvmSynthetic
operator fun invoke(lambda: () -> Boolean) = object : EnabledProvider {
inline operator fun invoke(crossinline lambda: () -> Boolean) = object : EnabledProvider {
override val isEnabled: Boolean
get() = lambda()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ interface MetadataEditor {
@JvmField
val NOTHING = MetadataEditor {}

operator fun invoke(lambda: (metadataDashboard: BasicDashboard) -> Unit) = object : MetadataEditor {
override fun editMetadata(metadataDashboard: BasicDashboard) {
lambda(metadataDashboard)
}
@JvmSynthetic
inline operator fun invoke(crossinline lambda: (metadataDashboard: BasicDashboard) -> Unit) = object : MetadataEditor {
override fun editMetadata(metadataDashboard: BasicDashboard) = lambda(metadataDashboard)
}
}
}
23 changes: 18 additions & 5 deletions core/src/main/java/com/first1444/dashboard/value/ValueProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ interface ValueProperty {
fun setValue(value: BasicValue)
val supportsSetting: Boolean

@FunctionalInterface
interface Getter {
fun get(): BasicValue
}
@FunctionalInterface
interface Setter {
fun set(value: BasicValue)
}
companion object {
@JvmStatic
fun createGetOnly(getter: () -> BasicValue) = object : ValueProperty {
@JvmSynthetic
inline fun createGetOnly(crossinline getter: () -> BasicValue) = object : ValueProperty {
override fun getValue(): BasicValue = getter()
override val supportsGetting: Boolean = true

Expand All @@ -20,7 +28,9 @@ interface ValueProperty {

}
@JvmStatic
fun createSetOnly(setter: (BasicValue) -> Unit) = object : ValueProperty {
fun createGetOnly(getter: Getter) = createGetOnly(getter::get)
@JvmSynthetic
inline fun createSetOnly(crossinline setter: (BasicValue) -> Unit) = object : ValueProperty {
override fun getValue(): BasicValue {
throw UnsupportedOperationException()
}
Expand All @@ -32,14 +42,17 @@ interface ValueProperty {
override val supportsSetting: Boolean = true
}
@JvmStatic
fun create(getter: () -> BasicValue, setter: (BasicValue) -> Unit) = object : ValueProperty {
fun createSetOnly(setter: Setter) = createSetOnly(setter::set)
@JvmSynthetic
inline fun create(crossinline getter: () -> BasicValue, crossinline setter: (BasicValue) -> Unit) = object : ValueProperty {
override fun getValue(): BasicValue = getter()
override val supportsGetting: Boolean = true
override fun setValue(value: BasicValue) {
setter(value)
}
override val supportsSetting: Boolean = true

}
@JvmStatic
fun create(getter: Getter, setter: Setter) = create(getter::get, setter::set)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface EntryListener {

companion object {
@JvmSynthetic
operator fun invoke(lambda: (EntryNotification) -> Unit) = object : EntryListener {
inline operator fun invoke(crossinline lambda: (EntryNotification) -> Unit) = object : EntryListener {
override fun onNotification(notification: EntryNotification) = lambda(notification)
}
}
Expand Down

0 comments on commit f61c0d1

Please sign in to comment.