Renamed io to output

This commit is contained in:
Alexander Nozik 2019-03-18 14:36:12 +03:00
parent 5eb2bcfce6
commit 875a5ecf54
7 changed files with 28 additions and 20 deletions

View File

@ -1,6 +1,7 @@
package hep.dataforge.io package hep.dataforge.io
import hep.dataforge.context.AbstractPlugin import hep.dataforge.context.AbstractPlugin
import hep.dataforge.context.Context
import hep.dataforge.context.Plugin import hep.dataforge.context.Plugin
import hep.dataforge.context.PluginTag import hep.dataforge.context.PluginTag
import hep.dataforge.context.PluginTag.Companion.DATAFORGE_GROUP 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.EmptyName
import hep.dataforge.names.Name import hep.dataforge.names.Name
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlin.reflect.KClass import kotlin.reflect.KClass
/** /**
* A manager for outputs * A manager for outputs
*/ */
interface OutputManager : Plugin { 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 stage represents the node or directory for the output. Empty means root node.
* @param name represents the name inside the node. * @param name represents the name inside the node.
* @param meta configuration for [Output] (not for rendered object) * @param meta configuration for [Output] (not for rendered object)
*
*/ */
operator fun get(name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output<Any> operator fun <T : Any> get(type: KClass<out T>, name: Name, stage: Name = EmptyName, meta: Meta = EmptyMeta): Output<T>
/**
* Get an output specialized for giver ntype
*/
fun <T : Any> typed(type: KClass<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 * 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, name: Name,
stage: Name = EmptyName, stage: Name = EmptyName,
meta: Meta = EmptyMeta meta: Meta = EmptyMeta
): Output<T> { ): 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. * System console output.
* The [ConsoleOutput] is used when no other [OutputManager] is provided. * The [ConsoleOutput] is used when no other [OutputManager] is provided.
@ -52,12 +58,10 @@ expect val ConsoleOutput: Output<Any>
object ConsoleOutputManager : AbstractPlugin(), OutputManager { object ConsoleOutputManager : AbstractPlugin(), OutputManager {
override val tag: PluginTag = PluginTag("output.console", group = DATAFORGE_GROUP) 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> get(type: KClass<out T>, name: Name, stage: Name, meta: Meta): Output<T> = ConsoleOutput
override fun <T : Any> typed(type: KClass<T>, name: Name, stage: Name, meta: Meta): Output<T> = ConsoleOutput
} }
/** /**
* A dispatcher for output tasks. * A dispatcher for output tasks.
*/ */
expect val OutputDispatcher : CoroutineDispatcher expect val Dispatchers.Output: CoroutineDispatcher

View File

@ -5,6 +5,7 @@ import hep.dataforge.io.TextRenderer.Companion.TEXT_RENDERER_TYPE
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.provider.Type import hep.dataforge.provider.Type
import hep.dataforge.provider.provideAll import hep.dataforge.provider.provideAll
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -32,12 +33,15 @@ class TextOutput(override val context: Context, private val output: kotlinx.io.c
value value
} }
} }
context.launch(OutputDispatcher) { context.launch(Dispatchers.Output) {
renderer.run { output.render(obj) } renderer.run { output.render(obj) }
} }
} }
} }
/**
* A text or binary renderer based on [kotlinx.io.core.Output]
*/
@Type(TEXT_RENDERER_TYPE) @Type(TEXT_RENDERER_TYPE)
interface TextRenderer { interface TextRenderer {
/** /**

View File

@ -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

View File

@ -10,4 +10,4 @@ import kotlinx.io.streams.asOutput
*/ */
actual val ConsoleOutput: Output<Any> = TextOutput(Global, System.out.asOutput()) actual val ConsoleOutput: Output<Any> = TextOutput(Global, System.out.asOutput())
actual val OutputDispatcher = Dispatchers.IO actual val Dispatchers.Output get() = Dispatchers.IO

View File

@ -24,7 +24,7 @@ include(
":dataforge-meta-io", ":dataforge-meta-io",
":dataforge-context", ":dataforge-context",
":dataforge-data", ":dataforge-data",
":dataforge-io", ":dataforge-output",
":dataforge-workspace", ":dataforge-workspace",
":dataforge-scripting" ":dataforge-scripting"
) )