Fix circular imports

This commit is contained in:
Ruslan Mustakov 2017-07-31 18:16:42 +07:00
commit fb8c42a2cc
4 changed files with 106 additions and 77 deletions

View file

@ -23,7 +23,7 @@ proc newArray*(arr: GodotArray): Array {.inline.} =
new(result, arrayFinalizer) new(result, arrayFinalizer)
result.godotArray = arr result.godotArray = arr
import poolarrays, variants import poolarrays
proc newArray*(pca: PoolColorArray): Array {.inline.} = proc newArray*(pca: PoolColorArray): Array {.inline.} =
new(result, arrayFinalizer) new(result, arrayFinalizer)
@ -127,10 +127,10 @@ proc len*(self: Array): int {.inline.} =
proc sort*(self: var Array) {.inline.} = proc sort*(self: var Array) {.inline.} =
self.godotArray.sort() self.godotArray.sort()
iterator items*(arr: Array): Variant {.inline.} = iterator items*(arr: Array): Variant =
for i in 0..<arr.len: for i in 0..<arr.len:
yield arr[i] yield arr[i]
iterator pairs*(arr: Array): tuple[key: int, val: Variant] {.inline.} = iterator pairs*(arr: Array): tuple[key: int, val: Variant] =
for i in 0..<arr.len: for i in 0..<arr.len:
yield (i, arr[i]) yield (i, arr[i])

View file

@ -35,6 +35,13 @@ proc clear*(self: var Dictionary) {.inline.} =
import variants, arrays import variants, arrays
proc newVariant*(dict: Dictionary): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant[], dict.godotDictionary)
proc asDictionary*(self: Variant): Dictionary {.inline.} =
newDictionary(self.godotVariant[].asGodotDictionary)
proc contains*(self: Dictionary; key: Variant): bool {.inline.}= proc contains*(self: Dictionary; key: Variant): bool {.inline.}=
self.godotDictionary.contains(key.godotVariant[]) self.godotDictionary.contains(key.godotVariant[])
@ -47,8 +54,6 @@ proc del*(self: var Dictionary; key: Variant) {.inline.} =
proc hash*(self: Dictionary): Hash {.inline.} = proc hash*(self: Dictionary): Hash {.inline.} =
hash(self.godotDictionary.godotHash()) hash(self.godotDictionary.godotHash())
import arrays, variants
proc keys*(self: Dictionary): Array {.inline.} = proc keys*(self: Dictionary): Array {.inline.} =
newArray(self.godotDictionary.keys()) newArray(self.godotDictionary.keys())
@ -79,16 +84,16 @@ proc toJson*(self: Dictionary): string {.inline.} =
result = $s result = $s
s.deinit() s.deinit()
iterator keys*(dict: Dictionary): Variant {.inline.} = iterator keys*(dict: Dictionary): Variant =
let keyArr = dict.keys() let keyArr = dict.keys()
for key in keyArr: for key in keyArr:
yield key yield key
iterator values*(dict: Dictionary): Variant {.inline.} = iterator values*(dict: Dictionary): Variant =
let valArr = dict.values() let valArr = dict.values()
for val in valArr: for val in valArr:
yield val yield val
iterator pairs*(dict: Dictionary): tuple[key, val: Variant] {.inline.} = iterator pairs*(dict: Dictionary): tuple[key, val: Variant] =
for key in keys(dict): for key in keys(dict):
yield (key, dict[key]) yield (key, dict[key])

View file

@ -3,68 +3,69 @@
import vector2, vector3, colors import vector2, vector3, colors
import internal.godotpoolarrays, internal.godotstrings import internal.godotpoolarrays, internal.godotstrings
# type
# PoolByteArray* = ref object
# godotPoolByteArray: GodotPoolByteArray
# PoolIntArray* = ref object
# godotPoolIntArray: GodotPoolIntArray
# PoolRealArray* = ref object
# godotPoolRealArray: GodotPoolRealArray
# PoolVector2Array* = ref object
# godotPoolVector2Array: GodotPoolVector2Array
# PoolVector3Array* = ref object
# godotPoolVector3Array: GodotPoolVector3Array
# PoolColorArray* = ref object
# godotPoolColorArray: GodotPoolColorArray
# PoolStringArray* = ref object
# godotPoolStringArray: GodotPoolStringArray
# proc godotPoolByteArray*(self: PoolByteArray):
# ptr GodotPoolByteArray {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolByteArray
# proc godotPoolIntArray*(self: PoolIntArray):
# ptr GodotPoolIntArray {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolIntArray
# proc godotPoolRealArray*(self: PoolRealArray):
# ptr GodotPoolRealArray {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolRealArray
# proc godotPoolVector2Array*(self: PoolVector2Array):
# ptr GodotPoolVector2Array {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolVector2Array
# proc godotPoolVector3Array*(self: PoolVector3Array):
# ptr GodotPoolVector3Array {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolVector3Array
# proc godotPoolColorArray*(self: PoolColorArray):
# ptr GodotPoolColorArray {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolColorArray
# proc godotPoolStringArray*(self: PoolStringArray):
# ptr GodotPoolStringArray {.inline.} =
# ## WARNING: do not keep the returned value for longer than the lifetime of
# ## the array.
# addr self.godotPoolStringArray
template definePoolArrayBase(T, GodotT, DataT, fieldName, newProcName,
initProcName) =
type type
PoolByteArray* = ref object T* = ref object
godotPoolByteArray: GodotPoolByteArray fieldName: GodotT
PoolIntArray* = ref object
godotPoolIntArray: GodotPoolIntArray
PoolRealArray* = ref object
godotPoolRealArray: GodotPoolRealArray
PoolVector2Array* = ref object
godotPoolVector2Array: GodotPoolVector2Array
PoolVector3Array* = ref object
godotPoolVector3Array: GodotPoolVector3Array
PoolColorArray* = ref object
godotPoolColorArray: GodotPoolColorArray
PoolStringArray* = ref object
godotPoolStringArray: GodotPoolStringArray
proc godotPoolByteArray*(self: PoolByteArray):
ptr GodotPoolByteArray {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolByteArray
proc godotPoolIntArray*(self: PoolIntArray):
ptr GodotPoolIntArray {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolIntArray
proc godotPoolRealArray*(self: PoolRealArray):
ptr GodotPoolRealArray {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolRealArray
proc godotPoolVector2Array*(self: PoolVector2Array):
ptr GodotPoolVector2Array {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolVector2Array
proc godotPoolVector3Array*(self: PoolVector3Array):
ptr GodotPoolVector3Array {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolVector3Array
proc godotPoolColorArray*(self: PoolColorArray):
ptr GodotPoolColorArray {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolColorArray
proc godotPoolStringArray*(self: PoolStringArray):
ptr GodotPoolStringArray {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
addr self.godotPoolStringArray
import godotbase, arrays
template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
noData = false) =
proc poolArrayFinalizer(arr: T) = proc poolArrayFinalizer(arr: T) =
arr.fieldName.deinit() arr.fieldName.deinit()
@ -73,14 +74,22 @@ template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
new(result, poolArrayFinalizer) new(result, poolArrayFinalizer)
initProcName(result.fieldName) initProcName(result.fieldName)
proc newProcName*(arr: Array): T {.inline.} = proc fieldName*(self: T): ptr GodotT {.inline.} =
new(result, poolArrayFinalizer) ## WARNING: do not keep the returned value for longer than the lifetime of
initProcName(result.fieldName, arr.godotArray[]) ## the array.
addr self.fieldName
proc newProcName*(arr: GodotT): T {.inline.} = proc newProcName*(arr: GodotT): T {.inline.} =
new(result, poolArrayFinalizer) new(result, poolArrayFinalizer)
result.fieldName = arr result.fieldName = arr
template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
noData = false) =
proc newProcName*(arr: Array): T {.inline.} =
new(result, poolArrayFinalizer)
initProcName(result.fieldName, arr.godotArray[])
proc add*(self: var T; arr: T) {.inline.} = proc add*(self: var T; arr: T) {.inline.} =
self.fieldName.add(arr.fieldName) self.fieldName.add(arr.fieldName)
@ -120,6 +129,30 @@ template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
for i in 0..<arr.len: for i in 0..<arr.len:
yield (i, arr[i]) yield (i, arr[i])
definePoolArrayBase(PoolByteArray, GodotPoolByteArray, uint8,
godotPoolByteArray, newPoolByteArray,
initGodotPoolByteArray)
definePoolArrayBase(PoolIntArray, GodotPoolIntArray, cint,
godotPoolIntArray, newPoolIntArray,
initGodotPoolIntArray)
definePoolArrayBase(PoolRealArray, GodotPoolRealArray, float64,
godotPoolRealArray, newPoolRealArray,
initGodotPoolRealArray)
definePoolArrayBase(PoolVector2Array, GodotPoolVector2Array, Vector2,
godotPoolVector2Array, newPoolVector2Array,
initGodotPoolVector2Array)
definePoolArrayBase(PoolVector3Array, GodotPoolVector3Array, Vector3,
godotPoolVector3Array, newPoolVector3Array,
initGodotPoolVector3Array)
definePoolArrayBase(PoolColorArray, GodotPoolColorArray, Color,
godotPoolColorArray, newPoolColorArray,
initGodotPoolColorArray)
definePoolArrayBase(PoolStringArray, GodotPoolStringArray, string,
godotPoolStringArray, newPoolStringArray,
initGodotPoolStringArray)
import godotbase, arrays
definePoolArray(PoolByteArray, GodotPoolByteArray, uint8, definePoolArray(PoolByteArray, GodotPoolByteArray, uint8,
godotPoolByteArray, newPoolByteArray, godotPoolByteArray, newPoolByteArray,
initGodotPoolByteArray) initGodotPoolByteArray)

View file

@ -25,7 +25,7 @@ proc godotVariant*(v: Variant): ptr GodotVariant {.inline.} =
proc getType*(v: Variant): VariantType = proc getType*(v: Variant): VariantType =
v.godotVariant.getType() v.godotVariant.getType()
proc variantFinalizer(v: Variant) = proc variantFinalizer*(v: Variant) =
if not v.noDeinit: if not v.noDeinit:
v.godotVariant.deinit() v.godotVariant.deinit()
@ -75,7 +75,7 @@ proc newVariant*(s: string): Variant {.inline.} =
godotStr.deinit() godotStr.deinit()
import basis, nodepaths import basis, nodepaths
import planes, poolarrays, quats, rect2, rect3, rids import planes, quats, rect2, rect3, rids
import transforms, transform2d, vector2 import transforms, transform2d, vector2
import vector3, colors import vector3, colors
@ -135,7 +135,7 @@ proc newVariant*(obj: ptr GodotObject): Variant {.inline.} =
new(result, variantFinalizer) new(result, variantFinalizer)
initGodotVariant(result.godotVariant, obj) initGodotVariant(result.godotVariant, obj)
import arrays import arrays, poolarrays
proc newVariant*(arr: Array): Variant {.inline.} = proc newVariant*(arr: Array): Variant {.inline.} =
new(result, variantFinalizer) new(result, variantFinalizer)
@ -163,12 +163,6 @@ proc newVariant*(pv3a: PoolVector3Array): Variant {.inline.} =
proc newVariant*(pca: PoolColorArray): Variant {.inline.} = proc newVariant*(pca: PoolColorArray): Variant {.inline.} =
initGodotVariant(result.godotVariant, pca.godotPoolColorArray[]) initGodotVariant(result.godotVariant, pca.godotPoolColorArray[])
import dictionaries
proc newVariant*(dict: Dictionary): Variant {.inline.} =
new(result, variantFinalizer)
initGodotVariant(result.godotVariant, dict.godotDictionary[])
proc asBool*(self: Variant): bool {.inline.} = proc asBool*(self: Variant): bool {.inline.} =
self.godotVariant.asBool() self.godotVariant.asBool()
@ -249,9 +243,6 @@ proc asPoolVector3Array*(self: Variant): PoolVector3Array =
proc asPoolColorArray*(self: Variant): PoolColorArray = proc asPoolColorArray*(self: Variant): PoolColorArray =
newPoolColorArray(self.godotVariant.asGodotPoolColorArray()) newPoolColorArray(self.godotVariant.asGodotPoolColorArray())
proc asDictionary*(self: Variant): Dictionary {.inline.} =
newDictionary(self.godotVariant.asGodotDictionary)
proc hasMethod*(self: Variant; meth: string): bool = proc hasMethod*(self: Variant; meth: string): bool =
var s = meth.toGodotString() var s = meth.toGodotString()
result = self.godotVariant.hasMethod(s) result = self.godotVariant.hasMethod(s)