It is currently Tue Jun 18, 2013 5:48 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Simple way to use variable parameters for Procedures
PostPosted: Fri Mar 16, 2012 6:26 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4739
Location: Berlin - Germany
Code:
;==================================================================
; ###  Example to use variable parameters for Procedures    ###
; Author:            Thomas <ts-soft> Schulz
; Date:              Mrz 16, 2012
; Version:           1.0
; Target OS:         All
; Target Compiler:   PureBasic 4.40 and later
;==================================================================

EnableExplicit

Structure V_ARG
  Type.l
  StructureUnion
    a.a
    b.b
    c.c
    d.d
    f.f
    i.i
    l.l
    *s.String
    q.q
    u.u
    w.w
  EndStructureUnion
EndStructure

Procedure V_ARG_Add(Array V_ARG.V_ARG(1), Type.l, *value)
  Protected index = ArraySize(V_ARG())
  Protected *v.Quad = *value
  ReDim V_ARG(index + 1)

  With V_ARG(index)
    \Type = Type
    \q = *v\q
  EndWith
EndProcedure

; example
Procedure Foo(Array V_ARG.V_ARG(1))
  Protected i
  Protected count = ArraySize(V_ARG())
  Debug "Count of parameters: " + Str(count)
  Debug ""
  For i = 0 To count - 1
    With V_ARG(i)
      Select \Type
        Case #PB_Ascii
          Debug "Parameter " + Str(i + 1) + " = UByte, Value = " + Str(V_ARG(i)\a)
        Case #PB_Byte
          Debug "Parameter " + Str(i + 1) + " = Byte, Value = " + Str(V_ARG(i)\b)
        Case #PB_Character
          Debug "Parameter " + Str(i + 1) + " = Char, Value = " + Str(V_ARG(i)\c)
        Case #PB_Double
          Debug "Parameter " + Str(i + 1) + " = Double, Value = " + StrD(V_ARG(i)\d)
        Case #PB_Float
          Debug "Parameter " + Str(i + 1) + " = FLoat, Value = " + StrF(V_ARG(i)\f)
        Case #PB_Integer
          Debug "Parameter " + Str(i + 1) + " = Integer, Value = " + Str(V_ARG(i)\i)
        Case #PB_Long
          Debug "Parameter " + Str(i + 1) + " = Long, Value = " + Str(V_ARG(i)\l)
        Case #PB_Quad
          Debug "Parameter" + Str(i + 1) + " = Quad, Value = " + Str(V_ARG(i)\q)
        Case #PB_String
          Debug "Parameter " + Str(i + 1) + " = String, Value = " + PeekS(V_ARG(i)\s)
        Case #PB_Unicode
          Debug "Parameter " + Str(i + 1) + " = UWord, Value = " + Str(V_ARG(i)\u)
        Case #PB_Word
          Debug "Parameter " + Str(i + 1) + " = Word, Value = " + Str(V_ARG(i)\w)
      EndSelect
    EndWith
  Next
EndProcedure

Dim para_array.V_ARG(0)

Define Text.String
Text\s = "Feel the ..Pure.. Power"
V_ARG_Add(para_array(), #PB_String, @Text)

Define pi.d = #PI
V_ARG_Add(para_array(), #PB_Double, @pi)

Define word.w = 65535
V_ARG_Add(para_array(), #PB_Word, @word)
V_ARG_Add(para_array(), #PB_Unicode, @word)

Foo(para_array())



Have Fun :D

_________________
PureBasic 5.11 | Windows 7 SP1 (x64) | Linux Mint 15 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Top
 Profile  
 
 Post subject: Re: Simple way to use variable parameters for Procedures
PostPosted: Sat Mar 17, 2012 2:44 am 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2919
Location: Wales, UK
woa, that's too clever......... :mrgreen:

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
 
 Post subject: Re: Simple way to use variable parameters for Procedures
PostPosted: Sat Mar 17, 2012 5:57 pm 
Offline
Addict
Addict

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2573
Location: Southwest OH - USA
IdeasVacuum wrote:
woa, that's too clever......... :mrgreen:


Mr Soft is VERY clever :)

Thanks for sharing.


Top
 Profile  
 
 Post subject: Re: Simple way to use variable parameters for Procedures
PostPosted: Sat Mar 17, 2012 6:03 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4739
Location: Berlin - Germany
You can use this, for example a interpreter like program, using only one prototype for all functions.
It is simple, as the title say, but it is a hint for some projects.

_________________
PureBasic 5.11 | Windows 7 SP1 (x64) | Linux Mint 15 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye