[Implemented] Array and LinkedList as parameter in interface

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

[Implemented] Array and LinkedList as parameter in interface

Post by NicTheQuick »

Hi,
I didn't found a feature request, which describes my problem.

I want to do something like this:

Code: Select all

Interface bla
  test1(List())
  test2(Array(1))
EndInterface
When the following code works, why not with interfaces?

Code: Select all

Procedure test1(List())
EndProcedure

Procedure test2(Array(1))
EndProcedure
Here's an example, that shows, what I mean and want.

Code: Select all

Interface foo
  destroy()
  addList(List.l())
  getList(List.l())
EndInterface

Structure foo_s
  *VTable
  count.l
  *mem
EndStructure

Procedure foo_create()
  Protected *foo.foo_s
  
  *foo = AllocateMemory(SizeOf(foo_s))
  If Not *foo : ProcedureReturn #False : EndIf
  
  *foo\VTable = ?foo_VTable
  
  ProcedureReturn *foo
EndProcedure

Procedure foo_destroy(*foo.foo_s)
  If *foo\mem : FreeMemory(*foo\mem) : EndIf
  FreeMemory(*foo)
EndProcedure

Procedure foo_addList(*foo.foo_s, List.l())
  Protected toadd.l = CountList(List()), *tmp.Long, i.l
  
  If Not toadd : ProcedureReturn #True : EndIf
  
  *tmp = ReAllocateMemory(*foo\mem, (*foo\count + toadd) * SizeOf(Long))
  If Not *tmp : ProcedureReturn #False : EndIf
  
  *foo\mem = *tmp
  *tmp + SizeOf(Long) * *foo\count
  ForEach List()
    *tmp\l = List()
    *tmp + SizeOf(Long)
  Next
  
  *foo\count + toadd
  
  ProcedureReturn #True
EndProcedure

Procedure foo_getList(*foo.foo_s, List.l())
  Protected *p.Long, i.l
  
  ClearList(List())
  
  *p = *foo\mem
  i = *foo\count
  While i
    If AddElement(List())
      List() = *p\l
    EndIf
    *p + SizeOf(Long)
    i - 1
  Wend
  
  ProcedureReturn #True
EndProcedure

DataSection
  foo_VTable:
    Data.l @foo_destroy(), @foo_addList(), @foo_getList()
EndDataSection

Define foo.foo, i.l

NewList elements.l()

For i = 0 To 3
  If AddElement(elements())
    elements() = i
  EndIf
Next

foo = foo_create()

;-begin
;add the list 3 times
foo\addList(elements())
foo\addList(elements())
foo\addList(elements())

;gets the 3-times-list
foo\getList(foo, elements())
;-end

; ;add the list 3 times
; foo_addList(foo, elements())
; foo_addList(foo, elements())
; foo_addList(foo, elements())
; 
; ;gets the 3-times-list
; foo_getList(foo, elements())

;debug the list
ForEach elements()
  Debug elements()
Next

foo\destroy()
Comment 'addList(List.l())' and 'getList(List.l())' in the interface definition
and the lines between ';-begin' and ';-end' out and decomment the
following lines to see, what should happen.
LuckyLuke
Enthusiast
Enthusiast
Posts: 181
Joined: Fri Jun 06, 2003 2:41 pm
Location: Belgium

Post by LuckyLuke »

I would welcome this feature too ...
+1 :)

LuckyLuke
Post Reply