summaryrefslogtreecommitdiff
path: root/opendc-stdlib/src/main/kotlin
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2018-02-16 15:25:19 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2018-02-18 23:38:35 +0100
commitf8a4095d1824df095ea91253f914bc0512646684 (patch)
tree1966b6f341cbaf634ec7c55fcaf1f195a6d45a73 /opendc-stdlib/src/main/kotlin
parentb84a995a05fecb9ef90c9184959f285f324e7411 (diff)
refactor(#18): Provide access to process context in nested calls
This change provides a method in the standard library to access the process context in nested suspending function calls.
Diffstat (limited to 'opendc-stdlib/src/main/kotlin')
-rw-r--r--opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt44
1 files changed, 44 insertions, 0 deletions
diff --git a/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt b/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt
new file mode 100644
index 00000000..acf3fe41
--- /dev/null
+++ b/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt
@@ -0,0 +1,44 @@
+package com.atlarge.opendc.simulator
+
+import kotlin.coroutines.experimental.intrinsics.*
+
+/**
+ * Try to find the [Context] instance associated with the [Process] in the call chain which has (indirectly) invoked the
+ * caller of this method.
+ *
+ * Note however that this method does not guarantee type-safety as this method allows the user to cast to a context
+ * with different generic type arguments.
+ *
+ * @return The context that has been found or `null` if this method is not called in a simulation context.
+ */
+suspend fun <S, M> contextOrNull(): Context<S, M>? = suspendCoroutineOrReturn { it.context[Context] }
+
+/**
+ * Find the [Context] instance associated with the [Process] in the call chain which has (indirectly) invoked the
+ * caller of this method.
+ *
+ * Note however that this method does not guarantee type-safety as this method allows the user to cast to a context
+ * with different generic type arguments.
+ *
+ * @throws IllegalStateException if the context cannot be found.
+ * @return The context that has been found.
+ */
+suspend fun <S, M> context(): Context<S, M> =
+ contextOrNull() ?: throw IllegalStateException("The suspending call does not have an associated process context")
+
+/**
+ * Try to find the untyped [Context] instance associated with the [Process] in the call chain which has (indirectly)
+ * invoked the caller of this method.
+ *
+ * @return The untyped context that has been found or `null` if this method is not called in a simulation context.
+ */
+suspend fun untypedContextOrNull(): Context<*, *>? = contextOrNull<Any?, Any?>()
+
+/**
+ * Find the [Context] instance associated with the [Process] in the call chain which has (indirectly) invoked the
+ * caller of this method.
+ *
+ * @throws IllegalStateException if the context cannot be found.
+ * @return The untyped context that has been found.
+ */
+suspend fun untypedContext(): Context<*, *> = context<Any?, Any?>()