Page 1 of 1

Using of structures for procedurereturns

Posted: Fri Feb 08, 2008 10:32 am
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.

Posted: Fri Feb 08, 2008 10:50 am
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

Posted: Fri Feb 08, 2008 11:38 am
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.

Posted: Fri Feb 08, 2008 11:50 am
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;
}

Posted: Fri Feb 08, 2008 12:20 pm
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).

Posted: Fri Feb 08, 2008 2:35 pm
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.

Posted: Sat Feb 09, 2008 11:05 pm
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.

Posted: Sun Feb 10, 2008 3:03 am
by Dare
+1

Posted: Sun Feb 10, 2008 3:43 am
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

Re: Using of structures for procedurereturns

Posted: Sun Nov 13, 2022 7:06 pm
by NicTheQuick
After Purebasic now compiles to C I guess this feature could be more easy to implement than ever. What do you think?