Renamed io to output
This commit is contained in:
parent
5eb2bcfce6
commit
875a5ecf54
@ -1,6 +1,7 @@
|
||||
package hep.dataforge.io
|
||||
|
||||
import hep.dataforge.context.AbstractPlugin
|
||||
import hep.dataforge.context.Context
|
||||
import hep.dataforge.context.Plugin
|
||||
import hep.dataforge.context.PluginTag
|
||||
import hep.dataforge.context.PluginTag.Companion.DATAFORGE_GROUP
|
||||
@ -9,40 +10,45 @@ import hep.dataforge.meta.Meta
|
||||
import hep.dataforge.names.EmptyName
|
||||
import hep.dataforge.names.Name
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/**
|
||||
* A manager for outputs
|
||||
*/
|
||||
interface OutputManager : Plugin {
|
||||
|
||||
/**
|
||||
* Provide an output for given name and stage.
|
||||
*
|
||||
* Get an output specialized for given type, name and stage.
|
||||
* @param stage represents the node or directory for the output. Empty means root node.
|
||||
* @param name represents the name inside the node.
|
||||
* @param meta configuration for [Output] (not for rendered object)
|
||||
*
|
||||
*/
|
||||
operator fun get(name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output<Any>
|
||||
|
||||
/**
|
||||
* Get an output specialized for giver ntype
|
||||
*/
|
||||
fun <T : Any> typed(type: KClass<T>, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output<T>
|
||||
|
||||
operator fun <T : Any> get(type: KClass<out T>, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an output manager for a context
|
||||
*/
|
||||
val Context.output: OutputManager get() = plugins.get() ?: ConsoleOutputManager
|
||||
|
||||
/**
|
||||
* Get an output with given [name], [stage] and reified content type
|
||||
*/
|
||||
inline fun <reified T : Any> OutputManager.typed(
|
||||
inline operator fun <reified T : Any> OutputManager.get(
|
||||
name: Name,
|
||||
stage: Name = EmptyName,
|
||||
meta: Meta = EmptyMeta
|
||||
): Output<T> {
|
||||
return typed(T::class, name, stage, meta)
|
||||
return get(T::class, name, stage, meta)
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly render an object using the most suitable renderer
|
||||
*/
|
||||
fun OutputManager.render(obj: Any, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta) =
|
||||
get(obj::class, name,stage).render(obj,meta)
|
||||
|
||||
/**
|
||||
* System console output.
|
||||
* The [ConsoleOutput] is used when no other [OutputManager] is provided.
|
||||
@ -52,12 +58,10 @@ expect val ConsoleOutput: Output<Any>
|
||||
object ConsoleOutputManager : AbstractPlugin(), OutputManager {
|
||||
override val tag: PluginTag = PluginTag("output.console", group = DATAFORGE_GROUP)
|
||||
|
||||
override fun get(name: Name, stage: Name, meta: Meta): Output<Any> = ConsoleOutput
|
||||
|
||||
override fun <T : Any> typed(type: KClass<T>, name: Name, stage: Name, meta: Meta): Output<T> = ConsoleOutput
|
||||
override fun <T : Any> get(type: KClass<out T>, name: Name, stage: Name, meta: Meta): Output<T> = ConsoleOutput
|
||||
}
|
||||
|
||||
/**
|
||||
* A dispatcher for output tasks.
|
||||
*/
|
||||
expect val OutputDispatcher : CoroutineDispatcher
|
||||
expect val Dispatchers.Output: CoroutineDispatcher
|
@ -5,6 +5,7 @@ import hep.dataforge.io.TextRenderer.Companion.TEXT_RENDERER_TYPE
|
||||
import hep.dataforge.meta.Meta
|
||||
import hep.dataforge.provider.Type
|
||||
import hep.dataforge.provider.provideAll
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@ -32,12 +33,15 @@ class TextOutput(override val context: Context, private val output: kotlinx.io.c
|
||||
value
|
||||
}
|
||||
}
|
||||
context.launch(OutputDispatcher) {
|
||||
context.launch(Dispatchers.Output) {
|
||||
renderer.run { output.render(obj) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A text or binary renderer based on [kotlinx.io.core.Output]
|
||||
*/
|
||||
@Type(TEXT_RENDERER_TYPE)
|
||||
interface TextRenderer {
|
||||
/**
|
@ -19,4 +19,4 @@ actual val ConsoleOutput: Output<Any> = object : Output<Any> {
|
||||
|
||||
}
|
||||
|
||||
actual val OutputDispatcher: CoroutineDispatcher = Dispatchers.Default
|
||||
actual val Dispatchers.Output: CoroutineDispatcher get() = Dispatchers.Default
|
@ -10,4 +10,4 @@ import kotlinx.io.streams.asOutput
|
||||
*/
|
||||
actual val ConsoleOutput: Output<Any> = TextOutput(Global, System.out.asOutput())
|
||||
|
||||
actual val OutputDispatcher = Dispatchers.IO
|
||||
actual val Dispatchers.Output get() = Dispatchers.IO
|
@ -24,7 +24,7 @@ include(
|
||||
":dataforge-meta-io",
|
||||
":dataforge-context",
|
||||
":dataforge-data",
|
||||
":dataforge-io",
|
||||
":dataforge-output",
|
||||
":dataforge-workspace",
|
||||
":dataforge-scripting"
|
||||
)
|
Loading…
Reference in New Issue
Block a user