[Resolved] Call procedure with several parameters by name

Just starting out? Need help? Post your questions and find answers here.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: [Resolved] Call procedure with several parameters by nam

Post by chi »

Mijikai wrote:Mby someone can tell my why i cant do this:
...
From the remarks section of CallFunctionFast() - Note: This function is not very flexible and does not handle string/float/double/quad parameters or string/float/double/quad returns. The use of prototypes is now strongly recommended.
Et cetera is my worst enemy
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Resolved] Call procedure with several parameters by nam

Post by Mijikai »

chi wrote:
Mijikai wrote:Mby someone can tell my why i cant do this:
...
From the remarks section of CallFunctionFast() - Note: This function is not very flexible and does not handle string/float/double/quad parameters or string/float/double/quad returns. The use of prototypes is now strongly recommended.
Thanks , i should have looked into the help :?
With that cleared up, i was able to produce some code :)

Give it a try @Kcc :wink:

Code:

Code: Select all


;PROC CALL Module
;by Mijikai
;PB v.5.62 - tested on Windows 7 x64

DeclareModule PROC
  Declare.i Add(*Procedure,Name.s,P1.i = #Null,P2.i = #Null,P3.i = #Null,P4.i = #Null,P5.i = #Null,P6.i = #Null,P7.i = #Null,P8.i = #Null,P9.i = #Null,P10.i = #Null)
  Declare.s Name(Entry.i)
  Declare.i Count(Entry.i)
  Declare.i Call(Paramater.s)
  Declare.i Remove(Entry.i,Name.s = #Null$)
  Declare.i Clear()
EndDeclareModule

Module PROC
  
  EnableExplicit
  
  Structure PROC_TYPE;could also be used to get the parameters of the last call...
      i.i
      f.f
      d.d
      s.s
  EndStructure

  Structure PROC_STRUCT
    name.s
    *proc
    count.a
    Array type.i(9,1)
    param.PROC_TYPE[10]
  EndStructure

  Global NewList proc.PROC_STRUCT()

  Procedure.i Add(*Procedure,Name.s,P1.i = #Null,P2.i = #Null,P3.i = #Null,P4.i = #Null,P5.i = #Null,P6.i = #Null,P7.i = #Null,P8.i = #Null,P9.i = #Null,P10.i = #Null)
    Protected index.i
    If *Procedure And Name
      If AddElement(proc())
        proc()\name = Name
        proc()\proc = *Procedure
        proc()\type(0,0) = P1
        proc()\type(1,0) = P2
        proc()\type(2,0) = P3
        proc()\type(3,0) = P4
        proc()\type(4,0) = P5
        proc()\type(5,0) = P6
        proc()\type(6,0) = P7
        proc()\type(7,0) = P8
        proc()\type(8,0) = P9
        proc()\type(9,0) = P10
        For index = 0 To 9
          If proc()\type(index,0)
            Select proc()\type(index,0)
              Case #PB_String
                proc()\type(index,1) = @proc()\param[index]\s
              Case #PB_Float
                proc()\type(index,1) = @proc()\param[index]\f
              Case #PB_Double
                proc()\type(index,1) = @proc()\param[index]\d
            EndSelect
            proc()\count + 1
          EndIf
        Next
        ProcedureReturn @proc()
      EndIf
    EndIf
  EndProcedure
  
  Procedure.i Remove(*Entry.PROC_STRUCT,Name.s = #Null$)
    If ListSize(proc())
      If Name
        ForEach proc()
          If proc()\name = Name
            DeleteElement(proc())
          EndIf
        Next
      Else
        ChangeCurrentElement(proc(),*Entry)
        DeleteElement(proc())
      EndIf
    EndIf
  EndProcedure
  
  Procedure.i Count(*Entry.PROC_STRUCT);returns parameter count
    With *Entry
      ProcedureReturn \count
    EndWith
  EndProcedure
  
  Procedure.s Name(*Entry.PROC_STRUCT)
    With *Entry
      ProcedureReturn \name
    EndWith
  EndProcedure
  
  Procedure.i Call(Parameter.s)
    Protected index.i
    Protected name.s
    Protected count.i
    name = StringField(Parameter,1, ",")
    If name
      ForEach proc()
        If proc()\name = name
          count + 2
          If proc()\count = CountString(Parameter, ",")
            For index = 0 To proc()\count - 1
              Select proc()\type(index,0)
                Case #PB_String
                  proc()\param[index]\s = StringField(Parameter,count,",")
                  proc()\type(index,1) = @proc()\param[index]\s
                Case #PB_Float
                  proc()\param[index]\f = ValF(StringField(Parameter,count,","))
                Case #PB_Double
                  proc()\param[index]\d = ValD(StringField(Parameter,count,","))
                Default
                  proc()\type(index,1) = Val(StringField(Parameter,count,","))        
              EndSelect
              count + 1
            Next
            Select proc()\count
              Case 0
                ProcedureReturn CallFunctionFast(proc()\proc)
              Case 1
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1))
              Case 2
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1))
              Case 3
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1))
              Case 4
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1))
              Case 5
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1))
              Case 6
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1))
              Case 7
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1))
              Case 8
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1),proc()\type(7,1))
              Case 9
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1),proc()\type(7,1),proc()\type(8,1))
              Case 10
                ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1),proc()\type(7,1),proc()\type(8,1),proc()\type(9,1))
            EndSelect  
          EndIf
        EndIf
      Next
    EndIf
  EndProcedure 
    
  Procedure.i Clear()
    ClearList(proc())
  EndProcedure
    
EndModule

EnableExplicit

Global Entry.i

Procedure.i ProcTest(TestString.s,*A.Double,*B.Float,C.u);(only) floats and doubles will be supplied through pointers!
  Debug *A\d
  Debug *B\f
  Debug C
  MessageRequester("Test!",TestString)
EndProcedure

Entry = PROC::Add(@ProcTest(),"Test",#PB_String,#PB_Double,#PB_Float,#PB_Unicode);add a procedure (name can be customized also) and define its parameters...

If Entry
  PROC::Call("Test,Hallo!,123.456,654.452,22");call it!
  Debug "----------"
  Debug PROC::Name(Entry);name
  Debug PROC::Count(Entry);parameter count
  Debug PROC::Remove(Entry);delete one procedure
  ;PROC::Clear();delete all procedures
EndIf 

End
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: [Resolved] Call procedure with several parameters by nam

Post by Kwai chang caine »

Waooouuh !!! :shock:
Thanks a lot MASTER for this nice code 8)
So powerfull,....than KCC spent one hour to try to understand :oops:

Image

You can create a code like this 8) and KCC cannot have enough brain for use it :oops:
This is that "THE DREAM TEAM" :mrgreen:
Again thanks for the great job you do for me 8) 8)

Code: Select all

;PROC CALL Module
;by Mijikai
;PB v.5.62 - tested on Windows 7 x64
;PB v.5.62 x86 - tested on Windows 10 x64

; *********************************************************************************************************
;                                           MIJIKAI JEWEL CODE 
; *********************************************************************************************************

DeclareModule PROC
 Declare.i Add(*Procedure,Name.s,P1.i = #Null,P2.i = #Null,P3.i = #Null,P4.i = #Null,P5.i = #Null,P6.i = #Null,P7.i = #Null,P8.i = #Null,P9.i = #Null,P10.i = #Null)
 Declare.s Name(Entry.i)
 Declare.i Count(Entry.i)
 Declare.i Call(Paramater.s)
 Declare.i Remove(Entry.i,Name.s = #Null$)
 Declare.i Clear()
EndDeclareModule

Module PROC
 
 EnableExplicit
 
 Structure PROC_TYPE;could also be used to get the parameters of the last call...
  i.i
  f.f
  d.d
  s.s
 EndStructure
 
 Structure PROC_STRUCT
  name.s
  *proc
  count.a
  Array type.i(9,1)
  param.PROC_TYPE[10]
 EndStructure
 
 Global NewList proc.PROC_STRUCT()
 
 Procedure.i Add(*Procedure,Name.s,P1.i = #Null,P2.i = #Null,P3.i = #Null,P4.i = #Null,P5.i = #Null,P6.i = #Null,P7.i = #Null,P8.i = #Null,P9.i = #Null,P10.i = #Null)
  Protected index.i
  If *Procedure And Name
   If AddElement(proc())
    proc()\name = Name
    proc()\proc = *Procedure
    proc()\type(0,0) = P1
    proc()\type(1,0) = P2
    proc()\type(2,0) = P3
    proc()\type(3,0) = P4
    proc()\type(4,0) = P5
    proc()\type(5,0) = P6
    proc()\type(6,0) = P7
    proc()\type(7,0) = P8
    proc()\type(8,0) = P9
    proc()\type(9,0) = P10
    For index = 0 To 9
     If proc()\type(index,0)
      Select proc()\type(index,0)
       Case #PB_String
        proc()\type(index,1) = @proc()\param[index]\s
       Case #PB_Float
        proc()\type(index,1) = @proc()\param[index]\f
       Case #PB_Double
        proc()\type(index,1) = @proc()\param[index]\d
      EndSelect
      proc()\count + 1
     EndIf
    Next
    ProcedureReturn @proc()
   EndIf
  EndIf
 EndProcedure
 
 Procedure.i Remove(*Entry.PROC_STRUCT,Name.s = #Null$)
  If ListSize(proc())
   If Name
    ForEach proc()
     If proc()\name = Name
      DeleteElement(proc())
     EndIf
    Next
   Else
    ChangeCurrentElement(proc(),*Entry)
    DeleteElement(proc())
   EndIf
  EndIf
 EndProcedure
 
 Procedure.i Count(*Entry.PROC_STRUCT);returns parameter count
  With *Entry
   ProcedureReturn \count
  EndWith
 EndProcedure
 
 Procedure.s Name(*Entry.PROC_STRUCT)
  With *Entry
   ProcedureReturn \name
  EndWith
 EndProcedure
 
 Procedure.i Call(Parameter.s)
  Protected index.i
  Protected name.s
  Protected count.i
  name = StringField(Parameter,1, ",")
  If name
   ForEach proc()
    If proc()\name = name
     count + 2
     If proc()\count = CountString(Parameter, ",")
      For index = 0 To proc()\count - 1
       Select proc()\type(index,0)
        Case #PB_String
         proc()\param[index]\s = StringField(Parameter,count,",")
         proc()\type(index,1) = @proc()\param[index]\s
        Case #PB_Float
         proc()\param[index]\f = ValF(StringField(Parameter,count,","))
        Case #PB_Double
         proc()\param[index]\d = ValD(StringField(Parameter,count,","))
        Default
         proc()\type(index,1) = Val(StringField(Parameter,count,","))       
       EndSelect
       count + 1
      Next
      Select proc()\count
       Case 0
        ProcedureReturn CallFunctionFast(proc()\proc)
       Case 1
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1))
       Case 2
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1))
       Case 3
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1))
       Case 4
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1))
       Case 5
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1))
       Case 6
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1))
       Case 7
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1))
       Case 8
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1),proc()\type(7,1))
       Case 9
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1),proc()\type(7,1),proc()\type(8,1))
       Case 10
        ProcedureReturn CallFunctionFast(proc()\proc,proc()\type(0,1),proc()\type(1,1),proc()\type(2,1),proc()\type(3,1),proc()\type(4,1),proc()\type(5,1),proc()\type(6,1),proc()\type(7,1),proc()\type(8,1),proc()\type(9,1))
      EndSelect 
     EndIf
    EndIf
   Next
  EndIf
 EndProcedure
 
 Procedure.i Clear()
  ClearList(proc())
 EndProcedure
 
EndModule

EnableExplicit

Global Entry.i

; *********************************************************************************************************
;                                             KCC PROCEDURES
; *********************************************************************************************************

Procedure MessageWithoutParameter()
 MessageRequester("Without Parameter", "No parameter")
EndProcedure

Procedure MessageOneParameter(Text.s)
 MessageRequester("Only one Parameter", Text)
EndProcedure

Procedure MessageTwoParameters(Text1.s, Text2.s)
 MessageRequester("Two parameters", Text1 + Text2)
EndProcedure

Procedure MessageThreeParameters(Text1.s, Text2.s, Text3.s)
 MessageRequester("Three parameters", Text1 + Text2 + Text3)
EndProcedure

; *********************************************************************************************************
;                                            PROCEDURES CALL 
; *********************************************************************************************************

Entry = PROC::Add(@MessageWithoutParameter(),"MessageWithoutParameter")

If Entry 
 PROC::Call("MessageWithoutParameter")
 Debug PROC::Name(Entry);name
 Debug PROC::Count(Entry);parameter count
 Debug PROC::Remove(Entry);delete one procedure
 Debug ""
EndIf

Entry = PROC::Add(@MessageOneParameter(),"MessageOneParameter", #PB_String)

If Entry 
 PROC::Call("MessageOneParameter, Hello it's KCC")
 Debug PROC::Name(Entry);name
 Debug PROC::Count(Entry);parameter count
 Debug PROC::Remove(Entry);delete one procedure
 Debug ""
EndIf

Entry = PROC::Add(@MessageTwoParameters(),"MessageTwoParameters", #PB_String, #PB_String)

If Entry 
 PROC::Call("MessageTwoParameters, Hello it's KCC a second time, but it's more long")
 Debug PROC::Name(Entry);name
 Debug PROC::Count(Entry);parameter count
 Debug PROC::Remove(Entry);delete one procedure
 Debug ""
EndIf

Entry = PROC::Add(@MessageThreeParameters(),"MessageThreeParameters", #PB_String, #PB_String, #PB_String)

If Entry 
 PROC::Call("MessageThreeParameters, Hello it's KCC another time, and this time, it's one time too much")
 Debug PROC::Name(Entry);name
 Debug PROC::Count(Entry);parameter count
 Debug PROC::Remove(Entry);delete one procedure
 Debug ""
EndIf

End
ImageThe happiness is a road...
Not a destination
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: [Resolved] Call procedure with several parameters by nam

Post by Mijikai »

Thanks KCC im glad you like it :)
Ps. your gifs are just great :mrgreen:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: [Resolved] Call procedure with several parameters by nam

Post by Kwai chang caine »

Thanks also for your compliment 8)
I have an idea :idea: and propose to you an exchange...
All my GIF against 1/10e of your great knowledge 8)
Finally..you surely not need to have all that, you can sharing :lol:
ImageThe happiness is a road...
Not a destination
Post Reply