godot.core.bridge.Dictionary.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of godot-library-debug Show documentation
Show all versions of godot-library-debug Show documentation
Contains godot api as kotlin classes and jvm cpp interaction code.
@file:Suppress("unused", "PackageDirectoryMismatch")
package godot.core
import godot.annotation.CoreTypeHelper
import godot.core.memory.MemoryManager
import godot.core.memory.TransferContext
import godot.util.MapIterator
import godot.util.VoidPtr
class Dictionary : NativeCoreType, MutableMap {
internal var keyVariantType = VariantType.NIL
internal var valueVariantType = VariantType.NIL
@PublishedApi
internal constructor(handle: VoidPtr) {
keyVariantType = VariantType.ANY
valueVariantType = VariantType.ANY
_handle = handle
MemoryManager.registerNativeCoreType(this, VariantType.DICTIONARY)
}
@PublishedApi
internal constructor(keyVariantType: VariantType, valueVariantType: VariantType) {
this.keyVariantType = keyVariantType
this.valueVariantType = valueVariantType
_handle = Bridge.engine_call_constructor()
MemoryManager.registerNativeCoreType(this, VariantType.DICTIONARY)
}
//########################PUBLIC###############################
//PROPERTIES
override val size: Int
get() {
Bridge.engine_call_size(_handle)
return TransferContext.readReturnValue(VariantType.JVM_INT) as Int
}
override val keys: MutableSet
get() = object : AbstractMutableSet() {
override fun add(element: K): Boolean = throw UnsupportedOperationException("Add is not supported on keys")
override fun clear() {
[email protected]()
}
override operator fun contains(element: K): Boolean = containsKey(element)
override operator fun iterator(): MutableIterator {
val entryIterator = keys().iterator()
return object : MutableIterator {
override fun hasNext(): Boolean = entryIterator.hasNext()
override fun next(): K = entryIterator.next()
override fun remove() = throw UnsupportedOperationException("Remove is not supported on keys")
}
}
override fun remove(element: K): Boolean {
if (containsKey(element)) {
[email protected](element)
return true
}
return false
}
override val size: Int get() = [email protected]
}
override val values: MutableCollection
get() = object : AbstractMutableCollection() {
override fun add(element: V): Boolean =
throw UnsupportedOperationException("Add is not supported on values")
override fun clear() = [email protected]()
override operator fun contains(element: V): Boolean = containsValue(element)
override operator fun iterator(): MutableIterator {
val entryIterator = values().iterator()
return object : MutableIterator {
override fun hasNext(): Boolean = entryIterator.hasNext()
override fun next(): V = entryIterator.next()
override fun remove() = throw UnsupportedOperationException("Remove is not supported on values")
}
}
override val size: Int get() = [email protected]
}
override val entries: MutableSet>
get() = object : AbstractMutableSet>() {
override fun add(element: MutableMap.MutableEntry): Boolean {
val ret = has(element.key)
this@Dictionary[element.key] = element.value
return ret
}
override fun clear() {
[email protected]()
}
override operator fun contains(element: MutableMap.MutableEntry): Boolean {
val value = get(element.key, null)
if (value == element.value) {
return true
}
return false
}
override operator fun iterator(): MapIterator {
return MapIterator(
[email protected](),
this@Dictionary::get,
this@Dictionary::set,
this@Dictionary::erase
)
}
override fun remove(element: MutableMap.MutableEntry): Boolean {
val value = get(element.key, null)
if (value == element.value) {
[email protected](element.key)
return true
}
return false
}
override val size: Int get() = [email protected]
}
//CONSTRUCTOR
/**
* Create a shallow copy of the Dictionary
*/
constructor(other: Dictionary) {
keyVariantType = other.keyVariantType
valueVariantType = other.valueVariantType
_handle = other._handle
MemoryManager.registerNativeCoreType(this, VariantType.DICTIONARY)
}
//API
/**
* Clear the dictionary, removing all key/value pairs.
*/
override fun clear() {
Bridge.engine_call_clear(_handle)
}
override fun containsKey(key: K) = contains(key)
override fun containsValue(value: V): Boolean {
values.forEach {
if (it == value)
return true
}
return false
}
/**
* Creates a copy of the dictionary, and returns it.
* The deep parameter causes inner dictionaries and arrays to be copied recursively, but does not apply to objects.
*/
fun duplicate(deep: Boolean): Dictionary {
TransferContext.writeArguments(VariantType.BOOL to deep)
Bridge.engine_call_duplicate(_handle)
@Suppress("UNCHECKED_CAST")
return (TransferContext.readReturnValue(VariantType.DICTIONARY) as Dictionary).also {
it.keyVariantType = keyVariantType
it.valueVariantType = valueVariantType
}
}
/**
* Erase a dictionary key/value pair by key. Doesn't return a Boolean like the GDScript version because the GDNative function doesn't return anything
*/
fun erase(key: K) {
TransferContext.writeArguments(keyVariantType to key)
Bridge.engine_call_erase(_handle)
}
fun findKey(value: V): K {
TransferContext.writeArguments(valueVariantType to value)
Bridge.engine_call_find_key(_handle)
@Suppress("UNCHECKED_CAST")
return TransferContext.readReturnValue(keyVariantType, false) as K
}
/**
* Returns the current value for the specified key in the Dictionary.
* If the key does not exist, the method returns the value of the optional default argument, or null if it is omitted.
*/
fun get(key: K, default: V?): V? {
TransferContext.writeArguments(keyVariantType to key, valueVariantType to default)
Bridge.engine_call_get(_handle)
@Suppress("UNCHECKED_CAST")
return TransferContext.readReturnValue(valueVariantType, true) as V
}
/**
* Returns true if the dictionary has a given key.
* Note: This is equivalent to using the in operator as follows:
*/
fun has(key: K): Boolean {
TransferContext.writeArguments(keyVariantType to key)
Bridge.engine_call_has(_handle)
return TransferContext.readReturnValue(VariantType.BOOL) as Boolean
}
/**
* Returns true if the dictionary has all of the keys in the given array.
*/
fun hasAll(keys: VariantArray): Boolean {
TransferContext.writeArguments(VariantType.ARRAY to keys)
Bridge.engine_call_hasAll(_handle)
return TransferContext.readReturnValue(VariantType.BOOL) as Boolean
}
/**
* Returns a hashed integer value representing the dictionary contents. This can be used to compare dictionaries by value
*/
fun hash(): Int {
Bridge.engine_call_hash(_handle)
return TransferContext.readReturnValue(VariantType.JVM_INT) as Int
}
/**
* Returns true if the dictionary is read-only. See [makeReadOnly]
*/
fun isReadOnly(): Boolean {
Bridge.engine_call_is_read_only(_handle)
return TransferContext.readReturnValue(VariantType.BOOL, false) as Boolean
}
/**
* Returns true if the dictionary is empty.
*/
override fun isEmpty(): Boolean {
Bridge.engine_call_is_empty(_handle)
return TransferContext.readReturnValue(VariantType.BOOL) as Boolean
}
/**
* Returns the list of keys in the Dictionary.
*/
fun keys(): VariantArray {
Bridge.engine_call_keys(_handle)
@Suppress("UNCHECKED_CAST")
return (TransferContext.readReturnValue(VariantType.ARRAY) as VariantArray).also {
it.variantType = keyVariantType
}
}
fun makeReadOnly() {
Bridge.engine_call_make_read_only(_handle)
}
fun merge(dictionary: Dictionary, overwrite: Boolean = false) {
TransferContext.writeArguments(VariantType.DICTIONARY to dictionary, VariantType.BOOL to overwrite)
Bridge.engine_call_merge(_handle)
}
override fun put(key: K, value: V): V? {
val ret = get(key, null)
set(key, value)
return ret
}
override fun putAll(from: Map) {
from.forEach {
set(it.key, it.value)
}
}
override fun remove(key: K): V? {
val ret = get(key, null)
erase(key)
return ret
}
/**
* Returns the list of values in the Dictionary.
*/
fun values(): VariantArray {
Bridge.engine_call_values(_handle)
@Suppress("UNCHECKED_CAST")
return (TransferContext.readReturnValue(VariantType.ARRAY) as VariantArray).also {
it.variantType = valueVariantType
}
}
//UTILITIES
override operator fun get(key: K): V {
TransferContext.writeArguments(keyVariantType to key)
Bridge.engine_call_operator_get(_handle)
@Suppress("UNCHECKED_CAST")
return TransferContext.readReturnValue(valueVariantType, true) as V
}
@CoreTypeHelper
inline fun get(key: K, block: V.() -> R): R {
val localCopy = this[key]
val ret = localCopy.block()
this[key] = localCopy
return ret
}
operator fun set(key: K, value: V) {
TransferContext.writeArguments(keyVariantType to key, valueVariantType to value)
Bridge.engine_call_operator_set(_handle)
}
operator fun contains(key: K): Boolean = has(key)
override fun equals(other: Any?): Boolean {
if (other == null || other !is Dictionary<*, *>) {
return false
}
TransferContext.writeArguments(VariantType.DICTIONARY to this, VariantType.DICTIONARY to other)
Bridge.engine_call_equals(_handle)
return TransferContext.readReturnValue(VariantType.BOOL) as Boolean
}
override fun hashCode(): Int {
return _handle.hashCode()
}
override fun toString(): String {
return "Dictionary($size)"
}
@Suppress("FunctionName")
private object Bridge {
external fun engine_call_constructor(): VoidPtr
external fun engine_call_clear(_handle: VoidPtr)
external fun engine_call_duplicate(_handle: VoidPtr)
external fun engine_call_erase(_handle: VoidPtr)
external fun engine_call_find_key(_handle: VoidPtr)
external fun engine_call_get(_handle: VoidPtr)
external fun engine_call_has(_handle: VoidPtr)
external fun engine_call_hasAll(_handle: VoidPtr)
external fun engine_call_hash(_handle: VoidPtr)
external fun engine_call_is_empty(_handle: VoidPtr)
external fun engine_call_is_read_only(_handle: VoidPtr)
external fun engine_call_keys(_handle: VoidPtr)
external fun engine_call_make_read_only(_handle: VoidPtr)
external fun engine_call_merge(_handle: VoidPtr)
external fun engine_call_size(_handle: VoidPtr)
external fun engine_call_values(_handle: VoidPtr)
external fun engine_call_operator_get(_handle: VoidPtr)
external fun engine_call_operator_set(_handle: VoidPtr)
external fun engine_call_equals(_handle: VoidPtr)
}
companion object {
inline operator fun invoke(): Dictionary {
val keyVariantType = variantMapper[K::class]
checkNotNull(keyVariantType) {
"Can't create a Dictionary with generic key ${K::class}."
}
val valVariantType = variantMapper[V::class]
checkNotNull(valVariantType) {
"Can't create a Dictionary with generic value ${V::class}."
}
return Dictionary(keyVariantType, valVariantType)
}
}
}
inline fun dictionaryOf(vararg args: Pair) = Dictionary().also {
it.putAll(args)
}
/**
* Convert a Map into a Dictionary
*/
inline fun Map.toDictionary() = Dictionary().also {
it.putAll(this)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy