Add variant conversion for Option[T]
This commit is contained in:
parent
c2db807da4
commit
1b8e7ccd98
2 changed files with 23 additions and 2 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
version = "0.7.23"
|
version = "0.7.24"
|
||||||
author = "Xored Software, Inc."
|
author = "Xored Software, Inc."
|
||||||
description = "Godot Engine bindings"
|
description = "Godot Engine bindings"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# Copyright 2018 Xored Software, Inc.
|
# Copyright 2018 Xored Software, Inc.
|
||||||
|
|
||||||
import tables, typetraits, macros, unicode, strutils, sets
|
import tables, typetraits, macros, unicode, strutils, sets, options
|
||||||
import gdnativeapi
|
import gdnativeapi
|
||||||
import core/godotcoretypes, core/godotbase
|
import core/godotcoretypes, core/godotbase
|
||||||
import core/vector2, core/rect2,
|
import core/vector2, core/rect2,
|
||||||
|
|
@ -788,6 +788,27 @@ proc fromVariant*[T: array](s: var T, val: Variant): ConversionResult =
|
||||||
else:
|
else:
|
||||||
result = ConversionResult.TypeError
|
result = ConversionResult.TypeError
|
||||||
|
|
||||||
|
proc godotTypeInfo*[T](OptT: typedesc[Option[T]]): GodotTypeInfo =
|
||||||
|
when compiles(godotTypeInfo(T)):
|
||||||
|
result.variantType = godotTypeInfo(T)
|
||||||
|
else:
|
||||||
|
result.variantType = VariantType.Nil
|
||||||
|
|
||||||
|
proc toVariant*[T](option: Option[T]): Variant =
|
||||||
|
if option.isSome():
|
||||||
|
result = toVariant(option.unsafeGet())
|
||||||
|
else:
|
||||||
|
result = newVariant()
|
||||||
|
|
||||||
|
proc fromVariant*[T](option: var Option[T], val: Variant): ConversionResult =
|
||||||
|
if val.getType() == VariantType.Nil:
|
||||||
|
option = none(T)
|
||||||
|
else:
|
||||||
|
var v: T
|
||||||
|
result = fromVariant(v, val)
|
||||||
|
if result == ConversionResult.OK:
|
||||||
|
option = some(result)
|
||||||
|
|
||||||
proc godotTypeInfo*(T: typedesc[Table|TableRef|OrderedTable|OrderedTableRef]): GodotTypeInfo {.inline.} =
|
proc godotTypeInfo*(T: typedesc[Table|TableRef|OrderedTable|OrderedTableRef]): GodotTypeInfo {.inline.} =
|
||||||
result.variantType = VariantType.Dictionary
|
result.variantType = VariantType.Dictionary
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue