summaryrefslogtreecommitdiff
path: root/opendc-stdlib/src/main/kotlin/com/atlarge/opendc/simulator/Helpers.kt
blob: 0f6392edd9a0e55fc414ffe12176cf82a69941c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.atlarge.opendc.simulator

import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn

/**
 * 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?>()