Using of structures for procedurereturns

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:

Using of structures for procedurereturns

Post by NicTheQuick »

Hi @ all!

I have the follow wish:
I can't write much, but I hope you will understand what I mean.

Code: Select all

Structure bla
  a.l
  b.l
EndStructure

Procedure.bla juhu()
  Protected *p.bla
  *p = AllocateMemory(SizeOf(bla))
  *p\a = 1
  *p\b = 2
  ProcedureReturn *p
EndProcedure

Debug juhu()\a   ;Output: 1
An other example:

Code: Select all

Interface Vector
  Add(*v)
  Mul(*v)
EndInterface

Procedure.Vector Vector_Add(*x, *v)
  ;some code
  ProcedureReturn *x
EndProcedure

Procedure.Vector Vector_Mul(*x, *v)
  ;some code
  ProcedureReturn *x
EndProcedure

*v1.Vector
*v2.Vector
*v3.Vector

*v1\Add(*v2)\Mul(*v3)

;instead of

*v1\Add(*v2)
*v1\Mul(*v3)
If there is a feature request like this, please post the url.
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post by inc. »

Even more easier if no pointers are needed

Code: Select all

Structure bla
  a.l
  b.l
EndStructure

Procedure.bla juhu()
  Protected p.bla 
  p\a = 1
  p\b = 2
  ProcedureReturn p
EndProcedure
... but this would PB also force to support assigning values to structures.

Code: Select all

Test.bla = juhu()
Also passing structures via procedure arguments.

Code: Select all

Procedure.bla juhu(x.bla)
  Protected p.bla
  p\a = x\a
  p\b = x\b
  ProcedureReturn p
EndProcedure
Check out OOP support for PB here!
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

If I am reading your request correctly then I also requested this: http://www.purebasic.fr/english/viewtop ... highlight=

@inc: it is not to be able to assign structures or use them without pointers. It is to return a pointer and be able to use that instead of having to create a pointer variable from which to access the members.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post by inc. »

@inc: it is not to be able to assign structures or use them without pointers. It is to return a pointer and be able to use that instead of having to create a pointer variable from which to access the members.
Thats the workout for Purebasic for finally getting the purpose but in C/C++ you can return a structured variable directly instead of just a pointer like shown in your link. And thats what Nik is trying to point out - like me too.

Lets have a look at this one as it shows how it can be achieved in C and C++:

Code: Select all

// The Structure

struct AVS_Value {
  short type;  // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
  short array_size;
  union {
    void * clip
    char boolean;
    int integer;
    float floating_pt;
    const char * string;
    const AVS_Value * array;
  } d;
};




// The function accepting a structure variable as argument and also returning a structure (NOT a pointer to) as well.


AVS_Value avs_invoke(AVS_ScriptEnvironment * p, const char * name, AVS_Value args, const char * * arg_names)
{
	AVS_Value v = {0,0};
	p->error = 0;
	try {
		AVSValue v0 = p->env->Invoke(name, *(AVSValue *)&args, arg_names);
		new ((AVSValue *)&v) AVSValue(v0);
	} catch (IScriptEnvironment::NotFound) {
    p->error = "Function Not Found";
	} catch (AvisynthError err) {
		p->error = err.msg;
	}
  if (p->error)
    v = avs_new_value_error(p->error);
	return v;
}
So its exactly what I did point out above ;-)



Here the main details pointed out where you can see that actually no pointer is used...

AVS_Value avs_invoke(AVS_ScriptEnvironment* p, const char* name, AVS_Value args, const char** arg_names)
{
__AVS_Value v = {0,0};
__... AVSValue v0 = p->env->Invoke(name, *(AVSValue*)&args, arg_names);
__...
__...
__return v;
}
Check out OOP support for PB here!
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

inc. wrote:And thats what Nik is trying to point out
My interpretation of Nik's request is different from yours (based on the code that Nik posted in his request, since that used only pointers).
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

+1 (to be able to access structure fields directly on procedure results)

i think Nic's simple request has better chance to be implemented than the further one, though that would be cool too.
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Post by NicTheQuick »

tinman has understood me. I want be able to use the members of a structure
directly by returning a pointer. There is no need to copy a structured
variable, but it would be the next step.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

+1
Dare2 cut down to size
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Returning a structure from a procedure is a great idea!!

As Procedure.l and Procedure.s allocated a long or a string for a ProcedureReturn it would be nice if every else declared structure can be used so that the called procedure declares the variable on its type.

So, If Procedure.Vector ProcName() is declared the calling Varialble Test.Vector=ProcName() gives the realy exact same result into this variable called "test".

Nice idea!! Uh, I'm really drunken, so please sorry my really bad english. :D
Tranquil
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Using of structures for procedurereturns

Post by NicTheQuick »

After Purebasic now compiles to C I guess this feature could be more easy to implement than ever. What do you think?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply