structures as parameters (ByValue)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

structures as parameters (ByValue)

Post by IceSoft »

I belive this is very important for some purebasic wrappers

Code: Select all

Structure myStruct
  a.f
  b.f
EndStructure

Procedure test(d.myStruct)
  Debug d\a
  Debug d\b
EndProcedure


aa.myStruct
aa\a = 10.0
aa\b = 5.0

test(aa)
Same for some return valueas as structures
Last edited by IceSoft on Sat Aug 14, 2010 9:15 pm, edited 1 time in total.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: structures as parameters

Post by TomS »

Code: Select all

Structure myStruct
  a.f
  b.f
EndStructure

Procedure test(*d.myStruct)
  Debug *d\a
  Debug *d\b
EndProcedure


aa.myStruct
aa\a = 10.0
aa\b = 5.0

test(aa)
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: structures as parameters

Post by STARGÅTE »

and for Return:

Code: Select all

Structure myStruct
  a.f
  b.f
EndStructure

Procedure test(*d.myStruct)
  *d\a = 10.0
  *d\b = 5.0
EndProcedure

test(aa.myStruct) 
Debug aa\a
Debug aa\b
but you will:?

Code: Select all

Structure myStruct
  a.f
  b.f
EndStructure

Procedure.myStruct test()
  d.myStruct
  d\a = 10.0
  d\b = 5.0
  ProcedureReturn d
EndProcedure

aa.myStruct = test() 
Debug aa\a
Debug aa\b
Right ?

but i thinks ist the same way :wink:
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: structures as parameters

Post by KJ67 »

It is a nice suggestion I like it :), it would open up to clearer implementations of this kind of things.

Code: Select all

Structure Polynom
  a.i:  b.i :  c.i
EndStructure

Structure PolynomProduct
  a1.i: b1.i: c1.i
  a2.i: b2.i: c2.i
  a3.i: b3.i: c3.i
EndStructure

Procedure.PolynomProduct PMul(a.Polynom, b.Polynom)
  Protected result.PolynomProduct
  ;... code...
  ProcedureReturn result 
EndProcedure
But there may be a huge chunk of data on the stack, just imagine...

Code: Select all

Procedure.iso_image CatchCDImage(Drive)
EndProcedure
The best preparation for tomorrow is doing your best today.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: structures as parameters by value (not reference)

Post by IceSoft »

By reference:
Procedure test(*d.myStruct)

by value:
Procedure test(d.myStruct)
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: structures as parameters

Post by STARGÅTE »

ah, you will work with "a copy" of structure in your procedure ...
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: structures as parameters

Post by TomS »

Code: Select all

By Value: aa\a = 10.0
By Value: test(aa)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: structures as parameters by value (not reference)

Post by Demivec »

Code: Select all

;by value
Procedure test(*d)
  Protected e.myStruct
  CopyStructure(*d, e, myStruct)
  Debug e\a
EndProcedure

test(aa.myStruct)
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: structures as parameters by value (not reference)

Post by IceSoft »

Demivec wrote:

Code: Select all

;by value
Procedure test(*d)
  Protected e.myStruct
  CopyStructure(*d, e, myStruct)
  Debug e\a
EndProcedure

test(aa.myStruct)
This example works only for own written procedures. Not for external dll which want be wrapped for PB.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: structures as parameters by value (not reference)

Post by Demivec »

IceSoft wrote:This example works only for own written procedures. Not for external dll which want be wrapped for PB.
@IceSoft: You're correct, I had forgotten that you wanted this specifically for PureBasic wrappers. :oops:
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: structures as parameters (ByValue)

Post by cas »

If you want to communicate with external dll then you must use AllocateMemory()/FreeMemory().

Code: Select all

Structure test
  x.i
  y.i
EndStructure


Procedure create()
  Protected *dat.test=AllocateMemory(SizeOf(test))
  *dat\x=15
  *dat\y=45
  ProcedureReturn *dat
EndProcedure

Procedure test_separate_copy(*struct)
  Protected privatecopy.test
  CopyMemory(*struct,@privatecopy,SizeOf(test))
  Debug privatecopy\x
  Debug privatecopy\y
EndProcedure

Procedure test(*struct.test)
  Debug *struct\x
  Debug *struct\y
  *struct\y=80
EndProcedure

Define *mystruct.test=create()

test(*mystruct)

Debug *mystruct\x
Debug *mystruct\y

test_separate_copy(*mystruct)

FreeMemory(*mystruct)
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: structures as parameters (ByValue)

Post by IceSoft »

@cas,

As long you use *<varaiable name> as parameter as long it is always a pointer and this is byReference not byValue.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: structures as parameters (ByValue)

Post by cas »

Yes, but when you have reference then you know value to which it points, so i don't see a problem. Can you explain more how do you want to behave test() procedure in your first post? Do you want it to make separate protected d.myStruct which, when modified inside procedure, will not make changes in aa.myStruct?

Or you wan't to use C++ dll in PureBasic when it returns structure like this one(?):

Code: Select all

#define EXPORT extern "C" __declspec(dllexport)

struct DataStructure
{
  int x;
  int y;
};

EXPORT DataStructure TestReturnStructure() {
  DataStructure data;
  data.x=4;
  data.y=7;
  return data;
}
OrangeJuice
User
User
Posts: 24
Joined: Sat Jul 24, 2010 11:56 am

Re: structures as parameters (ByValue)

Post by OrangeJuice »

I think there was a good reason why C didn't allow that which i can't seem to remember right now. Maybe that is a slow thing to do?
Post Reply