Skip to content

Commit

Permalink
add JsOscTypeTags.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Sep 6, 2024
1 parent fca8b03 commit dd31c96
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions xemantic-osc-api/src/jsMain/kotlin/Package.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* xemantic-osc - Kotlin idiomatic and multiplatform OSC protocol support
* Copyright (C) 2024 Kazimierz Pogoda
*
* This file is part of xemantic-osc.
*
* xemantic-osc is free software: you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* xemantic-osc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with xemantic-osc.
* If not, see <https://www.gnu.org/licenses/>.
*/

package com.xemantic.osc
45 changes: 45 additions & 0 deletions xemantic-osc-api/src/jsMain/kotlin/type/JsOscTypeTags.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* xemantic-osc - Kotlin idiomatic and multiplatform OSC protocol support
* Copyright (C) 2024 Kazimierz Pogoda
*
* This file is part of xemantic-osc.
*
* xemantic-osc is free software: you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* xemantic-osc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with xemantic-osc.
* If not, see <https://www.gnu.org/licenses/>.
*/

package com.xemantic.osc.type

import com.xemantic.osc.OscException

public actual val Any.oscTypeTag: String get() = when (this) {
is Number -> if (isInteger(this)) "i" else "d"
is String -> "s"
is ByteArray -> "b"
is Boolean -> oscTypeTag
is OscImpulse -> "I"
is OscTimeTag -> "t"
is Long -> "h"
is Char -> "c"
is OscColor -> "r"
is OscMidiMessage -> "m"
is List<*> -> "[${oscTypeTags()}]"
else -> throw OscException(
"Type unsupported in OSC: ${this::class}"
)
}

@Suppress("NOTHING_TO_INLINE")
private inline fun isInteger(
@Suppress("UNUSED_PARAMETER") o: Any
): Boolean {
return js("Number.isInteger(o)") as Boolean
}

0 comments on commit dd31c96

Please sign in to comment.