Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arrays.nim: added Variant varargs type converter for newArray #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion godot/core/arrays.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ proc newArray*(arr: GodotArray): Array {.inline.} =

import variants

proc newArray*(s: varargs[Variant]): Array =
proc newArray*(s: varargs[Variant], `newVariant`): Array =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this syntax do? I think it's not a part of the older Nim version that we'd like to keep the library compatible with.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://nim-lang.github.io/Nim/manual.html#types-varargs It's a type conversion parameter. Anything not a Variant passed into varargs will have newVariant applied to it.

new(result, arrayFinalizer)
initGodotArray(result.godotArray)
for v in s:
Expand Down
20 changes: 16 additions & 4 deletions godot/core/poolarrays.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ template definePoolArrayBase(T, GodotT, DataT, fieldName, newProcName,
proc poolArrayFinalizer(arr: T) =
arr.fieldName.deinit()

proc newProcName*(): T {.inline.} =
new(result, poolArrayFinalizer)
initProcName(result.fieldName)

proc fieldName*(self: T): ptr GodotT {.inline.} =
## WARNING: do not keep the returned value for longer than the lifetime of
## the array.
Expand Down Expand Up @@ -88,6 +84,13 @@ template definePoolArray(T, GodotT, DataT, fieldName, newProcName, initProcName;
newProcName(self.fieldName.subarray(idxFrom.cint, idxTo.cint))

when not noData:
proc newProcName*(s: varargs[DataT]): T {.inline.} =
new(result)
initProcName(result.fieldName)
result.fieldName.setLen(s.len.cint)
for idx, data in s:
result.fieldName[idx.cint] = data

proc add*(self: T; data: DataT) {.inline.} =
self.fieldName.add(data)

Expand Down Expand Up @@ -138,6 +141,15 @@ definePoolArray(PoolStringArray, GodotPoolStringArray, string,
godotPoolStringArray, newPoolStringArray,
initGodotPoolStringArray, true)

proc newPoolStringArray*(s: varargs[string]): PoolStringArray {.inline.} =
new(result)
initGodotPoolStringArray(result.godotPoolStringArray)
result.godotPoolStringArray.setLen(s.len.cint)
for idx, str in s:
var gstr = str.toGodotString()
result.godotPoolStringArray[idx.cint] = gstr
gstr.deinit()

proc add*(self: PoolStringArray; data: string) =
var s = data.toGodotString()
self.godotPoolStringArray.add(s)
Expand Down